@gmx-io/sdk 2.9.1 → 2.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/src/clients/v2/index.js +61 -41
- package/build/cjs/src/clients/v2/solana.spec.js +188 -0
- package/build/cjs/src/configs/api.js +4 -0
- package/build/cjs/src/utils/fees/index.js +7 -1
- package/build/cjs/src/utils/fees/priceImpact.js +118 -18
- package/build/cjs/src/utils/gmsol/constants.js +9 -0
- package/build/cjs/src/utils/gmsol/index.js +53 -0
- package/build/cjs/src/utils/gmsol/numbers.js +176 -0
- package/build/cjs/src/utils/gmsol/types.js +2 -0
- package/build/cjs/src/utils/http/http.js +3 -4
- package/build/cjs/src/utils/http/httpFallback.js +11 -1
- package/build/cjs/src/utils/http/solana.js +17 -0
- package/build/cjs/src/utils/http/validation.js +21 -1
- package/build/cjs/src/utils/http/validation.spec.js +45 -0
- package/build/cjs/src/utils/markets/__tests__/api.spec.js +53 -0
- package/build/cjs/src/utils/markets/api.js +5 -0
- package/build/cjs/src/utils/markets/utils.js +25 -9
- package/build/cjs/src/utils/orders/api.js +3 -0
- package/build/cjs/src/utils/positions/__tests__/liquidationPrice.spec.js +64 -0
- package/build/cjs/src/utils/positions/api.js +3 -0
- package/build/cjs/src/utils/positions/utils.js +88 -19
- package/build/cjs/src/utils/prices/utils.js +69 -14
- package/build/cjs/src/utils/referrals/api.js +41 -0
- package/build/cjs/src/utils/swap/swapRouting.js +4 -0
- package/build/cjs/src/utils/swap/swapStats.js +6 -1
- package/build/cjs/src/utils/trade/__tests__/gmsolTradeMath.spec.js +1012 -0
- package/build/cjs/src/utils/trade/decrease.js +266 -56
- package/build/cjs/src/utils/trade/decreaseMarginCheck.js +23 -4
- package/build/cjs/src/utils/trade/increase.js +86 -28
- package/build/cjs/src/utils/trade/increaseMarginCheck.js +51 -9
- package/build/cjs/src/utils/trade/twapIncreaseMarginCheck.js +8 -3
- package/build/esm/src/clients/v2/index.js +39 -19
- package/build/esm/src/clients/v2/solana.spec.js +160 -0
- package/build/esm/src/configs/api.js +5 -1
- package/build/esm/src/utils/fees/index.js +7 -1
- package/build/esm/src/utils/fees/priceImpact.js +111 -12
- package/build/esm/src/utils/gmsol/constants.js +6 -0
- package/build/esm/src/utils/gmsol/index.js +43 -0
- package/build/esm/src/utils/gmsol/numbers.js +172 -0
- package/build/esm/src/utils/gmsol/types.js +1 -0
- package/build/esm/src/utils/http/http.js +3 -4
- package/build/esm/src/utils/http/httpFallback.js +11 -1
- package/build/esm/src/utils/http/solana.js +12 -0
- package/build/esm/src/utils/http/validation.js +18 -0
- package/build/esm/src/utils/http/validation.spec.js +43 -0
- package/build/esm/src/utils/markets/__tests__/api.spec.js +53 -0
- package/build/esm/src/utils/markets/api.js +6 -1
- package/build/esm/src/utils/markets/utils.js +23 -8
- package/build/esm/src/utils/orders/api.js +3 -0
- package/build/esm/src/utils/positions/__tests__/liquidationPrice.spec.js +62 -0
- package/build/esm/src/utils/positions/api.js +3 -0
- package/build/esm/src/utils/positions/utils.js +89 -20
- package/build/esm/src/utils/prices/utils.js +70 -15
- package/build/esm/src/utils/referrals/api.js +36 -0
- package/build/esm/src/utils/swap/swapRouting.js +4 -0
- package/build/esm/src/utils/swap/swapStats.js +7 -2
- package/build/esm/src/utils/trade/__tests__/gmsolTradeMath.spec.js +1010 -0
- package/build/esm/src/utils/trade/decrease.js +266 -56
- package/build/esm/src/utils/trade/decreaseMarginCheck.js +23 -4
- package/build/esm/src/utils/trade/increase.js +86 -28
- package/build/esm/src/utils/trade/increaseMarginCheck.js +53 -11
- package/build/esm/src/utils/trade/twapIncreaseMarginCheck.js +8 -3
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/types/src/clients/v2/index.d.ts +21 -12
- package/build/types/src/clients/v2/solana.spec.d.ts +1 -0
- package/build/types/src/utils/fees/index.d.ts +1 -1
- package/build/types/src/utils/fees/priceImpact.d.ts +7 -0
- package/build/types/src/utils/gmsol/constants.d.ts +5 -0
- package/build/types/src/utils/gmsol/index.d.ts +35 -0
- package/build/types/src/utils/gmsol/numbers.d.ts +1 -0
- package/build/types/src/utils/gmsol/types.d.ts +9 -0
- package/build/types/src/utils/http/http.d.ts +2 -5
- package/build/types/src/utils/http/httpFallback.d.ts +2 -5
- package/build/types/src/utils/http/solana.d.ts +4 -0
- package/build/types/src/utils/http/types.d.ts +17 -4
- package/build/types/src/utils/http/validation.d.ts +8 -0
- package/build/types/src/utils/http/validation.spec.d.ts +1 -0
- package/build/types/src/utils/markets/hashKeys.d.ts +1 -1
- package/build/types/src/utils/markets/types.d.ts +3 -2
- package/build/types/src/utils/markets/utils.d.ts +6 -2
- package/build/types/src/utils/orders/api.d.ts +2 -2
- package/build/types/src/utils/positions/__tests__/liquidationPrice.spec.d.ts +1 -0
- package/build/types/src/utils/positions/api.d.ts +2 -2
- package/build/types/src/utils/positions/types.d.ts +1 -0
- package/build/types/src/utils/positions/utils.d.ts +6 -1
- package/build/types/src/utils/prices/types.d.ts +16 -0
- package/build/types/src/utils/prices/utils.d.ts +3 -11
- package/build/types/src/utils/referrals/api.d.ts +8 -0
- package/build/types/src/utils/referrals/types.d.ts +5 -0
- package/build/types/src/utils/rpc/createViemRpc.d.ts +25 -3
- package/build/types/src/utils/trade/__tests__/gmsolTradeMath.spec.d.ts +1 -0
- package/build/types/src/utils/trade/decrease.d.ts +5 -0
- package/build/types/src/utils/trade/decreaseMarginCheck.d.ts +2 -0
- package/build/types/src/utils/trade/increase.d.ts +2 -0
- package/build/types/src/utils/trade/increaseMarginCheck.d.ts +2 -0
- package/build/types/src/utils/trade/twapIncreaseMarginCheck.d.ts +1 -0
- package/build/types/src/utils/trade/types.d.ts +6 -0
- package/package.json +10 -1
|
@@ -4,6 +4,7 @@ exports.getDefaultAcceptablePriceImpactBps = exports.getAcceptablePriceByPriceIm
|
|
|
4
4
|
const factors_1 = require("../../configs/factors");
|
|
5
5
|
const bigmath_1 = require("../bigmath");
|
|
6
6
|
const fees_1 = require("../fees");
|
|
7
|
+
const gmsol_1 = require("../gmsol");
|
|
7
8
|
const numbers_1 = require("../numbers");
|
|
8
9
|
const types_1 = require("../orders/types");
|
|
9
10
|
const tokens_1 = require("../tokens");
|
|
@@ -66,7 +67,16 @@ function getIncreaseEvaluationIndexPrice(p) {
|
|
|
66
67
|
}
|
|
67
68
|
exports.getIncreaseEvaluationIndexPrice = getIncreaseEvaluationIndexPrice;
|
|
68
69
|
function getAcceptablePriceInfo(p) {
|
|
69
|
-
|
|
70
|
+
if (p.marketInfo.tradeMath === "gmsol") {
|
|
71
|
+
p = { ...p, sizeDeltaUsd: (0, gmsol_1.getGmsolUsdAmount)(p.sizeDeltaUsd) };
|
|
72
|
+
}
|
|
73
|
+
const { isIncrease, isLong, indexPrice, sizeDeltaUsd, maxNegativePriceImpactBps } = p;
|
|
74
|
+
const marketInfo = p.marketInfo.tradeMath === "gmsol" && p.isLimit
|
|
75
|
+
? {
|
|
76
|
+
...p.marketInfo,
|
|
77
|
+
indexToken: { ...p.marketInfo.indexToken, prices: { minPrice: indexPrice, maxPrice: indexPrice } },
|
|
78
|
+
}
|
|
79
|
+
: p.marketInfo;
|
|
70
80
|
const { indexToken } = marketInfo;
|
|
71
81
|
const values = {
|
|
72
82
|
acceptablePrice: 0n,
|
|
@@ -102,11 +112,6 @@ function getAcceptablePriceInfo(p) {
|
|
|
102
112
|
shouldCapNegativeImpact: false,
|
|
103
113
|
sizeDeltaInTokens: p.sizeDeltaInTokens,
|
|
104
114
|
});
|
|
105
|
-
/**
|
|
106
|
-
* We display this value as price impact on action (increase or decrease)
|
|
107
|
-
* But for acceptable price calculation uncapped price impact is used
|
|
108
|
-
* Also on decrease action we calculate totalImpactUsd which will be deducted from the collateral
|
|
109
|
-
*/
|
|
110
115
|
values.priceImpactDeltaUsd = priceImpactDeltaUsd;
|
|
111
116
|
values.balanceWasImproved = balanceWasImproved;
|
|
112
117
|
if (values.priceImpactDeltaUsd > 0) {
|
|
@@ -115,24 +120,69 @@ function getAcceptablePriceInfo(p) {
|
|
|
115
120
|
else {
|
|
116
121
|
values.priceImpactDeltaAmount = (0, numbers_1.roundUpMagnitudeDivision)(values.priceImpactDeltaUsd * (0, numbers_1.expandDecimals)(1, indexToken.decimals), indexToken.prices.minPrice);
|
|
117
122
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
123
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
124
|
+
const priceParams = { ...p, marketInfo, priceImpactDeltaUsd };
|
|
125
|
+
const acceptablePrice = isIncrease
|
|
126
|
+
? getGmsolIncreaseAcceptablePrice(priceParams)
|
|
127
|
+
: getGmsolDecreaseAcceptablePrice(priceParams);
|
|
128
|
+
if (acceptablePrice === undefined) {
|
|
129
|
+
return values;
|
|
130
|
+
}
|
|
131
|
+
values.acceptablePrice = acceptablePrice;
|
|
132
|
+
values.acceptablePriceDeltaBps = (0, numbers_1.getBasisPoints)((values.acceptablePrice - indexPrice) * (shouldFlipPriceImpact ? -1n : 1n), indexPrice);
|
|
133
|
+
return values;
|
|
134
|
+
}
|
|
124
135
|
const acceptablePriceValues = getAcceptablePriceByPriceImpact({
|
|
125
136
|
isIncrease,
|
|
126
137
|
isLong,
|
|
127
138
|
indexPrice,
|
|
128
139
|
sizeDeltaUsd,
|
|
129
|
-
priceImpactDeltaUsd
|
|
140
|
+
priceImpactDeltaUsd,
|
|
130
141
|
});
|
|
131
142
|
values.acceptablePrice = acceptablePriceValues.acceptablePrice;
|
|
132
143
|
values.acceptablePriceDeltaBps = acceptablePriceValues.acceptablePriceDeltaBps;
|
|
133
144
|
return values;
|
|
134
145
|
}
|
|
135
146
|
exports.getAcceptablePriceInfo = getAcceptablePriceInfo;
|
|
147
|
+
function getGmsolIncreaseAcceptablePrice(p) {
|
|
148
|
+
const { indexPrice, sizeDeltaUsd, priceImpactDeltaUsd, isLong, marketInfo } = p;
|
|
149
|
+
const { indexToken } = marketInfo;
|
|
150
|
+
const tokenUnit = (0, numbers_1.expandDecimals)(1, indexToken.decimals);
|
|
151
|
+
let baseSizeDeltaInTokens = p.sizeDeltaInTokens;
|
|
152
|
+
if (baseSizeDeltaInTokens == null) {
|
|
153
|
+
baseSizeDeltaInTokens = isLong
|
|
154
|
+
? bigmath_1.bigMath.mulDiv(sizeDeltaUsd, tokenUnit, indexPrice)
|
|
155
|
+
: (0, numbers_1.roundUpMagnitudeDivision)(sizeDeltaUsd * tokenUnit, indexPrice);
|
|
156
|
+
}
|
|
157
|
+
const sizeDeltaInTokens = (0, gmsol_1.getGmsolIncreaseSizeDeltaInTokens)({
|
|
158
|
+
baseSizeDeltaInTokens,
|
|
159
|
+
priceImpactDeltaUsd,
|
|
160
|
+
indexToken,
|
|
161
|
+
isLong,
|
|
162
|
+
});
|
|
163
|
+
return sizeDeltaInTokens === 0n
|
|
164
|
+
? undefined
|
|
165
|
+
: (0, gmsol_1.getGmsolUsdAmount)(bigmath_1.bigMath.mulDiv(sizeDeltaUsd, 1n, sizeDeltaInTokens)) * tokenUnit;
|
|
166
|
+
}
|
|
167
|
+
function getGmsolDecreaseAcceptablePrice(p) {
|
|
168
|
+
const { marketInfo, indexPrice, sizeDeltaUsd, sizeDeltaInTokens, positionSizeInUsd, positionSizeInTokens, isLong } = p;
|
|
169
|
+
const cappedImpactUsd = (0, fees_1.capPositionImpactUsdByMaxPriceImpactFactor)(marketInfo, sizeDeltaUsd, p.priceImpactDeltaUsd);
|
|
170
|
+
const tokenUnit = (0, numbers_1.expandDecimals)(1, marketInfo.indexToken.decimals);
|
|
171
|
+
let priceAdjustment;
|
|
172
|
+
if (positionSizeInUsd && positionSizeInTokens) {
|
|
173
|
+
const impactAtEntrySizeUsd = (0, gmsol_1.getGmsolUsdAmount)(bigmath_1.bigMath.mulDiv(positionSizeInUsd, cappedImpactUsd, sizeDeltaUsd));
|
|
174
|
+
priceAdjustment = bigmath_1.bigMath.mulDiv(impactAtEntrySizeUsd, tokenUnit, positionSizeInTokens);
|
|
175
|
+
}
|
|
176
|
+
else if (sizeDeltaInTokens && sizeDeltaInTokens > 0n) {
|
|
177
|
+
priceAdjustment = bigmath_1.bigMath.mulDiv(cappedImpactUsd, tokenUnit, sizeDeltaInTokens);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
priceAdjustment = bigmath_1.bigMath.mulDiv(indexPrice, cappedImpactUsd, sizeDeltaUsd);
|
|
181
|
+
}
|
|
182
|
+
const priceUnit = tokenUnit * gmsol_1.GMSOL_PRECISION_SCALE;
|
|
183
|
+
priceAdjustment = bigmath_1.bigMath.mulDiv(priceAdjustment, 1n, priceUnit) * priceUnit;
|
|
184
|
+
return indexPrice + (isLong ? priceAdjustment : -priceAdjustment);
|
|
185
|
+
}
|
|
136
186
|
function getAcceptablePriceByPriceImpact(p) {
|
|
137
187
|
const { indexPrice, sizeDeltaUsd, priceImpactDeltaUsd } = p;
|
|
138
188
|
if (sizeDeltaUsd <= 0 || indexPrice == 0n) {
|
|
@@ -155,7 +205,12 @@ function getAcceptablePriceByPriceImpact(p) {
|
|
|
155
205
|
}
|
|
156
206
|
exports.getAcceptablePriceByPriceImpact = getAcceptablePriceByPriceImpact;
|
|
157
207
|
function getDefaultAcceptablePriceImpactBps(p) {
|
|
158
|
-
const { indexPrice, sizeDeltaUsd, priceImpactDeltaUsd, acceptablePriceImpactBuffer = factors_1.DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER, } = p;
|
|
208
|
+
const { indexPrice, sizeDeltaUsd, priceImpactDeltaUsd, acceptablePriceImpactBuffer = factors_1.DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER, executionPrice, } = p;
|
|
209
|
+
if (executionPrice !== undefined && executionPrice > 0n && indexPrice > 0n) {
|
|
210
|
+
const adversePriceDelta = (executionPrice - indexPrice) * (getShouldUseMaxPrice(p.isIncrease, p.isLong) ? 1n : -1n);
|
|
211
|
+
return ((adversePriceDelta > 0n ? (0, numbers_1.getBasisPoints)(adversePriceDelta, indexPrice, true) : 0n) +
|
|
212
|
+
BigInt(acceptablePriceImpactBuffer));
|
|
213
|
+
}
|
|
159
214
|
if (priceImpactDeltaUsd > 0) {
|
|
160
215
|
return BigInt(acceptablePriceImpactBuffer);
|
|
161
216
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchApiFeeDiscount = exports.parseFeeDiscount = void 0;
|
|
4
|
+
const validation_1 = require("../http/validation");
|
|
5
|
+
const objects_1 = require("../objects");
|
|
6
|
+
const FEE_DISCOUNT_ENDPOINT = "/v1/fee-discount";
|
|
7
|
+
function parseFeeDiscount(raw) {
|
|
8
|
+
if (!(0, objects_1.isRecord)(raw)) {
|
|
9
|
+
throw new Error(`${FEE_DISCOUNT_ENDPOINT}: expected an object`);
|
|
10
|
+
}
|
|
11
|
+
const record = raw;
|
|
12
|
+
const endpoint = FEE_DISCOUNT_ENDPOINT;
|
|
13
|
+
if (typeof record.attachedOnChain !== "boolean") {
|
|
14
|
+
throw new Error(`${FEE_DISCOUNT_ENDPOINT}: expected boolean field "attachedOnChain"`);
|
|
15
|
+
}
|
|
16
|
+
if (typeof record.tierId !== "number" || !Number.isSafeInteger(record.tierId) || record.tierId < 0) {
|
|
17
|
+
throw new Error(`${FEE_DISCOUNT_ENDPOINT}: expected number field "tierId"`);
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
userReferralInfo: {
|
|
21
|
+
userReferralCode: (0, validation_1.readApiString)({ record, name: "userReferralCode", endpoint }),
|
|
22
|
+
userReferralCodeString: (0, validation_1.readApiString)({ record, name: "userReferralCodeString", endpoint }),
|
|
23
|
+
referralCodeForTxn: (0, validation_1.readApiString)({ record, name: "referralCodeForTxn", endpoint }),
|
|
24
|
+
attachedOnChain: record.attachedOnChain,
|
|
25
|
+
affiliate: (0, validation_1.readApiString)({ record, name: "affiliate", endpoint }),
|
|
26
|
+
tierId: record.tierId,
|
|
27
|
+
totalRebate: (0, validation_1.readApiUnsignedBigInt)({ record, name: "totalRebate", endpoint }),
|
|
28
|
+
totalRebateFactor: (0, validation_1.readApiUnsignedBigInt)({ record, name: "totalRebateFactor", endpoint }),
|
|
29
|
+
discountShare: (0, validation_1.readApiUnsignedBigInt)({ record, name: "discountShare", endpoint }),
|
|
30
|
+
discountFactor: (0, validation_1.readApiUnsignedBigInt)({ record, name: "discountFactor", endpoint }),
|
|
31
|
+
},
|
|
32
|
+
orderFeeDiscountFactor: (0, validation_1.readApiUnsignedBigInt)({ record, name: "orderFeeDiscountFactor", endpoint }),
|
|
33
|
+
updatedAt: typeof record.updatedAt === "number" ? record.updatedAt : undefined,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
exports.parseFeeDiscount = parseFeeDiscount;
|
|
37
|
+
async function fetchApiFeeDiscount(ctx, params) {
|
|
38
|
+
const raw = await ctx.api.fetchJson(FEE_DISCOUNT_ENDPOINT, { query: { address: params.address } });
|
|
39
|
+
return parseFeeDiscount(raw);
|
|
40
|
+
}
|
|
41
|
+
exports.fetchApiFeeDiscount = fetchApiFeeDiscount;
|
|
@@ -5,6 +5,7 @@ const viem_1 = require("viem");
|
|
|
5
5
|
const bigmath_1 = require("../bigmath");
|
|
6
6
|
const fees_1 = require("../fees");
|
|
7
7
|
const getNaiveEstimatedGasBySwapCount_1 = require("../fees/getNaiveEstimatedGasBySwapCount");
|
|
8
|
+
const priceImpact_1 = require("../fees/priceImpact");
|
|
8
9
|
const markets_1 = require("../markets");
|
|
9
10
|
const numbers_1 = require("../numbers");
|
|
10
11
|
const objects_1 = require("../objects");
|
|
@@ -121,6 +122,9 @@ function getBestSwapPath({ routes, usdIn, estimator, networkEstimator, }) {
|
|
|
121
122
|
}
|
|
122
123
|
}
|
|
123
124
|
catch (e) {
|
|
125
|
+
if (e instanceof priceImpact_1.GmsolPriceImpactError) {
|
|
126
|
+
throw e;
|
|
127
|
+
}
|
|
124
128
|
continue;
|
|
125
129
|
}
|
|
126
130
|
}
|
|
@@ -5,6 +5,7 @@ const viem_1 = require("viem");
|
|
|
5
5
|
const tokens_1 = require("../../configs/tokens");
|
|
6
6
|
const bigmath_1 = require("../bigmath");
|
|
7
7
|
const fees_1 = require("../fees");
|
|
8
|
+
const gmsol_1 = require("../gmsol");
|
|
8
9
|
const markets_1 = require("../markets");
|
|
9
10
|
const numbers_1 = require("../numbers");
|
|
10
11
|
const objects_1 = require("../objects");
|
|
@@ -154,11 +155,15 @@ function getSwapStats(p) {
|
|
|
154
155
|
let priceImpactDeltaUsd;
|
|
155
156
|
let balanceWasImproved;
|
|
156
157
|
try {
|
|
157
|
-
const
|
|
158
|
+
const impactUsd = marketInfo.tradeMath === "gmsol" ? (0, tokens_2.convertToUsd)(amountIn, tokenIn.decimals, (0, gmsol_1.getGmsolMidPrice)(tokenIn)) : usdIn;
|
|
159
|
+
const priceImpactValues = (0, fees_1.getPriceImpactForSwap)(marketInfo, tokenIn, tokenOut, impactUsd, -impactUsd);
|
|
158
160
|
priceImpactDeltaUsd = priceImpactValues.priceImpactDeltaUsd;
|
|
159
161
|
balanceWasImproved = priceImpactValues.balanceWasImproved;
|
|
160
162
|
}
|
|
161
163
|
catch (e) {
|
|
164
|
+
if (e instanceof fees_1.GmsolPriceImpactError) {
|
|
165
|
+
throw e;
|
|
166
|
+
}
|
|
162
167
|
// Approximate if the market would be out of capacity
|
|
163
168
|
const capacityUsd = getSwapCapacityUsd(marketInfo, isFromLongToShort);
|
|
164
169
|
const swapFeeUsd = (0, fees_1.getSwapFee)(marketInfo, usdIn, false, swapPricingType);
|