@gmx-io/sdk 1.5.0-alpha-9 → 1.5.0-alpha-10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/scripts/subsquid-codegen.js +1 -1
- package/build/cjs/src/clients/v2/__tests__/data.spec.js +85 -0
- package/build/cjs/src/clients/v2/__tests__/decrease.spec.js +471 -0
- package/build/cjs/src/clients/v2/__tests__/editCancel.spec.js +259 -0
- package/build/cjs/src/clients/v2/__tests__/errors.spec.js +261 -0
- package/build/cjs/src/clients/v2/__tests__/increase.spec.js +764 -0
- package/build/cjs/src/clients/v2/__tests__/subaccount.spec.js +74 -0
- package/build/cjs/src/clients/v2/__tests__/swap.spec.js +192 -0
- package/build/cjs/src/clients/v2/__tests__/testUtil.js +136 -0
- package/build/cjs/src/clients/v2/index.js +162 -27
- package/build/cjs/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +48 -0
- package/build/cjs/src/codegen/prebuilt/hashedMarketConfigKeys.json +342 -0
- package/build/cjs/src/codegen/prebuilt/hashedMarketValuesKeys.json +90 -0
- package/build/cjs/src/codegen/subsquid.js +323 -1
- package/build/cjs/src/configs/chains.js +11 -1
- package/build/cjs/src/configs/contracts.js +7 -7
- package/build/cjs/src/configs/markets.js +56 -2
- package/build/cjs/src/configs/tokens.js +63 -132
- package/build/cjs/src/test/mock.js +1 -1
- package/build/cjs/src/utils/__tests__/markets.spec.js +10 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +2 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +2 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/swapOrders.spec.js +1 -0
- package/build/cjs/src/utils/__tests__/positions.spec.js +59 -7
- package/build/cjs/src/utils/__tests__/uiFeeReceiver.spec.js +34 -0
- package/build/cjs/src/utils/balances/api.js +65 -0
- package/build/cjs/src/utils/buyback/api.js +7 -0
- package/build/cjs/src/utils/buyback/types.js +2 -0
- package/build/cjs/src/utils/errors/index.js +1 -0
- package/build/cjs/src/utils/errors/isIgnoredEstimateGasError.js +23 -0
- package/build/cjs/src/utils/express/index.js +18 -0
- package/build/cjs/src/utils/express/types.js +2 -0
- package/build/cjs/src/utils/express/utils/batchOrderUtils.js +218 -0
- package/build/cjs/src/utils/express/utils/bridgeOutUtils.js +73 -0
- package/build/cjs/src/utils/express/utils/estimateExpressParams.js +244 -0
- package/build/cjs/src/utils/express/utils/expressParamsUtils.js +22 -0
- package/build/cjs/src/utils/express/utils/fetchGelatoGasTankBalance.js +13 -0
- package/build/cjs/src/utils/express/utils/gelatoRelayUtils.js +77 -0
- package/build/cjs/src/utils/express/utils/globalExpressParams.js +40 -0
- package/build/cjs/src/utils/express/utils/index.js +27 -0
- package/build/cjs/src/utils/express/utils/oracleParamsUtils.js +49 -0
- package/build/cjs/src/utils/express/utils/referralUtils.js +35 -0
- package/build/cjs/src/utils/express/utils/relayParamsUtils.js +208 -0
- package/build/cjs/src/utils/fees/priceImpact.js +1 -2
- package/build/cjs/src/utils/http/http.js +86 -13
- package/build/cjs/src/utils/markets/utils.js +8 -0
- package/build/cjs/src/utils/metrics/index.js +7 -0
- package/build/cjs/src/utils/numbers/utils.js +29 -27
- package/build/cjs/src/utils/orderTransactions/api.js +88 -0
- package/build/cjs/src/utils/orderTransactions/index.js +2 -0
- package/build/cjs/src/utils/orderTransactions/simulation.js +141 -0
- package/build/cjs/src/utils/orderTransactions/utils.js +4 -1
- package/build/cjs/src/utils/orderTransactions/validateTypedData.js +100 -0
- package/build/cjs/src/utils/positions/utils.js +5 -19
- package/build/cjs/src/utils/rpc/createViemRpc.js +39 -0
- package/build/cjs/src/utils/rpc/index.js +5 -0
- package/build/cjs/src/utils/signer/index.js +5 -0
- package/build/cjs/src/utils/signer/privateKeySigner.js +42 -0
- package/build/cjs/src/utils/subaccount/api.js +22 -0
- package/build/cjs/src/utils/subaccount/generateSubaccount.js +19 -0
- package/build/cjs/src/utils/subaccount/getEmptySubaccountApproval.js +24 -0
- package/build/cjs/src/utils/subaccount/getSubaccountApprovalTypedData.js +51 -0
- package/build/cjs/src/utils/subaccount/hashSubaccountApproval.js +28 -0
- package/build/cjs/src/utils/subaccount/index.js +30 -0
- package/build/cjs/src/utils/subaccount/types.js +2 -0
- package/build/cjs/src/utils/swap/swapPath.js +4 -1
- package/build/cjs/src/utils/trade/types.js +1 -1
- package/build/cjs/src/utils/tradeHistory/utils.js +14 -28
- package/build/cjs/src/utils/twap/index.js +4 -1
- package/build/cjs/src/utils/twap/uiFeeReceiver.js +23 -7
- package/build/esm/scripts/subsquid-codegen.js +1 -1
- package/build/esm/src/clients/v2/__tests__/data.spec.js +83 -0
- package/build/esm/src/clients/v2/__tests__/decrease.spec.js +469 -0
- package/build/esm/src/clients/v2/__tests__/editCancel.spec.js +257 -0
- package/build/esm/src/clients/v2/__tests__/errors.spec.js +259 -0
- package/build/esm/src/clients/v2/__tests__/increase.spec.js +762 -0
- package/build/esm/src/clients/v2/__tests__/subaccount.spec.js +72 -0
- package/build/esm/src/clients/v2/__tests__/swap.spec.js +190 -0
- package/build/esm/src/clients/v2/__tests__/testUtil.js +124 -0
- package/build/esm/src/clients/v2/index.js +136 -4
- package/build/esm/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +48 -0
- package/build/esm/src/codegen/prebuilt/hashedMarketConfigKeys.json +342 -0
- package/build/esm/src/codegen/prebuilt/hashedMarketValuesKeys.json +90 -0
- package/build/esm/src/codegen/subsquid.js +321 -0
- package/build/esm/src/configs/chains.js +11 -1
- package/build/esm/src/configs/contracts.js +7 -7
- package/build/esm/src/configs/markets.js +55 -1
- package/build/esm/src/configs/tokens.js +62 -130
- package/build/esm/src/test/mock.js +1 -1
- package/build/esm/src/utils/__tests__/markets.spec.js +10 -0
- package/build/esm/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +2 -0
- package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +2 -0
- package/build/esm/src/utils/__tests__/orderTransactions/swapOrders.spec.js +1 -0
- package/build/esm/src/utils/__tests__/positions.spec.js +60 -8
- package/build/esm/src/utils/__tests__/uiFeeReceiver.spec.js +35 -1
- package/build/esm/src/utils/balances/api.js +56 -0
- package/build/esm/src/utils/buyback/api.js +3 -0
- package/build/esm/src/utils/buyback/types.js +1 -0
- package/build/esm/src/utils/errors/index.js +1 -0
- package/build/esm/src/utils/errors/isIgnoredEstimateGasError.js +19 -0
- package/build/esm/src/utils/express/index.js +2 -0
- package/build/esm/src/utils/express/types.js +1 -0
- package/build/esm/src/utils/express/utils/batchOrderUtils.js +210 -0
- package/build/esm/src/utils/express/utils/bridgeOutUtils.js +69 -0
- package/build/esm/src/utils/express/utils/estimateExpressParams.js +238 -0
- package/build/esm/src/utils/express/utils/expressParamsUtils.js +16 -0
- package/build/esm/src/utils/express/utils/fetchGelatoGasTankBalance.js +9 -0
- package/build/esm/src/utils/express/utils/gelatoRelayUtils.js +71 -0
- package/build/esm/src/utils/express/utils/globalExpressParams.js +36 -0
- package/build/esm/src/utils/express/utils/index.js +11 -0
- package/build/esm/src/utils/express/utils/oracleParamsUtils.js +42 -0
- package/build/esm/src/utils/express/utils/referralUtils.js +30 -0
- package/build/esm/src/utils/express/utils/relayParamsUtils.js +198 -0
- package/build/esm/src/utils/fees/priceImpact.js +1 -2
- package/build/esm/src/utils/http/http.js +84 -12
- package/build/esm/src/utils/markets/utils.js +8 -0
- package/build/esm/src/utils/metrics/index.js +4 -0
- package/build/esm/src/utils/numbers/utils.js +25 -24
- package/build/esm/src/utils/orderTransactions/api.js +77 -0
- package/build/esm/src/utils/orderTransactions/index.js +2 -0
- package/build/esm/src/utils/orderTransactions/simulation.js +136 -0
- package/build/esm/src/utils/orderTransactions/utils.js +4 -1
- package/build/esm/src/utils/orderTransactions/validateTypedData.js +95 -0
- package/build/esm/src/utils/positions/utils.js +5 -19
- package/build/esm/src/utils/rpc/createViemRpc.js +35 -0
- package/build/esm/src/utils/rpc/index.js +1 -0
- package/build/esm/src/utils/signer/index.js +1 -0
- package/build/esm/src/utils/signer/privateKeySigner.js +38 -0
- package/build/esm/src/utils/subaccount/api.js +16 -0
- package/build/esm/src/utils/subaccount/generateSubaccount.js +15 -0
- package/build/esm/src/utils/subaccount/getEmptySubaccountApproval.js +20 -0
- package/build/esm/src/utils/subaccount/getSubaccountApprovalTypedData.js +47 -0
- package/build/esm/src/utils/subaccount/hashSubaccountApproval.js +24 -0
- package/build/esm/src/utils/subaccount/index.js +6 -0
- package/build/esm/src/utils/subaccount/types.js +1 -0
- package/build/esm/src/utils/swap/swapPath.js +5 -2
- package/build/esm/src/utils/trade/types.js +1 -1
- package/build/esm/src/utils/tradeHistory/utils.js +13 -26
- package/build/esm/src/utils/twap/index.js +1 -0
- package/build/esm/src/utils/twap/uiFeeReceiver.js +20 -6
- package/build/types/src/clients/v2/__tests__/data.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/decrease.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/editCancel.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/errors.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/increase.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/subaccount.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/swap.spec.d.ts +1 -0
- package/build/types/src/clients/v2/__tests__/testUtil.d.ts +25 -0
- package/build/types/src/clients/v2/index.d.ts +59 -1
- package/build/types/src/codegen/prebuilt/index.d.ts +480 -0
- package/build/types/src/codegen/subsquid.d.ts +1276 -44
- package/build/types/src/configs/chains.d.ts +10 -1
- package/build/types/src/configs/contracts.d.ts +6 -6
- package/build/types/src/configs/markets.d.ts +5 -0
- package/build/types/src/configs/tokens.d.ts +6 -1
- package/build/types/src/utils/balances/api.d.ts +39 -0
- package/build/types/src/utils/buyback/api.d.ts +5 -0
- package/build/types/src/utils/buyback/types.d.ts +15 -0
- package/build/types/src/utils/chains/types.d.ts +1 -0
- package/build/types/src/utils/errors/index.d.ts +1 -0
- package/build/types/src/utils/errors/isIgnoredEstimateGasError.d.ts +2 -0
- package/build/types/src/utils/express/index.d.ts +2 -0
- package/build/types/src/utils/express/types.d.ts +95 -0
- package/build/types/src/utils/express/utils/batchOrderUtils.d.ts +98 -0
- package/build/types/src/utils/express/utils/bridgeOutUtils.d.ts +21 -0
- package/build/types/src/utils/express/utils/estimateExpressParams.d.ts +37 -0
- package/build/types/src/utils/express/utils/expressParamsUtils.d.ts +19 -0
- package/build/types/src/utils/express/utils/fetchGelatoGasTankBalance.d.ts +5 -0
- package/build/types/src/utils/express/utils/gelatoRelayUtils.d.ts +25 -0
- package/build/types/src/utils/express/utils/globalExpressParams.d.ts +31 -0
- package/build/types/src/utils/express/utils/index.d.ts +11 -0
- package/build/types/src/utils/express/utils/oracleParamsUtils.d.ts +10 -0
- package/build/types/src/utils/express/utils/referralUtils.d.ts +17 -0
- package/build/types/src/utils/express/utils/relayParamsUtils.d.ts +63 -0
- package/build/types/src/utils/http/http.d.ts +16 -1
- package/build/types/src/utils/http/types.d.ts +3 -0
- package/build/types/src/utils/metrics/index.d.ts +4 -0
- package/build/types/src/utils/numbers/utils.d.ts +2 -5
- package/build/types/src/utils/orderTransactions/api.d.ts +158 -0
- package/build/types/src/utils/orderTransactions/index.d.ts +2 -0
- package/build/types/src/utils/orderTransactions/simulation.d.ts +26 -0
- package/build/types/src/utils/orderTransactions/validateTypedData.d.ts +6 -0
- package/build/types/src/utils/positions/utils.d.ts +1 -9
- package/build/types/src/utils/rpc/createViemRpc.d.ts +3 -0
- package/build/types/src/utils/rpc/index.d.ts +26 -0
- package/build/types/src/utils/signer/index.d.ts +23 -0
- package/build/types/src/utils/signer/privateKeySigner.d.ts +19 -0
- package/build/types/src/utils/subaccount/api.d.ts +50 -0
- package/build/types/src/utils/subaccount/generateSubaccount.d.ts +8 -0
- package/build/types/src/utils/subaccount/getEmptySubaccountApproval.d.ts +9 -0
- package/build/types/src/utils/subaccount/getSubaccountApprovalTypedData.d.ts +24 -0
- package/build/types/src/utils/subaccount/hashSubaccountApproval.d.ts +1 -0
- package/build/types/src/utils/subaccount/index.d.ts +7 -0
- package/build/types/src/utils/subaccount/types.d.ts +55 -0
- package/build/types/src/utils/tokens/types.d.ts +0 -1
- package/build/types/src/utils/trade/types.d.ts +1 -1
- package/build/types/src/utils/tradeHistory/utils.d.ts +0 -1
- package/build/types/src/utils/twap/index.d.ts +1 -0
- package/build/types/src/utils/twap/types.d.ts +2 -0
- package/build/types/src/utils/twap/uiFeeReceiver.d.ts +10 -2
- package/package.json +40 -3
|
@@ -0,0 +1,762 @@
|
|
|
1
|
+
import { describe, expect, it, beforeAll, afterAll } from "vitest";
|
|
2
|
+
import { getTestSdk, requireSigner, expressFlow, waitForOrderStatus, waitForOrdersUpdate, waitForPositionUpdate, activateTestSubaccount, hasRpcUrl, TEST_SYMBOL, TEST_SIZE_USD, TEST_COLLATERAL, } from "./testUtil";
|
|
3
|
+
const sdk = getTestSdk();
|
|
4
|
+
const signer = requireSigner();
|
|
5
|
+
const account = signer.address;
|
|
6
|
+
const ETH_USD_MARKET_ADDRESS = "0x70d95587d40A2caf56bd97485aB3Eec10Bee6336";
|
|
7
|
+
async function cancelAllOrders() {
|
|
8
|
+
try {
|
|
9
|
+
const prepared = await sdk.prepareCancelOrder({ all: true, mode: "express", from: account });
|
|
10
|
+
const sig = await sdk.signOrder(prepared, signer);
|
|
11
|
+
await sdk.submitOrder({
|
|
12
|
+
mode: prepared.mode,
|
|
13
|
+
requestId: prepared.requestId,
|
|
14
|
+
signature: sig,
|
|
15
|
+
from: account,
|
|
16
|
+
idempotencyKey: prepared.idempotencyKey,
|
|
17
|
+
eip712Data: {
|
|
18
|
+
batchParams: prepared.payload.batchParams,
|
|
19
|
+
relayParams: prepared.payload.relayParams,
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
catch { /* cleanup best-effort */ }
|
|
24
|
+
}
|
|
25
|
+
describe("increase orders", () => {
|
|
26
|
+
describe("express market increase", () => {
|
|
27
|
+
it("long: estimates → submit → verify position", async () => {
|
|
28
|
+
const positionsBefore = await sdk.fetchPositionsInfo({ address: account });
|
|
29
|
+
const longBefore = positionsBefore.find((p) => p.isLong && p.indexName?.includes("ETH/USD"));
|
|
30
|
+
const sizeBefore = longBefore ? BigInt(longBefore.sizeInUsd) : 0n;
|
|
31
|
+
const { prepared, submitted } = await expressFlow(sdk, signer, {
|
|
32
|
+
kind: "increase",
|
|
33
|
+
symbol: TEST_SYMBOL,
|
|
34
|
+
direction: "long",
|
|
35
|
+
orderType: "market",
|
|
36
|
+
size: TEST_SIZE_USD,
|
|
37
|
+
collateralToPay: TEST_COLLATERAL,
|
|
38
|
+
mode: "express",
|
|
39
|
+
from: account,
|
|
40
|
+
});
|
|
41
|
+
expect(prepared.estimates).toBeDefined();
|
|
42
|
+
const est = prepared.estimates;
|
|
43
|
+
expect(est.sizeDeltaUsd).toBe(TEST_SIZE_USD);
|
|
44
|
+
expect(est.executionFeeAmount).toBeGreaterThan(0n);
|
|
45
|
+
expect(est.positionFeeUsd).toBeGreaterThan(0n);
|
|
46
|
+
expect(est.acceptablePrice).toBeGreaterThan(0n);
|
|
47
|
+
expect(submitted.requestId).toBeDefined();
|
|
48
|
+
expect(submitted.status).toBeDefined();
|
|
49
|
+
const status = await waitForOrderStatus(sdk, submitted.requestId);
|
|
50
|
+
expect(status.requestId).toBe(submitted.requestId);
|
|
51
|
+
expect(status.status).toBe("executed");
|
|
52
|
+
// Position may take time to appear in the API after on-chain execution
|
|
53
|
+
const positionsAfter = await waitForPositionUpdate(sdk, account, (positions) => {
|
|
54
|
+
const p = positions.find((p) => p.isLong && p.indexName?.includes("ETH/USD"));
|
|
55
|
+
return p ? BigInt(p.sizeInUsd) > sizeBefore : false;
|
|
56
|
+
}, 60000);
|
|
57
|
+
const longAfter = positionsAfter.find((p) => p.isLong && p.indexName?.includes("ETH/USD"));
|
|
58
|
+
// With very small collateral ($1), position may be auto-liquidated before API reflects it
|
|
59
|
+
if (longAfter) {
|
|
60
|
+
expect(BigInt(longAfter.sizeInUsd)).toBeGreaterThan(sizeBefore);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
it("short: estimates match", async () => {
|
|
64
|
+
const { prepared, submitted } = await expressFlow(sdk, signer, {
|
|
65
|
+
kind: "increase",
|
|
66
|
+
symbol: TEST_SYMBOL,
|
|
67
|
+
direction: "short",
|
|
68
|
+
orderType: "market",
|
|
69
|
+
size: TEST_SIZE_USD,
|
|
70
|
+
collateralToPay: TEST_COLLATERAL,
|
|
71
|
+
mode: "express",
|
|
72
|
+
from: account,
|
|
73
|
+
});
|
|
74
|
+
expect(prepared.estimates).toBeDefined();
|
|
75
|
+
expect(prepared.estimates.sizeDeltaUsd).toBe(TEST_SIZE_USD);
|
|
76
|
+
expect(prepared.estimates.executionFeeAmount).toBeGreaterThan(0n);
|
|
77
|
+
expect(prepared.estimates.positionFeeUsd).toBeGreaterThan(0n);
|
|
78
|
+
expect(prepared.estimates.acceptablePrice).toBeGreaterThan(0n);
|
|
79
|
+
expect(submitted.requestId).toBeDefined();
|
|
80
|
+
const status = await sdk.fetchOrderStatus({ requestId: submitted.requestId });
|
|
81
|
+
expect(status.requestId).toBe(submitted.requestId);
|
|
82
|
+
});
|
|
83
|
+
it("larger collateral: position fee scales with size not collateral", async () => {
|
|
84
|
+
const collateral = { amount: 5000000n, token: "USDC" }; // 5 USDC vs 1 USDC in TEST_COLLATERAL
|
|
85
|
+
const [small, large] = await Promise.all([
|
|
86
|
+
sdk.prepareOrder({
|
|
87
|
+
kind: "increase",
|
|
88
|
+
symbol: TEST_SYMBOL,
|
|
89
|
+
direction: "long",
|
|
90
|
+
orderType: "market",
|
|
91
|
+
size: TEST_SIZE_USD,
|
|
92
|
+
collateralToPay: TEST_COLLATERAL,
|
|
93
|
+
mode: "express",
|
|
94
|
+
from: account,
|
|
95
|
+
}),
|
|
96
|
+
sdk.prepareOrder({
|
|
97
|
+
kind: "increase",
|
|
98
|
+
symbol: TEST_SYMBOL,
|
|
99
|
+
direction: "long",
|
|
100
|
+
orderType: "market",
|
|
101
|
+
size: TEST_SIZE_USD,
|
|
102
|
+
collateralToPay: collateral,
|
|
103
|
+
mode: "express",
|
|
104
|
+
from: account,
|
|
105
|
+
}),
|
|
106
|
+
]);
|
|
107
|
+
// Same size → same position fee regardless of collateral amount
|
|
108
|
+
expect(small.estimates.positionFeeUsd).toBe(large.estimates.positionFeeUsd);
|
|
109
|
+
expect(small.estimates.sizeDeltaUsd).toBe(large.estimates.sizeDeltaUsd);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
describe("collateral scenarios", () => {
|
|
113
|
+
it("same collateral — no swap (USDC collateralToken)", async () => {
|
|
114
|
+
const prepared = await sdk.prepareOrder({
|
|
115
|
+
kind: "increase",
|
|
116
|
+
symbol: TEST_SYMBOL,
|
|
117
|
+
direction: "long",
|
|
118
|
+
orderType: "market",
|
|
119
|
+
size: TEST_SIZE_USD,
|
|
120
|
+
collateralToken: "USDC",
|
|
121
|
+
collateralToPay: { amount: 5000000n, token: "USDC" },
|
|
122
|
+
mode: "express",
|
|
123
|
+
from: account,
|
|
124
|
+
});
|
|
125
|
+
expect(prepared.estimates).toBeDefined();
|
|
126
|
+
expect(prepared.estimates.sizeDeltaUsd).toBe(TEST_SIZE_USD);
|
|
127
|
+
// No swap needed — impact should be zero or minimal
|
|
128
|
+
const absImpact = prepared.estimates.swapPriceImpactDeltaUsd >= 0n
|
|
129
|
+
? prepared.estimates.swapPriceImpactDeltaUsd
|
|
130
|
+
: -prepared.estimates.swapPriceImpactDeltaUsd;
|
|
131
|
+
expect(absImpact).toBeLessThanOrEqual(10n ** 28n); // < $0.01
|
|
132
|
+
});
|
|
133
|
+
it("different collateral — with swap (pay USDC, default WETH position)", async () => {
|
|
134
|
+
// No collateralToken → defaults to WETH for long → USDC payment triggers swap
|
|
135
|
+
const prepared = await sdk.prepareOrder({
|
|
136
|
+
kind: "increase",
|
|
137
|
+
symbol: TEST_SYMBOL,
|
|
138
|
+
direction: "long",
|
|
139
|
+
orderType: "market",
|
|
140
|
+
size: TEST_SIZE_USD,
|
|
141
|
+
collateralToPay: { amount: 5000000n, token: "USDC" },
|
|
142
|
+
mode: "express",
|
|
143
|
+
from: account,
|
|
144
|
+
});
|
|
145
|
+
expect(prepared.estimates).toBeDefined();
|
|
146
|
+
expect(prepared.estimates.sizeDeltaUsd).toBe(TEST_SIZE_USD);
|
|
147
|
+
// USDC→WETH swap happens — swapPriceImpactDeltaUsd is defined (may be 0 for small amounts)
|
|
148
|
+
expect(prepared.estimates.swapPriceImpactDeltaUsd).toBeDefined();
|
|
149
|
+
});
|
|
150
|
+
it("same vs different collateral: swap impact comparison", async () => {
|
|
151
|
+
const [sameCollateral, diffCollateral] = await Promise.all([
|
|
152
|
+
sdk.prepareOrder({
|
|
153
|
+
kind: "increase",
|
|
154
|
+
symbol: TEST_SYMBOL,
|
|
155
|
+
direction: "long",
|
|
156
|
+
orderType: "market",
|
|
157
|
+
size: TEST_SIZE_USD,
|
|
158
|
+
collateralToken: "USDC",
|
|
159
|
+
collateralToPay: { amount: 5000000n, token: "USDC" },
|
|
160
|
+
mode: "express",
|
|
161
|
+
from: account,
|
|
162
|
+
}),
|
|
163
|
+
sdk.prepareOrder({
|
|
164
|
+
kind: "increase",
|
|
165
|
+
symbol: TEST_SYMBOL,
|
|
166
|
+
direction: "long",
|
|
167
|
+
orderType: "market",
|
|
168
|
+
size: TEST_SIZE_USD,
|
|
169
|
+
collateralToPay: { amount: 5000000n, token: "USDC" },
|
|
170
|
+
mode: "express",
|
|
171
|
+
from: account,
|
|
172
|
+
}),
|
|
173
|
+
]);
|
|
174
|
+
const sameImpact = sameCollateral.estimates.swapPriceImpactDeltaUsd;
|
|
175
|
+
const diffImpact = diffCollateral.estimates.swapPriceImpactDeltaUsd;
|
|
176
|
+
const absSame = sameImpact >= 0n ? sameImpact : -sameImpact;
|
|
177
|
+
const absDiff = diffImpact >= 0n ? diffImpact : -diffImpact;
|
|
178
|
+
// Same-collateral should have lower absolute swap impact
|
|
179
|
+
expect(absSame).toBeLessThanOrEqual(absDiff);
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
describe("manual swap path", () => {
|
|
183
|
+
it("manualSwapPath is accepted and returns valid estimates", async () => {
|
|
184
|
+
const [auto, manual] = await Promise.all([
|
|
185
|
+
sdk.prepareOrder({
|
|
186
|
+
kind: "increase",
|
|
187
|
+
symbol: TEST_SYMBOL,
|
|
188
|
+
direction: "long",
|
|
189
|
+
orderType: "market",
|
|
190
|
+
size: TEST_SIZE_USD,
|
|
191
|
+
collateralToPay: { amount: 5000000n, token: "USDC" },
|
|
192
|
+
mode: "express",
|
|
193
|
+
from: account,
|
|
194
|
+
}),
|
|
195
|
+
sdk.prepareOrder({
|
|
196
|
+
kind: "increase",
|
|
197
|
+
symbol: TEST_SYMBOL,
|
|
198
|
+
direction: "long",
|
|
199
|
+
orderType: "market",
|
|
200
|
+
size: TEST_SIZE_USD,
|
|
201
|
+
collateralToPay: { amount: 5000000n, token: "USDC" },
|
|
202
|
+
manualSwapPath: [ETH_USD_MARKET_ADDRESS],
|
|
203
|
+
mode: "express",
|
|
204
|
+
from: account,
|
|
205
|
+
}),
|
|
206
|
+
]);
|
|
207
|
+
expect(manual.estimates).toBeDefined();
|
|
208
|
+
expect(manual.estimates.sizeDeltaUsd).toBe(TEST_SIZE_USD);
|
|
209
|
+
// Both should have valid estimates — manual path should produce comparable results
|
|
210
|
+
expect(auto.estimates.sizeDeltaUsd).toBe(manual.estimates.sizeDeltaUsd);
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
describe("TPSL", () => {
|
|
214
|
+
let markPrice;
|
|
215
|
+
beforeAll(async () => {
|
|
216
|
+
const tickers = await sdk.fetchMarketsTickers({ symbols: [TEST_SYMBOL] });
|
|
217
|
+
markPrice = BigInt(tickers[0].maxPrice);
|
|
218
|
+
});
|
|
219
|
+
afterAll(cancelAllOrders);
|
|
220
|
+
it("TP + SL in same batch", async () => {
|
|
221
|
+
const { prepared, submitted } = await expressFlow(sdk, signer, {
|
|
222
|
+
kind: "increase",
|
|
223
|
+
symbol: TEST_SYMBOL,
|
|
224
|
+
direction: "long",
|
|
225
|
+
orderType: "market",
|
|
226
|
+
size: TEST_SIZE_USD,
|
|
227
|
+
collateralToPay: TEST_COLLATERAL,
|
|
228
|
+
mode: "express",
|
|
229
|
+
from: account,
|
|
230
|
+
tpsl: [
|
|
231
|
+
{ type: "take-profit", triggerPrice: markPrice * 105n / 100n, size: TEST_SIZE_USD },
|
|
232
|
+
{ type: "stop-loss", triggerPrice: markPrice * 95n / 100n, size: TEST_SIZE_USD },
|
|
233
|
+
],
|
|
234
|
+
});
|
|
235
|
+
expect(prepared.estimates).toBeDefined();
|
|
236
|
+
expect(submitted.requestId).toBeDefined();
|
|
237
|
+
const status = await waitForOrderStatus(sdk, submitted.requestId);
|
|
238
|
+
expect(status.status).toBe("executed");
|
|
239
|
+
// TP + SL should appear as conditional orders
|
|
240
|
+
const orders = await waitForOrdersUpdate(sdk, account, (o) => o.length >= 2, 30000);
|
|
241
|
+
expect(orders.length).toBeGreaterThanOrEqual(2);
|
|
242
|
+
});
|
|
243
|
+
it("TP only", async () => {
|
|
244
|
+
const ordersBefore = await sdk.fetchOrders({ address: account });
|
|
245
|
+
const { submitted } = await expressFlow(sdk, signer, {
|
|
246
|
+
kind: "increase",
|
|
247
|
+
symbol: TEST_SYMBOL,
|
|
248
|
+
direction: "long",
|
|
249
|
+
orderType: "market",
|
|
250
|
+
size: TEST_SIZE_USD,
|
|
251
|
+
collateralToPay: TEST_COLLATERAL,
|
|
252
|
+
mode: "express",
|
|
253
|
+
from: account,
|
|
254
|
+
tpsl: [{ type: "take-profit", triggerPrice: markPrice * 110n / 100n }],
|
|
255
|
+
});
|
|
256
|
+
const status = await waitForOrderStatus(sdk, submitted.requestId);
|
|
257
|
+
expect(status.status).toBe("executed");
|
|
258
|
+
const ordersAfter = await waitForOrdersUpdate(sdk, account, (o) => o.length > ordersBefore.length, 30000);
|
|
259
|
+
expect(ordersAfter.length).toBeGreaterThan(ordersBefore.length);
|
|
260
|
+
});
|
|
261
|
+
it("SL only", async () => {
|
|
262
|
+
const ordersBefore = await sdk.fetchOrders({ address: account });
|
|
263
|
+
const { submitted } = await expressFlow(sdk, signer, {
|
|
264
|
+
kind: "increase",
|
|
265
|
+
symbol: TEST_SYMBOL,
|
|
266
|
+
direction: "long",
|
|
267
|
+
orderType: "market",
|
|
268
|
+
size: TEST_SIZE_USD,
|
|
269
|
+
collateralToPay: TEST_COLLATERAL,
|
|
270
|
+
mode: "express",
|
|
271
|
+
from: account,
|
|
272
|
+
tpsl: [{ type: "stop-loss", triggerPrice: markPrice * 90n / 100n }],
|
|
273
|
+
});
|
|
274
|
+
const status = await waitForOrderStatus(sdk, submitted.requestId);
|
|
275
|
+
expect(status.status).toBe("executed");
|
|
276
|
+
const ordersAfter = await waitForOrdersUpdate(sdk, account, (o) => o.length > ordersBefore.length, 30000);
|
|
277
|
+
expect(ordersAfter.length).toBeGreaterThan(ordersBefore.length);
|
|
278
|
+
});
|
|
279
|
+
it("partial size TP", async () => {
|
|
280
|
+
const halfSize = TEST_SIZE_USD / 2n;
|
|
281
|
+
const ordersBefore = await sdk.fetchOrders({ address: account });
|
|
282
|
+
const { submitted } = await expressFlow(sdk, signer, {
|
|
283
|
+
kind: "increase",
|
|
284
|
+
symbol: TEST_SYMBOL,
|
|
285
|
+
direction: "long",
|
|
286
|
+
orderType: "market",
|
|
287
|
+
size: TEST_SIZE_USD,
|
|
288
|
+
collateralToPay: TEST_COLLATERAL,
|
|
289
|
+
mode: "express",
|
|
290
|
+
from: account,
|
|
291
|
+
tpsl: [{ type: "take-profit", triggerPrice: markPrice * 105n / 100n, size: halfSize }],
|
|
292
|
+
});
|
|
293
|
+
const status = await waitForOrderStatus(sdk, submitted.requestId);
|
|
294
|
+
expect(status.status).toBe("executed");
|
|
295
|
+
const ordersAfter = await waitForOrdersUpdate(sdk, account, (o) => o.length > ordersBefore.length, 30000);
|
|
296
|
+
expect(ordersAfter.length).toBeGreaterThan(ordersBefore.length);
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
describe("TPSL validation errors", () => {
|
|
300
|
+
it("tpsl on decrease rejects", async () => {
|
|
301
|
+
await expect(sdk.prepareOrder({
|
|
302
|
+
kind: "decrease",
|
|
303
|
+
symbol: TEST_SYMBOL,
|
|
304
|
+
direction: "long",
|
|
305
|
+
orderType: "market",
|
|
306
|
+
size: TEST_SIZE_USD,
|
|
307
|
+
mode: "express",
|
|
308
|
+
from: account,
|
|
309
|
+
tpsl: [{ type: "take-profit", triggerPrice: 1n * 10n ** 30n }],
|
|
310
|
+
})).rejects.toThrow();
|
|
311
|
+
});
|
|
312
|
+
it("tpsl on swap rejects", async () => {
|
|
313
|
+
await expect(sdk.prepareOrder({
|
|
314
|
+
kind: "swap",
|
|
315
|
+
symbol: TEST_SYMBOL,
|
|
316
|
+
orderType: "market",
|
|
317
|
+
size: 1000000n,
|
|
318
|
+
collateralToPay: { amount: 1000000n, token: "USDC" },
|
|
319
|
+
receiveToken: "ETH",
|
|
320
|
+
mode: "express",
|
|
321
|
+
from: account,
|
|
322
|
+
tpsl: [{ type: "take-profit", triggerPrice: 1n * 10n ** 30n }],
|
|
323
|
+
})).rejects.toThrow();
|
|
324
|
+
});
|
|
325
|
+
it("tpsl with TWAP orderType rejects", async () => {
|
|
326
|
+
await expect(sdk.prepareOrder({
|
|
327
|
+
kind: "increase",
|
|
328
|
+
symbol: TEST_SYMBOL,
|
|
329
|
+
direction: "long",
|
|
330
|
+
orderType: "twap",
|
|
331
|
+
size: TEST_SIZE_USD,
|
|
332
|
+
collateralToPay: TEST_COLLATERAL,
|
|
333
|
+
twapConfig: { duration: 600, parts: 3 },
|
|
334
|
+
mode: "express",
|
|
335
|
+
from: account,
|
|
336
|
+
tpsl: [{ type: "take-profit", triggerPrice: 1n * 10n ** 30n }],
|
|
337
|
+
})).rejects.toThrow();
|
|
338
|
+
});
|
|
339
|
+
it("empty tpsl array rejects", async () => {
|
|
340
|
+
await expect(sdk.prepareOrder({
|
|
341
|
+
kind: "increase",
|
|
342
|
+
symbol: TEST_SYMBOL,
|
|
343
|
+
direction: "long",
|
|
344
|
+
orderType: "market",
|
|
345
|
+
size: TEST_SIZE_USD,
|
|
346
|
+
collateralToPay: TEST_COLLATERAL,
|
|
347
|
+
mode: "express",
|
|
348
|
+
from: account,
|
|
349
|
+
tpsl: [],
|
|
350
|
+
})).rejects.toThrow();
|
|
351
|
+
});
|
|
352
|
+
it("tpsl entry missing triggerPrice rejects", async () => {
|
|
353
|
+
await expect(sdk.prepareOrder({
|
|
354
|
+
kind: "increase",
|
|
355
|
+
symbol: TEST_SYMBOL,
|
|
356
|
+
direction: "long",
|
|
357
|
+
orderType: "market",
|
|
358
|
+
size: TEST_SIZE_USD,
|
|
359
|
+
collateralToPay: TEST_COLLATERAL,
|
|
360
|
+
mode: "express",
|
|
361
|
+
from: account,
|
|
362
|
+
tpsl: [{ type: "take-profit" }],
|
|
363
|
+
})).rejects.toThrow();
|
|
364
|
+
});
|
|
365
|
+
it("tpsl entry with invalid type rejects", async () => {
|
|
366
|
+
await expect(sdk.prepareOrder({
|
|
367
|
+
kind: "increase",
|
|
368
|
+
symbol: TEST_SYMBOL,
|
|
369
|
+
direction: "long",
|
|
370
|
+
orderType: "market",
|
|
371
|
+
size: TEST_SIZE_USD,
|
|
372
|
+
collateralToPay: TEST_COLLATERAL,
|
|
373
|
+
mode: "express",
|
|
374
|
+
from: account,
|
|
375
|
+
tpsl: [{ type: "invalid", triggerPrice: 1n * 10n ** 30n }],
|
|
376
|
+
})).rejects.toThrow();
|
|
377
|
+
});
|
|
378
|
+
});
|
|
379
|
+
describe("TWAP increase", () => {
|
|
380
|
+
afterAll(cancelAllOrders);
|
|
381
|
+
it("TWAP market increase — creates sub-orders", async () => {
|
|
382
|
+
const twapSize = 200n * 10n ** 30n; // $200
|
|
383
|
+
const { prepared, submitted } = await expressFlow(sdk, signer, {
|
|
384
|
+
kind: "increase",
|
|
385
|
+
orderType: "twap",
|
|
386
|
+
symbol: TEST_SYMBOL,
|
|
387
|
+
direction: "long",
|
|
388
|
+
size: twapSize,
|
|
389
|
+
collateralToPay: { amount: 20000000n, token: "USDC" }, // 20 USDC — enough for fees per part
|
|
390
|
+
twapConfig: { duration: 600, parts: 2 },
|
|
391
|
+
mode: "express",
|
|
392
|
+
from: account,
|
|
393
|
+
});
|
|
394
|
+
expect(prepared.estimates).toBeDefined();
|
|
395
|
+
expect(prepared.estimates.sizeDeltaUsd).toBe(twapSize);
|
|
396
|
+
expect(submitted.requestId).toBeDefined();
|
|
397
|
+
const status = await waitForOrderStatus(sdk, submitted.requestId);
|
|
398
|
+
expect(["executed", "reverted"]).toContain(status.status);
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
describe("TWAP validation errors", () => {
|
|
402
|
+
it("missing twapConfig rejects", async () => {
|
|
403
|
+
await expect(sdk.prepareOrder({
|
|
404
|
+
kind: "increase",
|
|
405
|
+
symbol: TEST_SYMBOL,
|
|
406
|
+
direction: "long",
|
|
407
|
+
orderType: "twap",
|
|
408
|
+
size: TEST_SIZE_USD,
|
|
409
|
+
collateralToPay: TEST_COLLATERAL,
|
|
410
|
+
mode: "express",
|
|
411
|
+
from: account,
|
|
412
|
+
})).rejects.toThrow();
|
|
413
|
+
});
|
|
414
|
+
it("parts below minimum (1) rejects", async () => {
|
|
415
|
+
await expect(sdk.prepareOrder({
|
|
416
|
+
kind: "increase",
|
|
417
|
+
symbol: TEST_SYMBOL,
|
|
418
|
+
direction: "long",
|
|
419
|
+
orderType: "twap",
|
|
420
|
+
size: TEST_SIZE_USD,
|
|
421
|
+
collateralToPay: TEST_COLLATERAL,
|
|
422
|
+
twapConfig: { duration: 600, parts: 1 },
|
|
423
|
+
mode: "express",
|
|
424
|
+
from: account,
|
|
425
|
+
})).rejects.toThrow();
|
|
426
|
+
});
|
|
427
|
+
it("parts above maximum (31) rejects", async () => {
|
|
428
|
+
await expect(sdk.prepareOrder({
|
|
429
|
+
kind: "increase",
|
|
430
|
+
symbol: TEST_SYMBOL,
|
|
431
|
+
direction: "long",
|
|
432
|
+
orderType: "twap",
|
|
433
|
+
size: TEST_SIZE_USD,
|
|
434
|
+
collateralToPay: TEST_COLLATERAL,
|
|
435
|
+
twapConfig: { duration: 600, parts: 31 },
|
|
436
|
+
mode: "express",
|
|
437
|
+
from: account,
|
|
438
|
+
})).rejects.toThrow();
|
|
439
|
+
});
|
|
440
|
+
it("zero duration rejects", async () => {
|
|
441
|
+
await expect(sdk.prepareOrder({
|
|
442
|
+
kind: "increase",
|
|
443
|
+
symbol: TEST_SYMBOL,
|
|
444
|
+
direction: "long",
|
|
445
|
+
orderType: "twap",
|
|
446
|
+
size: TEST_SIZE_USD,
|
|
447
|
+
collateralToPay: TEST_COLLATERAL,
|
|
448
|
+
twapConfig: { duration: 0, parts: 3 },
|
|
449
|
+
mode: "express",
|
|
450
|
+
from: account,
|
|
451
|
+
})).rejects.toThrow();
|
|
452
|
+
});
|
|
453
|
+
});
|
|
454
|
+
describe("express limit increase", () => {
|
|
455
|
+
afterAll(cancelAllOrders);
|
|
456
|
+
it("estimates → submit → verify in orders", async () => {
|
|
457
|
+
const triggerPrice = 1n * 10n ** 30n; // $1
|
|
458
|
+
const { prepared, submitted } = await expressFlow(sdk, signer, {
|
|
459
|
+
kind: "increase",
|
|
460
|
+
symbol: TEST_SYMBOL,
|
|
461
|
+
direction: "long",
|
|
462
|
+
orderType: "limit",
|
|
463
|
+
size: TEST_SIZE_USD,
|
|
464
|
+
triggerPrice,
|
|
465
|
+
collateralToPay: TEST_COLLATERAL,
|
|
466
|
+
mode: "express",
|
|
467
|
+
from: account,
|
|
468
|
+
});
|
|
469
|
+
expect(prepared.estimates).toBeDefined();
|
|
470
|
+
expect(prepared.estimates.sizeDeltaUsd).toBe(TEST_SIZE_USD);
|
|
471
|
+
expect(prepared.estimates.executionFeeAmount).toBeGreaterThan(0n);
|
|
472
|
+
expect(prepared.estimates.positionFeeUsd).toBeGreaterThan(0n);
|
|
473
|
+
expect(prepared.estimates.acceptablePrice).toBeGreaterThan(triggerPrice);
|
|
474
|
+
expect(submitted.status).toBeDefined();
|
|
475
|
+
const status = await waitForOrderStatus(sdk, submitted.requestId);
|
|
476
|
+
expect(status.requestId).toBe(submitted.requestId);
|
|
477
|
+
expect(status.status).toBe("executed");
|
|
478
|
+
const orders = await waitForOrdersUpdate(sdk, account, (o) => o.length > 0, 30000);
|
|
479
|
+
expect(orders.length).toBeGreaterThan(0);
|
|
480
|
+
const order = orders.find((o) => o.isLong && BigInt(o.sizeDeltaUsd) > 0n);
|
|
481
|
+
expect(order).toBeDefined();
|
|
482
|
+
expect(order.isLong).toBe(true);
|
|
483
|
+
expect(BigInt(order.executionFee)).toBeGreaterThan(0n);
|
|
484
|
+
});
|
|
485
|
+
});
|
|
486
|
+
describe("slippage parameter", () => {
|
|
487
|
+
const baseRequest = {
|
|
488
|
+
kind: "increase",
|
|
489
|
+
symbol: TEST_SYMBOL,
|
|
490
|
+
direction: "long",
|
|
491
|
+
orderType: "market",
|
|
492
|
+
size: TEST_SIZE_USD,
|
|
493
|
+
collateralToPay: TEST_COLLATERAL,
|
|
494
|
+
mode: "express",
|
|
495
|
+
from: account,
|
|
496
|
+
};
|
|
497
|
+
it("slippage encoded in typed data payload", async () => {
|
|
498
|
+
const [prepared30, prepared300] = await Promise.all([
|
|
499
|
+
sdk.prepareOrder({ ...baseRequest, slippage: 30 }),
|
|
500
|
+
sdk.prepareOrder({ ...baseRequest, slippage: 300 }),
|
|
501
|
+
]);
|
|
502
|
+
expect(prepared30.estimates.sizeDeltaUsd).toBe(TEST_SIZE_USD);
|
|
503
|
+
expect(prepared300.estimates.sizeDeltaUsd).toBe(TEST_SIZE_USD);
|
|
504
|
+
// Slippage should be reflected in the typed data payload (batchParams)
|
|
505
|
+
// Higher slippage → different (wider) acceptablePrice in the on-chain params
|
|
506
|
+
const batch30 = JSON.stringify(prepared30.payload.batchParams);
|
|
507
|
+
const batch300 = JSON.stringify(prepared300.payload.batchParams);
|
|
508
|
+
expect(batch30).not.toBe(batch300);
|
|
509
|
+
});
|
|
510
|
+
it("slippage does not affect size/fee estimates", async () => {
|
|
511
|
+
const [prepared30, prepared300] = await Promise.all([
|
|
512
|
+
sdk.prepareOrder({ ...baseRequest, slippage: 30 }),
|
|
513
|
+
sdk.prepareOrder({ ...baseRequest, slippage: 300 }),
|
|
514
|
+
]);
|
|
515
|
+
expect(prepared30.estimates.sizeDeltaUsd).toBe(prepared300.estimates.sizeDeltaUsd);
|
|
516
|
+
expect(prepared30.estimates.positionFeeUsd).toBe(prepared300.estimates.positionFeeUsd);
|
|
517
|
+
});
|
|
518
|
+
});
|
|
519
|
+
describe("executionFeeBufferBps", () => {
|
|
520
|
+
const baseRequest = {
|
|
521
|
+
kind: "increase",
|
|
522
|
+
symbol: TEST_SYMBOL,
|
|
523
|
+
direction: "long",
|
|
524
|
+
orderType: "market",
|
|
525
|
+
size: TEST_SIZE_USD,
|
|
526
|
+
collateralToPay: TEST_COLLATERAL,
|
|
527
|
+
mode: "express",
|
|
528
|
+
from: account,
|
|
529
|
+
};
|
|
530
|
+
it("higher buffer → higher execution fee", async () => {
|
|
531
|
+
const [prepared0, prepared5000] = await Promise.all([
|
|
532
|
+
sdk.prepareOrder({ ...baseRequest, executionFeeBufferBps: 0 }),
|
|
533
|
+
sdk.prepareOrder({ ...baseRequest, executionFeeBufferBps: 5000 }),
|
|
534
|
+
]);
|
|
535
|
+
expect(prepared5000.estimates.executionFeeAmount).toBeGreaterThan(prepared0.estimates.executionFeeAmount);
|
|
536
|
+
});
|
|
537
|
+
it("buffer does not affect other estimates", async () => {
|
|
538
|
+
const [prepared0, prepared5000] = await Promise.all([
|
|
539
|
+
sdk.prepareOrder({ ...baseRequest, executionFeeBufferBps: 0 }),
|
|
540
|
+
sdk.prepareOrder({ ...baseRequest, executionFeeBufferBps: 5000 }),
|
|
541
|
+
]);
|
|
542
|
+
expect(prepared0.estimates.sizeDeltaUsd).toBe(prepared5000.estimates.sizeDeltaUsd);
|
|
543
|
+
expect(prepared0.estimates.positionFeeUsd).toBe(prepared5000.estimates.positionFeeUsd);
|
|
544
|
+
expect(prepared0.estimates.acceptablePrice).toBe(prepared5000.estimates.acceptablePrice);
|
|
545
|
+
});
|
|
546
|
+
});
|
|
547
|
+
describe("express convenience method", () => {
|
|
548
|
+
it("executeExpressOrder returns requestId and status", async () => {
|
|
549
|
+
const result = await sdk.executeExpressOrder({
|
|
550
|
+
kind: "increase",
|
|
551
|
+
symbol: TEST_SYMBOL,
|
|
552
|
+
direction: "long",
|
|
553
|
+
orderType: "market",
|
|
554
|
+
size: TEST_SIZE_USD,
|
|
555
|
+
collateralToPay: TEST_COLLATERAL,
|
|
556
|
+
mode: "express",
|
|
557
|
+
from: account,
|
|
558
|
+
}, signer);
|
|
559
|
+
expect(result.requestId).toBeDefined();
|
|
560
|
+
expect(result.status).toBeDefined();
|
|
561
|
+
});
|
|
562
|
+
});
|
|
563
|
+
describe("classic market increase", () => {
|
|
564
|
+
it("prepare returns transaction with estimates", async () => {
|
|
565
|
+
const prepared = await sdk.prepareOrder({
|
|
566
|
+
kind: "increase",
|
|
567
|
+
symbol: TEST_SYMBOL,
|
|
568
|
+
direction: "long",
|
|
569
|
+
orderType: "market",
|
|
570
|
+
size: TEST_SIZE_USD,
|
|
571
|
+
collateralToPay: TEST_COLLATERAL,
|
|
572
|
+
mode: "classic",
|
|
573
|
+
from: account,
|
|
574
|
+
});
|
|
575
|
+
expect(prepared.payloadType).toBe("transaction");
|
|
576
|
+
expect(prepared.payload.to).toBeDefined();
|
|
577
|
+
expect(prepared.payload.data).toBeDefined();
|
|
578
|
+
expect(prepared.payload.value).toBeDefined();
|
|
579
|
+
expect(prepared.estimates).toBeDefined();
|
|
580
|
+
expect(prepared.estimates.sizeDeltaUsd).toBe(TEST_SIZE_USD);
|
|
581
|
+
expect(prepared.estimates.executionFeeAmount).toBeGreaterThan(0n);
|
|
582
|
+
expect(prepared.estimates.positionFeeUsd).toBeGreaterThan(0n);
|
|
583
|
+
});
|
|
584
|
+
it.skipIf(!hasRpcUrl())("submit classic transaction on-chain", async () => {
|
|
585
|
+
const prepared = await sdk.prepareOrder({
|
|
586
|
+
kind: "increase",
|
|
587
|
+
symbol: TEST_SYMBOL,
|
|
588
|
+
direction: "long",
|
|
589
|
+
orderType: "market",
|
|
590
|
+
size: TEST_SIZE_USD,
|
|
591
|
+
collateralToPay: TEST_COLLATERAL,
|
|
592
|
+
mode: "classic",
|
|
593
|
+
from: account,
|
|
594
|
+
});
|
|
595
|
+
expect(prepared.payloadType).toBe("transaction");
|
|
596
|
+
const txHash = await signer.sendTransaction({
|
|
597
|
+
to: prepared.payload.to,
|
|
598
|
+
data: prepared.payload.data,
|
|
599
|
+
value: BigInt(prepared.payload.value ?? 0),
|
|
600
|
+
});
|
|
601
|
+
expect(txHash).toBeDefined();
|
|
602
|
+
expect(txHash.startsWith("0x")).toBe(true);
|
|
603
|
+
});
|
|
604
|
+
});
|
|
605
|
+
describe("with subaccount (1CT)", () => {
|
|
606
|
+
const sdkSub = getTestSdk();
|
|
607
|
+
beforeAll(async () => {
|
|
608
|
+
await activateTestSubaccount(sdkSub, signer, account);
|
|
609
|
+
});
|
|
610
|
+
afterAll(() => {
|
|
611
|
+
sdkSub.clearSubaccount();
|
|
612
|
+
});
|
|
613
|
+
it("express full flow with subaccount — submit via executeExpressOrder", async () => {
|
|
614
|
+
const result = await sdkSub.executeExpressOrder({
|
|
615
|
+
kind: "increase",
|
|
616
|
+
symbol: TEST_SYMBOL,
|
|
617
|
+
direction: "long",
|
|
618
|
+
orderType: "market",
|
|
619
|
+
size: TEST_SIZE_USD,
|
|
620
|
+
collateralToPay: TEST_COLLATERAL,
|
|
621
|
+
mode: "express",
|
|
622
|
+
from: account,
|
|
623
|
+
}, signer);
|
|
624
|
+
expect(result.requestId).toBeDefined();
|
|
625
|
+
expect(result.status).toBeDefined();
|
|
626
|
+
const status = await waitForOrderStatus(sdk, result.requestId);
|
|
627
|
+
expect(status.status).toBe("executed");
|
|
628
|
+
});
|
|
629
|
+
it("manual prepare + sign + submit with subaccount", async () => {
|
|
630
|
+
// Re-activate to get a fresh approval nonce (previous executeExpressOrder
|
|
631
|
+
// registered the old one, incrementing the on-chain nonce)
|
|
632
|
+
await activateTestSubaccount(sdkSub, signer, account);
|
|
633
|
+
const prepared = await sdkSub.prepareOrder({
|
|
634
|
+
kind: "increase",
|
|
635
|
+
symbol: TEST_SYMBOL,
|
|
636
|
+
direction: "long",
|
|
637
|
+
orderType: "market",
|
|
638
|
+
size: TEST_SIZE_USD,
|
|
639
|
+
collateralToPay: TEST_COLLATERAL,
|
|
640
|
+
mode: "express",
|
|
641
|
+
from: account,
|
|
642
|
+
subaccountAddress: sdkSub.subaccountAddress,
|
|
643
|
+
subaccountApproval: sdkSub.subaccountApprovalMessage,
|
|
644
|
+
});
|
|
645
|
+
expect(prepared.requestId).toBeDefined();
|
|
646
|
+
expect(prepared.payloadType).toBe("typed-data");
|
|
647
|
+
const signature = await sdkSub.signOrder(prepared, signer);
|
|
648
|
+
expect(signature.startsWith("0x")).toBe(true);
|
|
649
|
+
const submitted = await sdkSub.submitOrder({
|
|
650
|
+
mode: prepared.mode,
|
|
651
|
+
requestId: prepared.requestId,
|
|
652
|
+
signature,
|
|
653
|
+
from: account,
|
|
654
|
+
idempotencyKey: prepared.idempotencyKey,
|
|
655
|
+
eip712Data: {
|
|
656
|
+
batchParams: prepared.payload.batchParams,
|
|
657
|
+
relayParams: prepared.payload.relayParams,
|
|
658
|
+
subaccountApproval: sdkSub.subaccountApprovalMessage,
|
|
659
|
+
},
|
|
660
|
+
});
|
|
661
|
+
expect(submitted.requestId).toBeDefined();
|
|
662
|
+
expect(submitted.status).toBeDefined();
|
|
663
|
+
const status = await waitForOrderStatus(sdk, submitted.requestId);
|
|
664
|
+
expect(status.status).toBe("executed");
|
|
665
|
+
});
|
|
666
|
+
});
|
|
667
|
+
describe("error cases", () => {
|
|
668
|
+
it("missing symbol rejects", async () => {
|
|
669
|
+
await expect(sdk.prepareOrder({
|
|
670
|
+
kind: "increase",
|
|
671
|
+
direction: "long",
|
|
672
|
+
orderType: "market",
|
|
673
|
+
size: TEST_SIZE_USD,
|
|
674
|
+
collateralToPay: TEST_COLLATERAL,
|
|
675
|
+
mode: "express",
|
|
676
|
+
from: account,
|
|
677
|
+
})).rejects.toThrow();
|
|
678
|
+
});
|
|
679
|
+
it("missing direction rejects", async () => {
|
|
680
|
+
await expect(sdk.prepareOrder({
|
|
681
|
+
kind: "increase",
|
|
682
|
+
symbol: TEST_SYMBOL,
|
|
683
|
+
orderType: "market",
|
|
684
|
+
size: TEST_SIZE_USD,
|
|
685
|
+
collateralToPay: TEST_COLLATERAL,
|
|
686
|
+
mode: "express",
|
|
687
|
+
from: account,
|
|
688
|
+
})).rejects.toThrow();
|
|
689
|
+
});
|
|
690
|
+
it("missing size rejects", async () => {
|
|
691
|
+
await expect(sdk.prepareOrder({
|
|
692
|
+
kind: "increase",
|
|
693
|
+
symbol: TEST_SYMBOL,
|
|
694
|
+
direction: "long",
|
|
695
|
+
orderType: "market",
|
|
696
|
+
collateralToPay: TEST_COLLATERAL,
|
|
697
|
+
mode: "express",
|
|
698
|
+
from: account,
|
|
699
|
+
})).rejects.toThrow();
|
|
700
|
+
});
|
|
701
|
+
it("missing collateralToPay rejects", async () => {
|
|
702
|
+
await expect(sdk.prepareOrder({
|
|
703
|
+
kind: "increase",
|
|
704
|
+
symbol: TEST_SYMBOL,
|
|
705
|
+
direction: "long",
|
|
706
|
+
orderType: "market",
|
|
707
|
+
size: TEST_SIZE_USD,
|
|
708
|
+
mode: "express",
|
|
709
|
+
from: account,
|
|
710
|
+
})).rejects.toThrow();
|
|
711
|
+
});
|
|
712
|
+
it("invalid from address rejects", async () => {
|
|
713
|
+
await expect(sdk.prepareOrder({
|
|
714
|
+
kind: "increase",
|
|
715
|
+
symbol: TEST_SYMBOL,
|
|
716
|
+
direction: "long",
|
|
717
|
+
orderType: "market",
|
|
718
|
+
size: TEST_SIZE_USD,
|
|
719
|
+
collateralToPay: TEST_COLLATERAL,
|
|
720
|
+
mode: "express",
|
|
721
|
+
from: "0xinvalid",
|
|
722
|
+
})).rejects.toThrow();
|
|
723
|
+
});
|
|
724
|
+
it("invalid symbol rejects", async () => {
|
|
725
|
+
await expect(sdk.prepareOrder({
|
|
726
|
+
kind: "increase",
|
|
727
|
+
symbol: "NONEXISTENT/PAIR",
|
|
728
|
+
direction: "long",
|
|
729
|
+
orderType: "market",
|
|
730
|
+
size: TEST_SIZE_USD,
|
|
731
|
+
collateralToPay: TEST_COLLATERAL,
|
|
732
|
+
mode: "express",
|
|
733
|
+
from: account,
|
|
734
|
+
})).rejects.toThrow();
|
|
735
|
+
});
|
|
736
|
+
it("slippage too high rejects", async () => {
|
|
737
|
+
await expect(sdk.prepareOrder({
|
|
738
|
+
kind: "increase",
|
|
739
|
+
symbol: TEST_SYMBOL,
|
|
740
|
+
direction: "long",
|
|
741
|
+
orderType: "market",
|
|
742
|
+
size: TEST_SIZE_USD,
|
|
743
|
+
collateralToPay: TEST_COLLATERAL,
|
|
744
|
+
slippage: 20000,
|
|
745
|
+
mode: "express",
|
|
746
|
+
from: account,
|
|
747
|
+
})).rejects.toThrow();
|
|
748
|
+
});
|
|
749
|
+
it("invalid collateral token rejects", async () => {
|
|
750
|
+
await expect(sdk.prepareOrder({
|
|
751
|
+
kind: "increase",
|
|
752
|
+
symbol: TEST_SYMBOL,
|
|
753
|
+
direction: "long",
|
|
754
|
+
orderType: "market",
|
|
755
|
+
size: TEST_SIZE_USD,
|
|
756
|
+
collateralToPay: { amount: 1000000n, token: "FAKECOIN" },
|
|
757
|
+
mode: "express",
|
|
758
|
+
from: account,
|
|
759
|
+
})).rejects.toThrow();
|
|
760
|
+
});
|
|
761
|
+
});
|
|
762
|
+
});
|