@gmx-io/sdk 1.5.0-alpha-17 → 1.6.0
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/v1/modules/orders/utils.js +1 -1
- package/build/cjs/src/clients/v1/modules/trades/trades.js +1 -0
- package/build/cjs/src/clients/v2/__tests__/multichain.spec.js +2 -2
- package/build/cjs/src/clients/v2/__tests__/multichainTestUtil.js +2 -1
- package/build/cjs/src/clients/v2/index.js +52 -48
- package/build/cjs/src/codegen/subsquid.js +6 -0
- package/build/cjs/src/configs/__tests__/markets.spec.js +11 -0
- package/build/cjs/src/configs/api.js +38 -18
- package/build/cjs/src/utils/__tests__/objects.spec.js +16 -0
- package/build/cjs/src/utils/__tests__/positions.spec.js +163 -0
- package/build/cjs/src/utils/__tests__/uiFeeReceiver.spec.js +5 -0
- package/build/cjs/src/utils/apy/api.js +1 -1
- package/build/cjs/src/utils/balances/api.js +2 -2
- package/build/cjs/src/utils/buyback/api.js +1 -1
- package/build/cjs/src/utils/express/utils/oracleParamsUtils.js +1 -4
- package/build/cjs/src/utils/express/utils/relayParamsUtils.js +0 -1
- package/build/cjs/src/utils/{multichainTransferApi → gmxAccountApi}/api.js +8 -4
- package/build/cjs/src/utils/jitLiquidity/api.js +12 -0
- package/build/cjs/src/utils/jitLiquidity/index.js +19 -0
- package/build/cjs/src/utils/jitLiquidity/types.js +2 -0
- package/build/cjs/src/utils/jitLiquidity/utils.js +108 -0
- package/build/cjs/src/utils/jitLiquidity/utils.spec.js +133 -0
- package/build/cjs/src/utils/markets/api.js +4 -4
- package/build/cjs/src/utils/markets/utils.js +4 -2
- package/build/cjs/src/utils/multichain/__tests__/api.spec.js +4 -4
- package/build/cjs/src/utils/multichain/api.js +3 -0
- package/build/cjs/src/utils/multichain/quotes.js +0 -1
- package/build/cjs/src/utils/numbers/utils.js +1 -1
- package/build/cjs/src/utils/objects/utils.js +13 -1
- package/build/cjs/src/utils/orderTransactions/api.js +6 -6
- package/build/cjs/src/utils/orders/api.js +1 -1
- package/build/cjs/src/utils/pairs/api.js +1 -1
- package/build/cjs/src/utils/performance/api.js +2 -2
- package/build/cjs/src/utils/positions/api.js +1 -1
- package/build/cjs/src/utils/positions/utils.js +29 -1
- package/build/cjs/src/utils/prices/api.js +1 -1
- package/build/cjs/src/utils/rates/api.js +1 -1
- package/build/cjs/src/utils/rpc/createViemRpc.js +1 -1
- package/build/cjs/src/utils/staking/api.js +1 -1
- package/build/cjs/src/utils/subaccount/api.js +2 -2
- package/build/cjs/src/utils/tokens/api.js +1 -1
- package/build/cjs/src/utils/trade/__tests__/getMaxWithdrawAmount.spec.js +121 -0
- package/build/cjs/src/utils/trade/decrease.js +60 -3
- package/build/cjs/src/utils/trade/decreaseOutputs.js +47 -0
- package/build/cjs/src/utils/trade/index.js +1 -0
- package/build/cjs/src/utils/tradeHistory/utils.js +1 -0
- package/build/cjs/src/utils/trades/api.js +2 -2
- package/build/cjs/src/utils/twap/uiFeeReceiver.js +3 -0
- package/build/esm/scripts/subsquid-codegen.js +1 -1
- package/build/esm/src/clients/v1/modules/orders/utils.js +1 -1
- package/build/esm/src/clients/v1/modules/trades/trades.js +1 -0
- package/build/esm/src/clients/v2/__tests__/multichain.spec.js +3 -3
- package/build/esm/src/clients/v2/__tests__/multichainTestUtil.js +1 -0
- package/build/esm/src/clients/v2/index.js +5 -1
- package/build/esm/src/codegen/subsquid.js +6 -0
- package/build/esm/src/configs/__tests__/markets.spec.js +12 -1
- package/build/esm/src/configs/api.js +38 -18
- package/build/esm/src/utils/__tests__/objects.spec.js +17 -1
- package/build/esm/src/utils/__tests__/positions.spec.js +164 -1
- package/build/esm/src/utils/__tests__/uiFeeReceiver.spec.js +5 -0
- package/build/esm/src/utils/apy/api.js +1 -1
- package/build/esm/src/utils/balances/api.js +2 -2
- package/build/esm/src/utils/buyback/api.js +1 -1
- package/build/esm/src/utils/express/utils/oracleParamsUtils.js +1 -4
- package/build/esm/src/utils/express/utils/relayParamsUtils.js +0 -1
- package/build/esm/src/utils/{multichainTransferApi → gmxAccountApi}/api.js +8 -4
- package/build/esm/src/utils/jitLiquidity/api.js +8 -0
- package/build/esm/src/utils/jitLiquidity/index.js +3 -0
- package/build/esm/src/utils/jitLiquidity/types.js +1 -0
- package/build/esm/src/utils/jitLiquidity/utils.js +101 -0
- package/build/esm/src/utils/jitLiquidity/utils.spec.js +131 -0
- package/build/esm/src/utils/markets/api.js +4 -4
- package/build/esm/src/utils/markets/utils.js +4 -2
- package/build/esm/src/utils/multichain/__tests__/api.spec.js +4 -4
- package/build/esm/src/utils/multichain/api.js +3 -0
- package/build/esm/src/utils/multichain/quotes.js +0 -1
- package/build/esm/src/utils/numbers/utils.js +1 -1
- package/build/esm/src/utils/objects/utils.js +9 -0
- package/build/esm/src/utils/orderTransactions/api.js +6 -6
- package/build/esm/src/utils/orders/api.js +1 -1
- package/build/esm/src/utils/pairs/api.js +1 -1
- package/build/esm/src/utils/performance/api.js +2 -2
- package/build/esm/src/utils/positions/api.js +1 -1
- package/build/esm/src/utils/positions/utils.js +27 -0
- package/build/esm/src/utils/prices/api.js +1 -1
- package/build/esm/src/utils/rates/api.js +1 -1
- package/build/esm/src/utils/rpc/createViemRpc.js +1 -1
- package/build/esm/src/utils/staking/api.js +1 -1
- package/build/esm/src/utils/subaccount/api.js +2 -2
- package/build/esm/src/utils/tokens/api.js +1 -1
- package/build/esm/src/utils/trade/__tests__/getMaxWithdrawAmount.spec.js +119 -0
- package/build/esm/src/utils/trade/decrease.js +59 -3
- package/build/esm/src/utils/trade/decreaseOutputs.js +40 -0
- package/build/esm/src/utils/trade/index.js +1 -0
- package/build/esm/src/utils/tradeHistory/utils.js +1 -0
- package/build/esm/src/utils/trades/api.js +2 -2
- package/build/esm/src/utils/twap/uiFeeReceiver.js +3 -0
- package/build/tsconfig.tsbuildinfo +1 -0
- package/build/types/src/clients/v2/__tests__/multichainTestUtil.d.ts +1 -0
- package/build/types/src/clients/v2/index.d.ts +5 -2
- package/build/types/src/codegen/subsquid.d.ts +16 -0
- package/build/types/src/configs/api.d.ts +4 -3
- package/build/types/src/utils/express/utils/oracleParamsUtils.d.ts +1 -3
- package/build/types/src/utils/jitLiquidity/api.d.ts +5 -0
- package/build/types/src/utils/jitLiquidity/index.d.ts +3 -0
- package/build/types/src/utils/jitLiquidity/types.d.ts +23 -0
- package/build/types/src/utils/jitLiquidity/utils.d.ts +5 -0
- package/build/types/src/utils/jitLiquidity/utils.spec.d.ts +1 -0
- package/build/types/src/utils/multichain/api.d.ts +3 -2
- package/build/types/src/utils/multichain/quotes.d.ts +0 -1
- package/build/types/src/utils/objects/utils.d.ts +3 -0
- package/build/types/src/utils/positions/utils.d.ts +10 -0
- package/build/types/src/utils/rpc/index.d.ts +2 -1
- package/build/types/src/utils/trade/__tests__/getMaxWithdrawAmount.spec.d.ts +1 -0
- package/build/types/src/utils/trade/decrease.d.ts +8 -0
- package/build/types/src/utils/trade/decreaseOutputs.d.ts +16 -0
- package/build/types/src/utils/trade/index.d.ts +1 -0
- package/build/types/src/utils/tradeHistory/types.d.ts +1 -0
- package/build/types/src/utils/trades/types.d.ts +1 -0
- package/package.json +1 -1
- /package/build/cjs/src/utils/{multichainTransferApi → gmxAccountApi}/index.js +0 -0
- /package/build/esm/src/utils/{multichainTransferApi → gmxAccountApi}/index.js +0 -0
- /package/build/types/src/utils/{multichainTransferApi → gmxAccountApi}/api.d.ts +0 -0
- /package/build/types/src/utils/{multichainTransferApi → gmxAccountApi}/index.d.ts +0 -0
|
@@ -3,7 +3,7 @@ import { bigMath } from "../bigmath";
|
|
|
3
3
|
import { getPositionFee, getPriceImpactForPosition } from "../fees";
|
|
4
4
|
import { getCappedPoolPnl, getMarketPnl, getPoolUsdWithoutPnl } from "../markets";
|
|
5
5
|
import { expandDecimals, USD_DECIMALS } from "../numbers";
|
|
6
|
-
import { getEntryPrice, getLeverage, getLiquidationPrice, getPositionKey, getPositionNetValue, getPositionNetValueAfterAllFees, getPositionPendingFeesUsd, getPositionPnlAfterAllFees, getPositionPnlAfterFees, getPositionPnlUsd, getPositionValueUsd, parsePositionKey, } from "../positions";
|
|
6
|
+
import { getEntryPrice, getLeverage, getLiquidationPrice, getMinCollateralUsdForLiquidationPrice, getPositionKey, getPositionNetValue, getPositionNetValueAfterAllFees, getPositionPendingFeesUsd, getPositionPnlAfterAllFees, getPositionPnlAfterFees, getPositionPnlUsd, getPositionValueUsd, parsePositionKey, } from "../positions";
|
|
7
7
|
import { convertToUsd, getIsEquivalentTokens } from "../tokens";
|
|
8
8
|
vi.mock("../markets", () => ({
|
|
9
9
|
...vi.importActual("../markets"),
|
|
@@ -338,3 +338,166 @@ describe("getLiquidationPrice", () => {
|
|
|
338
338
|
expect(result).toBeDefined();
|
|
339
339
|
});
|
|
340
340
|
});
|
|
341
|
+
describe("getMinCollateralUsdForLiquidationPrice", () => {
|
|
342
|
+
// Tiny factor magnitudes here round to 0n under PRECISION=1e30 — keeping them this way
|
|
343
|
+
// lets us assert exact integer results. One test below uses realistic factors to lock in
|
|
344
|
+
// that the priceImpact cap also works at scale.
|
|
345
|
+
const marketInfo = {
|
|
346
|
+
indexToken: {
|
|
347
|
+
decimals: 8,
|
|
348
|
+
prices: { minPrice: expandDecimals(1, USD_DECIMALS), maxPrice: expandDecimals(1, USD_DECIMALS) },
|
|
349
|
+
},
|
|
350
|
+
minCollateralFactorForLiquidation: 1000n,
|
|
351
|
+
maxPositionImpactFactorForLiquidations: 500n,
|
|
352
|
+
maxPositionImpactFactorPositive: 1000n,
|
|
353
|
+
maxPositionImpactFactorNegative: 1000n,
|
|
354
|
+
};
|
|
355
|
+
beforeEach(() => {
|
|
356
|
+
getPositionFee.mockReturnValue({ positionFeeUsd: 50n });
|
|
357
|
+
getPriceImpactForPosition.mockReturnValue({ priceImpactDeltaUsd: -100n, balanceWasImproved: false });
|
|
358
|
+
convertToUsd.mockReturnValue(0n);
|
|
359
|
+
});
|
|
360
|
+
it("returns 0 when sizeInUsd <= 0", () => {
|
|
361
|
+
const result = getMinCollateralUsdForLiquidationPrice({
|
|
362
|
+
sizeInUsd: 0n,
|
|
363
|
+
sizeInTokens: 100n,
|
|
364
|
+
marketInfo,
|
|
365
|
+
pendingImpactAmount: 0n,
|
|
366
|
+
minCollateralUsd: 200n,
|
|
367
|
+
pnl: 0n,
|
|
368
|
+
isLong: true,
|
|
369
|
+
userReferralInfo: undefined,
|
|
370
|
+
});
|
|
371
|
+
expect(result).toBe(0n);
|
|
372
|
+
});
|
|
373
|
+
it("returns 0 when sizeInTokens <= 0", () => {
|
|
374
|
+
const result = getMinCollateralUsdForLiquidationPrice({
|
|
375
|
+
sizeInUsd: 1000n,
|
|
376
|
+
sizeInTokens: 0n,
|
|
377
|
+
marketInfo,
|
|
378
|
+
pendingImpactAmount: 0n,
|
|
379
|
+
minCollateralUsd: 200n,
|
|
380
|
+
pnl: 0n,
|
|
381
|
+
isLong: true,
|
|
382
|
+
userReferralInfo: undefined,
|
|
383
|
+
});
|
|
384
|
+
expect(result).toBe(0n);
|
|
385
|
+
});
|
|
386
|
+
it("returns liquidationCollateralUsd - pnl - priceImpactDeltaUsd + closingFeeUsd for a profitable position", () => {
|
|
387
|
+
const result = getMinCollateralUsdForLiquidationPrice({
|
|
388
|
+
sizeInUsd: 1000n,
|
|
389
|
+
sizeInTokens: 100n,
|
|
390
|
+
marketInfo,
|
|
391
|
+
pendingImpactAmount: 0n,
|
|
392
|
+
minCollateralUsd: 200n,
|
|
393
|
+
pnl: 500n,
|
|
394
|
+
isLong: true,
|
|
395
|
+
userReferralInfo: undefined,
|
|
396
|
+
});
|
|
397
|
+
expect(result).toBe(-250n);
|
|
398
|
+
});
|
|
399
|
+
it("returns a larger value for a losing position than a profitable one", () => {
|
|
400
|
+
const losing = getMinCollateralUsdForLiquidationPrice({
|
|
401
|
+
sizeInUsd: 1000n,
|
|
402
|
+
sizeInTokens: 100n,
|
|
403
|
+
marketInfo,
|
|
404
|
+
pendingImpactAmount: 0n,
|
|
405
|
+
minCollateralUsd: 200n,
|
|
406
|
+
pnl: -300n,
|
|
407
|
+
isLong: true,
|
|
408
|
+
userReferralInfo: undefined,
|
|
409
|
+
});
|
|
410
|
+
const profitable = getMinCollateralUsdForLiquidationPrice({
|
|
411
|
+
sizeInUsd: 1000n,
|
|
412
|
+
sizeInTokens: 100n,
|
|
413
|
+
marketInfo,
|
|
414
|
+
pendingImpactAmount: 0n,
|
|
415
|
+
minCollateralUsd: 200n,
|
|
416
|
+
pnl: 300n,
|
|
417
|
+
isLong: true,
|
|
418
|
+
userReferralInfo: undefined,
|
|
419
|
+
});
|
|
420
|
+
expect(losing).toBeGreaterThan(profitable);
|
|
421
|
+
expect(losing).toBe(550n);
|
|
422
|
+
expect(profitable).toBe(-50n);
|
|
423
|
+
});
|
|
424
|
+
it("uses minCollateralUsd when it exceeds the factor-based liquidation collateral", () => {
|
|
425
|
+
const result = getMinCollateralUsdForLiquidationPrice({
|
|
426
|
+
sizeInUsd: 1000n,
|
|
427
|
+
sizeInTokens: 100n,
|
|
428
|
+
marketInfo,
|
|
429
|
+
pendingImpactAmount: 0n,
|
|
430
|
+
minCollateralUsd: 500n,
|
|
431
|
+
pnl: 0n,
|
|
432
|
+
isLong: true,
|
|
433
|
+
userReferralInfo: undefined,
|
|
434
|
+
});
|
|
435
|
+
expect(result).toBe(550n);
|
|
436
|
+
});
|
|
437
|
+
it("includes pendingImpactUsd in priceImpactDeltaUsd", () => {
|
|
438
|
+
convertToUsd.mockReturnValueOnce(-30n);
|
|
439
|
+
const result = getMinCollateralUsdForLiquidationPrice({
|
|
440
|
+
sizeInUsd: 1000n,
|
|
441
|
+
sizeInTokens: 100n,
|
|
442
|
+
marketInfo,
|
|
443
|
+
pendingImpactAmount: -100n,
|
|
444
|
+
minCollateralUsd: 200n,
|
|
445
|
+
pnl: 0n,
|
|
446
|
+
isLong: true,
|
|
447
|
+
userReferralInfo: undefined,
|
|
448
|
+
});
|
|
449
|
+
expect(result).toBe(250n);
|
|
450
|
+
});
|
|
451
|
+
it("uses realistic factors so priceImpactDeltaUsd is not capped to zero", () => {
|
|
452
|
+
const realisticMarketInfo = {
|
|
453
|
+
indexToken: {
|
|
454
|
+
decimals: 8,
|
|
455
|
+
prices: { minPrice: expandDecimals(1, USD_DECIMALS), maxPrice: expandDecimals(1, USD_DECIMALS) },
|
|
456
|
+
},
|
|
457
|
+
minCollateralFactorForLiquidation: 1000n,
|
|
458
|
+
maxPositionImpactFactorForLiquidations: expandDecimals(1, 28),
|
|
459
|
+
maxPositionImpactFactorPositive: 1000n,
|
|
460
|
+
maxPositionImpactFactorNegative: 1000n,
|
|
461
|
+
};
|
|
462
|
+
const sizeInUsd = expandDecimals(1000, USD_DECIMALS);
|
|
463
|
+
getPositionFee.mockReturnValueOnce({ positionFeeUsd: expandDecimals(5, USD_DECIMALS) });
|
|
464
|
+
getPriceImpactForPosition.mockReturnValueOnce({
|
|
465
|
+
priceImpactDeltaUsd: -expandDecimals(20, USD_DECIMALS),
|
|
466
|
+
balanceWasImproved: false,
|
|
467
|
+
});
|
|
468
|
+
const result = getMinCollateralUsdForLiquidationPrice({
|
|
469
|
+
sizeInUsd,
|
|
470
|
+
sizeInTokens: 100n,
|
|
471
|
+
marketInfo: realisticMarketInfo,
|
|
472
|
+
pendingImpactAmount: 0n,
|
|
473
|
+
minCollateralUsd: expandDecimals(100, USD_DECIMALS),
|
|
474
|
+
pnl: expandDecimals(50, USD_DECIMALS),
|
|
475
|
+
isLong: true,
|
|
476
|
+
userReferralInfo: undefined,
|
|
477
|
+
});
|
|
478
|
+
expect(result).toBe(expandDecimals(65, USD_DECIMALS));
|
|
479
|
+
});
|
|
480
|
+
it("works for short positions identically", () => {
|
|
481
|
+
const long = getMinCollateralUsdForLiquidationPrice({
|
|
482
|
+
sizeInUsd: 1000n,
|
|
483
|
+
sizeInTokens: 100n,
|
|
484
|
+
marketInfo,
|
|
485
|
+
pendingImpactAmount: 0n,
|
|
486
|
+
minCollateralUsd: 200n,
|
|
487
|
+
pnl: 200n,
|
|
488
|
+
isLong: true,
|
|
489
|
+
userReferralInfo: undefined,
|
|
490
|
+
});
|
|
491
|
+
const short = getMinCollateralUsdForLiquidationPrice({
|
|
492
|
+
sizeInUsd: 1000n,
|
|
493
|
+
sizeInTokens: 100n,
|
|
494
|
+
marketInfo,
|
|
495
|
+
pendingImpactAmount: 0n,
|
|
496
|
+
minCollateralUsd: 200n,
|
|
497
|
+
pnl: 200n,
|
|
498
|
+
isLong: false,
|
|
499
|
+
userReferralInfo: undefined,
|
|
500
|
+
});
|
|
501
|
+
expect(short).toBe(long);
|
|
502
|
+
});
|
|
503
|
+
});
|
|
@@ -81,6 +81,11 @@ describe("uiFeeReceiver", () => {
|
|
|
81
81
|
});
|
|
82
82
|
});
|
|
83
83
|
describe("setUiFeeReceiverIsExpress", () => {
|
|
84
|
+
it("should preserve regular uiFeeReceiver addresses", () => {
|
|
85
|
+
const uiFeeReceiver = "0x496Ca0b922fd18a2cB9DDEe44969CDdC8D87Bc7F";
|
|
86
|
+
expect(setUiFeeReceiverIsExpress(uiFeeReceiver, true)).toEqual(uiFeeReceiver);
|
|
87
|
+
expect(setUiFeeReceiverIsExpress(uiFeeReceiver, false)).toEqual(uiFeeReceiver);
|
|
88
|
+
});
|
|
84
89
|
it("should correctly set isExpress for simple uiFeeReceiver", () => {
|
|
85
90
|
expect(setUiFeeReceiverIsExpress("0xff00000000000000000000000000000000000001", true)).toEqual("0xff00000000000000000000000000000100000001");
|
|
86
91
|
expect(setUiFeeReceiverIsExpress("0xff00000000000000000000000000000000000001", false)).toEqual("0xff00000000000000000000000000000000000001");
|
|
@@ -19,13 +19,13 @@ function parseAllowances(raw) {
|
|
|
19
19
|
}));
|
|
20
20
|
}
|
|
21
21
|
export async function fetchWalletBalances(ctx, params) {
|
|
22
|
-
return ctx.api.fetchJson("/balances/wallet", {
|
|
22
|
+
return ctx.api.fetchJson("/v1/balances/wallet", {
|
|
23
23
|
query: { address: params.address },
|
|
24
24
|
transform: parseWalletBalances,
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
27
|
export async function fetchAllowances(ctx, params) {
|
|
28
|
-
return ctx.api.fetchJson("/allowances", {
|
|
28
|
+
return ctx.api.fetchJson("/v1/allowances", {
|
|
29
29
|
query: { address: params.address, spender: params.spender },
|
|
30
30
|
transform: parseAllowances,
|
|
31
31
|
});
|
|
@@ -12,11 +12,8 @@ function getOracleParams({ chainId, tokenAddresses }) {
|
|
|
12
12
|
data: Array(uniqTokenAddresses.length).fill("0x"),
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
|
-
export function getOracleParamsForRelayParams({ chainId, gasPaymentTokenAddress, relayerFeeTokenAddress, feeSwapPath,
|
|
15
|
+
export function getOracleParamsForRelayParams({ chainId, gasPaymentTokenAddress, relayerFeeTokenAddress, feeSwapPath, }) {
|
|
16
16
|
const tokenAddresses = [gasPaymentTokenAddress, relayerFeeTokenAddress];
|
|
17
|
-
if (externalCalls) {
|
|
18
|
-
tokenAddresses.push(...externalCalls.sendTokens);
|
|
19
|
-
}
|
|
20
17
|
if (feeSwapPath.length) {
|
|
21
18
|
tokenAddresses.push(...getSwapPathOracleTokens({
|
|
22
19
|
chainId,
|
|
@@ -149,7 +149,6 @@ export function getBestFeeSwapStrategy(internalSwapAmounts, externalSwapQuote) {
|
|
|
149
149
|
export function getRawRelayerParams({ chainId, gasPaymentTokenAddress, relayerFeeTokenAddress, feeParams, externalCalls, tokenPermits, }) {
|
|
150
150
|
const oracleParams = getOracleParamsForRelayParams({
|
|
151
151
|
chainId,
|
|
152
|
-
externalCalls,
|
|
153
152
|
feeSwapPath: feeParams.feeSwapPath,
|
|
154
153
|
gasPaymentTokenAddress,
|
|
155
154
|
relayerFeeTokenAddress,
|
|
@@ -31,7 +31,9 @@ function parseWithdrawPrepareResponse(raw) {
|
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
33
|
export async function prepareCrossChainDeposit(ctx, request) {
|
|
34
|
-
return ctx.api.postJson("/
|
|
34
|
+
return ctx.api.postJson("/v1/gmx-account/deposit/cross-chain/prepare", request, {
|
|
35
|
+
transform: parseDepositPrepareResponse,
|
|
36
|
+
});
|
|
35
37
|
}
|
|
36
38
|
export async function executeCrossChainDeposit(ctx, signer, request) {
|
|
37
39
|
if (!signer.sendTransaction) {
|
|
@@ -50,10 +52,12 @@ export async function executeCrossChainDeposit(ctx, signer, request) {
|
|
|
50
52
|
};
|
|
51
53
|
}
|
|
52
54
|
export async function prepareCrossChainWithdraw(ctx, request) {
|
|
53
|
-
return ctx.api.postJson("/
|
|
55
|
+
return ctx.api.postJson("/v1/gmx-account/withdraw/cross-chain/prepare", request, {
|
|
56
|
+
transform: parseWithdrawPrepareResponse,
|
|
57
|
+
});
|
|
54
58
|
}
|
|
55
59
|
export async function submitCrossChainWithdraw(ctx, request) {
|
|
56
|
-
return ctx.api.postJson("/
|
|
60
|
+
return ctx.api.postJson("/v1/gmx-account/withdraw/cross-chain/submit", request);
|
|
57
61
|
}
|
|
58
62
|
export async function signCrossChainWithdrawPrepared(prepared, signer) {
|
|
59
63
|
const td = prepared.payload.typedData;
|
|
@@ -74,7 +78,7 @@ export async function executeCrossChainWithdraw(ctx, signer, request) {
|
|
|
74
78
|
});
|
|
75
79
|
}
|
|
76
80
|
export async function getCrossChainWithdrawStatus(ctx, requestId) {
|
|
77
|
-
return ctx.api.postJson("/
|
|
81
|
+
return ctx.api.postJson("/v1/gmx-account/withdraw/cross-chain/status", {
|
|
78
82
|
requestId,
|
|
79
83
|
});
|
|
80
84
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { parseJitLiquidityResponse } from "./utils";
|
|
2
|
+
export async function fetchApiJitLiquidityInfo(ctx, params) {
|
|
3
|
+
const response = await ctx.api.fetchJson(getJitLiquidityApiPath(params?.apiVersion ?? "v1"));
|
|
4
|
+
return parseJitLiquidityResponse(response);
|
|
5
|
+
}
|
|
6
|
+
function getJitLiquidityApiPath(apiVersion) {
|
|
7
|
+
return `/${apiVersion}/jit/liquidity_info`;
|
|
8
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { getRecord, getString, isRecord } from "../objects";
|
|
2
|
+
export function getJitLiquidityInfo(jitLiquidityMap, marketTokenAddress) {
|
|
3
|
+
return jitLiquidityMap?.[marketTokenAddress];
|
|
4
|
+
}
|
|
5
|
+
export function getJitMaxReservedUsd(jitLiquidityMap, marketTokenAddress, isLong) {
|
|
6
|
+
const info = getJitLiquidityInfo(jitLiquidityMap, marketTokenAddress);
|
|
7
|
+
return isLong ? info?.maxReservedUsdWithJitLong : info?.maxReservedUsdWithJitShort;
|
|
8
|
+
}
|
|
9
|
+
export function getJitGlvShiftParams(jitLiquidityMap, marketTokenAddress, isLong) {
|
|
10
|
+
const info = getJitLiquidityInfo(jitLiquidityMap, marketTokenAddress);
|
|
11
|
+
if (!info) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
return isLong ? info.glvShiftParamsLong : info.glvShiftParamsShort;
|
|
15
|
+
}
|
|
16
|
+
export function parseJitLiquidityResponse(response, isV2JitLiquidityInfoEnabled) {
|
|
17
|
+
const liquidityInfos = getLiquidityInfos(response);
|
|
18
|
+
const result = {};
|
|
19
|
+
for (const rawInfo of liquidityInfos) {
|
|
20
|
+
if (!isRecord(rawInfo)) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const market = getString(rawInfo.market);
|
|
24
|
+
if (!market) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const hasV2Shape = Boolean(getRecord(rawInfo.long) || getRecord(rawInfo.short));
|
|
28
|
+
const shouldParseV2 = isV2JitLiquidityInfoEnabled ?? hasV2Shape;
|
|
29
|
+
result[market] = shouldParseV2 ? parseV2JitLiquidityInfo(rawInfo) : parseV1JitLiquidityInfo(rawInfo);
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
function parseV1JitLiquidityInfo(rawInfo) {
|
|
34
|
+
const glvShiftParams = parseGlvShiftParams(rawInfo.glvShiftParams);
|
|
35
|
+
return {
|
|
36
|
+
maxReservedUsdWithJitLong: parseJitAmount(rawInfo.maxReservedUsdWithJitLong),
|
|
37
|
+
maxReservedUsdWithJitShort: parseJitAmount(rawInfo.maxReservedUsdWithJitShort),
|
|
38
|
+
glvShiftParamsLong: glvShiftParams,
|
|
39
|
+
glvShiftParamsShort: glvShiftParams,
|
|
40
|
+
glvShiftParams,
|
|
41
|
+
glv: getString(rawInfo.glv) ?? "",
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function parseV2JitLiquidityInfo(rawInfo) {
|
|
45
|
+
const longInfo = getRecord(rawInfo.long);
|
|
46
|
+
const shortInfo = getRecord(rawInfo.short);
|
|
47
|
+
if (!longInfo && !shortInfo) {
|
|
48
|
+
return parseV1JitLiquidityInfo(rawInfo);
|
|
49
|
+
}
|
|
50
|
+
const glvShiftParamsLong = parseGlvShiftParams(longInfo?.glvShiftParams ?? rawInfo.glvShiftParams);
|
|
51
|
+
const glvShiftParamsShort = parseGlvShiftParams(shortInfo?.glvShiftParams ?? rawInfo.glvShiftParams);
|
|
52
|
+
return {
|
|
53
|
+
maxReservedUsdWithJitLong: parseJitAmount(longInfo?.maxReservedUsd ?? rawInfo.maxReservedUsdWithJitLong),
|
|
54
|
+
maxReservedUsdWithJitShort: parseJitAmount(shortInfo?.maxReservedUsd ?? rawInfo.maxReservedUsdWithJitShort),
|
|
55
|
+
glvShiftParamsLong,
|
|
56
|
+
glvShiftParamsShort,
|
|
57
|
+
glvShiftParams: [...glvShiftParamsLong, ...glvShiftParamsShort],
|
|
58
|
+
glv: getString(rawInfo.glv) ?? "",
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function getLiquidityInfos(response) {
|
|
62
|
+
if (!isRecord(response)) {
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
const liquidityInfos = response.liquidityInfos ?? response.v2JitLiquidityInfos ?? response.v2JITLiquidityInfos;
|
|
66
|
+
return Array.isArray(liquidityInfos) ? liquidityInfos : [];
|
|
67
|
+
}
|
|
68
|
+
function parseGlvShiftParams(value) {
|
|
69
|
+
const rawParams = Array.isArray(value) ? value : value ? [value] : [];
|
|
70
|
+
return rawParams.map(parseGlvShiftParam).filter((param) => param !== undefined);
|
|
71
|
+
}
|
|
72
|
+
function parseGlvShiftParam(value) {
|
|
73
|
+
if (!isRecord(value)) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
const glv = getString(value.glv);
|
|
77
|
+
const fromMarket = getString(value.fromMarket);
|
|
78
|
+
const toMarket = getString(value.toMarket);
|
|
79
|
+
if (!glv || !fromMarket || !toMarket) {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
glv,
|
|
84
|
+
fromMarket,
|
|
85
|
+
toMarket,
|
|
86
|
+
marketTokenAmount: parseJitAmount(value.marketTokenAmount),
|
|
87
|
+
minMarketTokens: parseJitAmount(value.minMarketTokens),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function parseJitAmount(value) {
|
|
91
|
+
if (typeof value !== "string" && typeof value !== "number" && typeof value !== "bigint") {
|
|
92
|
+
return 0n;
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
const amount = BigInt(value);
|
|
96
|
+
return amount < 0n ? 0n : amount;
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
return 0n;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { getJitGlvShiftParams, parseJitLiquidityResponse } from "./utils";
|
|
3
|
+
describe("parseJitLiquidityResponse", () => {
|
|
4
|
+
it("parses v1 JIT liquidity info", () => {
|
|
5
|
+
const result = parseJitLiquidityResponse({
|
|
6
|
+
liquidityInfos: [
|
|
7
|
+
{
|
|
8
|
+
glv: "0xGlv",
|
|
9
|
+
market: "0xMarket",
|
|
10
|
+
maxReservedUsdWithJitLong: "100",
|
|
11
|
+
maxReservedUsdWithJitShort: "200",
|
|
12
|
+
glvShiftParams: [
|
|
13
|
+
{
|
|
14
|
+
glv: "0xGlv",
|
|
15
|
+
fromMarket: "0xFrom",
|
|
16
|
+
toMarket: "0xMarket",
|
|
17
|
+
marketTokenAmount: "10",
|
|
18
|
+
minMarketTokens: "9",
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
}, false);
|
|
24
|
+
expect(result["0xMarket"]).toEqual({
|
|
25
|
+
glv: "0xGlv",
|
|
26
|
+
maxReservedUsdWithJitLong: 100n,
|
|
27
|
+
maxReservedUsdWithJitShort: 200n,
|
|
28
|
+
glvShiftParams: [
|
|
29
|
+
{
|
|
30
|
+
glv: "0xGlv",
|
|
31
|
+
fromMarket: "0xFrom",
|
|
32
|
+
toMarket: "0xMarket",
|
|
33
|
+
marketTokenAmount: 10n,
|
|
34
|
+
minMarketTokens: 9n,
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
glvShiftParamsLong: [
|
|
38
|
+
{
|
|
39
|
+
glv: "0xGlv",
|
|
40
|
+
fromMarket: "0xFrom",
|
|
41
|
+
toMarket: "0xMarket",
|
|
42
|
+
marketTokenAmount: 10n,
|
|
43
|
+
minMarketTokens: 9n,
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
glvShiftParamsShort: [
|
|
47
|
+
{
|
|
48
|
+
glv: "0xGlv",
|
|
49
|
+
fromMarket: "0xFrom",
|
|
50
|
+
toMarket: "0xMarket",
|
|
51
|
+
marketTokenAmount: 10n,
|
|
52
|
+
minMarketTokens: 9n,
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
it("parses v2 JIT liquidity info with direction-specific shift params", () => {
|
|
58
|
+
const result = parseJitLiquidityResponse({
|
|
59
|
+
liquidityInfos: [
|
|
60
|
+
{
|
|
61
|
+
glv: "0xGlv",
|
|
62
|
+
market: "0xMarket",
|
|
63
|
+
long: {
|
|
64
|
+
maxReservedUsd: "300",
|
|
65
|
+
glvShiftParams: {
|
|
66
|
+
glv: "0xGlv",
|
|
67
|
+
fromMarket: "0xLongFrom",
|
|
68
|
+
toMarket: "0xMarket",
|
|
69
|
+
marketTokenAmount: "30",
|
|
70
|
+
minMarketTokens: "29",
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
short: {
|
|
74
|
+
maxReservedUsd: "400",
|
|
75
|
+
glvShiftParams: {
|
|
76
|
+
glv: "0xGlv",
|
|
77
|
+
fromMarket: "0xShortFrom",
|
|
78
|
+
toMarket: "0xMarket",
|
|
79
|
+
marketTokenAmount: "40",
|
|
80
|
+
minMarketTokens: "39",
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
});
|
|
86
|
+
expect(result["0xMarket"].maxReservedUsdWithJitLong).toBe(300n);
|
|
87
|
+
expect(result["0xMarket"].maxReservedUsdWithJitShort).toBe(400n);
|
|
88
|
+
expect(getJitGlvShiftParams(result, "0xMarket", true)).toEqual([
|
|
89
|
+
{
|
|
90
|
+
glv: "0xGlv",
|
|
91
|
+
fromMarket: "0xLongFrom",
|
|
92
|
+
toMarket: "0xMarket",
|
|
93
|
+
marketTokenAmount: 30n,
|
|
94
|
+
minMarketTokens: 29n,
|
|
95
|
+
},
|
|
96
|
+
]);
|
|
97
|
+
expect(getJitGlvShiftParams(result, "0xMarket", false)).toEqual([
|
|
98
|
+
{
|
|
99
|
+
glv: "0xGlv",
|
|
100
|
+
fromMarket: "0xShortFrom",
|
|
101
|
+
toMarket: "0xMarket",
|
|
102
|
+
marketTokenAmount: 40n,
|
|
103
|
+
minMarketTokens: 39n,
|
|
104
|
+
},
|
|
105
|
+
]);
|
|
106
|
+
});
|
|
107
|
+
it("falls back to zero for invalid and negative amount values", () => {
|
|
108
|
+
const result = parseJitLiquidityResponse({
|
|
109
|
+
liquidityInfos: [
|
|
110
|
+
{
|
|
111
|
+
market: "0xMarket",
|
|
112
|
+
maxReservedUsdWithJitLong: "-100",
|
|
113
|
+
maxReservedUsdWithJitShort: "invalid",
|
|
114
|
+
glvShiftParams: [
|
|
115
|
+
{
|
|
116
|
+
glv: "0xGlv",
|
|
117
|
+
fromMarket: "0xFrom",
|
|
118
|
+
toMarket: "0xMarket",
|
|
119
|
+
marketTokenAmount: "1.5",
|
|
120
|
+
minMarketTokens: "-9",
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
});
|
|
126
|
+
expect(result["0xMarket"].maxReservedUsdWithJitLong).toBe(0n);
|
|
127
|
+
expect(result["0xMarket"].maxReservedUsdWithJitShort).toBe(0n);
|
|
128
|
+
expect(result["0xMarket"].glvShiftParams[0].marketTokenAmount).toBe(0n);
|
|
129
|
+
expect(result["0xMarket"].glvShiftParams[0].minMarketTokens).toBe(0n);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { deserializeBigIntsInObject } from "../numbers";
|
|
2
2
|
export async function fetchApiMarketsInfo(ctx) {
|
|
3
|
-
const mInfos = await ctx.api.fetchJson("/markets/info");
|
|
3
|
+
const mInfos = await ctx.api.fetchJson("/v1/markets/info");
|
|
4
4
|
return mInfos.map((mInfo) => deserializeBigIntsInObject(mInfo, { handleInts: true }));
|
|
5
5
|
}
|
|
6
6
|
export async function fetchApiTokensData(ctx) {
|
|
7
|
-
const tInfos = await ctx.api.fetchJson("/tokens/info");
|
|
7
|
+
const tInfos = await ctx.api.fetchJson("/v1/tokens/info");
|
|
8
8
|
return tInfos.map((tInfo) => deserializeBigIntsInObject(tInfo, { handleInts: true }));
|
|
9
9
|
}
|
|
10
10
|
export async function fetchApiMarkets(ctx) {
|
|
11
|
-
const markets = await ctx.api.fetchJson("/markets");
|
|
11
|
+
const markets = await ctx.api.fetchJson("/v1/markets");
|
|
12
12
|
return markets.map((m) => deserializeBigIntsInObject(m, { handleInts: true }));
|
|
13
13
|
}
|
|
14
14
|
export async function fetchApiMarketsTickers(ctx, params) {
|
|
15
|
-
const tickers = await ctx.api.fetchJson("/markets/tickers", {
|
|
15
|
+
const tickers = await ctx.api.fetchJson("/v1/markets/tickers", {
|
|
16
16
|
query: {
|
|
17
17
|
addresses: params?.addresses,
|
|
18
18
|
symbols: params?.symbols,
|
|
@@ -51,10 +51,12 @@ export function getTokenPoolType(marketInfo, tokenAddress) {
|
|
|
51
51
|
if (longToken.address === shortToken.address && tokenAddress === longToken.address) {
|
|
52
52
|
return "long";
|
|
53
53
|
}
|
|
54
|
-
if (tokenAddress === longToken.address ||
|
|
54
|
+
if (tokenAddress === longToken.address ||
|
|
55
|
+
(tokenAddress === NATIVE_TOKEN_ADDRESS && longToken.isWrapped)) {
|
|
55
56
|
return "long";
|
|
56
57
|
}
|
|
57
|
-
if (tokenAddress === shortToken.address ||
|
|
58
|
+
if (tokenAddress === shortToken.address ||
|
|
59
|
+
(tokenAddress === NATIVE_TOKEN_ADDRESS && shortToken.isWrapped)) {
|
|
58
60
|
return "short";
|
|
59
61
|
}
|
|
60
62
|
return undefined;
|
|
@@ -7,7 +7,7 @@ import { CHAIN_ID_TO_ENDPOINT_ID } from "../../../configs/multichain";
|
|
|
7
7
|
import { buildCrossChainWithdrawBridgeOutParams, buildSameChainDepositTxn, buildSameChainWithdrawBridgeOutParams, buildSameChainWithdrawTxn, } from "../api";
|
|
8
8
|
const ACCOUNT = "0x1111111111111111111111111111111111111111";
|
|
9
9
|
const USDC_ARBITRUM = "0xaf88d065e77c8cC2239327C5EDb3A432268e5831";
|
|
10
|
-
const
|
|
10
|
+
const STARGATE_USDC_ARBITRUM = "0xe8CDF27AcD73a434D661C84887215F7598e7d0d3";
|
|
11
11
|
const ROUTER_ARBITRUM = getContract(ARBITRUM, "MultichainTransferRouter");
|
|
12
12
|
const VAULT_ARBITRUM = getContract(ARBITRUM, "MultichainVault");
|
|
13
13
|
const WETH_ARBITRUM = "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1";
|
|
@@ -108,13 +108,13 @@ describe("multichain api: buildCrossChainWithdrawBridgeOutParams", () => {
|
|
|
108
108
|
tokenAddress: USDC_ARBITRUM,
|
|
109
109
|
amount: 1000000n,
|
|
110
110
|
dstChainId: SOURCE_BASE_MAINNET,
|
|
111
|
-
stargateAddress:
|
|
111
|
+
stargateAddress: STARGATE_USDC_ARBITRUM,
|
|
112
112
|
slippageBps: 50,
|
|
113
113
|
});
|
|
114
114
|
expect(params.token).toBe(USDC_ARBITRUM);
|
|
115
115
|
expect(params.amount).toBe(1000000n);
|
|
116
116
|
expect(params.minAmountOut).toBe(995000n);
|
|
117
|
-
expect(params.provider).toBe(
|
|
117
|
+
expect(params.provider).toBe(STARGATE_USDC_ARBITRUM);
|
|
118
118
|
const [decodedDstEid] = decodeAbiParameters([{ type: "uint32" }], params.data);
|
|
119
119
|
expect(decodedDstEid).toBe(CHAIN_ID_TO_ENDPOINT_ID[SOURCE_BASE_MAINNET]);
|
|
120
120
|
});
|
|
@@ -123,7 +123,7 @@ describe("multichain api: buildCrossChainWithdrawBridgeOutParams", () => {
|
|
|
123
123
|
tokenAddress: USDC_ARBITRUM,
|
|
124
124
|
amount: 1000000n,
|
|
125
125
|
dstChainId: SOURCE_BASE_MAINNET,
|
|
126
|
-
stargateAddress:
|
|
126
|
+
stargateAddress: STARGATE_USDC_ARBITRUM,
|
|
127
127
|
});
|
|
128
128
|
expect(params.minAmountOut).toBe(1000000n);
|
|
129
129
|
});
|
|
@@ -80,6 +80,9 @@ export function buildSameChainWithdrawTxn(request) {
|
|
|
80
80
|
*/
|
|
81
81
|
export function buildCrossChainWithdrawBridgeOutParams(params) {
|
|
82
82
|
const { tokenAddress, amount, dstChainId, stargateAddress, slippageBps = 0 } = params;
|
|
83
|
+
if (!Number.isInteger(slippageBps) || slippageBps < 0 || slippageBps >= 10000) {
|
|
84
|
+
throw new Error(`slippageBps must be an integer in range [0, 10000), got ${slippageBps}`);
|
|
85
|
+
}
|
|
83
86
|
const dstEid = getLayerZeroEndpointId(dstChainId);
|
|
84
87
|
if (dstEid === undefined) {
|
|
85
88
|
throw new Error(`No LayerZero endpoint id for destination chain ${dstChainId}`);
|
|
@@ -555,6 +555,7 @@ export function removeTrailingZeros(amount) {
|
|
|
555
555
|
return amount;
|
|
556
556
|
return amountWithoutZeros;
|
|
557
557
|
}
|
|
558
|
+
const NUMERIC_STRING_REGEX = /^-?\d+$/;
|
|
558
559
|
export function serializeBigIntsInObject(obj) {
|
|
559
560
|
const result = Array.isArray(obj) ? [] : {};
|
|
560
561
|
for (const key in obj) {
|
|
@@ -571,7 +572,6 @@ export function serializeBigIntsInObject(obj) {
|
|
|
571
572
|
}
|
|
572
573
|
return result;
|
|
573
574
|
}
|
|
574
|
-
const NUMERIC_STRING_REGEX = /^-?\d+$/;
|
|
575
575
|
export function deserializeBigIntsInObject(obj, opts) {
|
|
576
576
|
const handleInts = opts?.handleInts ?? false;
|
|
577
577
|
const result = Array.isArray(obj) ? [] : {};
|
|
@@ -12,6 +12,15 @@ export function getByKey(obj, key) {
|
|
|
12
12
|
return undefined;
|
|
13
13
|
return obj[key];
|
|
14
14
|
}
|
|
15
|
+
export function isRecord(value) {
|
|
16
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
17
|
+
}
|
|
18
|
+
export function getRecord(value) {
|
|
19
|
+
return isRecord(value) ? value : undefined;
|
|
20
|
+
}
|
|
21
|
+
export function getString(value) {
|
|
22
|
+
return typeof value === "string" ? value : undefined;
|
|
23
|
+
}
|
|
15
24
|
export function deleteByKey(obj, key) {
|
|
16
25
|
const newObj = { ...obj };
|
|
17
26
|
delete newObj[key];
|