@gmx-io/sdk 1.5.0-alpha-18 → 1.6.1
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/index.js +44 -40
- 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/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/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/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/index.js +4 -0
- 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/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/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/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/index.d.ts +3 -0
- 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/objects/utils.d.ts +3 -0
- package/build/types/src/utils/orderTransactions/api.d.ts +1 -0
- package/build/types/src/utils/positions/utils.d.ts +10 -0
- 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
|
@@ -31,7 +31,9 @@ function parseWithdrawPrepareResponse(raw) {
|
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
33
|
export async function prepareCrossChainDeposit(ctx, request) {
|
|
34
|
-
return ctx.api.postJson("/gmx-account/deposit/cross-chain/prepare", request, {
|
|
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("/gmx-account/withdraw/cross-chain/prepare", request, {
|
|
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("/gmx-account/withdraw/cross-chain/submit", request);
|
|
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("/gmx-account/withdraw/cross-chain/status", {
|
|
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;
|
|
@@ -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];
|
|
@@ -17,30 +17,30 @@ function parsePrepareResponse(raw) {
|
|
|
17
17
|
return { ...raw, estimates: parseEstimates(raw.estimates) };
|
|
18
18
|
}
|
|
19
19
|
export async function prepareOrder(ctx, request) {
|
|
20
|
-
return ctx.api.postJson("/orders/txns/prepare", request, {
|
|
20
|
+
return ctx.api.postJson("/v1/orders/txns/prepare", request, {
|
|
21
21
|
transform: parsePrepareResponse,
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
24
|
export async function submitOrder(ctx, request) {
|
|
25
|
-
return ctx.api.postJson("/orders/txns/submit", request);
|
|
25
|
+
return ctx.api.postJson("/v1/orders/txns/submit", request);
|
|
26
26
|
}
|
|
27
27
|
export async function prepareEditOrder(ctx, request) {
|
|
28
|
-
return ctx.api.postJson("/orders/txns/edit/prepare", request, {
|
|
28
|
+
return ctx.api.postJson("/v1/orders/txns/edit/prepare", request, {
|
|
29
29
|
transform: parsePrepareResponse,
|
|
30
30
|
});
|
|
31
31
|
}
|
|
32
32
|
export async function prepareCancelOrder(ctx, request) {
|
|
33
|
-
return ctx.api.postJson("/orders/txns/cancel/prepare", request, {
|
|
33
|
+
return ctx.api.postJson("/v1/orders/txns/cancel/prepare", request, {
|
|
34
34
|
transform: parsePrepareResponse,
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
export async function prepareCollateral(ctx, request) {
|
|
38
|
-
return ctx.api.postJson("/orders/txns/collateral/prepare", request, {
|
|
38
|
+
return ctx.api.postJson("/v1/orders/txns/collateral/prepare", request, {
|
|
39
39
|
transform: parsePrepareResponse,
|
|
40
40
|
});
|
|
41
41
|
}
|
|
42
42
|
export async function fetchOrderStatus(ctx, request) {
|
|
43
|
-
return ctx.api.postJson("/orders/txns/status", request);
|
|
43
|
+
return ctx.api.postJson("/v1/orders/txns/status", request);
|
|
44
44
|
}
|
|
45
45
|
export async function signPreparedOrder(prepared, signer, chainId, accountAddress) {
|
|
46
46
|
if (prepared.payloadType !== "typed-data") {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export async function fetchApiPerformanceAnnualized(ctx, params) {
|
|
2
|
-
return ctx.api.fetchJson("/performance/annualized", {
|
|
2
|
+
return ctx.api.fetchJson("/v1/performance/annualized", {
|
|
3
3
|
query: {
|
|
4
4
|
period: params?.period,
|
|
5
5
|
address: params?.address,
|
|
@@ -7,7 +7,7 @@ export async function fetchApiPerformanceAnnualized(ctx, params) {
|
|
|
7
7
|
});
|
|
8
8
|
}
|
|
9
9
|
export async function fetchApiPerformanceSnapshots(ctx, params) {
|
|
10
|
-
return ctx.api.fetchJson("/performance/snapshots", {
|
|
10
|
+
return ctx.api.fetchJson("/v1/performance/snapshots", {
|
|
11
11
|
query: {
|
|
12
12
|
period: params?.period,
|
|
13
13
|
address: params?.address,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { deserializeBigIntsInObject } from "../numbers";
|
|
2
2
|
export async function fetchApiPositionsInfo(ctx, params) {
|
|
3
|
-
const positions = await ctx.api.fetchJson("/positions", {
|
|
3
|
+
const positions = await ctx.api.fetchJson("/v1/positions", {
|
|
4
4
|
query: {
|
|
5
5
|
address: params.address,
|
|
6
6
|
includeRelatedOrders: params.includeRelatedOrders || undefined,
|
|
@@ -148,6 +148,33 @@ export function getLiquidationPrice(p) {
|
|
|
148
148
|
}
|
|
149
149
|
return liquidationPrice;
|
|
150
150
|
}
|
|
151
|
+
export function getMinCollateralUsdForLiquidationPrice(p) {
|
|
152
|
+
const { sizeInUsd, sizeInTokens, marketInfo, pendingImpactAmount, minCollateralUsd, pnl, isLong, userReferralInfo } = p;
|
|
153
|
+
if (sizeInUsd <= 0 || sizeInTokens <= 0) {
|
|
154
|
+
return 0n;
|
|
155
|
+
}
|
|
156
|
+
const closingFeeUsd = getPositionFee(marketInfo, sizeInUsd, false, userReferralInfo).positionFeeUsd;
|
|
157
|
+
const maxNegativePriceImpactUsd = -1n * applyFactor(sizeInUsd, marketInfo.maxPositionImpactFactorForLiquidations);
|
|
158
|
+
let priceImpactDeltaUsd = getPriceImpactForPosition(marketInfo, -sizeInUsd, isLong, {
|
|
159
|
+
fallbackToZero: true,
|
|
160
|
+
}).priceImpactDeltaUsd;
|
|
161
|
+
if (priceImpactDeltaUsd > 0) {
|
|
162
|
+
priceImpactDeltaUsd = capPositionImpactUsdByMaxPriceImpactFactor(marketInfo, sizeInUsd, priceImpactDeltaUsd);
|
|
163
|
+
}
|
|
164
|
+
const pendingImpactUsd = convertToUsd(pendingImpactAmount, marketInfo.indexToken.decimals, pendingImpactAmount > 0 ? marketInfo.indexToken.prices.minPrice : marketInfo.indexToken.prices.maxPrice);
|
|
165
|
+
priceImpactDeltaUsd = priceImpactDeltaUsd + pendingImpactUsd;
|
|
166
|
+
if (priceImpactDeltaUsd > 0) {
|
|
167
|
+
priceImpactDeltaUsd = 0n;
|
|
168
|
+
}
|
|
169
|
+
else if (priceImpactDeltaUsd < maxNegativePriceImpactUsd) {
|
|
170
|
+
priceImpactDeltaUsd = maxNegativePriceImpactUsd;
|
|
171
|
+
}
|
|
172
|
+
let liquidationCollateralUsd = applyFactor(sizeInUsd, marketInfo.minCollateralFactorForLiquidation);
|
|
173
|
+
if (liquidationCollateralUsd < minCollateralUsd) {
|
|
174
|
+
liquidationCollateralUsd = minCollateralUsd;
|
|
175
|
+
}
|
|
176
|
+
return liquidationCollateralUsd - pnl - priceImpactDeltaUsd + closingFeeUsd;
|
|
177
|
+
}
|
|
151
178
|
export function getNetPriceImpactDeltaUsdForDecrease({ marketInfo, sizeInUsd, pendingImpactAmount, priceImpactDeltaUsd, sizeDeltaUsd, }) {
|
|
152
179
|
const { proportionalPendingImpactDeltaUsd } = getProportionalPendingImpactValues({
|
|
153
180
|
sizeInUsd,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { deserializeBigIntsInObject } from "../numbers";
|
|
2
2
|
export async function fetchApiStakingPower(ctx, params) {
|
|
3
|
-
const data = await ctx.api.fetchJson("/staking/power", {
|
|
3
|
+
const data = await ctx.api.fetchJson("/v1/staking/power", {
|
|
4
4
|
query: { address: params.address },
|
|
5
5
|
});
|
|
6
6
|
return deserializeBigIntsInObject(data, { handleInts: true });
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { validateSubaccountApprovalTypedData } from "../orderTransactions/validateTypedData";
|
|
2
2
|
export async function fetchSubaccountStatus(ctx, request) {
|
|
3
|
-
return ctx.api.postJson("/subaccounts/status", request);
|
|
3
|
+
return ctx.api.postJson("/v1/subaccounts/status", request);
|
|
4
4
|
}
|
|
5
5
|
export async function prepareSubaccountApproval(ctx, request) {
|
|
6
|
-
return ctx.api.postJson("/subaccounts/approval/prepare", request);
|
|
6
|
+
return ctx.api.postJson("/v1/subaccounts/approval/prepare", request);
|
|
7
7
|
}
|
|
8
8
|
export async function signSubaccountApproval(prepared, signer, options) {
|
|
9
9
|
const domain = prepared.domain;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { mockMarketsInfoData, mockTokensData, usdToToken } from "../../../test/mock";
|
|
3
|
+
import { applyFactor, expandDecimals, USD_DECIMALS } from "../../numbers";
|
|
4
|
+
import { getLiquidationPrice } from "../../positions";
|
|
5
|
+
import { getMaxWithdrawAmount } from "../decrease";
|
|
6
|
+
describe("getMaxWithdrawAmount", () => {
|
|
7
|
+
const ETH_PRICE = expandDecimals(11952, 29); // $1195.20
|
|
8
|
+
const USDC_PRICE = expandDecimals(1, 30);
|
|
9
|
+
function buildScenario() {
|
|
10
|
+
const tokensData = mockTokensData({
|
|
11
|
+
ETH: { prices: { minPrice: ETH_PRICE, maxPrice: ETH_PRICE } },
|
|
12
|
+
USDC: { prices: { minPrice: USDC_PRICE, maxPrice: USDC_PRICE } },
|
|
13
|
+
});
|
|
14
|
+
const marketKey = "ETH-ETH-USDC";
|
|
15
|
+
const marketsInfoData = mockMarketsInfoData(tokensData, [marketKey], {
|
|
16
|
+
[marketKey]: {
|
|
17
|
+
longInterestUsd: expandDecimals(100000000, USD_DECIMALS),
|
|
18
|
+
shortInterestUsd: expandDecimals(100000000, USD_DECIMALS),
|
|
19
|
+
longInterestInTokens: usdToToken(100000000, tokensData.ETH),
|
|
20
|
+
shortInterestInTokens: usdToToken(100000000, tokensData.ETH),
|
|
21
|
+
minCollateralFactor: expandDecimals(5, 27),
|
|
22
|
+
minCollateralFactorForLiquidation: expandDecimals(4, 27),
|
|
23
|
+
minCollateralFactorForOpenInterestLong: 0n,
|
|
24
|
+
minCollateralFactorForOpenInterestShort: 0n,
|
|
25
|
+
positionFeeFactorForBalanceWasNotImproved: expandDecimals(5, 26),
|
|
26
|
+
positionFeeFactorForBalanceWasImproved: expandDecimals(5, 26),
|
|
27
|
+
maxPositionImpactFactorForLiquidations: expandDecimals(1, 28),
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
const marketInfo = marketsInfoData[marketKey];
|
|
31
|
+
const usdcToken = tokensData.USDC;
|
|
32
|
+
const ethToken = tokensData.ETH;
|
|
33
|
+
const sizeInUsd = expandDecimals(50000, USD_DECIMALS);
|
|
34
|
+
const collateralUsd = expandDecimals(4000, USD_DECIMALS);
|
|
35
|
+
const sizeInTokens = (sizeInUsd * expandDecimals(1, ethToken.decimals)) / expandDecimals(1200, USD_DECIMALS);
|
|
36
|
+
const collateralAmount = (collateralUsd * expandDecimals(1, usdcToken.decimals)) / USDC_PRICE;
|
|
37
|
+
const position = {
|
|
38
|
+
key: "test-position",
|
|
39
|
+
contractKey: "test-position",
|
|
40
|
+
account: "0xtest",
|
|
41
|
+
marketAddress: marketKey,
|
|
42
|
+
collateralTokenAddress: usdcToken.address,
|
|
43
|
+
sizeInUsd,
|
|
44
|
+
sizeInTokens,
|
|
45
|
+
collateralAmount,
|
|
46
|
+
pendingBorrowingFeesUsd: 0n,
|
|
47
|
+
increasedAtTime: 0n,
|
|
48
|
+
decreasedAtTime: 0n,
|
|
49
|
+
isLong: true,
|
|
50
|
+
fundingFeeAmount: 0n,
|
|
51
|
+
claimableLongTokenAmount: 0n,
|
|
52
|
+
claimableShortTokenAmount: 0n,
|
|
53
|
+
pnl: -expandDecimals(200, USD_DECIMALS),
|
|
54
|
+
positionFeeAmount: 0n,
|
|
55
|
+
traderDiscountAmount: 0n,
|
|
56
|
+
uiFeeAmount: 0n,
|
|
57
|
+
pendingImpactAmount: -expandDecimals(5, 16),
|
|
58
|
+
data: "",
|
|
59
|
+
marketInfo,
|
|
60
|
+
market: marketInfo,
|
|
61
|
+
indexToken: ethToken,
|
|
62
|
+
longToken: ethToken,
|
|
63
|
+
shortToken: usdcToken,
|
|
64
|
+
indexName: "ETH",
|
|
65
|
+
poolName: "USDC",
|
|
66
|
+
collateralToken: usdcToken,
|
|
67
|
+
pnlToken: usdcToken,
|
|
68
|
+
markPrice: ETH_PRICE,
|
|
69
|
+
entryPrice: expandDecimals(1200, USD_DECIMALS),
|
|
70
|
+
liquidationPrice: undefined,
|
|
71
|
+
collateralUsd,
|
|
72
|
+
remainingCollateralUsd: collateralUsd,
|
|
73
|
+
remainingCollateralAmount: collateralAmount,
|
|
74
|
+
hasLowCollateral: false,
|
|
75
|
+
pnlPercentage: 0n,
|
|
76
|
+
pnlAfterFees: -expandDecimals(200, USD_DECIMALS),
|
|
77
|
+
pnlAfterFeesPercentage: 0n,
|
|
78
|
+
pendingFundingFeesUsd: 0n,
|
|
79
|
+
pendingClaimableFundingFeesUsd: 0n,
|
|
80
|
+
};
|
|
81
|
+
return { position, usdcToken };
|
|
82
|
+
}
|
|
83
|
+
it("max-withdraw keeps nextLiqPrice on the safe side of markPrice", () => {
|
|
84
|
+
const { position, usdcToken } = buildScenario();
|
|
85
|
+
const minCollateralUsd = expandDecimals(1, USD_DECIMALS);
|
|
86
|
+
const collateralPrice = usdcToken.prices.minPrice;
|
|
87
|
+
const maxWithdraw = getMaxWithdrawAmount({
|
|
88
|
+
position,
|
|
89
|
+
minCollateralUsd,
|
|
90
|
+
collateralPrice,
|
|
91
|
+
collateralDecimals: usdcToken.decimals,
|
|
92
|
+
userReferralInfo: undefined,
|
|
93
|
+
});
|
|
94
|
+
const withdrawUsd = (maxWithdraw * collateralPrice) / expandDecimals(1, usdcToken.decimals);
|
|
95
|
+
const nextCollateralUsd = position.collateralUsd - withdrawUsd;
|
|
96
|
+
const nextCollateralAmount = (nextCollateralUsd * expandDecimals(1, usdcToken.decimals)) / collateralPrice;
|
|
97
|
+
const nextLiqPrice = getLiquidationPrice({
|
|
98
|
+
sizeInUsd: position.sizeInUsd,
|
|
99
|
+
sizeInTokens: position.sizeInTokens,
|
|
100
|
+
collateralUsd: nextCollateralUsd,
|
|
101
|
+
collateralAmount: nextCollateralAmount,
|
|
102
|
+
collateralToken: position.collateralToken,
|
|
103
|
+
marketInfo: position.marketInfo,
|
|
104
|
+
pendingImpactAmount: position.pendingImpactAmount,
|
|
105
|
+
userReferralInfo: undefined,
|
|
106
|
+
pendingFundingFeesUsd: 0n,
|
|
107
|
+
pendingBorrowingFeesUsd: 0n,
|
|
108
|
+
isLong: position.isLong,
|
|
109
|
+
minCollateralUsd,
|
|
110
|
+
});
|
|
111
|
+
expect(nextLiqPrice).toBeDefined();
|
|
112
|
+
expect(nextLiqPrice).toBeLessThanOrEqual(position.markPrice);
|
|
113
|
+
});
|
|
114
|
+
it("test factors don't collapse to zero under PRECISION=1e30", () => {
|
|
115
|
+
const { position } = buildScenario();
|
|
116
|
+
expect(applyFactor(position.sizeInUsd, position.marketInfo.minCollateralFactorForLiquidation)).toBeGreaterThan(0n);
|
|
117
|
+
expect(applyFactor(position.sizeInUsd, position.marketInfo.minCollateralFactor)).toBeGreaterThan(0n);
|
|
118
|
+
});
|
|
119
|
+
});
|