@gmx-io/sdk 1.2.0 → 1.2.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/src/abis/ClaimHandler.json +439 -0
- package/build/cjs/src/abis/index.js +4 -0
- package/build/cjs/src/configs/__tests__/markets.spec.js +1 -1
- package/build/cjs/src/configs/__tests__/tokens.spec.js +1 -1
- package/build/cjs/src/configs/batch.js +12 -0
- package/build/cjs/src/configs/contracts.js +2 -0
- package/build/cjs/src/configs/dataStore.js +13 -3
- package/build/cjs/src/configs/express.js +2 -1
- package/build/cjs/src/configs/markets.js +35 -0
- package/build/cjs/src/configs/oracleKeeper.js +19 -35
- package/build/cjs/src/configs/tokens.js +55 -0
- package/build/cjs/src/index.js +3 -3
- package/build/cjs/src/modules/orders/helpers.js +9 -0
- package/build/cjs/src/modules/orders/helpers.spec.js +1 -1
- package/build/cjs/src/modules/orders/orders.js +3 -3
- package/build/cjs/src/modules/utils/utils.js +3 -3
- package/build/cjs/src/prebuilt/hashedKinkModelMarketRatesKeys.json +40 -0
- package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +255 -0
- package/build/cjs/src/prebuilt/hashedMarketValuesKeys.json +75 -0
- package/build/cjs/src/types/subsquid.js +91 -1
- package/build/cjs/src/types/swapStrategy.js +2 -0
- package/build/cjs/src/types/trade.js +1 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +2 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +2 -0
- package/build/cjs/src/utils/__tests__/uiFeeReceiver.spec.js +10 -0
- package/build/cjs/src/utils/errors/transactionsErrors.js +7 -1
- package/build/cjs/src/utils/orderTransactions.js +0 -1
- package/build/cjs/src/utils/swap/botanixStaking.js +90 -0
- package/build/cjs/src/utils/swap/buildSwapStrategy.js +224 -0
- package/build/cjs/src/utils/swap/externalSwapPath.js +41 -0
- package/build/cjs/src/utils/swap/externalSwapQuoteByPath.js +27 -0
- package/build/cjs/src/utils/swap/swapValues.js +250 -11
- package/build/cjs/src/utils/trade/amounts.js +89 -41
- package/build/cjs/src/utils/trade/trade.js +2 -2
- package/build/cjs/src/utils/twap/uiFeeReceiver.js +9 -9
- package/build/esm/src/abis/ClaimHandler.json +439 -0
- package/build/esm/src/abis/index.js +4 -0
- package/build/esm/src/configs/__tests__/markets.spec.js +1 -1
- package/build/esm/src/configs/__tests__/tokens.spec.js +1 -1
- package/build/esm/src/configs/batch.js +13 -1
- package/build/esm/src/configs/contracts.js +2 -0
- package/build/esm/src/configs/dataStore.js +8 -0
- package/build/esm/src/configs/express.js +1 -0
- package/build/esm/src/configs/markets.js +35 -0
- package/build/esm/src/configs/oracleKeeper.js +17 -29
- package/build/esm/src/configs/tokens.js +55 -0
- package/build/esm/src/index.js +3 -3
- package/build/esm/src/modules/orders/helpers.js +9 -0
- package/build/esm/src/modules/orders/helpers.spec.js +1 -1
- package/build/esm/src/modules/orders/orders.js +3 -3
- package/build/esm/src/modules/utils/utils.js +3 -3
- package/build/esm/src/prebuilt/hashedKinkModelMarketRatesKeys.json +40 -0
- package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +255 -0
- package/build/esm/src/prebuilt/hashedMarketValuesKeys.json +75 -0
- package/build/esm/src/types/subsquid.js +90 -0
- package/build/esm/src/types/swapStrategy.js +1 -0
- package/build/esm/src/types/trade.js +1 -0
- package/build/esm/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +2 -0
- package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +2 -0
- package/build/esm/src/utils/__tests__/uiFeeReceiver.spec.js +11 -1
- package/build/esm/src/utils/errors/transactionsErrors.js +7 -1
- package/build/esm/src/utils/orderTransactions.js +0 -1
- package/build/esm/src/utils/swap/botanixStaking.js +83 -0
- package/build/esm/src/utils/swap/buildSwapStrategy.js +219 -0
- package/build/esm/src/utils/swap/externalSwapPath.js +37 -0
- package/build/esm/src/utils/swap/externalSwapQuoteByPath.js +23 -0
- package/build/esm/src/utils/swap/swapValues.js +248 -9
- package/build/esm/src/utils/trade/amounts.js +89 -41
- package/build/esm/src/utils/trade/trade.js +2 -2
- package/build/esm/src/utils/twap/uiFeeReceiver.js +9 -9
- package/build/types/src/abis/index.d.ts +1 -1
- package/build/types/src/configs/batch.d.ts +2 -1
- package/build/types/src/configs/contracts.d.ts +2 -0
- package/build/types/src/configs/dataStore.d.ts +4 -0
- package/build/types/src/configs/express.d.ts +1 -0
- package/build/types/src/configs/oracleKeeper.d.ts +2 -3
- package/build/types/src/types/subsquid.d.ts +303 -0
- package/build/types/src/types/swapStrategy.d.ts +33 -0
- package/build/types/src/types/tokens.d.ts +5 -0
- package/build/types/src/types/trade.d.ts +18 -4
- package/build/types/src/utils/swap/botanixStaking.d.ts +11 -0
- package/build/types/src/utils/swap/buildSwapStrategy.d.ts +22 -0
- package/build/types/src/utils/swap/externalSwapPath.d.ts +13 -0
- package/build/types/src/utils/swap/externalSwapQuoteByPath.d.ts +6 -0
- package/build/types/src/utils/swap/swapValues.d.ts +10 -3
- package/build/types/src/utils/trade/amounts.d.ts +5 -2
- package/build/types/src/utils/twap/uiFeeReceiver.d.ts +7 -7
- package/package.json +1 -1
|
@@ -3,7 +3,150 @@ import { bigMath } from "../bigmath";
|
|
|
3
3
|
import { getTotalSwapVolumeFromSwapStats } from "../fees";
|
|
4
4
|
import { applyFactor } from "../numbers";
|
|
5
5
|
import { convertToTokenAmount, convertToUsd, getAmountByRatio, getIsEquivalentTokens, getIsStake, getIsUnstake, } from "../tokens";
|
|
6
|
+
import { buildReverseSwapStrategy, buildSwapStrategy } from "./buildSwapStrategy";
|
|
6
7
|
export function getSwapAmountsByFromValue(p) {
|
|
8
|
+
const { tokenIn, tokenOut, amountIn, triggerRatio, isLimit, swapOptimizationOrder, uiFeeFactor, allowedSwapSlippageBps, marketsInfoData, chainId, externalSwapQuoteParams, } = p;
|
|
9
|
+
if (!externalSwapQuoteParams) {
|
|
10
|
+
return getSwapAmountsByFromValueDefault(p);
|
|
11
|
+
}
|
|
12
|
+
const swapStrategy = buildSwapStrategy({
|
|
13
|
+
amountIn,
|
|
14
|
+
tokenIn,
|
|
15
|
+
tokenOut,
|
|
16
|
+
marketsInfoData,
|
|
17
|
+
chainId,
|
|
18
|
+
swapOptimizationOrder,
|
|
19
|
+
externalSwapQuoteParams,
|
|
20
|
+
});
|
|
21
|
+
const swapPathStats = swapStrategy.swapPathStats;
|
|
22
|
+
const totalSwapVolume = getTotalSwapVolumeFromSwapStats(swapPathStats?.swapSteps);
|
|
23
|
+
const swapUiFeeUsd = applyFactor(totalSwapVolume, uiFeeFactor);
|
|
24
|
+
const swapUiFeeAmount = convertToTokenAmount(swapUiFeeUsd, tokenOut.decimals, swapStrategy.priceOut);
|
|
25
|
+
const defaultAmounts = {
|
|
26
|
+
amountIn,
|
|
27
|
+
usdIn: swapStrategy.usdIn,
|
|
28
|
+
amountOut: swapStrategy.amountOut,
|
|
29
|
+
usdOut: swapStrategy.usdOut,
|
|
30
|
+
minOutputAmount: swapStrategy.amountOut,
|
|
31
|
+
priceIn: swapStrategy.priceIn,
|
|
32
|
+
priceOut: swapStrategy.priceOut,
|
|
33
|
+
swapStrategy,
|
|
34
|
+
};
|
|
35
|
+
let amountOut = swapStrategy.amountOut;
|
|
36
|
+
let usdOut = swapStrategy.usdOut;
|
|
37
|
+
let minOutputAmount = 0n;
|
|
38
|
+
if (isLimit) {
|
|
39
|
+
if (!triggerRatio) {
|
|
40
|
+
return defaultAmounts;
|
|
41
|
+
}
|
|
42
|
+
amountOut = getAmountByRatio({
|
|
43
|
+
fromToken: tokenIn,
|
|
44
|
+
toToken: tokenOut,
|
|
45
|
+
fromTokenAmount: amountIn,
|
|
46
|
+
ratio: triggerRatio.ratio,
|
|
47
|
+
shouldInvertRatio: triggerRatio.largestToken.address === tokenOut.address,
|
|
48
|
+
allowedSwapSlippageBps,
|
|
49
|
+
});
|
|
50
|
+
usdOut = convertToUsd(amountOut, tokenOut.decimals, swapStrategy.priceOut);
|
|
51
|
+
amountOut = convertToTokenAmount(usdOut, tokenOut.decimals, swapStrategy.priceOut);
|
|
52
|
+
minOutputAmount = amountOut;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
usdOut = swapStrategy.usdOut - swapUiFeeUsd;
|
|
56
|
+
amountOut = swapStrategy.amountOut - swapUiFeeAmount;
|
|
57
|
+
minOutputAmount = amountOut;
|
|
58
|
+
}
|
|
59
|
+
if (amountOut < 0) {
|
|
60
|
+
amountOut = 0n;
|
|
61
|
+
usdOut = 0n;
|
|
62
|
+
minOutputAmount = 0n;
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
amountIn,
|
|
66
|
+
usdIn: swapStrategy.usdIn,
|
|
67
|
+
amountOut,
|
|
68
|
+
usdOut,
|
|
69
|
+
priceIn: swapStrategy.priceIn,
|
|
70
|
+
priceOut: swapStrategy.priceOut,
|
|
71
|
+
minOutputAmount,
|
|
72
|
+
swapStrategy,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export function getSwapAmountsByToValue(p) {
|
|
76
|
+
const { tokenIn, tokenOut, amountOut, triggerRatio, isLimit, uiFeeFactor, swapOptimizationOrder, allowedSwapSlippageBps, marketsInfoData, chainId, externalSwapQuoteParams, } = p;
|
|
77
|
+
if (!externalSwapQuoteParams) {
|
|
78
|
+
return getSwapAmountsByToValueDefault(p);
|
|
79
|
+
}
|
|
80
|
+
const swapStrategy = buildReverseSwapStrategy({
|
|
81
|
+
amountOut,
|
|
82
|
+
tokenIn,
|
|
83
|
+
tokenOut,
|
|
84
|
+
marketsInfoData,
|
|
85
|
+
chainId,
|
|
86
|
+
externalSwapQuoteParams,
|
|
87
|
+
swapOptimizationOrder,
|
|
88
|
+
});
|
|
89
|
+
const uiFeeUsd = applyFactor(swapStrategy.usdIn, uiFeeFactor);
|
|
90
|
+
const defaultAmounts = {
|
|
91
|
+
amountIn: swapStrategy.amountIn,
|
|
92
|
+
usdIn: swapStrategy.usdIn,
|
|
93
|
+
amountOut: swapStrategy.amountOut,
|
|
94
|
+
usdOut: swapStrategy.usdOut,
|
|
95
|
+
minOutputAmount: swapStrategy.amountOut,
|
|
96
|
+
priceIn: swapStrategy.priceIn,
|
|
97
|
+
priceOut: swapStrategy.priceOut,
|
|
98
|
+
swapStrategy,
|
|
99
|
+
};
|
|
100
|
+
if (!swapStrategy.swapPathStats) {
|
|
101
|
+
return defaultAmounts;
|
|
102
|
+
}
|
|
103
|
+
let amountIn = swapStrategy.amountIn;
|
|
104
|
+
let usdIn = swapStrategy.usdIn;
|
|
105
|
+
let minOutputAmount = 0n;
|
|
106
|
+
if (isLimit) {
|
|
107
|
+
if (!triggerRatio) {
|
|
108
|
+
return defaultAmounts;
|
|
109
|
+
}
|
|
110
|
+
amountIn = getAmountByRatio({
|
|
111
|
+
fromToken: tokenOut,
|
|
112
|
+
toToken: tokenIn,
|
|
113
|
+
fromTokenAmount: amountOut,
|
|
114
|
+
ratio: triggerRatio.ratio,
|
|
115
|
+
shouldInvertRatio: triggerRatio.largestToken.address === tokenIn.address,
|
|
116
|
+
});
|
|
117
|
+
usdIn = convertToUsd(amountIn, tokenIn.decimals, swapStrategy.priceIn);
|
|
118
|
+
if (allowedSwapSlippageBps !== undefined) {
|
|
119
|
+
usdIn += bigMath.mulDiv(usdIn, allowedSwapSlippageBps ?? 0n, BASIS_POINTS_DIVISOR_BIGINT);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
usdIn =
|
|
123
|
+
usdIn +
|
|
124
|
+
swapStrategy.swapPathStats.totalSwapFeeUsd +
|
|
125
|
+
uiFeeUsd -
|
|
126
|
+
swapStrategy.swapPathStats.totalSwapPriceImpactDeltaUsd;
|
|
127
|
+
}
|
|
128
|
+
amountIn = convertToTokenAmount(usdIn, tokenIn.decimals, swapStrategy.priceIn);
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
usdIn = swapStrategy.usdIn + uiFeeUsd;
|
|
132
|
+
amountIn = convertToTokenAmount(usdIn, tokenIn.decimals, swapStrategy.priceIn);
|
|
133
|
+
}
|
|
134
|
+
if (amountIn < 0) {
|
|
135
|
+
amountIn = 0n;
|
|
136
|
+
usdIn = 0n;
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
amountIn: swapStrategy.amountIn,
|
|
140
|
+
amountOut: swapStrategy.amountOut,
|
|
141
|
+
usdIn,
|
|
142
|
+
minOutputAmount,
|
|
143
|
+
usdOut: swapStrategy.usdOut,
|
|
144
|
+
priceIn: swapStrategy.priceIn,
|
|
145
|
+
priceOut: swapStrategy.priceOut,
|
|
146
|
+
swapStrategy,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
function getSwapAmountsByFromValueDefault(p) {
|
|
7
150
|
const { tokenIn, tokenOut, amountIn, triggerRatio, isLimit, swapOptimizationOrder, findSwapPath, uiFeeFactor, allowedSwapSlippageBps, } = p;
|
|
8
151
|
const priceIn = tokenIn.prices.minPrice;
|
|
9
152
|
const priceOut = tokenOut.prices.maxPrice;
|
|
@@ -11,6 +154,18 @@ export function getSwapAmountsByFromValue(p) {
|
|
|
11
154
|
let amountOut = 0n;
|
|
12
155
|
let usdOut = 0n;
|
|
13
156
|
let minOutputAmount = 0n;
|
|
157
|
+
const defaultSwapStrategy = {
|
|
158
|
+
type: "noSwap",
|
|
159
|
+
externalSwapQuote: undefined,
|
|
160
|
+
swapPathStats: undefined,
|
|
161
|
+
amountIn,
|
|
162
|
+
amountOut,
|
|
163
|
+
usdIn,
|
|
164
|
+
usdOut,
|
|
165
|
+
priceIn,
|
|
166
|
+
priceOut,
|
|
167
|
+
feesUsd: 0n,
|
|
168
|
+
};
|
|
14
169
|
const defaultAmounts = {
|
|
15
170
|
amountIn,
|
|
16
171
|
usdIn,
|
|
@@ -19,7 +174,7 @@ export function getSwapAmountsByFromValue(p) {
|
|
|
19
174
|
minOutputAmount,
|
|
20
175
|
priceIn,
|
|
21
176
|
priceOut,
|
|
22
|
-
|
|
177
|
+
swapStrategy: defaultSwapStrategy,
|
|
23
178
|
};
|
|
24
179
|
if (amountIn <= 0) {
|
|
25
180
|
return defaultAmounts;
|
|
@@ -28,6 +183,18 @@ export function getSwapAmountsByFromValue(p) {
|
|
|
28
183
|
amountOut = amountIn;
|
|
29
184
|
usdOut = usdIn;
|
|
30
185
|
minOutputAmount = amountOut;
|
|
186
|
+
const swapStrategy = {
|
|
187
|
+
type: "noSwap",
|
|
188
|
+
externalSwapQuote: undefined,
|
|
189
|
+
swapPathStats: undefined,
|
|
190
|
+
amountIn,
|
|
191
|
+
amountOut,
|
|
192
|
+
usdIn,
|
|
193
|
+
usdOut,
|
|
194
|
+
priceIn,
|
|
195
|
+
priceOut,
|
|
196
|
+
feesUsd: 0n,
|
|
197
|
+
};
|
|
31
198
|
return {
|
|
32
199
|
amountIn,
|
|
33
200
|
usdIn,
|
|
@@ -36,7 +203,7 @@ export function getSwapAmountsByFromValue(p) {
|
|
|
36
203
|
minOutputAmount,
|
|
37
204
|
priceIn,
|
|
38
205
|
priceOut,
|
|
39
|
-
|
|
206
|
+
swapStrategy,
|
|
40
207
|
};
|
|
41
208
|
}
|
|
42
209
|
if (getIsStake(tokenIn, tokenOut) || getIsUnstake(tokenIn, tokenOut)) {
|
|
@@ -75,6 +242,18 @@ export function getSwapAmountsByFromValue(p) {
|
|
|
75
242
|
usdOut = 0n;
|
|
76
243
|
minOutputAmount = 0n;
|
|
77
244
|
}
|
|
245
|
+
const swapStrategy = {
|
|
246
|
+
type: "internalSwap",
|
|
247
|
+
externalSwapQuote: undefined,
|
|
248
|
+
swapPathStats,
|
|
249
|
+
amountIn,
|
|
250
|
+
amountOut,
|
|
251
|
+
usdIn,
|
|
252
|
+
usdOut,
|
|
253
|
+
priceIn,
|
|
254
|
+
priceOut,
|
|
255
|
+
feesUsd: usdIn - usdOut,
|
|
256
|
+
};
|
|
78
257
|
return {
|
|
79
258
|
amountIn,
|
|
80
259
|
usdIn,
|
|
@@ -83,10 +262,10 @@ export function getSwapAmountsByFromValue(p) {
|
|
|
83
262
|
priceIn,
|
|
84
263
|
priceOut,
|
|
85
264
|
minOutputAmount,
|
|
86
|
-
|
|
265
|
+
swapStrategy,
|
|
87
266
|
};
|
|
88
267
|
}
|
|
89
|
-
|
|
268
|
+
function getSwapAmountsByToValueDefault(p) {
|
|
90
269
|
const { tokenIn, tokenOut, amountOut, triggerRatio, isLimit, findSwapPath, uiFeeFactor, swapOptimizationOrder, allowedSwapSlippageBps, } = p;
|
|
91
270
|
const priceIn = tokenIn.prices.minPrice;
|
|
92
271
|
const priceOut = tokenOut.prices.maxPrice;
|
|
@@ -95,6 +274,18 @@ export function getSwapAmountsByToValue(p) {
|
|
|
95
274
|
let minOutputAmount = amountOut;
|
|
96
275
|
let amountIn = 0n;
|
|
97
276
|
let usdIn = 0n;
|
|
277
|
+
const defaultSwapStrategy = {
|
|
278
|
+
type: "noSwap",
|
|
279
|
+
externalSwapQuote: undefined,
|
|
280
|
+
swapPathStats: undefined,
|
|
281
|
+
amountIn,
|
|
282
|
+
amountOut,
|
|
283
|
+
usdIn,
|
|
284
|
+
usdOut,
|
|
285
|
+
priceIn,
|
|
286
|
+
priceOut,
|
|
287
|
+
feesUsd: 0n,
|
|
288
|
+
};
|
|
98
289
|
const defaultAmounts = {
|
|
99
290
|
amountIn,
|
|
100
291
|
usdIn,
|
|
@@ -103,7 +294,7 @@ export function getSwapAmountsByToValue(p) {
|
|
|
103
294
|
minOutputAmount,
|
|
104
295
|
priceIn,
|
|
105
296
|
priceOut,
|
|
106
|
-
|
|
297
|
+
swapStrategy: defaultSwapStrategy,
|
|
107
298
|
};
|
|
108
299
|
if (amountOut <= 0) {
|
|
109
300
|
return defaultAmounts;
|
|
@@ -111,6 +302,18 @@ export function getSwapAmountsByToValue(p) {
|
|
|
111
302
|
if (getIsEquivalentTokens(tokenIn, tokenOut)) {
|
|
112
303
|
amountIn = amountOut;
|
|
113
304
|
usdIn = usdOut;
|
|
305
|
+
const swapStrategy = {
|
|
306
|
+
type: "noSwap",
|
|
307
|
+
externalSwapQuote: undefined,
|
|
308
|
+
swapPathStats: undefined,
|
|
309
|
+
amountIn,
|
|
310
|
+
amountOut,
|
|
311
|
+
usdIn,
|
|
312
|
+
usdOut,
|
|
313
|
+
priceIn,
|
|
314
|
+
priceOut,
|
|
315
|
+
feesUsd: 0n,
|
|
316
|
+
};
|
|
114
317
|
return {
|
|
115
318
|
amountIn,
|
|
116
319
|
usdIn,
|
|
@@ -119,7 +322,7 @@ export function getSwapAmountsByToValue(p) {
|
|
|
119
322
|
minOutputAmount,
|
|
120
323
|
priceIn,
|
|
121
324
|
priceOut,
|
|
122
|
-
|
|
325
|
+
swapStrategy,
|
|
123
326
|
};
|
|
124
327
|
}
|
|
125
328
|
if (getIsStake(tokenIn, tokenOut) || getIsUnstake(tokenIn, tokenOut)) {
|
|
@@ -159,6 +362,18 @@ export function getSwapAmountsByToValue(p) {
|
|
|
159
362
|
amountIn = 0n;
|
|
160
363
|
usdIn = 0n;
|
|
161
364
|
}
|
|
365
|
+
const swapStrategy = {
|
|
366
|
+
type: "internalSwap",
|
|
367
|
+
externalSwapQuote: undefined,
|
|
368
|
+
swapPathStats,
|
|
369
|
+
amountIn,
|
|
370
|
+
amountOut,
|
|
371
|
+
usdIn,
|
|
372
|
+
usdOut,
|
|
373
|
+
priceIn,
|
|
374
|
+
priceOut,
|
|
375
|
+
feesUsd: usdIn - usdOut,
|
|
376
|
+
};
|
|
162
377
|
return {
|
|
163
378
|
amountIn,
|
|
164
379
|
usdIn,
|
|
@@ -167,7 +382,7 @@ export function getSwapAmountsByToValue(p) {
|
|
|
167
382
|
minOutputAmount,
|
|
168
383
|
priceIn,
|
|
169
384
|
priceOut,
|
|
170
|
-
|
|
385
|
+
swapStrategy,
|
|
171
386
|
};
|
|
172
387
|
}
|
|
173
388
|
export function getSwapPathComparator(order) {
|
|
@@ -194,6 +409,18 @@ function getPlainSwapAmountsByFromToken(tokenIn, tokenOut, amountIn) {
|
|
|
194
409
|
const amountOut = convertToTokenAmount(usdOut, tokenOut.decimals, tokenOut.prices.maxPrice);
|
|
195
410
|
const priceIn = tokenIn.prices.minPrice;
|
|
196
411
|
const priceOut = tokenOut.prices.maxPrice;
|
|
412
|
+
const swapStrategy = {
|
|
413
|
+
type: "noSwap",
|
|
414
|
+
externalSwapQuote: undefined,
|
|
415
|
+
swapPathStats: undefined,
|
|
416
|
+
amountIn,
|
|
417
|
+
amountOut,
|
|
418
|
+
usdIn,
|
|
419
|
+
usdOut,
|
|
420
|
+
priceIn,
|
|
421
|
+
priceOut,
|
|
422
|
+
feesUsd: 0n,
|
|
423
|
+
};
|
|
197
424
|
return {
|
|
198
425
|
amountIn,
|
|
199
426
|
usdIn,
|
|
@@ -202,7 +429,7 @@ function getPlainSwapAmountsByFromToken(tokenIn, tokenOut, amountIn) {
|
|
|
202
429
|
minOutputAmount: amountOut,
|
|
203
430
|
priceIn,
|
|
204
431
|
priceOut,
|
|
205
|
-
|
|
432
|
+
swapStrategy,
|
|
206
433
|
};
|
|
207
434
|
}
|
|
208
435
|
function getPlainSwapAmountsByToToken(tokenIn, tokenOut, amountOut) {
|
|
@@ -211,6 +438,18 @@ function getPlainSwapAmountsByToToken(tokenIn, tokenOut, amountOut) {
|
|
|
211
438
|
const usdOut = convertToUsd(amountOut, tokenOut.decimals, priceOut);
|
|
212
439
|
const usdIn = usdOut;
|
|
213
440
|
const amountIn = convertToTokenAmount(usdIn, tokenIn.decimals, priceIn);
|
|
441
|
+
const swapStrategy = {
|
|
442
|
+
type: "noSwap",
|
|
443
|
+
externalSwapQuote: undefined,
|
|
444
|
+
swapPathStats: undefined,
|
|
445
|
+
amountIn,
|
|
446
|
+
amountOut,
|
|
447
|
+
usdIn,
|
|
448
|
+
usdOut,
|
|
449
|
+
priceIn,
|
|
450
|
+
priceOut,
|
|
451
|
+
feesUsd: 0n,
|
|
452
|
+
};
|
|
214
453
|
return {
|
|
215
454
|
amountIn,
|
|
216
455
|
usdIn,
|
|
@@ -219,6 +458,6 @@ function getPlainSwapAmountsByToToken(tokenIn, tokenOut, amountOut) {
|
|
|
219
458
|
minOutputAmount: amountOut,
|
|
220
459
|
priceIn,
|
|
221
460
|
priceOut,
|
|
222
|
-
|
|
461
|
+
swapStrategy,
|
|
223
462
|
};
|
|
224
463
|
}
|
|
@@ -9,14 +9,25 @@ import { getAcceptablePriceInfo, getDefaultAcceptablePriceImpactBps, getMarkPric
|
|
|
9
9
|
import { getSwapAmountsByFromValue, getSwapAmountsByToValue } from "../swap";
|
|
10
10
|
import { convertToTokenAmount, convertToUsd, getIsEquivalentTokens, getTokensRatioByPrice } from "../tokens";
|
|
11
11
|
export function getIncreasePositionAmounts(p) {
|
|
12
|
-
const { marketInfo, indexToken, initialCollateralToken, collateralToken, initialCollateralAmount, indexTokenAmount, isLong, leverage, triggerPrice, limitOrderType, position, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, externalSwapQuote, findSwapPath, userReferralInfo, uiFeeFactor, strategy, } = p;
|
|
12
|
+
const { marketInfo, indexToken, initialCollateralToken, collateralToken, initialCollateralAmount, indexTokenAmount, isLong, leverage, triggerPrice, limitOrderType, position, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, externalSwapQuote, findSwapPath, userReferralInfo, uiFeeFactor, strategy, marketsInfoData, chainId, externalSwapQuoteParams, } = p;
|
|
13
|
+
const swapStrategy = {
|
|
14
|
+
type: "noSwap",
|
|
15
|
+
externalSwapQuote: undefined,
|
|
16
|
+
swapPathStats: undefined,
|
|
17
|
+
amountIn: 0n,
|
|
18
|
+
amountOut: 0n,
|
|
19
|
+
usdIn: 0n,
|
|
20
|
+
usdOut: 0n,
|
|
21
|
+
priceIn: 0n,
|
|
22
|
+
priceOut: 0n,
|
|
23
|
+
feesUsd: 0n,
|
|
24
|
+
};
|
|
13
25
|
const values = {
|
|
14
26
|
initialCollateralAmount: 0n,
|
|
15
27
|
initialCollateralUsd: 0n,
|
|
16
28
|
collateralDeltaAmount: 0n,
|
|
17
29
|
collateralDeltaUsd: 0n,
|
|
18
|
-
|
|
19
|
-
externalSwapQuote: undefined,
|
|
30
|
+
swapStrategy,
|
|
20
31
|
indexTokenAmount: 0n,
|
|
21
32
|
sizeDeltaUsd: 0n,
|
|
22
33
|
sizeDeltaInTokens: 0n,
|
|
@@ -65,26 +76,37 @@ export function getIncreasePositionAmounts(p) {
|
|
|
65
76
|
values.estimatedLeverage = leverage;
|
|
66
77
|
values.initialCollateralAmount = initialCollateralAmount;
|
|
67
78
|
values.initialCollateralUsd = convertToUsd(initialCollateralAmount, initialCollateralToken.decimals, values.initialCollateralPrice);
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
79
|
+
if (externalSwapQuote) {
|
|
80
|
+
const swapStrategy = {
|
|
81
|
+
type: "externalSwap",
|
|
82
|
+
externalSwapQuote,
|
|
83
|
+
swapPathStats: undefined,
|
|
84
|
+
...externalSwapQuote,
|
|
85
|
+
};
|
|
86
|
+
values.swapStrategy = swapStrategy;
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
const swapAmounts = getSwapAmountsByFromValue({
|
|
90
|
+
tokenIn: initialCollateralToken,
|
|
91
|
+
tokenOut: collateralToken,
|
|
92
|
+
amountIn: initialCollateralAmount,
|
|
93
|
+
isLimit: false,
|
|
94
|
+
findSwapPath,
|
|
95
|
+
uiFeeFactor,
|
|
96
|
+
swapOptimizationOrder,
|
|
97
|
+
marketsInfoData,
|
|
98
|
+
chainId,
|
|
99
|
+
externalSwapQuoteParams,
|
|
100
|
+
});
|
|
101
|
+
values.swapStrategy = swapAmounts.swapStrategy;
|
|
102
|
+
}
|
|
103
|
+
const swapAmountOut = values.swapStrategy.amountOut;
|
|
80
104
|
const baseCollateralUsd = convertToUsd(swapAmountOut, collateralToken.decimals, values.collateralPrice);
|
|
81
105
|
const baseSizeDeltaUsd = bigMath.mulDiv(baseCollateralUsd, leverage, BASIS_POINTS_DIVISOR_BIGINT);
|
|
82
106
|
const basePriceImpactDeltaUsd = getPriceImpactForPosition(marketInfo, baseSizeDeltaUsd, isLong);
|
|
83
107
|
const basePositionFeeInfo = getPositionFee(marketInfo, baseSizeDeltaUsd, basePriceImpactDeltaUsd > 0, userReferralInfo);
|
|
84
108
|
const baseUiFeeUsd = applyFactor(baseSizeDeltaUsd, uiFeeFactor);
|
|
85
|
-
const totalSwapVolumeUsd =
|
|
86
|
-
? getTotalSwapVolumeFromSwapStats(values.swapPathStats?.swapSteps)
|
|
87
|
-
: 0n;
|
|
109
|
+
const totalSwapVolumeUsd = getTotalSwapVolumeFromSwapStats(values.swapStrategy.swapPathStats?.swapSteps);
|
|
88
110
|
values.swapUiFeeUsd = applyFactor(totalSwapVolumeUsd, uiFeeFactor);
|
|
89
111
|
values.sizeDeltaUsd = bigMath.mulDiv(baseCollateralUsd - basePositionFeeInfo.positionFeeUsd - baseUiFeeUsd - values.swapUiFeeUsd, leverage, BASIS_POINTS_DIVISOR_BIGINT);
|
|
90
112
|
values.indexTokenAmount = convertToTokenAmount(values.sizeDeltaUsd, indexToken.decimals, values.indexPrice);
|
|
@@ -127,17 +149,30 @@ export function getIncreasePositionAmounts(p) {
|
|
|
127
149
|
});
|
|
128
150
|
values.collateralDeltaUsd = collateralDeltaUsd;
|
|
129
151
|
values.collateralDeltaAmount = collateralDeltaAmount;
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
152
|
+
if (externalSwapQuote) {
|
|
153
|
+
const swapStrategy = {
|
|
154
|
+
type: "externalSwap",
|
|
155
|
+
externalSwapQuote,
|
|
156
|
+
swapPathStats: undefined,
|
|
157
|
+
...externalSwapQuote,
|
|
158
|
+
};
|
|
159
|
+
values.swapStrategy = swapStrategy;
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
const swapAmounts = getSwapAmountsByToValue({
|
|
163
|
+
tokenIn: initialCollateralToken,
|
|
164
|
+
tokenOut: collateralToken,
|
|
165
|
+
amountOut: baseCollateralAmount,
|
|
166
|
+
isLimit: false,
|
|
167
|
+
findSwapPath,
|
|
168
|
+
uiFeeFactor,
|
|
169
|
+
marketsInfoData,
|
|
170
|
+
chainId,
|
|
171
|
+
externalSwapQuoteParams,
|
|
172
|
+
});
|
|
173
|
+
values.swapStrategy = swapAmounts.swapStrategy;
|
|
174
|
+
}
|
|
175
|
+
const swapAmountIn = values.swapStrategy.amountIn;
|
|
141
176
|
values.initialCollateralAmount = swapAmountIn;
|
|
142
177
|
values.initialCollateralUsd = convertToUsd(values.initialCollateralAmount, initialCollateralToken.decimals, values.initialCollateralPrice);
|
|
143
178
|
}
|
|
@@ -154,18 +189,31 @@ export function getIncreasePositionAmounts(p) {
|
|
|
154
189
|
if (initialCollateralAmount !== undefined && initialCollateralAmount > 0) {
|
|
155
190
|
values.initialCollateralAmount = initialCollateralAmount;
|
|
156
191
|
values.initialCollateralUsd = convertToUsd(initialCollateralAmount, initialCollateralToken.decimals, values.initialCollateralPrice);
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
192
|
+
if (externalSwapQuote) {
|
|
193
|
+
const swapStrategy = {
|
|
194
|
+
type: "externalSwap",
|
|
195
|
+
externalSwapQuote,
|
|
196
|
+
swapPathStats: undefined,
|
|
197
|
+
...externalSwapQuote,
|
|
198
|
+
};
|
|
199
|
+
values.swapStrategy = swapStrategy;
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
const swapAmounts = getSwapAmountsByFromValue({
|
|
203
|
+
tokenIn: initialCollateralToken,
|
|
204
|
+
tokenOut: collateralToken,
|
|
205
|
+
amountIn: initialCollateralAmount,
|
|
206
|
+
isLimit: false,
|
|
207
|
+
findSwapPath,
|
|
208
|
+
uiFeeFactor,
|
|
209
|
+
swapOptimizationOrder,
|
|
210
|
+
marketsInfoData,
|
|
211
|
+
chainId,
|
|
212
|
+
externalSwapQuoteParams,
|
|
213
|
+
});
|
|
214
|
+
values.swapStrategy = swapAmounts.swapStrategy;
|
|
215
|
+
}
|
|
216
|
+
const swapAmountIn = values.swapStrategy.amountIn;
|
|
169
217
|
const baseCollateralUsd = convertToUsd(swapAmountIn, initialCollateralToken.decimals, values.initialCollateralPrice);
|
|
170
218
|
values.collateralDeltaUsd =
|
|
171
219
|
baseCollateralUsd -
|
|
@@ -18,12 +18,12 @@ export function getSwapCount({ isSwap, isIncrease, increaseAmounts, decreaseAmou
|
|
|
18
18
|
if (isSwap) {
|
|
19
19
|
if (!swapAmounts)
|
|
20
20
|
return undefined;
|
|
21
|
-
return swapAmounts.swapPathStats?.swapPath.length ?? 0;
|
|
21
|
+
return swapAmounts.swapStrategy.swapPathStats?.swapPath.length ?? 0;
|
|
22
22
|
}
|
|
23
23
|
else if (isIncrease) {
|
|
24
24
|
if (!increaseAmounts)
|
|
25
25
|
return undefined;
|
|
26
|
-
return increaseAmounts.swapPathStats?.swapPath.length ?? 0;
|
|
26
|
+
return increaseAmounts.swapStrategy.swapPathStats?.swapPath.length ?? 0;
|
|
27
27
|
}
|
|
28
28
|
else {
|
|
29
29
|
if (decreaseAmounts?.decreaseSwapType === undefined)
|
|
@@ -3,14 +3,14 @@ const VERSION = "01";
|
|
|
3
3
|
const PREFIX = "0xff0000";
|
|
4
4
|
/**
|
|
5
5
|
* Ui fee receiver structure:
|
|
6
|
-
* 0
|
|
7
|
-
* 4
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* 19
|
|
11
|
-
* 20 byte (
|
|
6
|
+
* 0-3 (4) bytes (0-7 chars) - PREFIX
|
|
7
|
+
* 4-15 (12) bytes (8-32 chars) - 12 bytes buffer
|
|
8
|
+
* 16 (1) byte (33-34 chars) - isExpress flag
|
|
9
|
+
* 17 (1) byte (35-36 chars) - numberOfParts (hex encoded)
|
|
10
|
+
* 18-19 (2) bytes (37-40 chars) - twapId
|
|
11
|
+
* 20 (1) byte (41-42 chars) - VERSION
|
|
12
12
|
*
|
|
13
|
-
* Total: 20 bytes (
|
|
13
|
+
* Total: 0x + 20 bytes (41 hex characters)
|
|
14
14
|
*/
|
|
15
15
|
export function generateTwapId() {
|
|
16
16
|
return Math.floor(Math.random() * 256 * 256)
|
|
@@ -20,8 +20,8 @@ export function generateTwapId() {
|
|
|
20
20
|
export function createTwapUiFeeReceiver({ numberOfParts }) {
|
|
21
21
|
const twapId = generateTwapId();
|
|
22
22
|
const numberOfPartsInHex = numberOfParts.toString(16).padStart(2, "0");
|
|
23
|
-
const isExpressHex = "00";
|
|
24
23
|
const buffer = "00".repeat(12);
|
|
24
|
+
const isExpressHex = "00";
|
|
25
25
|
return `${PREFIX}${buffer}${isExpressHex}${numberOfPartsInHex}${twapId}${VERSION}`;
|
|
26
26
|
}
|
|
27
27
|
export function decodeTwapUiFeeReceiver(address) {
|
|
@@ -39,5 +39,5 @@ export function isValidTwapUiFeeReceiver(address) {
|
|
|
39
39
|
}
|
|
40
40
|
export function setUiFeeReceiverIsExpress(uiFeeReceiver, isExpress) {
|
|
41
41
|
const isExpressInHex = isExpress ? "01" : "00";
|
|
42
|
-
return `${uiFeeReceiver.slice(0,
|
|
42
|
+
return `${uiFeeReceiver.slice(0, 16 * 2)}${isExpressInHex}${uiFeeReceiver.slice(17 * 2)}`;
|
|
43
43
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Abi } from "viem";
|
|
2
|
-
export type AbiId = "CustomErrors" | "DataStore" | "ERC721" | "ERC20" | "EventEmitter" | "ExchangeRouter" | "GlpManager" | "GlvReader" | "GlvRouter" | "GMT" | "GmxMigrator" | "GovToken" | "MintableBaseToken" | "Multicall" | "OrderBook" | "OrderBookReader" | "OrderExecutor" | "PositionManager" | "PositionRouter" | "Reader" | "ReaderV2" | "ReferralStorage" | "RewardReader" | "RewardRouter" | "RewardTracker" | "Router-v2" | "Router" | "SubaccountRouter" | "SyntheticsReader" | "SyntheticsRouter" | "Timelock" | "Token" | "Treasury" | "UniPool" | "UniswapV2" | "Vault" | "VaultReader" | "VaultV2" | "VaultV2b" | "Vester" | "WETH" | "YieldFarm" | "YieldToken" | "SubaccountGelatoRelayRouter" | "ERC20PermitInterface" | "GelatoRelayRouter" | "ArbitrumNodeInterface";
|
|
2
|
+
export type AbiId = "CustomErrors" | "DataStore" | "ERC721" | "ERC20" | "EventEmitter" | "ExchangeRouter" | "GlpManager" | "GlvReader" | "GlvRouter" | "GMT" | "GmxMigrator" | "GovToken" | "MintableBaseToken" | "Multicall" | "OrderBook" | "OrderBookReader" | "OrderExecutor" | "PositionManager" | "PositionRouter" | "Reader" | "ReaderV2" | "ReferralStorage" | "RewardReader" | "RewardRouter" | "RewardTracker" | "Router-v2" | "Router" | "SubaccountRouter" | "SyntheticsReader" | "SyntheticsRouter" | "Timelock" | "Token" | "Treasury" | "UniPool" | "UniswapV2" | "Vault" | "VaultReader" | "VaultV2" | "VaultV2b" | "Vester" | "WETH" | "YieldFarm" | "YieldToken" | "SubaccountGelatoRelayRouter" | "ERC20PermitInterface" | "GelatoRelayRouter" | "ArbitrumNodeInterface" | "StBTC" | "ClaimHandler";
|
|
3
3
|
/** Copied from ethers to enable compatibility with GMX UI */
|
|
4
4
|
interface JsonFragmentType {
|
|
5
5
|
readonly name?: string;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ClientConfig, MulticallBatchOptions } from "viem";
|
|
2
|
-
|
|
2
|
+
import { UiSupportedChain } from "./chains";
|
|
3
|
+
export declare const BATCH_CONFIGS: Record<UiSupportedChain, {
|
|
3
4
|
http: MulticallBatchOptions;
|
|
4
5
|
client: ClientConfig["batch"];
|
|
5
6
|
}>;
|
|
@@ -57,6 +57,7 @@ export declare const CONTRACTS: {
|
|
|
57
57
|
ChainlinkPriceFeedProvider: string;
|
|
58
58
|
Multicall: string;
|
|
59
59
|
ArbitrumNodeInterface: string;
|
|
60
|
+
ClaimHandler: string;
|
|
60
61
|
};
|
|
61
62
|
43114: {
|
|
62
63
|
Vault: string;
|
|
@@ -115,6 +116,7 @@ export declare const CONTRACTS: {
|
|
|
115
116
|
ChainlinkPriceFeedProvider: string;
|
|
116
117
|
Multicall: string;
|
|
117
118
|
ArbitrumNodeInterface: "0x0000000000000000000000000000000000000000";
|
|
119
|
+
ClaimHandler: string;
|
|
118
120
|
};
|
|
119
121
|
3637: {
|
|
120
122
|
DataStore: string;
|
|
@@ -94,6 +94,8 @@ export declare const GASLESS_FEATURE_DISABLED_KEY: string;
|
|
|
94
94
|
export declare const GELATO_RELAY_FEE_MULTIPLIER_FACTOR_KEY: string;
|
|
95
95
|
export declare const REQUEST_EXPIRATION_TIME_KEY: string;
|
|
96
96
|
export declare const GMX_SIMULATION_ORIGIN: string;
|
|
97
|
+
export declare const CLAIM_TERMS_KEY: string;
|
|
98
|
+
export declare const GENERAL_CLAIM_FEATURE_DISABLED: string;
|
|
97
99
|
export declare function subaccountExpiresAtKey(account: string, subaccount: string, actionType: string): string;
|
|
98
100
|
export declare function glvShiftLastExecutedAtKey(glvAddress: string): string;
|
|
99
101
|
export declare function glvShiftMinIntervalKey(glvAddress: string): string;
|
|
@@ -161,3 +163,5 @@ export declare function subaccountActionCountKey(account: string, subaccount: st
|
|
|
161
163
|
export declare function subaccountAutoTopUpAmountKey(account: string, subaccount: string): string;
|
|
162
164
|
export declare function priceFeedKey(token: string): string;
|
|
163
165
|
export declare function gaslessFeatureDisabledKey(module: string): string;
|
|
166
|
+
export declare function claimTermsKey(distributionId: bigint): string;
|
|
167
|
+
export declare function claimsDisabledKey(distributionId: bigint): string;
|
|
@@ -7,6 +7,7 @@ export declare const DEFAULT_PERMIT_DEADLINE_DURATION: number;
|
|
|
7
7
|
export declare const DEFAULT_EXPRESS_ORDER_DEADLINE_DURATION: number;
|
|
8
8
|
export declare const MIN_GELATO_USD_BALANCE_FOR_SPONSORED_CALL: bigint;
|
|
9
9
|
export declare const MIN_RELAYER_FEE_USD: bigint;
|
|
10
|
+
export declare const EXPRESS_EXTRA_EXECUTION_FEE_BUFFER_BPS = 1000;
|
|
10
11
|
export declare function getGasPaymentTokens(chainId: number): string[];
|
|
11
12
|
export declare function getDefaultGasPaymentToken(chainId: number): string;
|
|
12
13
|
export declare function getRelayerFeeToken(chainId: number): Token;
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export declare function getOracleKeeperUrl(chainId: number
|
|
2
|
-
export declare function
|
|
3
|
-
export declare function getOracleKeeperRandomIndex(chainId: number, bannedIndexes?: number[]): number;
|
|
1
|
+
export declare function getOracleKeeperUrl(chainId: number): any;
|
|
2
|
+
export declare function getOracleKeeperFallbackUrls(chainId: number): any;
|