@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
|
@@ -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
|
+
});
|
|
@@ -3,7 +3,7 @@ import { bigMath } from "../bigmath";
|
|
|
3
3
|
import { getPositionFee } from "../fees";
|
|
4
4
|
import { applyFactor, BASIS_POINTS_DIVISOR_BIGINT, expandDecimals, getBasisPoints, roundUpDivision, USD_DECIMALS, MaxUint256, } from "../numbers";
|
|
5
5
|
import { DecreasePositionSwapType, OrderType, SwapPricingType } from "../orders/types";
|
|
6
|
-
import { getLeverage, getLiquidationPrice, getMinCollateralFactorForPosition, getNetPriceImpactDeltaUsdForDecrease, getPositionPnlUsd, } from "../positions";
|
|
6
|
+
import { getLeverage, getLiquidationPrice, getMinCollateralFactorForPosition, getMinCollateralUsdForLiquidationPrice, getNetPriceImpactDeltaUsdForDecrease, getPositionPnlUsd, } from "../positions";
|
|
7
7
|
import { getAcceptablePriceInfo, getDefaultAcceptablePriceImpactBps, getMarkPrice, getOrderThresholdType, } from "../prices";
|
|
8
8
|
import { getSwapAmountsByFromValue, getSwapStats } from "../swap";
|
|
9
9
|
import { convertToTokenAmount, convertToUsd, getIsEquivalentTokens } from "../tokens";
|
|
@@ -207,10 +207,14 @@ export function getDecreasePositionAmounts(p) {
|
|
|
207
207
|
values.payedOutputUsd = convertToUsd(payedInfo.paidOutputAmount, collateralToken.decimals, values.collateralPrice);
|
|
208
208
|
values.payedRemainingCollateralAmount = payedInfo.paidRemainingCollateralAmount;
|
|
209
209
|
values.payedRemainingCollateralUsd = convertToUsd(payedInfo.paidRemainingCollateralAmount, collateralToken.decimals, values.collateralPrice);
|
|
210
|
+
let netCollateralOutputAmount = 0n;
|
|
211
|
+
let netCollateralOutputUsd = 0n;
|
|
210
212
|
// Collateral delta
|
|
211
213
|
if (values.isFullClose) {
|
|
212
214
|
values.collateralDeltaUsd = estimatedCollateralUsd;
|
|
213
215
|
values.collateralDeltaAmount = position.collateralAmount;
|
|
216
|
+
netCollateralOutputAmount = payedInfo.remainingCollateralAmount;
|
|
217
|
+
netCollateralOutputUsd = convertToUsd(netCollateralOutputAmount, collateralToken.decimals, values.collateralPrice);
|
|
214
218
|
values.receiveTokenAmount = payedInfo.outputAmount + payedInfo.remainingCollateralAmount;
|
|
215
219
|
}
|
|
216
220
|
else if (keepLeverage &&
|
|
@@ -235,6 +239,8 @@ export function getDecreasePositionAmounts(p) {
|
|
|
235
239
|
? remainingCollateralUsd - bigMath.mulDiv(nextSizeInUsd, BASIS_POINTS_DIVISOR_BIGINT, leverageWithoutPnl)
|
|
236
240
|
: 0n;
|
|
237
241
|
values.collateralDeltaAmount = convertToTokenAmount(values.collateralDeltaUsd, collateralToken.decimals, values.collateralPrice);
|
|
242
|
+
netCollateralOutputAmount = values.collateralDeltaAmount;
|
|
243
|
+
netCollateralOutputUsd = values.collateralDeltaUsd;
|
|
238
244
|
values.receiveTokenAmount = payedInfo.outputAmount + values.collateralDeltaAmount;
|
|
239
245
|
}
|
|
240
246
|
else {
|
|
@@ -245,7 +251,21 @@ export function getDecreasePositionAmounts(p) {
|
|
|
245
251
|
values.receiveUsd = convertToUsd(values.receiveTokenAmount, collateralToken.decimals, values.collateralPrice);
|
|
246
252
|
// SwapCollateralTokenToPnlToken → primaryOutput (pnl token), otherwise → secondaryOutput (collateral token)
|
|
247
253
|
const pnlTokenPrice = pnlToken.prices.minPrice;
|
|
248
|
-
|
|
254
|
+
const remainingPnlOutputUsd = convertToUsd(payedInfo.outputAmount, collateralToken.decimals, values.collateralPrice) ?? 0n;
|
|
255
|
+
if (values.decreaseSwapType === DecreasePositionSwapType.NoSwap &&
|
|
256
|
+
!getIsEquivalentTokens(pnlToken, collateralToken)) {
|
|
257
|
+
values.primaryOutput = {
|
|
258
|
+
tokenAddress: pnlToken.address,
|
|
259
|
+
amount: convertToTokenAmount(remainingPnlOutputUsd, pnlToken.decimals, pnlTokenPrice) ?? 0n,
|
|
260
|
+
usd: remainingPnlOutputUsd,
|
|
261
|
+
};
|
|
262
|
+
values.secondaryOutput = {
|
|
263
|
+
tokenAddress: collateralToken.address,
|
|
264
|
+
amount: netCollateralOutputAmount,
|
|
265
|
+
usd: netCollateralOutputUsd,
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
else if (values.decreaseSwapType === DecreasePositionSwapType.SwapCollateralTokenToPnlToken) {
|
|
249
269
|
values.primaryOutput = {
|
|
250
270
|
tokenAddress: pnlToken.address,
|
|
251
271
|
amount: convertToTokenAmount(values.receiveUsd, pnlToken.decimals, pnlTokenPrice) ?? 0n,
|
|
@@ -302,6 +322,33 @@ export function getMinCollateralUsdForLeverage(position, openInterestDelta) {
|
|
|
302
322
|
const minCollateralFactor = getMinCollateralFactorForPosition(position, openInterestDelta);
|
|
303
323
|
return applyFactor(position.sizeInUsd, minCollateralFactor);
|
|
304
324
|
}
|
|
325
|
+
export function getMaxWithdrawAmount(p) {
|
|
326
|
+
const { position, minCollateralUsd, collateralPrice, collateralDecimals, userReferralInfo } = p;
|
|
327
|
+
const minCollateralUsdForLeverage = getMinCollateralUsdForLeverage(position, 0n);
|
|
328
|
+
let minRequiredNextCollateralUsd = minCollateralUsdForLeverage;
|
|
329
|
+
if (minCollateralUsd !== undefined && minCollateralUsd > minRequiredNextCollateralUsd) {
|
|
330
|
+
minRequiredNextCollateralUsd = minCollateralUsd;
|
|
331
|
+
}
|
|
332
|
+
const minCollateralUsdForLiquidationPrice = getMinCollateralUsdForLiquidationPrice({
|
|
333
|
+
sizeInUsd: position.sizeInUsd,
|
|
334
|
+
sizeInTokens: position.sizeInTokens,
|
|
335
|
+
marketInfo: position.marketInfo,
|
|
336
|
+
pendingImpactAmount: position.pendingImpactAmount,
|
|
337
|
+
minCollateralUsd: minCollateralUsd ?? 0n,
|
|
338
|
+
pnl: position.pnl,
|
|
339
|
+
isLong: position.isLong,
|
|
340
|
+
userReferralInfo,
|
|
341
|
+
});
|
|
342
|
+
if (minCollateralUsdForLiquidationPrice > minRequiredNextCollateralUsd) {
|
|
343
|
+
minRequiredNextCollateralUsd = minCollateralUsdForLiquidationPrice;
|
|
344
|
+
}
|
|
345
|
+
const minRequiredCollateralUsd = minRequiredNextCollateralUsd + (position.pendingBorrowingFeesUsd ?? 0n) + (position.pendingFundingFeesUsd ?? 0n);
|
|
346
|
+
if (position.collateralUsd < minRequiredCollateralUsd) {
|
|
347
|
+
return 0n;
|
|
348
|
+
}
|
|
349
|
+
const maxWithdrawUsd = position.collateralUsd - minRequiredCollateralUsd;
|
|
350
|
+
return convertToTokenAmount(maxWithdrawUsd, collateralDecimals, collateralPrice) ?? 0n;
|
|
351
|
+
}
|
|
305
352
|
export function payForCollateralCost(p) {
|
|
306
353
|
const { initialCostUsd, collateralToken, collateralPrice, outputAmount, remainingCollateralAmount } = p;
|
|
307
354
|
const values = {
|
|
@@ -496,7 +543,7 @@ export function selectBetterDecreaseSwapType(p) {
|
|
|
496
543
|
return p.pathBUsdOut > p.pathAUsdOut ? "pathB" : "pathA";
|
|
497
544
|
}
|
|
498
545
|
export function getOptimalDecreaseAndSwapAmounts(p) {
|
|
499
|
-
const { marketInfo, collateralToken, isLong, position, closeSizeUsd, keepLeverage, triggerPrice, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, userReferralInfo, minCollateralUsd, minPositionSizeUsd, uiFeeFactor, triggerOrderType, isSetAcceptablePriceImpactEnabled, receiveToken, findSwapPath, findSwapPathFromPnl, marketsInfoData, chainId, } = p;
|
|
546
|
+
const { marketInfo, collateralToken, isLong, position, closeSizeUsd, keepLeverage, triggerPrice, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, userReferralInfo, minCollateralUsd, minPositionSizeUsd, uiFeeFactor, triggerOrderType, isSetAcceptablePriceImpactEnabled, receiveToken, forceDecreaseSwapType, findSwapPath, findSwapPathFromPnl, marketsInfoData, chainId, } = p;
|
|
500
547
|
const decreaseBaseParams = {
|
|
501
548
|
marketInfo,
|
|
502
549
|
collateralToken,
|
|
@@ -515,6 +562,15 @@ export function getOptimalDecreaseAndSwapAmounts(p) {
|
|
|
515
562
|
isSetAcceptablePriceImpactEnabled,
|
|
516
563
|
receiveToken,
|
|
517
564
|
};
|
|
565
|
+
if (forceDecreaseSwapType !== undefined) {
|
|
566
|
+
return {
|
|
567
|
+
decreaseAmounts: getDecreasePositionAmounts({
|
|
568
|
+
...decreaseBaseParams,
|
|
569
|
+
forceDecreaseSwapType,
|
|
570
|
+
}),
|
|
571
|
+
swapAmounts: undefined,
|
|
572
|
+
};
|
|
573
|
+
}
|
|
518
574
|
const pathADecrease = getDecreasePositionAmounts(decreaseBaseParams);
|
|
519
575
|
const pnlToken = isLong ? marketInfo.longToken : marketInfo.shortToken;
|
|
520
576
|
const shouldSwap = !getIsEquivalentTokens(collateralToken, receiveToken);
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { getIsEquivalentTokens } from "../tokens";
|
|
2
|
+
export function getDecreaseReceiveOutputs({ decreaseAmounts, tokensData, }) {
|
|
3
|
+
if (!decreaseAmounts || !tokensData) {
|
|
4
|
+
return [];
|
|
5
|
+
}
|
|
6
|
+
return [
|
|
7
|
+
{ type: "primary", output: decreaseAmounts.primaryOutput },
|
|
8
|
+
{ type: "secondary", output: decreaseAmounts.secondaryOutput },
|
|
9
|
+
].reduce((acc, { type, output }) => {
|
|
10
|
+
if (output.amount <= 0n) {
|
|
11
|
+
return acc;
|
|
12
|
+
}
|
|
13
|
+
const token = tokensData[output.tokenAddress];
|
|
14
|
+
if (!token) {
|
|
15
|
+
return acc;
|
|
16
|
+
}
|
|
17
|
+
acc.push({
|
|
18
|
+
type,
|
|
19
|
+
token,
|
|
20
|
+
amount: output.amount,
|
|
21
|
+
usd: output.usd,
|
|
22
|
+
});
|
|
23
|
+
return acc;
|
|
24
|
+
}, []);
|
|
25
|
+
}
|
|
26
|
+
export function getCanSplitReceive(position) {
|
|
27
|
+
if (!position?.pnlToken || !position.collateralToken) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
return !getIsEquivalentTokens(position.pnlToken, position.collateralToken);
|
|
31
|
+
}
|
|
32
|
+
export function getHasSplitReceiveOutputs(outputs) {
|
|
33
|
+
if (outputs.length < 2) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
return outputs.some((output) => !getIsEquivalentTokens(output.token, outputs[0].token));
|
|
37
|
+
}
|
|
38
|
+
export function getIsSplitReceiveAvailable(position, outputs) {
|
|
39
|
+
return getCanSplitReceive(position) && getHasSplitReceiveOutputs(outputs);
|
|
40
|
+
}
|
|
@@ -117,6 +117,7 @@ export function createRawTradeActionTransformer(marketsInfoData, wrappedToken, t
|
|
|
117
117
|
swapImpactUsd: rawAction.swapImpactUsd ? BigInt(rawAction.swapImpactUsd) : undefined,
|
|
118
118
|
totalImpactUsd: rawAction.totalImpactUsd ? BigInt(rawAction.totalImpactUsd) : undefined,
|
|
119
119
|
positionFeeAmount: rawAction.positionFeeAmount ? BigInt(rawAction.positionFeeAmount) : undefined,
|
|
120
|
+
traderDiscountAmount: rawAction.traderDiscountAmount ? BigInt(rawAction.traderDiscountAmount) : undefined,
|
|
120
121
|
borrowingFeeAmount: rawAction.borrowingFeeAmount ? BigInt(rawAction.borrowingFeeAmount) : undefined,
|
|
121
122
|
fundingFeeAmount: rawAction.fundingFeeAmount ? BigInt(rawAction.fundingFeeAmount) : undefined,
|
|
122
123
|
swapFeeUsd: rawAction.swapFeeUsd ? BigInt(rawAction.swapFeeUsd) : undefined,
|
|
@@ -7,7 +7,7 @@ function parseTradesResponse(raw) {
|
|
|
7
7
|
};
|
|
8
8
|
}
|
|
9
9
|
export async function fetchApiTrades(ctx, params) {
|
|
10
|
-
return ctx.api.fetchJson("/trades", {
|
|
10
|
+
return ctx.api.fetchJson("/v1/trades", {
|
|
11
11
|
query: {
|
|
12
12
|
address: params.address,
|
|
13
13
|
symbol: params.symbol,
|
|
@@ -22,7 +22,7 @@ export async function fetchApiTrades(ctx, params) {
|
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
24
|
export async function searchApiTrades(ctx, params) {
|
|
25
|
-
return ctx.api.postJson("/trades/search", params, {
|
|
25
|
+
return ctx.api.postJson("/v1/trades/search", params, {
|
|
26
26
|
transform: parseTradesResponse,
|
|
27
27
|
});
|
|
28
28
|
}
|
|
@@ -58,6 +58,9 @@ export function getUiFeeReceiverSource(uiFeeReceiver) {
|
|
|
58
58
|
return getUiFeeReceiverByte(uiFeeReceiver, SOURCE_BYTE_START, SOURCE_BYTE_END);
|
|
59
59
|
}
|
|
60
60
|
export function setUiFeeReceiverIsExpress(uiFeeReceiver, isExpress) {
|
|
61
|
+
if (!isValidTwapUiFeeReceiver(uiFeeReceiver)) {
|
|
62
|
+
return uiFeeReceiver;
|
|
63
|
+
}
|
|
61
64
|
return setUiFeeReceiverByte(uiFeeReceiver, EXPRESS_BYTE_START, EXPRESS_BYTE_END, isExpress);
|
|
62
65
|
}
|
|
63
66
|
export function setUiFeeReceiverIsJit(uiFeeReceiver, isJit) {
|