@gmx-io/sdk 1.5.0-alpha-11 → 1.5.0-alpha-12
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/src/clients/v2/__tests__/decrease.spec.js +14 -0
- package/build/cjs/src/configs/api.js +9 -9
- package/build/cjs/src/utils/orderTransactions/api.js +15 -10
- package/build/cjs/src/utils/orderTransactions/utils.js +2 -2
- package/build/cjs/src/utils/orderTransactions/validateTypedData.js +7 -0
- package/build/cjs/src/utils/subaccount/generateSubaccount.js +20 -2
- package/build/cjs/src/utils/subaccount/index.js +3 -1
- package/build/cjs/src/utils/swap/swapRouting.js +1 -1
- package/build/cjs/src/utils/trade/trade.js +7 -0
- package/build/cjs/src/utils/twap/index.js +10 -4
- package/build/esm/src/clients/v2/__tests__/decrease.spec.js +14 -0
- package/build/esm/src/configs/api.js +9 -9
- package/build/esm/src/utils/orderTransactions/api.js +15 -10
- package/build/esm/src/utils/orderTransactions/utils.js +2 -2
- package/build/esm/src/utils/orderTransactions/validateTypedData.js +7 -0
- package/build/esm/src/utils/subaccount/generateSubaccount.js +14 -1
- package/build/esm/src/utils/subaccount/index.js +1 -1
- package/build/esm/src/utils/swap/swapRouting.js +1 -1
- package/build/esm/src/utils/trade/trade.js +7 -0
- package/build/esm/src/utils/twap/index.js +10 -4
- package/build/types/src/utils/subaccount/generateSubaccount.d.ts +3 -0
- package/build/types/src/utils/subaccount/index.d.ts +1 -1
- package/package.json +1 -1
|
@@ -66,6 +66,7 @@ async function ensureLongPosition() {
|
|
|
66
66
|
direction: "long",
|
|
67
67
|
orderType: "market",
|
|
68
68
|
size: decreaseSize,
|
|
69
|
+
collateralToken: "USDC",
|
|
69
70
|
receiveToken: "USDC",
|
|
70
71
|
mode: "express",
|
|
71
72
|
from: account,
|
|
@@ -103,6 +104,7 @@ async function ensureLongPosition() {
|
|
|
103
104
|
orderType: "market",
|
|
104
105
|
size: decreaseSize,
|
|
105
106
|
keepLeverage: true,
|
|
107
|
+
collateralToken: "USDC",
|
|
106
108
|
receiveToken: "USDC",
|
|
107
109
|
mode: "express",
|
|
108
110
|
from: account,
|
|
@@ -138,6 +140,7 @@ async function ensureLongPosition() {
|
|
|
138
140
|
orderType: "market",
|
|
139
141
|
size: decreaseSize,
|
|
140
142
|
keepLeverage: false,
|
|
143
|
+
collateralToken: "USDC",
|
|
141
144
|
receiveToken: "USDC",
|
|
142
145
|
mode: "express",
|
|
143
146
|
from: account,
|
|
@@ -167,6 +170,7 @@ async function ensureLongPosition() {
|
|
|
167
170
|
direction: "long",
|
|
168
171
|
orderType: "market",
|
|
169
172
|
size: testUtil_1.TEST_SIZE_USD,
|
|
173
|
+
collateralToken: "USDC",
|
|
170
174
|
receiveToken: "ETH",
|
|
171
175
|
mode: "express",
|
|
172
176
|
from: account,
|
|
@@ -183,6 +187,7 @@ async function ensureLongPosition() {
|
|
|
183
187
|
direction: "long",
|
|
184
188
|
orderType: "market",
|
|
185
189
|
size: testUtil_1.TEST_SIZE_USD,
|
|
190
|
+
collateralToken: "USDC",
|
|
186
191
|
mode: "express",
|
|
187
192
|
from: account,
|
|
188
193
|
});
|
|
@@ -218,6 +223,7 @@ async function ensureLongPosition() {
|
|
|
218
223
|
symbol: testUtil_1.TEST_SYMBOL,
|
|
219
224
|
direction: "long",
|
|
220
225
|
size: decreaseSize,
|
|
226
|
+
collateralToken: "USDC",
|
|
221
227
|
receiveToken: "USDC",
|
|
222
228
|
twapConfig: { duration: 600, parts: 2 },
|
|
223
229
|
mode: "express",
|
|
@@ -240,6 +246,7 @@ async function ensureLongPosition() {
|
|
|
240
246
|
direction: "long",
|
|
241
247
|
orderType: "market",
|
|
242
248
|
size: decreaseSize,
|
|
249
|
+
collateralToken: "USDC",
|
|
243
250
|
receiveToken: "USDC",
|
|
244
251
|
mode: "express",
|
|
245
252
|
from: account,
|
|
@@ -261,6 +268,7 @@ async function ensureLongPosition() {
|
|
|
261
268
|
direction: "long",
|
|
262
269
|
orderType: "market",
|
|
263
270
|
size: testUtil_1.TEST_SIZE_USD,
|
|
271
|
+
collateralToken: "USDC",
|
|
264
272
|
receiveToken: "USDC",
|
|
265
273
|
mode: "express",
|
|
266
274
|
from: account,
|
|
@@ -306,6 +314,7 @@ async function ensureLongPosition() {
|
|
|
306
314
|
orderType: "stop-loss",
|
|
307
315
|
size: testUtil_1.TEST_SIZE_USD,
|
|
308
316
|
triggerPrice,
|
|
317
|
+
collateralToken: "USDC",
|
|
309
318
|
receiveToken: "USDC",
|
|
310
319
|
mode: "express",
|
|
311
320
|
from: account,
|
|
@@ -330,6 +339,7 @@ async function ensureLongPosition() {
|
|
|
330
339
|
orderType: "take-profit",
|
|
331
340
|
size: testUtil_1.TEST_SIZE_USD,
|
|
332
341
|
triggerPrice,
|
|
342
|
+
collateralToken: "USDC",
|
|
333
343
|
receiveToken: "USDC",
|
|
334
344
|
mode: "express",
|
|
335
345
|
from: account,
|
|
@@ -353,6 +363,7 @@ async function ensureLongPosition() {
|
|
|
353
363
|
direction: "long",
|
|
354
364
|
orderType: "market",
|
|
355
365
|
size: testUtil_1.TEST_SIZE_USD,
|
|
366
|
+
collateralToken: "USDC",
|
|
356
367
|
receiveToken: "USDC",
|
|
357
368
|
mode: "classic",
|
|
358
369
|
from: account,
|
|
@@ -373,6 +384,7 @@ async function ensureLongPosition() {
|
|
|
373
384
|
direction: "long",
|
|
374
385
|
orderType: "market",
|
|
375
386
|
size: decreaseSize,
|
|
387
|
+
collateralToken: "USDC",
|
|
376
388
|
receiveToken: "USDC",
|
|
377
389
|
mode: "classic",
|
|
378
390
|
from: account,
|
|
@@ -404,6 +416,7 @@ async function ensureLongPosition() {
|
|
|
404
416
|
direction: "long",
|
|
405
417
|
orderType: "market",
|
|
406
418
|
size: decreaseSize,
|
|
419
|
+
collateralToken: "USDC",
|
|
407
420
|
receiveToken: "USDC",
|
|
408
421
|
mode: "express",
|
|
409
422
|
from: account,
|
|
@@ -430,6 +443,7 @@ async function ensureLongPosition() {
|
|
|
430
443
|
direction: "long",
|
|
431
444
|
orderType: "market",
|
|
432
445
|
size: fullSize,
|
|
446
|
+
collateralToken: "USDC",
|
|
433
447
|
receiveToken: "USDC",
|
|
434
448
|
mode: "express",
|
|
435
449
|
from: account,
|
|
@@ -3,20 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.isApiSupported = exports.getApiFallbackUrls = exports.getApiUrl = void 0;
|
|
4
4
|
const chains_1 = require("./chains");
|
|
5
5
|
const API_URLS = {
|
|
6
|
-
[chains_1.ARBITRUM]: "https://arbitrum.gmxapi.io/
|
|
7
|
-
[chains_1.AVALANCHE]: "https://avalanche.gmxapi.io/
|
|
6
|
+
[chains_1.ARBITRUM]: "https://arbitrum.gmxapi.io/v1",
|
|
7
|
+
[chains_1.AVALANCHE]: "https://avalanche.gmxapi.io/v1",
|
|
8
8
|
[chains_1.AVALANCHE_FUJI]: undefined,
|
|
9
|
-
[chains_1.BOTANIX]: "https://botanix.gmxapi.io/
|
|
10
|
-
[chains_1.ARBITRUM_SEPOLIA]: "https://gmx-api-arbitrum-sepolia-yp6pp.ondigitalocean.app/
|
|
11
|
-
[chains_1.MEGAETH]: "https://megaeth.gmxapi.io/
|
|
9
|
+
[chains_1.BOTANIX]: "https://botanix.gmxapi.io/v1",
|
|
10
|
+
[chains_1.ARBITRUM_SEPOLIA]: "https://gmx-api-arbitrum-sepolia-yp6pp.ondigitalocean.app/v1",
|
|
11
|
+
[chains_1.MEGAETH]: "https://megaeth.gmxapi.io/v1",
|
|
12
12
|
};
|
|
13
13
|
const API_FALLBACK_URLS = {
|
|
14
|
-
[chains_1.ARBITRUM]: ["https://arbitrum.gmxapi.ai/
|
|
15
|
-
[chains_1.AVALANCHE]: ["https://avalanche.gmxapi.ai/
|
|
14
|
+
[chains_1.ARBITRUM]: ["https://arbitrum.gmxapi.ai/v1"],
|
|
15
|
+
[chains_1.AVALANCHE]: ["https://avalanche.gmxapi.ai/v1"],
|
|
16
16
|
[chains_1.AVALANCHE_FUJI]: [],
|
|
17
|
-
[chains_1.BOTANIX]: ["https://botanix.gmxapi.ai/
|
|
17
|
+
[chains_1.BOTANIX]: ["https://botanix.gmxapi.ai/v1"],
|
|
18
18
|
[chains_1.ARBITRUM_SEPOLIA]: [],
|
|
19
|
-
[chains_1.MEGAETH]: ["https://megaeth.gmxapi.ai/
|
|
19
|
+
[chains_1.MEGAETH]: ["https://megaeth.gmxapi.ai/v1"],
|
|
20
20
|
};
|
|
21
21
|
function getApiUrl(chainId) {
|
|
22
22
|
return API_URLS[chainId];
|
|
@@ -5,16 +5,21 @@ const validateTypedData_1 = require("./validateTypedData");
|
|
|
5
5
|
function parseEstimates(raw) {
|
|
6
6
|
if (!raw)
|
|
7
7
|
return undefined;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
try {
|
|
9
|
+
return {
|
|
10
|
+
positionPriceImpactDeltaUsd: BigInt(raw.positionPriceImpactDeltaUsd ?? "0"),
|
|
11
|
+
swapPriceImpactDeltaUsd: BigInt(raw.swapPriceImpactDeltaUsd ?? "0"),
|
|
12
|
+
executionFeeAmount: BigInt(raw.executionFeeAmount ?? "0"),
|
|
13
|
+
acceptablePrice: BigInt(raw.acceptablePrice ?? "0"),
|
|
14
|
+
sizeDeltaUsd: BigInt(raw.sizeDeltaUsd ?? "0"),
|
|
15
|
+
positionFeeUsd: BigInt(raw.positionFeeUsd ?? "0"),
|
|
16
|
+
borrowingFeeUsd: BigInt(raw.borrowingFeeUsd ?? "0"),
|
|
17
|
+
fundingFeeUsd: BigInt(raw.fundingFeeUsd ?? "0"),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
throw new Error(`Failed to parse order estimates: ${e instanceof Error ? e.message : e}`);
|
|
22
|
+
}
|
|
18
23
|
}
|
|
19
24
|
function parsePrepareResponse(raw) {
|
|
20
25
|
return { ...raw, estimates: parseEstimates(raw.estimates) };
|
|
@@ -167,8 +167,8 @@ function buildDecreaseOrderPayload(p) {
|
|
|
167
167
|
}
|
|
168
168
|
exports.buildDecreaseOrderPayload = buildDecreaseOrderPayload;
|
|
169
169
|
function buildTwapOrdersPayloads(p, twapParams) {
|
|
170
|
-
if (!twapParams.numberOfParts || twapParams.numberOfParts
|
|
171
|
-
throw new Error(`TWAP numberOfParts must be
|
|
170
|
+
if (!twapParams.numberOfParts || twapParams.numberOfParts < 2) {
|
|
171
|
+
throw new Error(`TWAP numberOfParts must be >= 2, got ${twapParams.numberOfParts}`);
|
|
172
172
|
}
|
|
173
173
|
const uiFeeReceiver = (0, uiFeeReceiver_1.createTwapUiFeeReceiver)({ numberOfParts: twapParams.numberOfParts, source: twapParams.source });
|
|
174
174
|
if ((0, orders_1.isSwapOrderType)(p.orderType)) {
|
|
@@ -70,6 +70,13 @@ accountAddress) {
|
|
|
70
70
|
throw new Error(`Order receiver "${receiver}" does not match signer "${signerAddress}". Possible malicious typed data.`);
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
|
+
const cancellationReceiver = order?.addresses?.cancellationReceiver;
|
|
74
|
+
if (cancellationReceiver) {
|
|
75
|
+
const checksummedCancellationReceiver = (0, viem_1.getAddress)(cancellationReceiver);
|
|
76
|
+
if (checksummedCancellationReceiver !== ZERO_ADDRESS && !allowedReceivers.has(checksummedCancellationReceiver)) {
|
|
77
|
+
throw new Error(`Order cancellationReceiver "${cancellationReceiver}" does not match signer "${signerAddress}". Possible malicious typed data.`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
73
80
|
}
|
|
74
81
|
}
|
|
75
82
|
}
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateSubaccount = void 0;
|
|
6
|
+
exports.createSubaccountSignerFromConfig = exports.decryptSubaccountPrivateKey = exports.generateSubaccount = void 0;
|
|
7
|
+
const crypto_js_1 = __importDefault(require("crypto-js"));
|
|
4
8
|
const viem_1 = require("viem");
|
|
5
9
|
const accounts_1 = require("viem/accounts");
|
|
6
10
|
const express_1 = require("../../configs/express");
|
|
@@ -10,10 +14,24 @@ async function generateSubaccount(mainSigner) {
|
|
|
10
14
|
const pk = (0, viem_1.keccak256)(signature);
|
|
11
15
|
const account = (0, accounts_1.privateKeyToAccount)(pk);
|
|
12
16
|
const signer = new signer_1.PrivateKeySigner(pk);
|
|
17
|
+
const encryptedPrivateKey = crypto_js_1.default.AES.encrypt(pk, mainSigner.address).toString();
|
|
13
18
|
return {
|
|
14
19
|
address: account.address,
|
|
15
20
|
signer,
|
|
16
|
-
privateKey:
|
|
21
|
+
privateKey: encryptedPrivateKey,
|
|
17
22
|
};
|
|
18
23
|
}
|
|
19
24
|
exports.generateSubaccount = generateSubaccount;
|
|
25
|
+
function decryptSubaccountPrivateKey(encryptedPrivateKey, mainAccountAddress) {
|
|
26
|
+
const decrypted = crypto_js_1.default.AES.decrypt(encryptedPrivateKey, mainAccountAddress).toString(crypto_js_1.default.enc.Utf8);
|
|
27
|
+
if (!decrypted) {
|
|
28
|
+
throw new Error("Failed to decrypt subaccount private key");
|
|
29
|
+
}
|
|
30
|
+
return decrypted;
|
|
31
|
+
}
|
|
32
|
+
exports.decryptSubaccountPrivateKey = decryptSubaccountPrivateKey;
|
|
33
|
+
function createSubaccountSignerFromConfig(encryptedPrivateKey, mainAccountAddress) {
|
|
34
|
+
const pk = decryptSubaccountPrivateKey(encryptedPrivateKey, mainAccountAddress);
|
|
35
|
+
return new signer_1.PrivateKeySigner(pk);
|
|
36
|
+
}
|
|
37
|
+
exports.createSubaccountSignerFromConfig = createSubaccountSignerFromConfig;
|
|
@@ -14,10 +14,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.signSubaccountApproval = exports.prepareSubaccountApproval = exports.fetchSubaccountStatus = exports.getSubaccountApprovalTypedData = exports.getEmptySubaccountApproval = exports.hashSubaccountApproval = exports.generateSubaccount = void 0;
|
|
17
|
+
exports.signSubaccountApproval = exports.prepareSubaccountApproval = exports.fetchSubaccountStatus = exports.getSubaccountApprovalTypedData = exports.getEmptySubaccountApproval = exports.hashSubaccountApproval = exports.createSubaccountSignerFromConfig = exports.decryptSubaccountPrivateKey = exports.generateSubaccount = void 0;
|
|
18
18
|
__exportStar(require("./types"), exports);
|
|
19
19
|
var generateSubaccount_1 = require("./generateSubaccount");
|
|
20
20
|
Object.defineProperty(exports, "generateSubaccount", { enumerable: true, get: function () { return generateSubaccount_1.generateSubaccount; } });
|
|
21
|
+
Object.defineProperty(exports, "decryptSubaccountPrivateKey", { enumerable: true, get: function () { return generateSubaccount_1.decryptSubaccountPrivateKey; } });
|
|
22
|
+
Object.defineProperty(exports, "createSubaccountSignerFromConfig", { enumerable: true, get: function () { return generateSubaccount_1.createSubaccountSignerFromConfig; } });
|
|
21
23
|
var hashSubaccountApproval_1 = require("./hashSubaccountApproval");
|
|
22
24
|
Object.defineProperty(exports, "hashSubaccountApproval", { enumerable: true, get: function () { return hashSubaccountApproval_1.hashSubaccountApproval; } });
|
|
23
25
|
var getEmptySubaccountApproval_1 = require("./getEmptySubaccountApproval");
|
|
@@ -103,7 +103,7 @@ function getBestSwapPath({ routes, usdIn, estimator, networkEstimator, }) {
|
|
|
103
103
|
if (routes.length === 0) {
|
|
104
104
|
return undefined;
|
|
105
105
|
}
|
|
106
|
-
let bestRoute =
|
|
106
|
+
let bestRoute = undefined;
|
|
107
107
|
let bestUsdOut = 0n;
|
|
108
108
|
for (const route of routes) {
|
|
109
109
|
try {
|
|
@@ -6,7 +6,11 @@ const bigmath_1 = require("../bigmath");
|
|
|
6
6
|
const types_1 = require("../orders/types");
|
|
7
7
|
const prices_1 = require("../prices");
|
|
8
8
|
const types_2 = require("./types");
|
|
9
|
+
const MAX_ALLOWED_SLIPPAGE = 5000; // 50%
|
|
9
10
|
function applySlippageToPrice(allowedSlippage, price, isIncrease, isLong) {
|
|
11
|
+
if (allowedSlippage < 0 || allowedSlippage > MAX_ALLOWED_SLIPPAGE) {
|
|
12
|
+
throw new Error(`allowedSlippage must be between 0 and ${MAX_ALLOWED_SLIPPAGE} bps, got ${allowedSlippage}`);
|
|
13
|
+
}
|
|
10
14
|
const shouldIncreasePrice = (0, prices_1.getShouldUseMaxPrice)(isIncrease, isLong);
|
|
11
15
|
const slippageBasisPoints = shouldIncreasePrice
|
|
12
16
|
? factors_1.BASIS_POINTS_DIVISOR + allowedSlippage
|
|
@@ -15,6 +19,9 @@ function applySlippageToPrice(allowedSlippage, price, isIncrease, isLong) {
|
|
|
15
19
|
}
|
|
16
20
|
exports.applySlippageToPrice = applySlippageToPrice;
|
|
17
21
|
function applySlippageToMinOut(allowedSlippage, minOutputAmount) {
|
|
22
|
+
if (allowedSlippage < 0 || allowedSlippage > MAX_ALLOWED_SLIPPAGE) {
|
|
23
|
+
throw new Error(`allowedSlippage must be between 0 and ${MAX_ALLOWED_SLIPPAGE} bps, got ${allowedSlippage}`);
|
|
24
|
+
}
|
|
18
25
|
const slippageBasisPoints = factors_1.BASIS_POINTS_DIVISOR - allowedSlippage;
|
|
19
26
|
return bigmath_1.bigMath.mulDiv(minOutputAmount, BigInt(slippageBasisPoints), factors_1.BASIS_POINTS_DIVISOR_BIGINT);
|
|
20
27
|
}
|
|
@@ -33,10 +33,13 @@ function getTwapDurationInSeconds(duration) {
|
|
|
33
33
|
}
|
|
34
34
|
exports.getTwapDurationInSeconds = getTwapDurationInSeconds;
|
|
35
35
|
function getTwapValidFromTime(duration, numberOfParts, partIndex) {
|
|
36
|
+
if (numberOfParts < 2) {
|
|
37
|
+
throw new Error(`TWAP numberOfParts must be >= 2, got ${numberOfParts}`);
|
|
38
|
+
}
|
|
36
39
|
const durationMinutes = duration.hours * 60 + duration.minutes;
|
|
37
|
-
const
|
|
40
|
+
const durationSeconds = durationMinutes * 60;
|
|
38
41
|
const startTime = Math.ceil(Date.now() / 1000);
|
|
39
|
-
return BigInt(Math.floor(startTime + (
|
|
42
|
+
return BigInt(Math.floor(startTime + (durationSeconds / (numberOfParts - 1)) * partIndex));
|
|
40
43
|
}
|
|
41
44
|
exports.getTwapValidFromTime = getTwapValidFromTime;
|
|
42
45
|
function changeTwapNumberOfPartsValue(value) {
|
|
@@ -61,11 +64,14 @@ function getTwapOrderKey({ twapId, orderType, pool, isLong, collateralTokenSymbo
|
|
|
61
64
|
}
|
|
62
65
|
exports.getTwapOrderKey = getTwapOrderKey;
|
|
63
66
|
function makeTwapValidFromTimeGetter(duration, numberOfParts) {
|
|
67
|
+
if (numberOfParts < 2) {
|
|
68
|
+
throw new Error(`TWAP numberOfParts must be >= 2, got ${numberOfParts}`);
|
|
69
|
+
}
|
|
64
70
|
const durationMinutes = duration.hours * 60 + duration.minutes;
|
|
65
|
-
const
|
|
71
|
+
const durationSeconds = durationMinutes * 60;
|
|
66
72
|
const startTime = Math.ceil(Date.now() / 1000);
|
|
67
73
|
return (part) => {
|
|
68
|
-
return BigInt(Math.floor(startTime + (
|
|
74
|
+
return BigInt(Math.floor(startTime + (durationSeconds / (numberOfParts - 1)) * part));
|
|
69
75
|
};
|
|
70
76
|
}
|
|
71
77
|
exports.makeTwapValidFromTimeGetter = makeTwapValidFromTimeGetter;
|
|
@@ -64,6 +64,7 @@ describe("decrease orders", () => {
|
|
|
64
64
|
direction: "long",
|
|
65
65
|
orderType: "market",
|
|
66
66
|
size: decreaseSize,
|
|
67
|
+
collateralToken: "USDC",
|
|
67
68
|
receiveToken: "USDC",
|
|
68
69
|
mode: "express",
|
|
69
70
|
from: account,
|
|
@@ -101,6 +102,7 @@ describe("decrease orders", () => {
|
|
|
101
102
|
orderType: "market",
|
|
102
103
|
size: decreaseSize,
|
|
103
104
|
keepLeverage: true,
|
|
105
|
+
collateralToken: "USDC",
|
|
104
106
|
receiveToken: "USDC",
|
|
105
107
|
mode: "express",
|
|
106
108
|
from: account,
|
|
@@ -136,6 +138,7 @@ describe("decrease orders", () => {
|
|
|
136
138
|
orderType: "market",
|
|
137
139
|
size: decreaseSize,
|
|
138
140
|
keepLeverage: false,
|
|
141
|
+
collateralToken: "USDC",
|
|
139
142
|
receiveToken: "USDC",
|
|
140
143
|
mode: "express",
|
|
141
144
|
from: account,
|
|
@@ -165,6 +168,7 @@ describe("decrease orders", () => {
|
|
|
165
168
|
direction: "long",
|
|
166
169
|
orderType: "market",
|
|
167
170
|
size: TEST_SIZE_USD,
|
|
171
|
+
collateralToken: "USDC",
|
|
168
172
|
receiveToken: "ETH",
|
|
169
173
|
mode: "express",
|
|
170
174
|
from: account,
|
|
@@ -181,6 +185,7 @@ describe("decrease orders", () => {
|
|
|
181
185
|
direction: "long",
|
|
182
186
|
orderType: "market",
|
|
183
187
|
size: TEST_SIZE_USD,
|
|
188
|
+
collateralToken: "USDC",
|
|
184
189
|
mode: "express",
|
|
185
190
|
from: account,
|
|
186
191
|
});
|
|
@@ -216,6 +221,7 @@ describe("decrease orders", () => {
|
|
|
216
221
|
symbol: TEST_SYMBOL,
|
|
217
222
|
direction: "long",
|
|
218
223
|
size: decreaseSize,
|
|
224
|
+
collateralToken: "USDC",
|
|
219
225
|
receiveToken: "USDC",
|
|
220
226
|
twapConfig: { duration: 600, parts: 2 },
|
|
221
227
|
mode: "express",
|
|
@@ -238,6 +244,7 @@ describe("decrease orders", () => {
|
|
|
238
244
|
direction: "long",
|
|
239
245
|
orderType: "market",
|
|
240
246
|
size: decreaseSize,
|
|
247
|
+
collateralToken: "USDC",
|
|
241
248
|
receiveToken: "USDC",
|
|
242
249
|
mode: "express",
|
|
243
250
|
from: account,
|
|
@@ -259,6 +266,7 @@ describe("decrease orders", () => {
|
|
|
259
266
|
direction: "long",
|
|
260
267
|
orderType: "market",
|
|
261
268
|
size: TEST_SIZE_USD,
|
|
269
|
+
collateralToken: "USDC",
|
|
262
270
|
receiveToken: "USDC",
|
|
263
271
|
mode: "express",
|
|
264
272
|
from: account,
|
|
@@ -304,6 +312,7 @@ describe("decrease orders", () => {
|
|
|
304
312
|
orderType: "stop-loss",
|
|
305
313
|
size: TEST_SIZE_USD,
|
|
306
314
|
triggerPrice,
|
|
315
|
+
collateralToken: "USDC",
|
|
307
316
|
receiveToken: "USDC",
|
|
308
317
|
mode: "express",
|
|
309
318
|
from: account,
|
|
@@ -328,6 +337,7 @@ describe("decrease orders", () => {
|
|
|
328
337
|
orderType: "take-profit",
|
|
329
338
|
size: TEST_SIZE_USD,
|
|
330
339
|
triggerPrice,
|
|
340
|
+
collateralToken: "USDC",
|
|
331
341
|
receiveToken: "USDC",
|
|
332
342
|
mode: "express",
|
|
333
343
|
from: account,
|
|
@@ -351,6 +361,7 @@ describe("decrease orders", () => {
|
|
|
351
361
|
direction: "long",
|
|
352
362
|
orderType: "market",
|
|
353
363
|
size: TEST_SIZE_USD,
|
|
364
|
+
collateralToken: "USDC",
|
|
354
365
|
receiveToken: "USDC",
|
|
355
366
|
mode: "classic",
|
|
356
367
|
from: account,
|
|
@@ -371,6 +382,7 @@ describe("decrease orders", () => {
|
|
|
371
382
|
direction: "long",
|
|
372
383
|
orderType: "market",
|
|
373
384
|
size: decreaseSize,
|
|
385
|
+
collateralToken: "USDC",
|
|
374
386
|
receiveToken: "USDC",
|
|
375
387
|
mode: "classic",
|
|
376
388
|
from: account,
|
|
@@ -402,6 +414,7 @@ describe("decrease orders", () => {
|
|
|
402
414
|
direction: "long",
|
|
403
415
|
orderType: "market",
|
|
404
416
|
size: decreaseSize,
|
|
417
|
+
collateralToken: "USDC",
|
|
405
418
|
receiveToken: "USDC",
|
|
406
419
|
mode: "express",
|
|
407
420
|
from: account,
|
|
@@ -428,6 +441,7 @@ describe("decrease orders", () => {
|
|
|
428
441
|
direction: "long",
|
|
429
442
|
orderType: "market",
|
|
430
443
|
size: fullSize,
|
|
444
|
+
collateralToken: "USDC",
|
|
431
445
|
receiveToken: "USDC",
|
|
432
446
|
mode: "express",
|
|
433
447
|
from: account,
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { ARBITRUM, ARBITRUM_SEPOLIA, AVALANCHE, AVALANCHE_FUJI, BOTANIX, MEGAETH } from "./chains";
|
|
2
2
|
const API_URLS = {
|
|
3
|
-
[ARBITRUM]: "https://arbitrum.gmxapi.io/
|
|
4
|
-
[AVALANCHE]: "https://avalanche.gmxapi.io/
|
|
3
|
+
[ARBITRUM]: "https://arbitrum.gmxapi.io/v1",
|
|
4
|
+
[AVALANCHE]: "https://avalanche.gmxapi.io/v1",
|
|
5
5
|
[AVALANCHE_FUJI]: undefined,
|
|
6
|
-
[BOTANIX]: "https://botanix.gmxapi.io/
|
|
7
|
-
[ARBITRUM_SEPOLIA]: "https://gmx-api-arbitrum-sepolia-yp6pp.ondigitalocean.app/
|
|
8
|
-
[MEGAETH]: "https://megaeth.gmxapi.io/
|
|
6
|
+
[BOTANIX]: "https://botanix.gmxapi.io/v1",
|
|
7
|
+
[ARBITRUM_SEPOLIA]: "https://gmx-api-arbitrum-sepolia-yp6pp.ondigitalocean.app/v1",
|
|
8
|
+
[MEGAETH]: "https://megaeth.gmxapi.io/v1",
|
|
9
9
|
};
|
|
10
10
|
const API_FALLBACK_URLS = {
|
|
11
|
-
[ARBITRUM]: ["https://arbitrum.gmxapi.ai/
|
|
12
|
-
[AVALANCHE]: ["https://avalanche.gmxapi.ai/
|
|
11
|
+
[ARBITRUM]: ["https://arbitrum.gmxapi.ai/v1"],
|
|
12
|
+
[AVALANCHE]: ["https://avalanche.gmxapi.ai/v1"],
|
|
13
13
|
[AVALANCHE_FUJI]: [],
|
|
14
|
-
[BOTANIX]: ["https://botanix.gmxapi.ai/
|
|
14
|
+
[BOTANIX]: ["https://botanix.gmxapi.ai/v1"],
|
|
15
15
|
[ARBITRUM_SEPOLIA]: [],
|
|
16
|
-
[MEGAETH]: ["https://megaeth.gmxapi.ai/
|
|
16
|
+
[MEGAETH]: ["https://megaeth.gmxapi.ai/v1"],
|
|
17
17
|
};
|
|
18
18
|
export function getApiUrl(chainId) {
|
|
19
19
|
return API_URLS[chainId];
|
|
@@ -2,16 +2,21 @@ import { validateOrderTypedData } from "./validateTypedData";
|
|
|
2
2
|
function parseEstimates(raw) {
|
|
3
3
|
if (!raw)
|
|
4
4
|
return undefined;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
5
|
+
try {
|
|
6
|
+
return {
|
|
7
|
+
positionPriceImpactDeltaUsd: BigInt(raw.positionPriceImpactDeltaUsd ?? "0"),
|
|
8
|
+
swapPriceImpactDeltaUsd: BigInt(raw.swapPriceImpactDeltaUsd ?? "0"),
|
|
9
|
+
executionFeeAmount: BigInt(raw.executionFeeAmount ?? "0"),
|
|
10
|
+
acceptablePrice: BigInt(raw.acceptablePrice ?? "0"),
|
|
11
|
+
sizeDeltaUsd: BigInt(raw.sizeDeltaUsd ?? "0"),
|
|
12
|
+
positionFeeUsd: BigInt(raw.positionFeeUsd ?? "0"),
|
|
13
|
+
borrowingFeeUsd: BigInt(raw.borrowingFeeUsd ?? "0"),
|
|
14
|
+
fundingFeeUsd: BigInt(raw.fundingFeeUsd ?? "0"),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
throw new Error(`Failed to parse order estimates: ${e instanceof Error ? e.message : e}`);
|
|
19
|
+
}
|
|
15
20
|
}
|
|
16
21
|
function parsePrepareResponse(raw) {
|
|
17
22
|
return { ...raw, estimates: parseEstimates(raw.estimates) };
|
|
@@ -158,8 +158,8 @@ export function buildDecreaseOrderPayload(p) {
|
|
|
158
158
|
};
|
|
159
159
|
}
|
|
160
160
|
export function buildTwapOrdersPayloads(p, twapParams) {
|
|
161
|
-
if (!twapParams.numberOfParts || twapParams.numberOfParts
|
|
162
|
-
throw new Error(`TWAP numberOfParts must be
|
|
161
|
+
if (!twapParams.numberOfParts || twapParams.numberOfParts < 2) {
|
|
162
|
+
throw new Error(`TWAP numberOfParts must be >= 2, got ${twapParams.numberOfParts}`);
|
|
163
163
|
}
|
|
164
164
|
const uiFeeReceiver = createTwapUiFeeReceiver({ numberOfParts: twapParams.numberOfParts, source: twapParams.source });
|
|
165
165
|
if (isSwapOrderType(p.orderType)) {
|
|
@@ -67,6 +67,13 @@ accountAddress) {
|
|
|
67
67
|
throw new Error(`Order receiver "${receiver}" does not match signer "${signerAddress}". Possible malicious typed data.`);
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
|
+
const cancellationReceiver = order?.addresses?.cancellationReceiver;
|
|
71
|
+
if (cancellationReceiver) {
|
|
72
|
+
const checksummedCancellationReceiver = getAddress(cancellationReceiver);
|
|
73
|
+
if (checksummedCancellationReceiver !== ZERO_ADDRESS && !allowedReceivers.has(checksummedCancellationReceiver)) {
|
|
74
|
+
throw new Error(`Order cancellationReceiver "${cancellationReceiver}" does not match signer "${signerAddress}". Possible malicious typed data.`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
70
77
|
}
|
|
71
78
|
}
|
|
72
79
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import cryptoJs from "crypto-js";
|
|
1
2
|
import { keccak256 } from "viem";
|
|
2
3
|
import { privateKeyToAccount } from "viem/accounts";
|
|
3
4
|
import { SUBACCOUNT_MESSAGE } from "../../configs/express";
|
|
@@ -7,9 +8,21 @@ export async function generateSubaccount(mainSigner) {
|
|
|
7
8
|
const pk = keccak256(signature);
|
|
8
9
|
const account = privateKeyToAccount(pk);
|
|
9
10
|
const signer = new PrivateKeySigner(pk);
|
|
11
|
+
const encryptedPrivateKey = cryptoJs.AES.encrypt(pk, mainSigner.address).toString();
|
|
10
12
|
return {
|
|
11
13
|
address: account.address,
|
|
12
14
|
signer,
|
|
13
|
-
privateKey:
|
|
15
|
+
privateKey: encryptedPrivateKey,
|
|
14
16
|
};
|
|
15
17
|
}
|
|
18
|
+
export function decryptSubaccountPrivateKey(encryptedPrivateKey, mainAccountAddress) {
|
|
19
|
+
const decrypted = cryptoJs.AES.decrypt(encryptedPrivateKey, mainAccountAddress).toString(cryptoJs.enc.Utf8);
|
|
20
|
+
if (!decrypted) {
|
|
21
|
+
throw new Error("Failed to decrypt subaccount private key");
|
|
22
|
+
}
|
|
23
|
+
return decrypted;
|
|
24
|
+
}
|
|
25
|
+
export function createSubaccountSignerFromConfig(encryptedPrivateKey, mainAccountAddress) {
|
|
26
|
+
const pk = decryptSubaccountPrivateKey(encryptedPrivateKey, mainAccountAddress);
|
|
27
|
+
return new PrivateKeySigner(pk);
|
|
28
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export * from "./types";
|
|
2
|
-
export { generateSubaccount } from "./generateSubaccount";
|
|
2
|
+
export { generateSubaccount, decryptSubaccountPrivateKey, createSubaccountSignerFromConfig, } from "./generateSubaccount";
|
|
3
3
|
export { hashSubaccountApproval } from "./hashSubaccountApproval";
|
|
4
4
|
export { getEmptySubaccountApproval } from "./getEmptySubaccountApproval";
|
|
5
5
|
export { getSubaccountApprovalTypedData, } from "./getSubaccountApprovalTypedData";
|
|
@@ -96,7 +96,7 @@ export function getBestSwapPath({ routes, usdIn, estimator, networkEstimator, })
|
|
|
96
96
|
if (routes.length === 0) {
|
|
97
97
|
return undefined;
|
|
98
98
|
}
|
|
99
|
-
let bestRoute =
|
|
99
|
+
let bestRoute = undefined;
|
|
100
100
|
let bestUsdOut = 0n;
|
|
101
101
|
for (const route of routes) {
|
|
102
102
|
try {
|
|
@@ -3,7 +3,11 @@ import { bigMath } from "../bigmath";
|
|
|
3
3
|
import { DecreasePositionSwapType } from "../orders/types";
|
|
4
4
|
import { getShouldUseMaxPrice } from "../prices";
|
|
5
5
|
import { TradeMode, TradeType, } from "./types";
|
|
6
|
+
const MAX_ALLOWED_SLIPPAGE = 5000; // 50%
|
|
6
7
|
export function applySlippageToPrice(allowedSlippage, price, isIncrease, isLong) {
|
|
8
|
+
if (allowedSlippage < 0 || allowedSlippage > MAX_ALLOWED_SLIPPAGE) {
|
|
9
|
+
throw new Error(`allowedSlippage must be between 0 and ${MAX_ALLOWED_SLIPPAGE} bps, got ${allowedSlippage}`);
|
|
10
|
+
}
|
|
7
11
|
const shouldIncreasePrice = getShouldUseMaxPrice(isIncrease, isLong);
|
|
8
12
|
const slippageBasisPoints = shouldIncreasePrice
|
|
9
13
|
? BASIS_POINTS_DIVISOR + allowedSlippage
|
|
@@ -11,6 +15,9 @@ export function applySlippageToPrice(allowedSlippage, price, isIncrease, isLong)
|
|
|
11
15
|
return bigMath.mulDiv(price, BigInt(slippageBasisPoints), BASIS_POINTS_DIVISOR_BIGINT);
|
|
12
16
|
}
|
|
13
17
|
export function applySlippageToMinOut(allowedSlippage, minOutputAmount) {
|
|
18
|
+
if (allowedSlippage < 0 || allowedSlippage > MAX_ALLOWED_SLIPPAGE) {
|
|
19
|
+
throw new Error(`allowedSlippage must be between 0 and ${MAX_ALLOWED_SLIPPAGE} bps, got ${allowedSlippage}`);
|
|
20
|
+
}
|
|
14
21
|
const slippageBasisPoints = BASIS_POINTS_DIVISOR - allowedSlippage;
|
|
15
22
|
return bigMath.mulDiv(minOutputAmount, BigInt(slippageBasisPoints), BASIS_POINTS_DIVISOR_BIGINT);
|
|
16
23
|
}
|
|
@@ -12,10 +12,13 @@ export function getTwapDurationInSeconds(duration) {
|
|
|
12
12
|
return duration.hours * 60 * 60 + duration.minutes * 60;
|
|
13
13
|
}
|
|
14
14
|
export function getTwapValidFromTime(duration, numberOfParts, partIndex) {
|
|
15
|
+
if (numberOfParts < 2) {
|
|
16
|
+
throw new Error(`TWAP numberOfParts must be >= 2, got ${numberOfParts}`);
|
|
17
|
+
}
|
|
15
18
|
const durationMinutes = duration.hours * 60 + duration.minutes;
|
|
16
|
-
const
|
|
19
|
+
const durationSeconds = durationMinutes * 60;
|
|
17
20
|
const startTime = Math.ceil(Date.now() / 1000);
|
|
18
|
-
return BigInt(Math.floor(startTime + (
|
|
21
|
+
return BigInt(Math.floor(startTime + (durationSeconds / (numberOfParts - 1)) * partIndex));
|
|
19
22
|
}
|
|
20
23
|
export function changeTwapNumberOfPartsValue(value) {
|
|
21
24
|
if (value < MIN_TWAP_NUMBER_OF_PARTS) {
|
|
@@ -37,10 +40,13 @@ export function getTwapOrderKey({ twapId, orderType, pool, isLong, collateralTok
|
|
|
37
40
|
return `${twapId}-${type}-${pool}-${collateralTokenSymbol}`;
|
|
38
41
|
}
|
|
39
42
|
export function makeTwapValidFromTimeGetter(duration, numberOfParts) {
|
|
43
|
+
if (numberOfParts < 2) {
|
|
44
|
+
throw new Error(`TWAP numberOfParts must be >= 2, got ${numberOfParts}`);
|
|
45
|
+
}
|
|
40
46
|
const durationMinutes = duration.hours * 60 + duration.minutes;
|
|
41
|
-
const
|
|
47
|
+
const durationSeconds = durationMinutes * 60;
|
|
42
48
|
const startTime = Math.ceil(Date.now() / 1000);
|
|
43
49
|
return (part) => {
|
|
44
|
-
return BigInt(Math.floor(startTime + (
|
|
50
|
+
return BigInt(Math.floor(startTime + (durationSeconds / (numberOfParts - 1)) * part));
|
|
45
51
|
};
|
|
46
52
|
}
|
|
@@ -3,6 +3,9 @@ import { PrivateKeySigner } from "../signer";
|
|
|
3
3
|
export type GeneratedSubaccount = {
|
|
4
4
|
address: string;
|
|
5
5
|
signer: PrivateKeySigner;
|
|
6
|
+
/** AES-encrypted private key (encrypted with main account address) */
|
|
6
7
|
privateKey: string;
|
|
7
8
|
};
|
|
8
9
|
export declare function generateSubaccount(mainSigner: IAbstractSigner): Promise<GeneratedSubaccount>;
|
|
10
|
+
export declare function decryptSubaccountPrivateKey(encryptedPrivateKey: string, mainAccountAddress: string): `0x${string}`;
|
|
11
|
+
export declare function createSubaccountSignerFromConfig(encryptedPrivateKey: string, mainAccountAddress: string): PrivateKeySigner;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export * from "./types";
|
|
2
|
-
export { generateSubaccount, type GeneratedSubaccount } from "./generateSubaccount";
|
|
2
|
+
export { generateSubaccount, decryptSubaccountPrivateKey, createSubaccountSignerFromConfig, type GeneratedSubaccount, } from "./generateSubaccount";
|
|
3
3
|
export { hashSubaccountApproval } from "./hashSubaccountApproval";
|
|
4
4
|
export { getEmptySubaccountApproval } from "./getEmptySubaccountApproval";
|
|
5
5
|
export { getSubaccountApprovalTypedData, type SubaccountApprovalTypedData, } from "./getSubaccountApprovalTypedData";
|