@gmx-io/sdk 1.0.5 → 1.1.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/ArbitrumNodeInterface.json +105 -0
- package/build/cjs/src/abis/CustomErrors.json +613 -16
- package/build/cjs/src/abis/ERC20PermitInterface.json +118 -0
- package/build/cjs/src/abis/GelatoRelayRouter.json +1426 -0
- package/build/cjs/src/abis/RelayParams.json +110 -0
- package/build/cjs/src/abis/SubaccountApproval.json +17 -0
- package/build/cjs/src/abis/SubaccountGelatoRelayRouter.json +1885 -0
- package/build/cjs/src/abis/SyntheticsReader.json +1 -131
- package/build/cjs/src/abis/__tests__/abis.spec.js +40 -0
- package/build/cjs/src/abis/index.js +8 -0
- package/build/cjs/src/configs/__tests__/permits.spec.js +109 -0
- package/build/cjs/src/configs/__tests__/tokens.spec.js +1 -1
- package/build/cjs/src/configs/chains.js +36 -5
- package/build/cjs/src/configs/contracts.js +15 -3
- package/build/cjs/src/configs/dataStore.js +26 -4
- package/build/cjs/src/configs/express.js +35 -0
- package/build/cjs/src/configs/factors.js +4 -3
- package/build/cjs/src/configs/markets.js +28 -0
- package/build/cjs/src/configs/tokens.js +90 -0
- package/build/cjs/src/configs/twap.js +10 -0
- package/build/cjs/src/index.js +3 -3
- package/build/cjs/src/modules/markets/index.js +1 -0
- package/build/cjs/src/modules/markets/query-builders.js +4 -0
- package/build/cjs/src/modules/orders/helpers.js +2 -0
- package/build/cjs/src/modules/utils/utils.js +11 -1
- package/build/cjs/src/prebuilt/hashedKinkModelMarketRatesKeys.json +32 -0
- package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +323 -0
- package/build/cjs/src/prebuilt/hashedMarketValuesKeys.json +60 -0
- package/build/cjs/src/test/mock.js +30 -1
- package/build/cjs/src/types/twap.js +2 -0
- package/build/cjs/src/utils/__tests__/numbers.spec.js +217 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/batchUtils.spec.js +241 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +595 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +296 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/externalCalls.spec.js +118 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +762 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/swapOrders.spec.js +469 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/updateOrder.spec.js +80 -0
- package/build/cjs/src/utils/__tests__/parseError.spec.js +263 -0
- package/build/cjs/src/utils/__tests__/uiFeeReceiver.spec.js +45 -0
- package/build/cjs/src/utils/bigmath.js +3 -0
- package/build/cjs/src/utils/callContract.js +2 -2
- package/build/cjs/src/utils/errors/index.js +18 -0
- package/build/cjs/src/utils/errors/parseError.js +150 -0
- package/build/cjs/src/utils/{contracts.js → errors/transactionsErrors.js} +35 -3
- package/build/cjs/src/utils/fees/executionFee.js +52 -1
- package/build/cjs/src/utils/fees/index.js +8 -4
- package/build/cjs/src/utils/gelatoRelay.js +5 -0
- package/build/cjs/src/utils/hash.js +6 -1
- package/build/cjs/src/utils/marketKeysAndConfigs.js +4 -0
- package/build/cjs/src/utils/markets.js +5 -1
- package/build/cjs/src/utils/numbers.js +559 -5
- package/build/cjs/src/utils/orderTransactions.js +638 -0
- package/build/cjs/src/utils/orders.js +7 -1
- package/build/cjs/src/utils/simulateExecuteOrder.js +6 -2
- package/build/cjs/src/utils/swap/__tests__/mockRouting.spec.js +11 -6
- package/build/cjs/src/utils/swap/__tests__/swapRouting.spec.js +4 -4
- package/build/cjs/src/utils/swap/__tests__/swapStats.spec.js +9 -0
- package/build/cjs/src/utils/swap/swapPath.js +30 -13
- package/build/cjs/src/utils/swap/swapRouting.js +4 -2
- package/build/cjs/src/utils/swap/swapStats.js +6 -5
- package/build/cjs/src/utils/testUtil.js +1 -1
- package/build/cjs/src/utils/time.js +30 -0
- package/build/cjs/src/utils/tokens.js +16 -2
- package/build/cjs/src/utils/twap/index.js +53 -0
- package/build/cjs/src/utils/twap/uiFeeReceiver.js +31 -0
- package/build/cjs/src/utils/types.js +13 -0
- package/build/esm/src/abis/ArbitrumNodeInterface.json +105 -0
- package/build/esm/src/abis/CustomErrors.json +613 -16
- package/build/esm/src/abis/ERC20PermitInterface.json +118 -0
- package/build/esm/src/abis/GelatoRelayRouter.json +1426 -0
- package/build/esm/src/abis/RelayParams.json +110 -0
- package/build/esm/src/abis/SubaccountApproval.json +17 -0
- package/build/esm/src/abis/SubaccountGelatoRelayRouter.json +1885 -0
- package/build/esm/src/abis/SyntheticsReader.json +1 -131
- package/build/esm/src/abis/__tests__/abis.spec.js +35 -0
- package/build/esm/src/abis/index.js +8 -0
- package/build/esm/src/configs/__tests__/permits.spec.js +107 -0
- package/build/esm/src/configs/__tests__/tokens.spec.js +3 -3
- package/build/esm/src/configs/chains.js +33 -3
- package/build/esm/src/configs/contracts.js +15 -3
- package/build/esm/src/configs/dataStore.js +19 -1
- package/build/esm/src/configs/express.js +29 -0
- package/build/esm/src/configs/factors.js +1 -3
- package/build/esm/src/configs/markets.js +28 -0
- package/build/esm/src/configs/tokens.js +90 -0
- package/build/esm/src/configs/twap.js +7 -0
- package/build/esm/src/index.js +4 -4
- package/build/esm/src/modules/markets/index.js +1 -0
- package/build/esm/src/modules/markets/query-builders.js +4 -0
- package/build/esm/src/modules/orders/helpers.js +2 -0
- package/build/esm/src/modules/utils/utils.js +13 -3
- package/build/esm/src/prebuilt/hashedKinkModelMarketRatesKeys.json +32 -0
- package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +323 -0
- package/build/esm/src/prebuilt/hashedMarketValuesKeys.json +60 -0
- package/build/esm/src/test/mock.js +28 -0
- package/build/esm/src/types/twap.js +1 -0
- package/build/esm/src/utils/__tests__/numbers.spec.js +219 -2
- package/build/esm/src/utils/__tests__/orderTransactions/batchUtils.spec.js +239 -0
- package/build/esm/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +593 -0
- package/build/esm/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +294 -0
- package/build/esm/src/utils/__tests__/orderTransactions/externalCalls.spec.js +116 -0
- package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +760 -0
- package/build/esm/src/utils/__tests__/orderTransactions/swapOrders.spec.js +467 -0
- package/build/esm/src/utils/__tests__/orderTransactions/updateOrder.spec.js +78 -0
- package/build/esm/src/utils/__tests__/parseError.spec.js +261 -0
- package/build/esm/src/utils/__tests__/uiFeeReceiver.spec.js +43 -0
- package/build/esm/src/utils/bigmath.js +3 -0
- package/build/esm/src/utils/callContract.js +3 -3
- package/build/esm/src/utils/errors/index.js +2 -0
- package/build/esm/src/utils/errors/parseError.js +141 -0
- package/build/esm/src/utils/{contracts.js → errors/transactionsErrors.js} +30 -1
- package/build/esm/src/utils/fees/executionFee.js +48 -1
- package/build/esm/src/utils/fees/index.js +8 -4
- package/build/esm/src/utils/gelatoRelay.js +2 -0
- package/build/esm/src/utils/hash.js +5 -1
- package/build/esm/src/utils/marketKeysAndConfigs.js +5 -1
- package/build/esm/src/utils/markets.js +3 -0
- package/build/esm/src/utils/numbers.js +516 -1
- package/build/esm/src/utils/orderTransactions.js +607 -0
- package/build/esm/src/utils/orders.js +5 -0
- package/build/esm/src/utils/simulateExecuteOrder.js +6 -2
- package/build/esm/src/utils/swap/__tests__/mockRouting.spec.js +11 -6
- package/build/esm/src/utils/swap/__tests__/swapRouting.spec.js +4 -4
- package/build/esm/src/utils/swap/__tests__/swapStats.spec.js +9 -0
- package/build/esm/src/utils/swap/swapPath.js +30 -13
- package/build/esm/src/utils/swap/swapRouting.js +4 -2
- package/build/esm/src/utils/swap/swapStats.js +6 -5
- package/build/esm/src/utils/testUtil.js +2 -2
- package/build/esm/src/utils/time.js +23 -0
- package/build/esm/src/utils/tokens.js +14 -1
- package/build/esm/src/utils/twap/index.js +44 -0
- package/build/esm/src/utils/twap/uiFeeReceiver.js +24 -0
- package/build/esm/src/utils/types.js +8 -0
- package/build/types/src/abis/__tests__/abis.spec.d.ts +1 -0
- package/build/types/src/abis/index.d.ts +1 -1
- package/build/types/src/configs/__tests__/permits.spec.d.ts +1 -0
- package/build/types/src/configs/chains.d.ts +29 -1
- package/build/types/src/configs/contracts.d.ts +238 -1
- package/build/types/src/configs/dataStore.d.ts +10 -0
- package/build/types/src/configs/express.d.ts +12 -0
- package/build/types/src/configs/factors.d.ts +1 -3
- package/build/types/src/configs/tokens.d.ts +2 -2
- package/build/types/src/configs/twap.d.ts +7 -0
- package/build/types/src/modules/markets/query-builders.d.ts +1 -1
- package/build/types/src/modules/markets/types.d.ts +2 -2
- package/build/types/src/modules/orders/transactions/createIncreaseOrderTxn.d.ts +1 -1
- package/build/types/src/test/mock.d.ts +16 -1
- package/build/types/src/types/fees.d.ts +9 -0
- package/build/types/src/types/markets.d.ts +1 -0
- package/build/types/src/types/orders.d.ts +3 -0
- package/build/types/src/types/tokens.d.ts +27 -0
- package/build/types/src/types/trade.d.ts +2 -19
- package/build/types/src/types/twap.d.ts +11 -0
- package/build/types/src/utils/__tests__/orderTransactions/batchUtils.spec.d.ts +1 -0
- package/build/types/src/utils/__tests__/orderTransactions/decreaseOrders.spec.d.ts +1 -0
- package/build/types/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.d.ts +1 -0
- package/build/types/src/utils/__tests__/orderTransactions/externalCalls.spec.d.ts +1 -0
- package/build/types/src/utils/__tests__/orderTransactions/increaseOrders.spec.d.ts +1 -0
- package/build/types/src/utils/__tests__/orderTransactions/swapOrders.spec.d.ts +1 -0
- package/build/types/src/utils/__tests__/orderTransactions/updateOrder.spec.d.ts +1 -0
- package/build/types/src/utils/__tests__/parseError.spec.d.ts +1 -0
- package/build/types/src/utils/__tests__/uiFeeReceiver.spec.d.ts +1 -0
- package/build/types/src/utils/bigmath.d.ts +1 -0
- package/build/types/src/utils/errors/index.d.ts +2 -0
- package/build/types/src/utils/errors/parseError.d.ts +50 -0
- package/build/types/src/utils/errors/transactionsErrors.d.ts +34 -0
- package/build/types/src/utils/fees/executionFee.d.ts +35 -2
- package/build/types/src/utils/fees/index.d.ts +1 -1
- package/build/types/src/utils/gelatoRelay.d.ts +2 -0
- package/build/types/src/utils/hash.d.ts +2 -0
- package/build/types/src/utils/marketKeysAndConfigs.d.ts +1 -0
- package/build/types/src/utils/markets.d.ts +1 -0
- package/build/types/src/utils/numbers.d.ts +110 -1
- package/build/types/src/utils/orderTransactions.d.ts +231 -0
- package/build/types/src/utils/orders.d.ts +2 -1
- package/build/types/src/utils/simulateExecuteOrder.d.ts +4 -0
- package/build/types/src/utils/swap/swapPath.d.ts +5 -5
- package/build/types/src/utils/swap/swapRouting.d.ts +2 -2
- package/build/types/src/utils/swap/swapStats.d.ts +2 -0
- package/build/types/src/utils/time.d.ts +14 -0
- package/build/types/src/utils/tokens.d.ts +6 -5
- package/build/types/src/utils/twap/index.d.ts +17 -0
- package/build/types/src/utils/twap/uiFeeReceiver.d.ts +9 -0
- package/build/types/src/utils/types.d.ts +5 -0
- package/package.json +3 -1
- package/build/types/src/utils/contracts.d.ts +0 -19
|
@@ -5,7 +5,7 @@ const viem_1 = require("viem");
|
|
|
5
5
|
const abis_1 = require("../abis");
|
|
6
6
|
const contracts_1 = require("../configs/contracts");
|
|
7
7
|
const tokens_1 = require("../configs/tokens");
|
|
8
|
-
const
|
|
8
|
+
const errors_1 = require("./errors");
|
|
9
9
|
const tokens_2 = require("./tokens");
|
|
10
10
|
class SimulateExecuteOrderError extends Error {
|
|
11
11
|
constructor(message, cause) {
|
|
@@ -13,6 +13,10 @@ class SimulateExecuteOrderError extends Error {
|
|
|
13
13
|
this.cause = cause;
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
* @deprecated use simulateExecution instead
|
|
19
|
+
*/
|
|
16
20
|
async function simulateExecuteOrder(sdk, p) {
|
|
17
21
|
const chainId = sdk.chainId;
|
|
18
22
|
const client = sdk.publicClient;
|
|
@@ -62,7 +66,7 @@ async function simulateExecuteOrder(sdk, p) {
|
|
|
62
66
|
retryCount: 2,
|
|
63
67
|
delay: 200,
|
|
64
68
|
shouldRetry: (error) => {
|
|
65
|
-
const [message] = (0,
|
|
69
|
+
const [message] = (0, errors_1.extractTxnError)(error);
|
|
66
70
|
return message?.toLocaleLowerCase()?.includes("unsupported block number") ?? false;
|
|
67
71
|
},
|
|
68
72
|
});
|
|
@@ -36,6 +36,11 @@ const baseGasLimits = {
|
|
|
36
36
|
singleSwap: 1000000n,
|
|
37
37
|
swapOrder: 3000000n,
|
|
38
38
|
withdrawalMultiToken: 1500000n,
|
|
39
|
+
createOrderGasLimit: 1000000n,
|
|
40
|
+
updateOrderGasLimit: 1000000n,
|
|
41
|
+
cancelOrderGasLimit: 1000000n,
|
|
42
|
+
gelatoRelayFeeMultiplierFactor: 1000000000000000000000000000000n,
|
|
43
|
+
tokenPermitGasLimit: 1000000n,
|
|
39
44
|
};
|
|
40
45
|
const baseGasPrice = 1650000002n;
|
|
41
46
|
(0, vitest_1.describe)("mockRouting", () => {
|
|
@@ -56,7 +61,7 @@ const baseGasPrice = 1650000002n;
|
|
|
56
61
|
},
|
|
57
62
|
});
|
|
58
63
|
const tokenSwapPaths = (0, swapRouting_1.getTokenSwapPathsForTokenPair)(swapPaths, "USDC", "DAI");
|
|
59
|
-
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData);
|
|
64
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, false);
|
|
60
65
|
const paths = (0, swapRouting_1.getNaiveBestMarketSwapPathsFromTokenSwapPaths)({
|
|
61
66
|
graph: marketAdjacencyGraph,
|
|
62
67
|
tokenSwapPaths,
|
|
@@ -90,7 +95,7 @@ const baseGasPrice = 1650000002n;
|
|
|
90
95
|
},
|
|
91
96
|
});
|
|
92
97
|
const tokenSwapPaths = (0, swapRouting_1.getTokenSwapPathsForTokenPair)(swapPaths, "USDC", "DAI");
|
|
93
|
-
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData);
|
|
98
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, false);
|
|
94
99
|
const paths = (0, swapRouting_1.getNaiveBestMarketSwapPathsFromTokenSwapPaths)({
|
|
95
100
|
graph: marketAdjacencyGraph,
|
|
96
101
|
tokenSwapPaths,
|
|
@@ -140,7 +145,7 @@ const baseGasPrice = 1650000002n;
|
|
|
140
145
|
},
|
|
141
146
|
});
|
|
142
147
|
const tokenSwapPaths = (0, swapRouting_1.getTokenSwapPathsForTokenPair)(swapPaths, "BTC", "DAI");
|
|
143
|
-
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData);
|
|
148
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, false);
|
|
144
149
|
const paths = (0, swapRouting_1.getNaiveBestMarketSwapPathsFromTokenSwapPaths)({
|
|
145
150
|
graph: marketAdjacencyGraph,
|
|
146
151
|
tokenSwapPaths,
|
|
@@ -184,7 +189,7 @@ const baseGasPrice = 1650000002n;
|
|
|
184
189
|
},
|
|
185
190
|
});
|
|
186
191
|
const tokenSwapPaths = (0, swapRouting_1.getTokenSwapPathsForTokenPair)(swapPaths, "USDC", "BTC");
|
|
187
|
-
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData);
|
|
192
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, false);
|
|
188
193
|
const paths = (0, swapRouting_1.getNaiveBestMarketSwapPathsFromTokenSwapPaths)({
|
|
189
194
|
graph: marketAdjacencyGraph,
|
|
190
195
|
tokenSwapPaths,
|
|
@@ -235,7 +240,7 @@ const baseGasPrice = 1650000002n;
|
|
|
235
240
|
},
|
|
236
241
|
});
|
|
237
242
|
const tokenSwapPaths = (0, swapRouting_1.getTokenSwapPathsForTokenPair)(swapPaths, "USDC", "BTC");
|
|
238
|
-
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData);
|
|
243
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, false);
|
|
239
244
|
const paths = (0, swapRouting_1.getNaiveBestMarketSwapPathsFromTokenSwapPaths)({
|
|
240
245
|
graph: marketAdjacencyGraph,
|
|
241
246
|
tokenSwapPaths,
|
|
@@ -285,7 +290,7 @@ const baseGasPrice = 1650000002n;
|
|
|
285
290
|
},
|
|
286
291
|
});
|
|
287
292
|
const tokenSwapPaths = (0, swapRouting_1.getTokenSwapPathsForTokenPair)(swapPaths, "BTC", "DAI");
|
|
288
|
-
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData);
|
|
293
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, false);
|
|
289
294
|
const fakeMultiplier = 100n;
|
|
290
295
|
const networkEstimator = (0, swapRouting_1.createNaiveNetworkEstimator)({
|
|
291
296
|
chainId: 1,
|
|
@@ -526,7 +526,7 @@ const dollar = 10n ** BigInt(factors_1.USD_DECIMALS);
|
|
|
526
526
|
isDisabled: true,
|
|
527
527
|
},
|
|
528
528
|
});
|
|
529
|
-
const estimator = (0, swapRouting_1.createSwapEstimator)(marketsInfoData);
|
|
529
|
+
const estimator = (0, swapRouting_1.createSwapEstimator)(marketsInfoData, false);
|
|
530
530
|
const result = estimator({
|
|
531
531
|
marketAddress: "ETH-ETH-USDC",
|
|
532
532
|
from: "ETH",
|
|
@@ -540,7 +540,7 @@ const dollar = 10n ** BigInt(factors_1.USD_DECIMALS);
|
|
|
540
540
|
const tokensData = (0, mock_1.mockTokensData)();
|
|
541
541
|
const marketKeys = ["ETH-ETH-USDC"];
|
|
542
542
|
const marketsInfoData = (0, mock_1.mockMarketsInfoData)(tokensData, marketKeys);
|
|
543
|
-
const estimator = (0, swapRouting_1.createSwapEstimator)(marketsInfoData);
|
|
543
|
+
const estimator = (0, swapRouting_1.createSwapEstimator)(marketsInfoData, false);
|
|
544
544
|
const result = estimator({
|
|
545
545
|
marketAddress: "NON-EXISTENT",
|
|
546
546
|
from: "ETH",
|
|
@@ -590,7 +590,7 @@ const dollar = 10n ** BigInt(factors_1.USD_DECIMALS);
|
|
|
590
590
|
isDisabled: true,
|
|
591
591
|
},
|
|
592
592
|
});
|
|
593
|
-
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData);
|
|
593
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, false);
|
|
594
594
|
const result = estimator({
|
|
595
595
|
marketAddress: "ETH-ETH-USDC",
|
|
596
596
|
from: "ETH",
|
|
@@ -604,7 +604,7 @@ const dollar = 10n ** BigInt(factors_1.USD_DECIMALS);
|
|
|
604
604
|
const tokensData = (0, mock_1.mockTokensData)();
|
|
605
605
|
const marketKeys = ["ETH-ETH-USDC"];
|
|
606
606
|
const marketsInfoData = (0, mock_1.mockMarketsInfoData)(tokensData, marketKeys);
|
|
607
|
-
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData);
|
|
607
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, false);
|
|
608
608
|
const result = estimator({
|
|
609
609
|
marketAddress: "NON-EXISTENT",
|
|
610
610
|
from: "ETH",
|
|
@@ -255,6 +255,7 @@ const mockMarketsInfoData = {
|
|
|
255
255
|
usdIn: 100n * dollar,
|
|
256
256
|
shouldUnwrapNativeToken: false,
|
|
257
257
|
shouldApplyPriceImpact: true,
|
|
258
|
+
isAtomicSwap: false,
|
|
258
259
|
});
|
|
259
260
|
(0, vitest_1.expect)(result).toBeUndefined();
|
|
260
261
|
});
|
|
@@ -267,6 +268,7 @@ const mockMarketsInfoData = {
|
|
|
267
268
|
usdIn: 100n * dollar,
|
|
268
269
|
shouldUnwrapNativeToken: false,
|
|
269
270
|
shouldApplyPriceImpact: true,
|
|
271
|
+
isAtomicSwap: false,
|
|
270
272
|
});
|
|
271
273
|
(0, vitest_1.expect)(result).toBeDefined();
|
|
272
274
|
(0, vitest_1.expect)(result?.swapPath).toEqual(["ETH-WETH-USDC"]);
|
|
@@ -288,6 +290,7 @@ const mockMarketsInfoData = {
|
|
|
288
290
|
usdIn: 100n * dollar,
|
|
289
291
|
shouldUnwrapNativeToken: false,
|
|
290
292
|
shouldApplyPriceImpact: true,
|
|
293
|
+
isAtomicSwap: false,
|
|
291
294
|
});
|
|
292
295
|
(0, vitest_1.expect)(result).toBeDefined();
|
|
293
296
|
(0, vitest_1.expect)(result?.swapPath).toEqual(["BTC-BTC-WETH", "BTC-BTC-USDC"]);
|
|
@@ -309,6 +312,7 @@ const mockMarketsInfoData = {
|
|
|
309
312
|
usdIn: 100n * dollar,
|
|
310
313
|
shouldUnwrapNativeToken: true,
|
|
311
314
|
shouldApplyPriceImpact: true,
|
|
315
|
+
isAtomicSwap: false,
|
|
312
316
|
});
|
|
313
317
|
(0, vitest_1.expect)(result).toBeDefined();
|
|
314
318
|
(0, vitest_1.expect)(result?.tokenOutAddress).toBe(tokens_1.NATIVE_TOKEN_ADDRESS);
|
|
@@ -322,6 +326,7 @@ const mockMarketsInfoData = {
|
|
|
322
326
|
usdIn: 100n * dollar,
|
|
323
327
|
shouldUnwrapNativeToken: false,
|
|
324
328
|
shouldApplyPriceImpact: true,
|
|
329
|
+
isAtomicSwap: false,
|
|
325
330
|
});
|
|
326
331
|
(0, vitest_1.expect)(result).toBeUndefined();
|
|
327
332
|
});
|
|
@@ -334,6 +339,7 @@ const mockMarketsInfoData = {
|
|
|
334
339
|
usdIn: 100n * dollar,
|
|
335
340
|
shouldUnwrapNativeToken: false,
|
|
336
341
|
shouldApplyPriceImpact: true,
|
|
342
|
+
isAtomicSwap: false,
|
|
337
343
|
});
|
|
338
344
|
const resultWithoutImpact = (0, swapStats_1.getSwapPathStats)({
|
|
339
345
|
marketsInfoData: testMarketsInfoData,
|
|
@@ -343,6 +349,7 @@ const mockMarketsInfoData = {
|
|
|
343
349
|
usdIn: 100n * dollar,
|
|
344
350
|
shouldUnwrapNativeToken: false,
|
|
345
351
|
shouldApplyPriceImpact: false,
|
|
352
|
+
isAtomicSwap: false,
|
|
346
353
|
});
|
|
347
354
|
if (!resultWithImpact || !resultWithoutImpact) {
|
|
348
355
|
throw new Error("Results should be defined");
|
|
@@ -358,6 +365,7 @@ const mockMarketsInfoData = {
|
|
|
358
365
|
usdIn: 100n * dollar,
|
|
359
366
|
shouldUnwrapNativeToken: false,
|
|
360
367
|
shouldApplyPriceImpact: true,
|
|
368
|
+
isAtomicSwap: false,
|
|
361
369
|
});
|
|
362
370
|
(0, vitest_1.expect)(result).toBeDefined();
|
|
363
371
|
(0, vitest_1.expect)(result?.swapSteps).toHaveLength(2);
|
|
@@ -382,6 +390,7 @@ const mockMarketsInfoData = {
|
|
|
382
390
|
usdIn: 100n * dollar,
|
|
383
391
|
shouldUnwrapNativeToken: false,
|
|
384
392
|
shouldApplyPriceImpact: true,
|
|
393
|
+
isAtomicSwap: false,
|
|
385
394
|
});
|
|
386
395
|
(0, vitest_1.expect)(result).toBeUndefined();
|
|
387
396
|
});
|
|
@@ -4,6 +4,7 @@ exports.createFindSwapPath = exports.getWrappedAddress = void 0;
|
|
|
4
4
|
const markets_1 = require("../../configs/markets");
|
|
5
5
|
const tokens_1 = require("../../configs/tokens");
|
|
6
6
|
const LruCache_1 = require("../LruCache");
|
|
7
|
+
const markets_2 = require("../markets");
|
|
7
8
|
const buildMarketsAdjacencyGraph_1 = require("./buildMarketsAdjacencyGraph");
|
|
8
9
|
const swapRouting_1 = require("./swapRouting");
|
|
9
10
|
const swapStats_1 = require("./swapStats");
|
|
@@ -12,16 +13,16 @@ const getWrappedAddress = (chainId, address) => {
|
|
|
12
13
|
};
|
|
13
14
|
exports.getWrappedAddress = getWrappedAddress;
|
|
14
15
|
const DEBUG_MARKET_ADJACENCY_GRAPH_CACHE = new LruCache_1.LRUCache(100);
|
|
15
|
-
function
|
|
16
|
-
if (!
|
|
16
|
+
function buildMarketAdjacencyGraph(chainId, disabledMarkets) {
|
|
17
|
+
if (!disabledMarkets?.length) {
|
|
17
18
|
return (0, swapRouting_1.getMarketAdjacencyGraph)(chainId);
|
|
18
19
|
}
|
|
19
|
-
const cacheKey = `${chainId}-${JSON.stringify(
|
|
20
|
+
const cacheKey = `${chainId}-${JSON.stringify(disabledMarkets)}`;
|
|
20
21
|
const cachedGraph = DEBUG_MARKET_ADJACENCY_GRAPH_CACHE.get(cacheKey);
|
|
21
22
|
if (cachedGraph) {
|
|
22
23
|
return cachedGraph;
|
|
23
24
|
}
|
|
24
|
-
const disabledMarketAddresses =
|
|
25
|
+
const disabledMarketAddresses = disabledMarkets;
|
|
25
26
|
const strippedMarkets = Object.fromEntries(Object.entries(markets_1.MARKETS[chainId]).filter(([marketAddress]) => !disabledMarketAddresses.includes(marketAddress)));
|
|
26
27
|
const graph = (0, buildMarketsAdjacencyGraph_1.buildMarketsAdjacencyGraph)(strippedMarkets);
|
|
27
28
|
DEBUG_MARKET_ADJACENCY_GRAPH_CACHE.set(cacheKey, graph);
|
|
@@ -29,22 +30,37 @@ function buildDebugMarketAdjacencyGraph(chainId, debugSwapMarketsConfig) {
|
|
|
29
30
|
}
|
|
30
31
|
const FALLBACK_FIND_SWAP_PATH = () => undefined;
|
|
31
32
|
const createFindSwapPath = (params) => {
|
|
32
|
-
const { chainId, fromTokenAddress, toTokenAddress, marketsInfoData,
|
|
33
|
+
const { chainId, fromTokenAddress, toTokenAddress, marketsInfoData, disabledMarkets, manualPath, gasEstimationParams, isExpressFeeSwap, maxSwapPathLength, } = params;
|
|
33
34
|
const wrappedFromAddress = (0, exports.getWrappedAddress)(chainId, fromTokenAddress);
|
|
34
35
|
const wrappedToAddress = (0, exports.getWrappedAddress)(chainId, toTokenAddress);
|
|
35
36
|
const wrappedToken = (0, tokens_1.getWrappedToken)(chainId);
|
|
36
|
-
|
|
37
|
+
let tokenSwapPaths = wrappedFromAddress && wrappedToAddress
|
|
37
38
|
? (0, swapRouting_1.getTokenSwapPathsForTokenPairPrebuilt)(chainId, wrappedFromAddress, wrappedToAddress)
|
|
38
39
|
: [];
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
if (maxSwapPathLength) {
|
|
41
|
+
/**
|
|
42
|
+
* As tokenSwapPath contains what tokens can we between input and output token,
|
|
43
|
+
* restricting intermediate tokens to 0 would mean we filter out any non-direct market swaps,
|
|
44
|
+
* length of 1 would mean all 2-step swaps
|
|
45
|
+
*/
|
|
46
|
+
const nonDirectPathLength = maxSwapPathLength - 1;
|
|
47
|
+
tokenSwapPaths = tokenSwapPaths.filter((path) => path.length <= nonDirectPathLength);
|
|
48
|
+
}
|
|
49
|
+
const finalDisabledMarkets = [...(disabledMarkets ?? [])];
|
|
50
|
+
if (isExpressFeeSwap) {
|
|
51
|
+
const expressSwapUnavailableMarkets = Object.values(marketsInfoData ?? {})
|
|
52
|
+
.filter((market) => !(0, markets_2.getIsMarketAvailableForExpressSwaps)(market))
|
|
53
|
+
.map((market) => market.marketTokenAddress);
|
|
54
|
+
finalDisabledMarkets.push(...expressSwapUnavailableMarkets);
|
|
55
|
+
}
|
|
56
|
+
const isAtomicSwap = Boolean(isExpressFeeSwap);
|
|
57
|
+
const marketAdjacencyGraph = buildMarketAdjacencyGraph(chainId, finalDisabledMarkets);
|
|
42
58
|
const cache = {};
|
|
43
59
|
if (!marketsInfoData) {
|
|
44
60
|
return FALLBACK_FIND_SWAP_PATH;
|
|
45
61
|
}
|
|
46
62
|
const marketEdgeLiquidityGetter = (0, swapRouting_1.createMarketEdgeLiquidityGetter)(marketsInfoData);
|
|
47
|
-
const naiveEstimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData);
|
|
63
|
+
const naiveEstimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, isAtomicSwap);
|
|
48
64
|
const naiveNetworkEstimator = gasEstimationParams
|
|
49
65
|
? (0, swapRouting_1.createNaiveNetworkEstimator)({
|
|
50
66
|
gasLimits: gasEstimationParams.gasLimits,
|
|
@@ -53,7 +69,7 @@ const createFindSwapPath = (params) => {
|
|
|
53
69
|
chainId,
|
|
54
70
|
})
|
|
55
71
|
: undefined;
|
|
56
|
-
const estimator = (0, swapRouting_1.createSwapEstimator)(marketsInfoData);
|
|
72
|
+
const estimator = (0, swapRouting_1.createSwapEstimator)(marketsInfoData, isAtomicSwap);
|
|
57
73
|
const findSwapPath = (usdIn, opts) => {
|
|
58
74
|
if (tokenSwapPaths.length === 0 || !fromTokenAddress || !wrappedFromAddress || !wrappedToAddress) {
|
|
59
75
|
return undefined;
|
|
@@ -63,8 +79,8 @@ const createFindSwapPath = (params) => {
|
|
|
63
79
|
return cache[cacheKey];
|
|
64
80
|
}
|
|
65
81
|
let swapPath = undefined;
|
|
66
|
-
if (
|
|
67
|
-
swapPath =
|
|
82
|
+
if (manualPath !== undefined) {
|
|
83
|
+
swapPath = manualPath;
|
|
68
84
|
}
|
|
69
85
|
else if (opts?.order || usdIn === 0n) {
|
|
70
86
|
const primaryOrder = opts?.order?.at(0) === "length" ? "length" : "liquidity";
|
|
@@ -123,6 +139,7 @@ const createFindSwapPath = (params) => {
|
|
|
123
139
|
shouldUnwrapNativeToken: toTokenAddress === tokens_1.NATIVE_TOKEN_ADDRESS,
|
|
124
140
|
shouldApplyPriceImpact: true,
|
|
125
141
|
usdIn,
|
|
142
|
+
isAtomicSwap,
|
|
126
143
|
});
|
|
127
144
|
cache[cacheKey] = result;
|
|
128
145
|
return result;
|
|
@@ -11,7 +11,7 @@ const objects_1 = require("../objects");
|
|
|
11
11
|
const constants_1 = require("./constants");
|
|
12
12
|
const preparedSwapData_1 = require("./preparedSwapData");
|
|
13
13
|
const swapStats_1 = require("./swapStats");
|
|
14
|
-
const createSwapEstimator = (marketsInfoData) => {
|
|
14
|
+
const createSwapEstimator = (marketsInfoData, isAtomicSwap) => {
|
|
15
15
|
return (e, usdIn) => {
|
|
16
16
|
const marketInfo = marketsInfoData[e.marketAddress];
|
|
17
17
|
if (!marketInfo || marketInfo.isDisabled) {
|
|
@@ -25,6 +25,7 @@ const createSwapEstimator = (marketsInfoData) => {
|
|
|
25
25
|
tokenInAddress: e.from,
|
|
26
26
|
tokenOutAddress: e.to,
|
|
27
27
|
shouldApplyPriceImpact: true,
|
|
28
|
+
isAtomicSwap,
|
|
28
29
|
});
|
|
29
30
|
const isOutLiquidity = swapStats?.isOutLiquidity;
|
|
30
31
|
const isOutCapacity = swapStats?.isOutCapacity;
|
|
@@ -52,7 +53,7 @@ const createMarketEdgeLiquidityGetter = (marketsInfoData) => {
|
|
|
52
53
|
};
|
|
53
54
|
};
|
|
54
55
|
exports.createMarketEdgeLiquidityGetter = createMarketEdgeLiquidityGetter;
|
|
55
|
-
const createNaiveSwapEstimator = (marketsInfoData) => {
|
|
56
|
+
const createNaiveSwapEstimator = (marketsInfoData, isAtomicSwap) => {
|
|
56
57
|
return (e, usdIn) => {
|
|
57
58
|
let marketInfo = marketsInfoData[e.marketAddress];
|
|
58
59
|
if (marketInfo === undefined || marketInfo.isDisabled) {
|
|
@@ -64,6 +65,7 @@ const createNaiveSwapEstimator = (marketsInfoData) => {
|
|
|
64
65
|
tokenInAddress: e.from,
|
|
65
66
|
tokenOutAddress: e.to,
|
|
66
67
|
shouldApplyPriceImpact: true,
|
|
68
|
+
isAtomicSwap,
|
|
67
69
|
});
|
|
68
70
|
const usdOut = swapStats?.usdOut;
|
|
69
71
|
if (usdOut === undefined || usdOut === 0n || swapStats.isOutCapacity || swapStats.isOutLiquidity) {
|
|
@@ -83,7 +83,7 @@ function getSwapPathOutputAddresses(p) {
|
|
|
83
83
|
}
|
|
84
84
|
exports.getSwapPathOutputAddresses = getSwapPathOutputAddresses;
|
|
85
85
|
function getSwapPathStats(p) {
|
|
86
|
-
const { marketsInfoData, swapPath, initialCollateralAddress, usdIn, shouldUnwrapNativeToken, shouldApplyPriceImpact, wrappedNativeTokenAddress, } = p;
|
|
86
|
+
const { marketsInfoData, swapPath, initialCollateralAddress, usdIn, shouldUnwrapNativeToken, shouldApplyPriceImpact, wrappedNativeTokenAddress, isAtomicSwap, } = p;
|
|
87
87
|
if (swapPath.length === 0) {
|
|
88
88
|
return undefined;
|
|
89
89
|
}
|
|
@@ -113,6 +113,7 @@ function getSwapPathStats(p) {
|
|
|
113
113
|
tokenOutAddress,
|
|
114
114
|
usdIn: usdOut,
|
|
115
115
|
shouldApplyPriceImpact,
|
|
116
|
+
isAtomicSwap,
|
|
116
117
|
});
|
|
117
118
|
tokenInAddress = swapStep.tokenOutAddress;
|
|
118
119
|
usdOut = swapStep.usdOut;
|
|
@@ -139,7 +140,7 @@ function getSwapPathStats(p) {
|
|
|
139
140
|
}
|
|
140
141
|
exports.getSwapPathStats = getSwapPathStats;
|
|
141
142
|
function getSwapStats(p) {
|
|
142
|
-
const { marketInfo, tokenInAddress, tokenOutAddress, usdIn, shouldApplyPriceImpact } = p;
|
|
143
|
+
const { marketInfo, tokenInAddress, tokenOutAddress, usdIn, shouldApplyPriceImpact, isAtomicSwap } = p;
|
|
143
144
|
const isWrap = tokenInAddress === tokens_1.NATIVE_TOKEN_ADDRESS;
|
|
144
145
|
const isUnwrap = tokenOutAddress === tokens_1.NATIVE_TOKEN_ADDRESS;
|
|
145
146
|
const tokenIn = (0, markets_1.getTokenPoolType)(marketInfo, tokenInAddress) === "long" ? marketInfo.longToken : marketInfo.shortToken;
|
|
@@ -154,7 +155,7 @@ function getSwapStats(p) {
|
|
|
154
155
|
catch (e) {
|
|
155
156
|
// Approximate if the market would be out of capacity
|
|
156
157
|
const capacityUsd = getSwapCapacityUsd(marketInfo, (0, markets_1.getTokenPoolType)(marketInfo, tokenInAddress) === "long");
|
|
157
|
-
const swapFeeUsd = (0, fees_1.getSwapFee)(marketInfo, usdIn, false);
|
|
158
|
+
const swapFeeUsd = (0, fees_1.getSwapFee)(marketInfo, usdIn, false, isAtomicSwap);
|
|
158
159
|
const usdInAfterFees = usdIn - swapFeeUsd;
|
|
159
160
|
const isOutCapacity = capacityUsd < usdInAfterFees;
|
|
160
161
|
return {
|
|
@@ -175,8 +176,8 @@ function getSwapStats(p) {
|
|
|
175
176
|
isOutCapacity,
|
|
176
177
|
};
|
|
177
178
|
}
|
|
178
|
-
const swapFeeAmount = (0, fees_1.getSwapFee)(marketInfo, amountIn, priceImpactDeltaUsd > 0);
|
|
179
|
-
const swapFeeUsd = (0, fees_1.getSwapFee)(marketInfo, usdIn, priceImpactDeltaUsd > 0);
|
|
179
|
+
const swapFeeAmount = (0, fees_1.getSwapFee)(marketInfo, amountIn, priceImpactDeltaUsd > 0, isAtomicSwap);
|
|
180
|
+
const swapFeeUsd = (0, fees_1.getSwapFee)(marketInfo, usdIn, priceImpactDeltaUsd > 0, isAtomicSwap);
|
|
180
181
|
const amountInAfterFees = amountIn - swapFeeAmount;
|
|
181
182
|
const usdInAfterFees = usdIn - swapFeeUsd;
|
|
182
183
|
let usdOut = usdInAfterFees;
|
|
@@ -5,7 +5,7 @@ const viem_1 = require("viem");
|
|
|
5
5
|
const chains_1 = require("../configs/chains");
|
|
6
6
|
const index_1 = require("../index");
|
|
7
7
|
const client = (0, viem_1.createTestClient)({
|
|
8
|
-
chain: (0, chains_1.
|
|
8
|
+
chain: (0, chains_1.getViemChain)(chains_1.ARBITRUM),
|
|
9
9
|
mode: "hardhat",
|
|
10
10
|
transport: (0, viem_1.http)(),
|
|
11
11
|
})
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.nowInSeconds = exports.periodToSeconds = exports.secondsToPeriod = exports.secondsFrom = void 0;
|
|
4
|
+
const SECONDS_IN_PERIOD = {
|
|
5
|
+
"1m": 60,
|
|
6
|
+
"5m": 60 * 5,
|
|
7
|
+
"15m": 60 * 15,
|
|
8
|
+
"1h": 60 * 60,
|
|
9
|
+
"4h": 60 * 60 * 4,
|
|
10
|
+
"1d": 60 * 60 * 24,
|
|
11
|
+
"1y": 60 * 60 * 24 * 365,
|
|
12
|
+
};
|
|
13
|
+
function secondsFrom(period) {
|
|
14
|
+
return SECONDS_IN_PERIOD[period];
|
|
15
|
+
}
|
|
16
|
+
exports.secondsFrom = secondsFrom;
|
|
17
|
+
function secondsToPeriod(seconds, period, roundUp = false) {
|
|
18
|
+
const secondsInPeriod = secondsFrom(period);
|
|
19
|
+
const roundedSeconds = roundUp ? Math.ceil(seconds / secondsInPeriod) : Math.floor(seconds / secondsInPeriod);
|
|
20
|
+
return roundedSeconds;
|
|
21
|
+
}
|
|
22
|
+
exports.secondsToPeriod = secondsToPeriod;
|
|
23
|
+
function periodToSeconds(periodsCount, period) {
|
|
24
|
+
return periodsCount * secondsFrom(period);
|
|
25
|
+
}
|
|
26
|
+
exports.periodToSeconds = periodToSeconds;
|
|
27
|
+
function nowInSeconds() {
|
|
28
|
+
return Math.floor(Date.now() / 1000);
|
|
29
|
+
}
|
|
30
|
+
exports.nowInSeconds = nowInSeconds;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getTokensRatioByPrice = exports.getIsUnwrap = exports.getIsWrap = exports.getAmountByRatio = exports.getTokensRatioByMinOutputAmountAndTriggerPrice = exports.getTokensRatioByAmounts = exports.getTokenData = exports.getIsEquivalentTokens = exports.getMidPrice = exports.convertToUsd = exports.convertToTokenAmount = exports.convertToContractTokenPrices = exports.convertToContractPrice = exports.parseContractPrice = void 0;
|
|
3
|
+
exports.getTokensRatioByPrice = exports.getIsUnwrap = exports.getIsWrap = exports.getAmountByRatio = exports.getTokensRatioByMinOutputAmountAndTriggerPrice = exports.getTokensRatioByAmounts = exports.getTokenData = exports.getIsEquivalentTokens = exports.getMidPrice = exports.convertBetweenTokens = exports.convertToUsd = exports.convertToTokenAmount = exports.convertToContractTokenPrices = exports.convertToContractPrice = exports.parseContractPrice = void 0;
|
|
4
4
|
const factors_1 = require("../configs/factors");
|
|
5
5
|
const tokens_1 = require("../configs/tokens");
|
|
6
6
|
const bigmath_1 = require("./bigmath");
|
|
@@ -10,7 +10,7 @@ function parseContractPrice(price, tokenDecimals) {
|
|
|
10
10
|
}
|
|
11
11
|
exports.parseContractPrice = parseContractPrice;
|
|
12
12
|
function convertToContractPrice(price, tokenDecimals) {
|
|
13
|
-
return price / (0, numbers_1.expandDecimals)(1, tokenDecimals);
|
|
13
|
+
return (price / (0, numbers_1.expandDecimals)(1, tokenDecimals));
|
|
14
14
|
}
|
|
15
15
|
exports.convertToContractPrice = convertToContractPrice;
|
|
16
16
|
function convertToContractTokenPrices(prices, tokenDecimals) {
|
|
@@ -34,6 +34,20 @@ function convertToUsd(tokenAmount, tokenDecimals, price) {
|
|
|
34
34
|
return (tokenAmount * price) / (0, numbers_1.expandDecimals)(1, tokenDecimals);
|
|
35
35
|
}
|
|
36
36
|
exports.convertToUsd = convertToUsd;
|
|
37
|
+
function convertBetweenTokens(tokenAmount, fromToken, toToken, maximize) {
|
|
38
|
+
if (tokenAmount === undefined || fromToken === undefined || toToken === undefined) {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
if (getIsEquivalentTokens(fromToken, toToken)) {
|
|
42
|
+
return tokenAmount;
|
|
43
|
+
}
|
|
44
|
+
const fromPrice = maximize ? fromToken.prices.maxPrice : fromToken.prices.minPrice;
|
|
45
|
+
const toPrice = maximize ? toToken.prices.minPrice : toToken.prices.maxPrice;
|
|
46
|
+
const usd = convertToUsd(tokenAmount, fromToken.decimals, fromPrice);
|
|
47
|
+
const amount = convertToTokenAmount(usd, toToken.decimals, toPrice);
|
|
48
|
+
return amount;
|
|
49
|
+
}
|
|
50
|
+
exports.convertBetweenTokens = convertBetweenTokens;
|
|
37
51
|
function getMidPrice(prices) {
|
|
38
52
|
return (prices.minPrice + prices.maxPrice) / 2n;
|
|
39
53
|
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeTwapValidFromTimeGetter = exports.getTwapOrderKey = exports.changeTwapNumberOfPartsValue = exports.getTwapValidFromTime = exports.getTwapDurationInSeconds = exports.getIsValidTwapParams = void 0;
|
|
4
|
+
const twap_1 = require("../../configs/twap");
|
|
5
|
+
const orders_1 = require("../orders");
|
|
6
|
+
function getIsValidTwapParams(duration, numberOfParts) {
|
|
7
|
+
return (duration.hours >= 0 &&
|
|
8
|
+
duration.minutes >= 0 &&
|
|
9
|
+
numberOfParts >= twap_1.MIN_TWAP_NUMBER_OF_PARTS &&
|
|
10
|
+
numberOfParts <= twap_1.MAX_TWAP_NUMBER_OF_PARTS);
|
|
11
|
+
}
|
|
12
|
+
exports.getIsValidTwapParams = getIsValidTwapParams;
|
|
13
|
+
function getTwapDurationInSeconds(duration) {
|
|
14
|
+
return duration.hours * 60 * 60 + duration.minutes * 60;
|
|
15
|
+
}
|
|
16
|
+
exports.getTwapDurationInSeconds = getTwapDurationInSeconds;
|
|
17
|
+
function getTwapValidFromTime(duration, numberOfParts, partIndex) {
|
|
18
|
+
const durationMinutes = duration.hours * 60 + duration.minutes;
|
|
19
|
+
const durationMs = durationMinutes * 60;
|
|
20
|
+
const startTime = Math.ceil(Date.now() / 1000);
|
|
21
|
+
return BigInt(Math.floor(startTime + (durationMs / (numberOfParts - 1)) * partIndex));
|
|
22
|
+
}
|
|
23
|
+
exports.getTwapValidFromTime = getTwapValidFromTime;
|
|
24
|
+
function changeTwapNumberOfPartsValue(value) {
|
|
25
|
+
if (value < twap_1.MIN_TWAP_NUMBER_OF_PARTS) {
|
|
26
|
+
return twap_1.MIN_TWAP_NUMBER_OF_PARTS;
|
|
27
|
+
}
|
|
28
|
+
if (value > twap_1.MAX_TWAP_NUMBER_OF_PARTS) {
|
|
29
|
+
return twap_1.MAX_TWAP_NUMBER_OF_PARTS;
|
|
30
|
+
}
|
|
31
|
+
if (isNaN(value)) {
|
|
32
|
+
return twap_1.DEFAULT_TWAP_NUMBER_OF_PARTS;
|
|
33
|
+
}
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
exports.changeTwapNumberOfPartsValue = changeTwapNumberOfPartsValue;
|
|
37
|
+
function getTwapOrderKey({ twapId, orderType, pool, isLong, collateralTokenSymbol, swapPath, account, initialCollateralToken, }) {
|
|
38
|
+
if ((0, orders_1.isSwapOrderType)(orderType)) {
|
|
39
|
+
return `${twapId}-${swapPath.join("-")}-${account}-${initialCollateralToken}`;
|
|
40
|
+
}
|
|
41
|
+
const type = isLong ? "long" : "short";
|
|
42
|
+
return `${twapId}-${type}-${pool}-${collateralTokenSymbol}`;
|
|
43
|
+
}
|
|
44
|
+
exports.getTwapOrderKey = getTwapOrderKey;
|
|
45
|
+
function makeTwapValidFromTimeGetter(duration, numberOfParts) {
|
|
46
|
+
const durationMinutes = duration.hours * 60 + duration.minutes;
|
|
47
|
+
const durationMs = durationMinutes * 60;
|
|
48
|
+
const startTime = Math.ceil(Date.now() / 1000);
|
|
49
|
+
return (part) => {
|
|
50
|
+
return BigInt(Math.floor(startTime + (durationMs / (numberOfParts - 1)) * part));
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
exports.makeTwapValidFromTimeGetter = makeTwapValidFromTimeGetter;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isValidTwapUiFeeReceiver = exports.decodeTwapUiFeeReceiver = exports.createTwapUiFeeReceiver = exports.generateTwapId = void 0;
|
|
4
|
+
const viem_1 = require("viem");
|
|
5
|
+
const VERSION = "01";
|
|
6
|
+
const PREFIX = "0xff";
|
|
7
|
+
function generateTwapId() {
|
|
8
|
+
return Math.floor(Math.random() * 256 * 256)
|
|
9
|
+
.toString(16)
|
|
10
|
+
.padStart(4, "0");
|
|
11
|
+
}
|
|
12
|
+
exports.generateTwapId = generateTwapId;
|
|
13
|
+
function createTwapUiFeeReceiver({ numberOfParts }) {
|
|
14
|
+
const twapId = generateTwapId();
|
|
15
|
+
const numberOfPartsInHex = numberOfParts.toString(16).padStart(2, "0");
|
|
16
|
+
return `${PREFIX}00${"00".repeat(14)}${numberOfPartsInHex}${twapId}${VERSION}`;
|
|
17
|
+
}
|
|
18
|
+
exports.createTwapUiFeeReceiver = createTwapUiFeeReceiver;
|
|
19
|
+
function decodeTwapUiFeeReceiver(address) {
|
|
20
|
+
const twapId = address.slice(36, 40);
|
|
21
|
+
if (!isValidTwapUiFeeReceiver(address) || twapId === "0000") {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const numberOfParts = parseInt(address.slice(34, 36), 16);
|
|
25
|
+
return { twapId, numberOfParts };
|
|
26
|
+
}
|
|
27
|
+
exports.decodeTwapUiFeeReceiver = decodeTwapUiFeeReceiver;
|
|
28
|
+
function isValidTwapUiFeeReceiver(address) {
|
|
29
|
+
return (0, viem_1.isAddress)(address) && address.toLowerCase().startsWith(PREFIX.toLowerCase());
|
|
30
|
+
}
|
|
31
|
+
exports.isValidTwapUiFeeReceiver = isValidTwapUiFeeReceiver;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assertDefined = exports.mustNeverExist = void 0;
|
|
4
|
+
const mustNeverExist = (x) => {
|
|
5
|
+
throw new Error(`Must never exist: ${x}`);
|
|
6
|
+
};
|
|
7
|
+
exports.mustNeverExist = mustNeverExist;
|
|
8
|
+
const assertDefined = (x) => {
|
|
9
|
+
if (x === undefined)
|
|
10
|
+
throw new Error(`Expected defined value, got undefined`);
|
|
11
|
+
return x;
|
|
12
|
+
};
|
|
13
|
+
exports.assertDefined = assertDefined;
|