@gmx-io/sdk 1.1.2 → 1.2.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/src/abis/StBTC.json +1225 -0
- package/build/cjs/src/abis/SyntheticsReader.json +480 -19
- package/build/cjs/src/configs/__tests__/tokens.spec.js +1 -0
- package/build/cjs/src/configs/chains.js +57 -6
- package/build/cjs/src/configs/contracts.js +64 -68
- package/build/cjs/src/configs/dataStore.js +4 -3
- package/build/cjs/src/configs/express.js +2 -1
- package/build/cjs/src/configs/markets.js +37 -0
- package/build/cjs/src/configs/oracleKeeper.js +1 -0
- package/build/cjs/src/configs/tokens.js +130 -1
- package/build/cjs/src/modules/orders/helpers.js +12 -13
- package/build/cjs/src/modules/positions/positions.js +9 -0
- package/build/cjs/src/prebuilt/hashedKinkModelMarketRatesKeys.json +42 -0
- package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +257 -0
- package/build/cjs/src/prebuilt/hashedMarketValuesKeys.json +77 -0
- package/build/cjs/src/types/subsquid.js +103 -1
- package/build/cjs/src/utils/__tests__/numbers.spec.js +17 -9
- package/build/cjs/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +2 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +2 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/swapOrders.spec.js +1 -0
- package/build/cjs/src/utils/__tests__/prices.spec.js +3 -2
- package/build/cjs/src/utils/__tests__/uiFeeReceiver.spec.js +9 -0
- package/build/cjs/src/utils/errors/transactionsErrors.js +2 -1
- package/build/cjs/src/utils/fees/executionFee.js +9 -1
- package/build/cjs/src/utils/numbers.js +52 -34
- package/build/cjs/src/utils/orderTransactions.js +3 -0
- package/build/cjs/src/utils/orders.js +8 -2
- package/build/cjs/src/utils/prices.js +1 -1
- package/build/cjs/src/utils/swap/swapValues.js +40 -0
- package/build/cjs/src/utils/tokens.js +10 -1
- package/build/cjs/src/utils/twap/uiFeeReceiver.js +26 -6
- package/build/esm/src/abis/StBTC.json +1225 -0
- package/build/esm/src/abis/SyntheticsReader.json +480 -19
- package/build/esm/src/configs/__tests__/tokens.spec.js +2 -1
- package/build/esm/src/configs/chains.js +56 -5
- package/build/esm/src/configs/contracts.js +65 -69
- package/build/esm/src/configs/dataStore.js +1 -0
- package/build/esm/src/configs/express.js +4 -3
- package/build/esm/src/configs/markets.js +38 -1
- package/build/esm/src/configs/oracleKeeper.js +2 -1
- package/build/esm/src/configs/tokens.js +131 -2
- package/build/esm/src/modules/orders/helpers.js +12 -13
- package/build/esm/src/modules/positions/positions.js +9 -0
- package/build/esm/src/prebuilt/hashedKinkModelMarketRatesKeys.json +42 -0
- package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +257 -0
- package/build/esm/src/prebuilt/hashedMarketValuesKeys.json +77 -0
- package/build/esm/src/types/subsquid.js +102 -0
- package/build/esm/src/utils/__tests__/numbers.spec.js +18 -10
- package/build/esm/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +2 -0
- package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +2 -0
- package/build/esm/src/utils/__tests__/orderTransactions/swapOrders.spec.js +1 -0
- package/build/esm/src/utils/__tests__/prices.spec.js +3 -2
- package/build/esm/src/utils/__tests__/uiFeeReceiver.spec.js +9 -0
- package/build/esm/src/utils/errors/transactionsErrors.js +2 -1
- package/build/esm/src/utils/fees/executionFee.js +11 -3
- package/build/esm/src/utils/numbers.js +50 -31
- package/build/esm/src/utils/orderTransactions.js +3 -0
- package/build/esm/src/utils/orders.js +8 -2
- package/build/esm/src/utils/prices.js +1 -1
- package/build/esm/src/utils/swap/swapValues.js +41 -1
- package/build/esm/src/utils/tokens.js +7 -0
- package/build/esm/src/utils/twap/uiFeeReceiver.js +24 -5
- package/build/types/src/configs/chains.d.ts +20 -37
- package/build/types/src/configs/contracts.d.ts +60 -63
- package/build/types/src/configs/dataStore.d.ts +1 -0
- package/build/types/src/configs/markets.d.ts +2 -1
- package/build/types/src/configs/oracleKeeper.d.ts +1 -1
- package/build/types/src/configs/tokens.d.ts +1 -0
- package/build/types/src/index.d.ts +1 -1
- package/build/types/src/modules/base.d.ts +1 -1
- package/build/types/src/modules/orders/helpers.d.ts +10 -2
- package/build/types/src/modules/orders/orders.d.ts +10 -1
- package/build/types/src/types/orders.d.ts +1 -1
- package/build/types/src/types/sdk.d.ts +2 -1
- package/build/types/src/types/subsquid.d.ts +388 -0
- package/build/types/src/types/tokens.d.ts +1 -0
- package/build/types/src/types/trade.d.ts +1 -0
- package/build/types/src/utils/multicall.d.ts +1 -1
- package/build/types/src/utils/numbers.d.ts +8 -3
- package/build/types/src/utils/orderTransactions.d.ts +2 -3
- package/build/types/src/utils/prices.d.ts +1 -1
- package/build/types/src/utils/tokens.d.ts +2 -0
- package/build/types/src/utils/twap/uiFeeReceiver.d.ts +13 -0
- package/package.json +1 -1
|
@@ -61,7 +61,8 @@ describe("getTriggerThresholdType", () => {
|
|
|
61
61
|
const result = getOrderThresholdType(OrderType.StopIncrease, false);
|
|
62
62
|
expect(result).toBe(TriggerThresholdType.Below);
|
|
63
63
|
});
|
|
64
|
-
it("
|
|
65
|
-
|
|
64
|
+
it("returns undefined for invalid order type", () => {
|
|
65
|
+
const result = getOrderThresholdType("SomeInvalidType", true);
|
|
66
|
+
expect(result).toBeUndefined();
|
|
66
67
|
});
|
|
67
68
|
});
|
|
@@ -11,12 +11,21 @@ describe("uiFeeReceiver", () => {
|
|
|
11
11
|
it("should return undefined if twapId inside is 0000", () => {
|
|
12
12
|
expect(decodeTwapUiFeeReceiver("0xff00000000000000000000000000000000000001")).toBeUndefined();
|
|
13
13
|
});
|
|
14
|
+
it("should correctly decode isExpress", () => {
|
|
15
|
+
expect(decodeTwapUiFeeReceiver("0xff0000000000000000000000000000010a123401")).toEqual({
|
|
16
|
+
isExpress: true,
|
|
17
|
+
twapId: "1234",
|
|
18
|
+
numberOfParts: 10,
|
|
19
|
+
});
|
|
20
|
+
});
|
|
14
21
|
it("should return the twapId and numberOfParts if the uiFeeReceiver is valid twap", () => {
|
|
15
22
|
expect(decodeTwapUiFeeReceiver("0xff0000000000000000000000000000000a123401")).toEqual({
|
|
23
|
+
isExpress: false,
|
|
16
24
|
twapId: "1234",
|
|
17
25
|
numberOfParts: 10,
|
|
18
26
|
});
|
|
19
27
|
expect(decodeTwapUiFeeReceiver("0xff000000000000000000000000000000153a4f01")).toEqual({
|
|
28
|
+
isExpress: false,
|
|
20
29
|
twapId: "3a4f",
|
|
21
30
|
numberOfParts: 21,
|
|
22
31
|
});
|
|
@@ -16,9 +16,10 @@ const TX_ERROR_PATTERNS = {
|
|
|
16
16
|
[TxErrorType.UserDenied]: [
|
|
17
17
|
{ msg: "User denied transaction signature" },
|
|
18
18
|
{ msg: "User rejected" },
|
|
19
|
+
{ msg: "User cancelled" },
|
|
19
20
|
{ msg: "user rejected action" },
|
|
20
21
|
{ msg: "ethers-user-denied" },
|
|
21
|
-
{ msg: "
|
|
22
|
+
{ msg: "Action cancelled by user" },
|
|
22
23
|
{ msg: "Signing aborted by user" },
|
|
23
24
|
],
|
|
24
25
|
[TxErrorType.Slippage]: [
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { getExcessiveExecutionFee, getHighExecutionFee } from "../../configs/chains";
|
|
1
|
+
import { getExcessiveExecutionFee, getHighExecutionFee, MIN_EXECUTION_FEE_USD } from "../../configs/chains";
|
|
2
2
|
import { USD_DECIMALS } from "../../configs/factors";
|
|
3
3
|
import { NATIVE_TOKEN_ADDRESS } from "../../configs/tokens";
|
|
4
4
|
import { DecreasePositionSwapType } from "../../types/orders";
|
|
5
|
+
import { bigMath } from "../bigmath";
|
|
5
6
|
import { applyFactor, expandDecimals } from "../numbers";
|
|
6
|
-
import { convertBetweenTokens, convertToUsd, getTokenData } from "../tokens";
|
|
7
|
+
import { convertBetweenTokens, convertToTokenAmount, convertToUsd, getTokenData } from "../tokens";
|
|
7
8
|
export function getExecutionFee(chainId, gasLimits,
|
|
8
9
|
// TODO optimize we only need the native token data
|
|
9
10
|
tokensData, estimatedGasLimit, gasPrice, oraclePriceCount, numberOfParts) {
|
|
@@ -16,7 +17,14 @@ tokensData, estimatedGasLimit, gasPrice, oraclePriceCount, numberOfParts) {
|
|
|
16
17
|
const multiplierFactor = gasLimits.estimatedFeeMultiplierFactor;
|
|
17
18
|
const gasLimit = baseGasLimit + applyFactor(estimatedGasLimit, multiplierFactor);
|
|
18
19
|
// #endregion
|
|
19
|
-
|
|
20
|
+
// avoid botanix gas spikes when chain is not actively used
|
|
21
|
+
const minGasCostUsd = MIN_EXECUTION_FEE_USD[chainId];
|
|
22
|
+
const minGasCost = convertToTokenAmount(minGasCostUsd, nativeToken.decimals, nativeToken.prices.minPrice);
|
|
23
|
+
let feeTokenAmountPerExecution = gasLimit * gasPrice;
|
|
24
|
+
if (minGasCost) {
|
|
25
|
+
feeTokenAmountPerExecution = bigMath.max(feeTokenAmountPerExecution, minGasCost);
|
|
26
|
+
}
|
|
27
|
+
const feeTokenAmount = feeTokenAmountPerExecution * BigInt(numberOfParts ?? 1);
|
|
20
28
|
const feeUsd = convertToUsd(feeTokenAmount, nativeToken.decimals, nativeToken.prices.minPrice);
|
|
21
29
|
const isFeeHigh = feeUsd > expandDecimals(getHighExecutionFee(chainId), USD_DECIMALS);
|
|
22
30
|
const isFeeVeryHigh = feeUsd > expandDecimals(getExcessiveExecutionFee(chainId), USD_DECIMALS);
|
|
@@ -127,7 +127,8 @@ export function formatPercentage(percentage, opts = {}) {
|
|
|
127
127
|
return `${sign}${formatAmount(bigMath.abs(percentage), bps ? 2 : PERCENT_PRECISION_DECIMALS, displayDecimals)}%`;
|
|
128
128
|
}
|
|
129
129
|
export function formatTokenAmount(amount, tokenDecimals, symbol, opts = {}) {
|
|
130
|
-
const {
|
|
130
|
+
const { showAllSignificant = false, fallbackToZero = false, useCommas = false, minThreshold = "0", maxThreshold, } = opts;
|
|
131
|
+
const displayDecimals = opts.displayDecimals ?? (opts.isStable ? 2 : 4);
|
|
131
132
|
const symbolStr = symbol ? ` ${symbol}` : "";
|
|
132
133
|
if (typeof amount !== "bigint" || !tokenDecimals) {
|
|
133
134
|
if (fallbackToZero) {
|
|
@@ -147,7 +148,7 @@ export function formatTokenAmount(amount, tokenDecimals, symbol, opts = {}) {
|
|
|
147
148
|
else {
|
|
148
149
|
const exceedingInfo = getLimitedDisplay(amount, tokenDecimals, { maxThreshold, minThreshold });
|
|
149
150
|
const symbol = exceedingInfo.symbol ? `${exceedingInfo.symbol} ` : "";
|
|
150
|
-
amountStr = `${symbol}${sign}${formatAmount(exceedingInfo.value, tokenDecimals, displayDecimals, useCommas)}`;
|
|
151
|
+
amountStr = `${symbol}${sign}${formatAmount(exceedingInfo.value, tokenDecimals, displayDecimals, useCommas, undefined)}`;
|
|
151
152
|
}
|
|
152
153
|
return `${amountStr}${symbolStr}`;
|
|
153
154
|
}
|
|
@@ -223,14 +224,20 @@ export function formatAmountHuman(amount, tokenDecimals, showDollar = false, dis
|
|
|
223
224
|
}
|
|
224
225
|
return `${isNegative ? "-" : ""}${sign}${absN.toFixed(displayDecimals)}`;
|
|
225
226
|
}
|
|
226
|
-
export function formatBalanceAmount(amount, tokenDecimals, tokenSymbol, showZero = false, toExponential = true) {
|
|
227
|
+
export function formatBalanceAmount(amount, tokenDecimals, tokenSymbol, { showZero = false, toExponential = true, isStable = false, } = {}) {
|
|
227
228
|
if (amount === undefined)
|
|
228
229
|
return "-";
|
|
229
230
|
if (amount === 0n) {
|
|
230
231
|
if (showZero === true) {
|
|
231
232
|
if (tokenSymbol) {
|
|
233
|
+
if (isStable) {
|
|
234
|
+
return `0.00 ${tokenSymbol}`;
|
|
235
|
+
}
|
|
232
236
|
return `0.0000 ${tokenSymbol}`;
|
|
233
237
|
}
|
|
238
|
+
if (isStable) {
|
|
239
|
+
return "0.00";
|
|
240
|
+
}
|
|
234
241
|
return "0.0000";
|
|
235
242
|
}
|
|
236
243
|
return "-";
|
|
@@ -238,14 +245,15 @@ export function formatBalanceAmount(amount, tokenDecimals, tokenSymbol, showZero
|
|
|
238
245
|
const absAmount = bigMath.abs(amount);
|
|
239
246
|
const absAmountFloat = bigintToNumber(absAmount, tokenDecimals);
|
|
240
247
|
let value = "";
|
|
248
|
+
const baseDecimals = isStable ? 2 : 4;
|
|
241
249
|
if (absAmountFloat >= 1)
|
|
242
|
-
value = formatAmount(amount, tokenDecimals,
|
|
250
|
+
value = formatAmount(amount, tokenDecimals, baseDecimals, true);
|
|
243
251
|
else if (absAmountFloat >= 0.1)
|
|
244
|
-
value = formatAmount(amount, tokenDecimals,
|
|
252
|
+
value = formatAmount(amount, tokenDecimals, baseDecimals + 1, true);
|
|
245
253
|
else if (absAmountFloat >= 0.01)
|
|
246
|
-
value = formatAmount(amount, tokenDecimals,
|
|
254
|
+
value = formatAmount(amount, tokenDecimals, baseDecimals + 2, true);
|
|
247
255
|
else if (absAmountFloat >= 0.001)
|
|
248
|
-
value = formatAmount(amount, tokenDecimals,
|
|
256
|
+
value = formatAmount(amount, tokenDecimals, baseDecimals + 3, true);
|
|
249
257
|
else if (absAmountFloat >= 1e-8)
|
|
250
258
|
value = formatAmount(amount, tokenDecimals, 8, true);
|
|
251
259
|
else {
|
|
@@ -262,15 +270,6 @@ export function formatBalanceAmount(amount, tokenDecimals, tokenSymbol, showZero
|
|
|
262
270
|
}
|
|
263
271
|
return value;
|
|
264
272
|
}
|
|
265
|
-
export function formatBalanceAmountWithUsd(amount, amountUsd, tokenDecimals, tokenSymbol, showZero = false) {
|
|
266
|
-
if (showZero === false && amount === 0n) {
|
|
267
|
-
return "-";
|
|
268
|
-
}
|
|
269
|
-
const value = formatBalanceAmount(amount, tokenDecimals, tokenSymbol, showZero);
|
|
270
|
-
const usd = formatUsd(amountUsd);
|
|
271
|
-
// Regular space
|
|
272
|
-
return `${value} (${usd})`;
|
|
273
|
-
}
|
|
274
273
|
export function formatFactor(factor) {
|
|
275
274
|
if (factor == 0n) {
|
|
276
275
|
return "0";
|
|
@@ -542,26 +541,46 @@ export function deserializeBigIntsInObject(obj) {
|
|
|
542
541
|
}
|
|
543
542
|
return result;
|
|
544
543
|
}
|
|
545
|
-
export function calculateDisplayDecimals(price, decimals = USD_DECIMALS, visualMultiplier = 1) {
|
|
544
|
+
export function calculateDisplayDecimals(price, decimals = USD_DECIMALS, visualMultiplier = 1, isStable = false) {
|
|
546
545
|
if (price === undefined || price === 0n)
|
|
547
546
|
return 2;
|
|
548
547
|
const priceNumber = bigintToNumber(bigMath.abs(price) * BigInt(visualMultiplier), decimals);
|
|
549
548
|
if (isNaN(priceNumber))
|
|
550
549
|
return 2;
|
|
551
|
-
if (
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
550
|
+
if (isStable) {
|
|
551
|
+
if (priceNumber >= 0.1)
|
|
552
|
+
return 2;
|
|
553
|
+
if (priceNumber >= 0.01)
|
|
554
|
+
return 3;
|
|
555
|
+
if (priceNumber >= 0.001)
|
|
556
|
+
return 4;
|
|
557
|
+
if (priceNumber >= 0.0001)
|
|
558
|
+
return 5;
|
|
559
|
+
if (priceNumber >= 0.00001)
|
|
560
|
+
return 6;
|
|
561
|
+
if (priceNumber >= 0.000001)
|
|
562
|
+
return 7;
|
|
563
|
+
if (priceNumber >= 0.0000001)
|
|
564
|
+
return 8;
|
|
565
|
+
if (priceNumber >= 0.00000001)
|
|
566
|
+
return 9;
|
|
567
|
+
}
|
|
568
|
+
else {
|
|
569
|
+
if (priceNumber >= 1000)
|
|
570
|
+
return 2;
|
|
571
|
+
if (priceNumber >= 100)
|
|
572
|
+
return 3;
|
|
573
|
+
if (priceNumber >= 1)
|
|
574
|
+
return 4;
|
|
575
|
+
if (priceNumber >= 0.1)
|
|
576
|
+
return 5;
|
|
577
|
+
if (priceNumber >= 0.01)
|
|
578
|
+
return 6;
|
|
579
|
+
if (priceNumber >= 0.0001)
|
|
580
|
+
return 7;
|
|
581
|
+
if (priceNumber >= 0.00001)
|
|
582
|
+
return 8;
|
|
583
|
+
}
|
|
565
584
|
return 9;
|
|
566
585
|
}
|
|
567
586
|
export function clamp(value, min, max) {
|
|
@@ -171,6 +171,9 @@ export function buildTwapOrdersPayloads(p, twapParams) {
|
|
|
171
171
|
referralCode: params.referralCode,
|
|
172
172
|
autoCancel: params.autoCancel,
|
|
173
173
|
allowedSlippage: 0,
|
|
174
|
+
...(params.expectedOutputAmount !== undefined && {
|
|
175
|
+
expectedOutputAmount: params.expectedOutputAmount / BigInt(twapParams.numberOfParts),
|
|
176
|
+
}),
|
|
174
177
|
payTokenAmount: params.payTokenAmount / BigInt(twapParams.numberOfParts),
|
|
175
178
|
executionFeeAmount: params.executionFeeAmount / BigInt(twapParams.numberOfParts),
|
|
176
179
|
validFromTime: getTwapValidFromTime(twapParams.duration, twapParams.numberOfParts, i),
|
|
@@ -158,7 +158,10 @@ export function getOrderInfo(p) {
|
|
|
158
158
|
shouldApplyPriceImpact: true,
|
|
159
159
|
isAtomicSwap: false,
|
|
160
160
|
});
|
|
161
|
-
|
|
161
|
+
let triggerThresholdType;
|
|
162
|
+
if (!isMarketOrderType(order.orderType)) {
|
|
163
|
+
triggerThresholdType = getOrderThresholdType(order.orderType, order.isLong);
|
|
164
|
+
}
|
|
162
165
|
const orderInfo = {
|
|
163
166
|
...order,
|
|
164
167
|
swapPathStats,
|
|
@@ -176,7 +179,10 @@ export function getOrderInfo(p) {
|
|
|
176
179
|
}
|
|
177
180
|
}
|
|
178
181
|
export function isVisibleOrder(orderType) {
|
|
179
|
-
return isLimitOrderType(orderType) ||
|
|
182
|
+
return (isLimitOrderType(orderType) ||
|
|
183
|
+
isTriggerDecreaseOrderType(orderType) ||
|
|
184
|
+
isLimitSwapOrderType(orderType) ||
|
|
185
|
+
isMarketOrderType(orderType));
|
|
180
186
|
}
|
|
181
187
|
export function isOrderForPosition(order, positionKey) {
|
|
182
188
|
const { account, marketAddress, collateralAddress, isLong } = parsePositionKey(positionKey);
|
|
@@ -33,7 +33,7 @@ export function getOrderThresholdType(orderType, isLong) {
|
|
|
33
33
|
if (orderType === OrderType.StopLossDecrease) {
|
|
34
34
|
return isLong ? TriggerThresholdType.Below : TriggerThresholdType.Above;
|
|
35
35
|
}
|
|
36
|
-
|
|
36
|
+
return undefined;
|
|
37
37
|
}
|
|
38
38
|
export function getAcceptablePriceInfo(p) {
|
|
39
39
|
const { marketInfo, isIncrease, isLong, indexPrice, sizeDeltaUsd, maxNegativePriceImpactBps } = p;
|
|
@@ -2,7 +2,7 @@ import { BASIS_POINTS_DIVISOR_BIGINT } from "../../configs/factors";
|
|
|
2
2
|
import { bigMath } from "../bigmath";
|
|
3
3
|
import { getTotalSwapVolumeFromSwapStats } from "../fees";
|
|
4
4
|
import { applyFactor } from "../numbers";
|
|
5
|
-
import { convertToTokenAmount, convertToUsd, getAmountByRatio, getIsEquivalentTokens } from "../tokens";
|
|
5
|
+
import { convertToTokenAmount, convertToUsd, getAmountByRatio, getIsEquivalentTokens, getIsStake, getIsUnstake, } from "../tokens";
|
|
6
6
|
export function getSwapAmountsByFromValue(p) {
|
|
7
7
|
const { tokenIn, tokenOut, amountIn, triggerRatio, isLimit, swapOptimizationOrder, findSwapPath, uiFeeFactor, allowedSwapSlippageBps, } = p;
|
|
8
8
|
const priceIn = tokenIn.prices.minPrice;
|
|
@@ -39,6 +39,9 @@ export function getSwapAmountsByFromValue(p) {
|
|
|
39
39
|
swapPathStats: undefined,
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
|
+
if (getIsStake(tokenIn, tokenOut) || getIsUnstake(tokenIn, tokenOut)) {
|
|
43
|
+
return getPlainSwapAmountsByFromToken(tokenIn, tokenOut, amountIn);
|
|
44
|
+
}
|
|
42
45
|
const swapPathStats = findSwapPath(defaultAmounts.usdIn, { order: swapOptimizationOrder });
|
|
43
46
|
const totalSwapVolume = getTotalSwapVolumeFromSwapStats(swapPathStats?.swapSteps);
|
|
44
47
|
const swapUiFeeUsd = applyFactor(totalSwapVolume, uiFeeFactor);
|
|
@@ -119,6 +122,9 @@ export function getSwapAmountsByToValue(p) {
|
|
|
119
122
|
swapPathStats: undefined,
|
|
120
123
|
};
|
|
121
124
|
}
|
|
125
|
+
if (getIsStake(tokenIn, tokenOut) || getIsUnstake(tokenIn, tokenOut)) {
|
|
126
|
+
return getPlainSwapAmountsByToToken(tokenIn, tokenOut, amountOut);
|
|
127
|
+
}
|
|
122
128
|
const baseUsdIn = usdOut;
|
|
123
129
|
const swapPathStats = findSwapPath(baseUsdIn, { order: swapOptimizationOrder });
|
|
124
130
|
if (!swapPathStats) {
|
|
@@ -182,3 +188,37 @@ export function getSwapPathComparator(order) {
|
|
|
182
188
|
return 0;
|
|
183
189
|
};
|
|
184
190
|
}
|
|
191
|
+
function getPlainSwapAmountsByFromToken(tokenIn, tokenOut, amountIn) {
|
|
192
|
+
const usdIn = convertToUsd(amountIn, tokenIn.decimals, tokenIn.prices.minPrice);
|
|
193
|
+
const usdOut = usdIn;
|
|
194
|
+
const amountOut = convertToTokenAmount(usdOut, tokenOut.decimals, tokenOut.prices.maxPrice);
|
|
195
|
+
const priceIn = tokenIn.prices.minPrice;
|
|
196
|
+
const priceOut = tokenOut.prices.maxPrice;
|
|
197
|
+
return {
|
|
198
|
+
amountIn,
|
|
199
|
+
usdIn,
|
|
200
|
+
amountOut,
|
|
201
|
+
usdOut,
|
|
202
|
+
minOutputAmount: amountOut,
|
|
203
|
+
priceIn,
|
|
204
|
+
priceOut,
|
|
205
|
+
swapPathStats: undefined,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
function getPlainSwapAmountsByToToken(tokenIn, tokenOut, amountOut) {
|
|
209
|
+
const priceIn = tokenIn.prices.minPrice;
|
|
210
|
+
const priceOut = tokenOut.prices.maxPrice;
|
|
211
|
+
const usdOut = convertToUsd(amountOut, tokenOut.decimals, priceOut);
|
|
212
|
+
const usdIn = usdOut;
|
|
213
|
+
const amountIn = convertToTokenAmount(usdIn, tokenIn.decimals, priceIn);
|
|
214
|
+
return {
|
|
215
|
+
amountIn,
|
|
216
|
+
usdIn,
|
|
217
|
+
amountOut,
|
|
218
|
+
usdOut,
|
|
219
|
+
minOutputAmount: amountOut,
|
|
220
|
+
priceIn,
|
|
221
|
+
priceOut,
|
|
222
|
+
swapPathStats: undefined,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
@@ -132,6 +132,13 @@ export function getIsWrap(token1, token2) {
|
|
|
132
132
|
export function getIsUnwrap(token1, token2) {
|
|
133
133
|
return token1.isWrapped && token2.isNative;
|
|
134
134
|
}
|
|
135
|
+
export function getIsStake(token1, token2) {
|
|
136
|
+
return (token1.isWrapped || token1.isNative) && token2.isStaking;
|
|
137
|
+
}
|
|
138
|
+
export function getIsUnstake(token1, token2) {
|
|
139
|
+
// can't unstake straight to native token
|
|
140
|
+
return token1.isStaking && token2.isWrapped;
|
|
141
|
+
}
|
|
135
142
|
export function getTokensRatioByPrice(p) {
|
|
136
143
|
const { fromToken, toToken, fromPrice, toPrice } = p;
|
|
137
144
|
const [largestToken, smallestToken, largestPrice, smallestPrice] = fromPrice > toPrice ? [fromToken, toToken, fromPrice, toPrice] : [toToken, fromToken, toPrice, fromPrice];
|
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import { isAddress } from "viem";
|
|
2
2
|
const VERSION = "01";
|
|
3
|
-
const PREFIX = "
|
|
3
|
+
const PREFIX = "0xff0000";
|
|
4
|
+
/**
|
|
5
|
+
* Ui fee receiver structure:
|
|
6
|
+
* 0..3 bytes (0..7 chars) - PREFIX
|
|
7
|
+
* 4..16 bytes (8..27 chars) - 12 bytes buffer
|
|
8
|
+
* 17 byte (27..29 chars) - isExpress flag
|
|
9
|
+
* 18 byte (30..32 chars) - numberOfParts (hex encoded)
|
|
10
|
+
* 19..20 bytes (34..38 chars) - twapId
|
|
11
|
+
* 20 byte (38..40 chars) - VERSION
|
|
12
|
+
*
|
|
13
|
+
* Total: 20 bytes (40 hex characters)
|
|
14
|
+
*/
|
|
4
15
|
export function generateTwapId() {
|
|
5
16
|
return Math.floor(Math.random() * 256 * 256)
|
|
6
17
|
.toString(16)
|
|
@@ -9,16 +20,24 @@ export function generateTwapId() {
|
|
|
9
20
|
export function createTwapUiFeeReceiver({ numberOfParts }) {
|
|
10
21
|
const twapId = generateTwapId();
|
|
11
22
|
const numberOfPartsInHex = numberOfParts.toString(16).padStart(2, "0");
|
|
12
|
-
|
|
23
|
+
const isExpressHex = "00";
|
|
24
|
+
const buffer = "00".repeat(12);
|
|
25
|
+
return `${PREFIX}${buffer}${isExpressHex}${numberOfPartsInHex}${twapId}${VERSION}`;
|
|
13
26
|
}
|
|
14
27
|
export function decodeTwapUiFeeReceiver(address) {
|
|
15
|
-
const
|
|
28
|
+
const byteString = address.slice(2);
|
|
29
|
+
const twapId = byteString.slice(34, 38);
|
|
30
|
+
const isExpress = byteString.slice(30, 32) === "01";
|
|
16
31
|
if (!isValidTwapUiFeeReceiver(address) || twapId === "0000") {
|
|
17
32
|
return;
|
|
18
33
|
}
|
|
19
|
-
const numberOfParts = parseInt(
|
|
20
|
-
return { twapId, numberOfParts };
|
|
34
|
+
const numberOfParts = parseInt(byteString.slice(32, 34), 16);
|
|
35
|
+
return { twapId, numberOfParts, isExpress };
|
|
21
36
|
}
|
|
22
37
|
export function isValidTwapUiFeeReceiver(address) {
|
|
23
38
|
return isAddress(address) && address.toLowerCase().startsWith(PREFIX.toLowerCase());
|
|
24
39
|
}
|
|
40
|
+
export function setUiFeeReceiverIsExpress(uiFeeReceiver, isExpress) {
|
|
41
|
+
const isExpressInHex = isExpress ? "01" : "00";
|
|
42
|
+
return `${uiFeeReceiver.slice(0, 27)}${isExpressInHex}${uiFeeReceiver.slice(29)}`;
|
|
43
|
+
}
|
|
@@ -2,52 +2,35 @@ import { Chain } from "viem/chains";
|
|
|
2
2
|
export declare const AVALANCHE = 43114;
|
|
3
3
|
export declare const AVALANCHE_FUJI = 43113;
|
|
4
4
|
export declare const ARBITRUM = 42161;
|
|
5
|
-
export declare const BSС_MAINNET = 56;
|
|
6
|
-
export declare const BSС_TESTNET = 97;
|
|
7
5
|
export declare const ETH_MAINNET = 1;
|
|
8
|
-
export declare const
|
|
9
|
-
export declare const
|
|
10
|
-
export declare const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
43113: string;
|
|
16
|
-
};
|
|
17
|
-
export declare const HIGH_EXECUTION_FEES_MAP: Record<number, number>;
|
|
6
|
+
export declare const BOTANIX = 3637;
|
|
7
|
+
export declare const SUPPORTED_CHAIN_IDS: UiSupportedChain[];
|
|
8
|
+
export declare const SUPPORTED_CHAIN_IDS_DEV: UiSupportedChain[];
|
|
9
|
+
export type UiContractsChain = typeof ARBITRUM | typeof AVALANCHE | typeof AVALANCHE_FUJI | typeof BOTANIX;
|
|
10
|
+
export type UiSupportedChain = UiContractsChain;
|
|
11
|
+
export declare const CHAIN_NAMES_MAP: Record<UiContractsChain, string>;
|
|
12
|
+
export declare const HIGH_EXECUTION_FEES_MAP: Record<UiContractsChain, number>;
|
|
18
13
|
export declare const MAX_FEE_PER_GAS_MAP: Record<number, bigint>;
|
|
19
14
|
export declare const GAS_PRICE_PREMIUM_MAP: Record<number, bigint>;
|
|
20
|
-
export declare const MAX_PRIORITY_FEE_PER_GAS_MAP: Record<
|
|
21
|
-
export declare const EXCESSIVE_EXECUTION_FEES_MAP: Record<
|
|
15
|
+
export declare const MAX_PRIORITY_FEE_PER_GAS_MAP: Record<UiSupportedChain, bigint | undefined>;
|
|
16
|
+
export declare const EXCESSIVE_EXECUTION_FEES_MAP: Record<UiSupportedChain, number>;
|
|
17
|
+
export declare const MIN_EXECUTION_FEE_USD: Record<UiSupportedChain, bigint | undefined>;
|
|
22
18
|
export declare const GAS_PRICE_BUFFER_MAP: Record<number, bigint>;
|
|
19
|
+
export declare const botanix: Chain;
|
|
23
20
|
export declare function getChainName(chainId: number): any;
|
|
24
21
|
export declare const getViemChain: (chainId: number) => Chain;
|
|
25
|
-
export declare function getHighExecutionFee(chainId: number):
|
|
26
|
-
export declare function getExcessiveExecutionFee(chainId: number):
|
|
22
|
+
export declare function getHighExecutionFee(chainId: number): any;
|
|
23
|
+
export declare function getExcessiveExecutionFee(chainId: number): any;
|
|
27
24
|
export declare function isSupportedChain(chainId: number, dev?: boolean): boolean;
|
|
28
25
|
export declare const EXECUTION_FEE_CONFIG_V2: {
|
|
29
|
-
[chainId
|
|
26
|
+
[chainId in UiSupportedChain]: {
|
|
30
27
|
shouldUseMaxPriorityFeePerGas: boolean;
|
|
31
28
|
defaultBufferBps?: number;
|
|
32
29
|
};
|
|
33
30
|
};
|
|
34
|
-
export declare const GAS_LIMITS_STATIC_CONFIG: {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
};
|
|
41
|
-
43114: {
|
|
42
|
-
createOrderGasLimit: bigint;
|
|
43
|
-
updateOrderGasLimit: bigint;
|
|
44
|
-
cancelOrderGasLimit: bigint;
|
|
45
|
-
tokenPermitGasLimit: bigint;
|
|
46
|
-
};
|
|
47
|
-
43113: {
|
|
48
|
-
createOrderGasLimit: bigint;
|
|
49
|
-
updateOrderGasLimit: bigint;
|
|
50
|
-
cancelOrderGasLimit: bigint;
|
|
51
|
-
tokenPermitGasLimit: bigint;
|
|
52
|
-
};
|
|
53
|
-
};
|
|
31
|
+
export declare const GAS_LIMITS_STATIC_CONFIG: Record<UiSupportedChain, {
|
|
32
|
+
createOrderGasLimit: bigint;
|
|
33
|
+
updateOrderGasLimit: bigint;
|
|
34
|
+
cancelOrderGasLimit: bigint;
|
|
35
|
+
tokenPermitGasLimit: bigint;
|
|
36
|
+
}>;
|
|
@@ -1,68 +1,5 @@
|
|
|
1
1
|
import { type Address } from "viem";
|
|
2
2
|
export declare const CONTRACTS: {
|
|
3
|
-
56: {
|
|
4
|
-
Treasury: string;
|
|
5
|
-
BUSD: string;
|
|
6
|
-
GMT: string;
|
|
7
|
-
Vault: string;
|
|
8
|
-
Router: string;
|
|
9
|
-
Reader: string;
|
|
10
|
-
AmmFactory: string;
|
|
11
|
-
AmmFactoryV2: string;
|
|
12
|
-
OrderBook: string;
|
|
13
|
-
OrderBookReader: string;
|
|
14
|
-
GmxMigrator: string;
|
|
15
|
-
USDG: string;
|
|
16
|
-
NATIVE_TOKEN: string;
|
|
17
|
-
XGMT: string;
|
|
18
|
-
GMT_USDG_PAIR: string;
|
|
19
|
-
XGMT_USDG_PAIR: string;
|
|
20
|
-
GMT_USDG_FARM: string;
|
|
21
|
-
XGMT_USDG_FARM: string;
|
|
22
|
-
USDG_YIELD_TRACKER: string;
|
|
23
|
-
XGMT_YIELD_TRACKER: string;
|
|
24
|
-
GMT_USDG_FARM_TRACKER_XGMT: string;
|
|
25
|
-
GMT_USDG_FARM_TRACKER_NATIVE: string;
|
|
26
|
-
XGMT_USDG_FARM_TRACKER_XGMT: string;
|
|
27
|
-
XGMT_USDG_FARM_TRACKER_NATIVE: string;
|
|
28
|
-
AUTO: string;
|
|
29
|
-
AUTO_USDG_PAIR: string;
|
|
30
|
-
AUTO_USDG_FARM: string;
|
|
31
|
-
AUTO_USDG_FARM_TRACKER_XGMT: string;
|
|
32
|
-
AUTO_USDG_FARM_TRACKER_NATIVE: string;
|
|
33
|
-
GMT_GMX_IOU: string;
|
|
34
|
-
XGMT_GMX_IOU: string;
|
|
35
|
-
GMT_USDG_GMX_IOU: string;
|
|
36
|
-
XGMT_USDG_GMX_IOU: string;
|
|
37
|
-
};
|
|
38
|
-
97: {
|
|
39
|
-
Vault: string;
|
|
40
|
-
Router: string;
|
|
41
|
-
Reader: string;
|
|
42
|
-
AmmFactory: string;
|
|
43
|
-
AmmFactoryV2: string;
|
|
44
|
-
OrderBook: string;
|
|
45
|
-
OrderBookReader: string;
|
|
46
|
-
GmxMigrator: string;
|
|
47
|
-
USDG: string;
|
|
48
|
-
GMT: string;
|
|
49
|
-
NATIVE_TOKEN: string;
|
|
50
|
-
XGMT: string;
|
|
51
|
-
GMT_USDG_PAIR: string;
|
|
52
|
-
XGMT_USDG_PAIR: string;
|
|
53
|
-
GMT_USDG_FARM: string;
|
|
54
|
-
XGMT_USDG_FARM: string;
|
|
55
|
-
USDG_YIELD_TRACKER: string;
|
|
56
|
-
XGMT_YIELD_TRACKER: string;
|
|
57
|
-
GMT_USDG_FARM_TRACKER_XGMT: string;
|
|
58
|
-
GMT_USDG_FARM_TRACKER_NATIVE: string;
|
|
59
|
-
XGMT_USDG_FARM_TRACKER_XGMT: string;
|
|
60
|
-
XGMT_USDG_FARM_TRACKER_NATIVE: string;
|
|
61
|
-
GMT_GMX_IOU: string;
|
|
62
|
-
XGMT_GMX_IOU: string;
|
|
63
|
-
GMT_USDG_GMX_IOU: string;
|
|
64
|
-
XGMT_USDG_GMX_IOU: string;
|
|
65
|
-
};
|
|
66
3
|
42161: {
|
|
67
4
|
Vault: string;
|
|
68
5
|
Router: string;
|
|
@@ -179,6 +116,66 @@ export declare const CONTRACTS: {
|
|
|
179
116
|
Multicall: string;
|
|
180
117
|
ArbitrumNodeInterface: "0x0000000000000000000000000000000000000000";
|
|
181
118
|
};
|
|
119
|
+
3637: {
|
|
120
|
+
DataStore: string;
|
|
121
|
+
EventEmitter: string;
|
|
122
|
+
SubaccountRouter: string;
|
|
123
|
+
ExchangeRouter: string;
|
|
124
|
+
DepositVault: string;
|
|
125
|
+
WithdrawalVault: string;
|
|
126
|
+
OrderVault: string;
|
|
127
|
+
ShiftVault: string;
|
|
128
|
+
SyntheticsReader: string;
|
|
129
|
+
SyntheticsRouter: string;
|
|
130
|
+
GlvReader: string;
|
|
131
|
+
GlvRouter: string;
|
|
132
|
+
GlvVault: string;
|
|
133
|
+
GelatoRelayRouter: string;
|
|
134
|
+
SubaccountGelatoRelayRouter: string;
|
|
135
|
+
ExternalHandler: string;
|
|
136
|
+
OpenOceanRouter: "0x0000000000000000000000000000000000000000";
|
|
137
|
+
ChainlinkPriceFeedProvider: string;
|
|
138
|
+
Multicall: string;
|
|
139
|
+
Timelock: string;
|
|
140
|
+
ArbitrumNodeInterface: "0x0000000000000000000000000000000000000000";
|
|
141
|
+
Vault: "0x0000000000000000000000000000000000000000";
|
|
142
|
+
Reader: "0x0000000000000000000000000000000000000000";
|
|
143
|
+
PositionRouter: "0x0000000000000000000000000000000000000000";
|
|
144
|
+
ReferralStorage: "0x0000000000000000000000000000000000000000";
|
|
145
|
+
ReferralReader: "0x0000000000000000000000000000000000000000";
|
|
146
|
+
VaultReader: "0x0000000000000000000000000000000000000000";
|
|
147
|
+
GlpManager: "0x0000000000000000000000000000000000000000";
|
|
148
|
+
RewardRouter: "0x0000000000000000000000000000000000000000";
|
|
149
|
+
RewardReader: "0x0000000000000000000000000000000000000000";
|
|
150
|
+
GlpRewardRouter: "0x0000000000000000000000000000000000000000";
|
|
151
|
+
StakedGmxTracker: "0x0000000000000000000000000000000000000000";
|
|
152
|
+
FeeGmxTracker: "0x0000000000000000000000000000000000000000";
|
|
153
|
+
GLP: "0x0000000000000000000000000000000000000000";
|
|
154
|
+
GMX: "0x0000000000000000000000000000000000000000";
|
|
155
|
+
ES_GMX: "0x0000000000000000000000000000000000000000";
|
|
156
|
+
BN_GMX: "0x0000000000000000000000000000000000000000";
|
|
157
|
+
USDG: "0x0000000000000000000000000000000000000000";
|
|
158
|
+
BonusGmxTracker: "0x0000000000000000000000000000000000000000";
|
|
159
|
+
StakedGlpTracker: "0x0000000000000000000000000000000000000000";
|
|
160
|
+
FeeGlpTracker: "0x0000000000000000000000000000000000000000";
|
|
161
|
+
ExtendedGmxTracker: "0x0000000000000000000000000000000000000000";
|
|
162
|
+
StakedGmxDistributor: "0x0000000000000000000000000000000000000000";
|
|
163
|
+
StakedGlpDistributor: "0x0000000000000000000000000000000000000000";
|
|
164
|
+
GmxVester: "0x0000000000000000000000000000000000000000";
|
|
165
|
+
GlpVester: "0x0000000000000000000000000000000000000000";
|
|
166
|
+
AffiliateVester: "0x0000000000000000000000000000000000000000";
|
|
167
|
+
Router: "0x0000000000000000000000000000000000000000";
|
|
168
|
+
GovToken: "0x0000000000000000000000000000000000000000";
|
|
169
|
+
ES_GMX_IOU: "0x0000000000000000000000000000000000000000";
|
|
170
|
+
OrderBook: "0x0000000000000000000000000000000000000000";
|
|
171
|
+
OrderExecutor: "0x0000000000000000000000000000000000000000";
|
|
172
|
+
OrderBookReader: "0x0000000000000000000000000000000000000000";
|
|
173
|
+
PositionManager: "0x0000000000000000000000000000000000000000";
|
|
174
|
+
UniswapGmxEthPool: "0x0000000000000000000000000000000000000000";
|
|
175
|
+
NATIVE_TOKEN: string;
|
|
176
|
+
StBTC: string;
|
|
177
|
+
PBTC: string;
|
|
178
|
+
};
|
|
182
179
|
43113: {
|
|
183
180
|
Vault: "0x0000000000000000000000000000000000000000";
|
|
184
181
|
Router: "0x0000000000000000000000000000000000000000";
|
|
@@ -92,6 +92,7 @@ export declare const SUBACCOUNT_EXPIRES_AT: string;
|
|
|
92
92
|
export declare const PRICE_FEED_KEY: string;
|
|
93
93
|
export declare const GASLESS_FEATURE_DISABLED_KEY: string;
|
|
94
94
|
export declare const GELATO_RELAY_FEE_MULTIPLIER_FACTOR_KEY: string;
|
|
95
|
+
export declare const REQUEST_EXPIRATION_TIME_KEY: string;
|
|
95
96
|
export declare const GMX_SIMULATION_ORIGIN: string;
|
|
96
97
|
export declare function subaccountExpiresAtKey(account: string, subaccount: string, actionType: string): string;
|
|
97
98
|
export declare function glvShiftLastExecutedAtKey(glvAddress: string): string;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { UiContractsChain } from "./chains";
|
|
1
2
|
export declare const SWAP_GRAPH_MAX_MARKETS_PER_TOKEN = 5;
|
|
2
3
|
export type MarketConfig = {
|
|
3
4
|
marketTokenAddress: string;
|
|
@@ -5,4 +6,4 @@ export type MarketConfig = {
|
|
|
5
6
|
longTokenAddress: string;
|
|
6
7
|
shortTokenAddress: string;
|
|
7
8
|
};
|
|
8
|
-
export declare const MARKETS: Record<
|
|
9
|
+
export declare const MARKETS: Record<UiContractsChain, Record<string, MarketConfig>>;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare function getOracleKeeperUrl(chainId: number, index: number):
|
|
1
|
+
export declare function getOracleKeeperUrl(chainId: number, index: number): any;
|
|
2
2
|
export declare function getOracleKeeperNextIndex(chainId: number, currentIndex: number): number;
|
|
3
3
|
export declare function getOracleKeeperRandomIndex(chainId: number, bannedIndexes?: number[]): number;
|
|
@@ -26,7 +26,7 @@ export declare class GmxSdk {
|
|
|
26
26
|
setAccount(account: Address): void;
|
|
27
27
|
executeMulticall<T = any>(request: MulticallRequestConfig<any>): Promise<T>;
|
|
28
28
|
callContract(address: Address, abi: Abi, method: string, params: any[], opts?: CallContractOpts): Promise<`0x${string}`>;
|
|
29
|
-
get chainId():
|
|
29
|
+
get chainId(): import("./configs/chains").UiContractsChain;
|
|
30
30
|
get chain(): import("viem").Chain;
|
|
31
31
|
get account(): `0x${string}`;
|
|
32
32
|
}
|