@gmx-io/sdk 1.4.0 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/src/abis/GlvReader.js +940 -940
- package/build/cjs/src/abis/IStargate.js +603 -0
- package/build/cjs/src/abis/LayerZeroProvider.js +273 -46
- package/build/cjs/src/abis/index.js +2 -2
- package/build/cjs/src/configs/batch.js +29 -103
- package/build/cjs/src/configs/chainIds.js +2 -2
- package/build/cjs/src/configs/chains.js +230 -151
- package/build/cjs/src/configs/contracts.js +1 -5
- package/build/cjs/src/configs/dataStore.js +5 -4
- package/build/cjs/src/configs/express.js +1 -1
- package/build/cjs/src/configs/gasLimits.js +41 -0
- package/build/cjs/src/configs/markets.js +102 -1
- package/build/cjs/src/configs/multichain.js +1 -0
- package/build/cjs/src/configs/oracleKeeper.js +18 -20
- package/build/cjs/src/configs/tokens.js +98 -180
- package/build/cjs/src/modules/markets/index.js +6 -2
- package/build/cjs/src/modules/markets/markets.spec.js +5 -5
- package/build/cjs/src/modules/markets/query-builders.js +8 -0
- package/build/cjs/src/modules/orders/helpers.js +4 -2
- package/build/cjs/src/modules/orders/helpers.spec.js +1 -1
- package/build/cjs/src/modules/orders/orders.spec.js +1 -1
- package/build/cjs/src/modules/positions/positions.spec.js +2 -2
- package/build/cjs/src/modules/tokens/tokens.spec.js +2 -2
- package/build/cjs/src/modules/trades/trades.spec.js +1 -1
- package/build/cjs/src/modules/utils/utils.js +7 -6
- package/build/cjs/src/prebuilt/hashedKinkModelMarketRatesKeys.json +64 -0
- package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +780 -0
- package/build/cjs/src/prebuilt/hashedMarketValuesKeys.json +120 -0
- package/build/cjs/src/test/mock.js +2 -0
- package/build/cjs/src/types/chains.js +2 -0
- package/build/cjs/src/types/orders.js +8 -2
- package/build/cjs/src/types/subsquid.js +241 -1
- package/build/cjs/src/types/tokens.js +7 -0
- package/build/cjs/src/utils/__tests__/abort.spec.js +122 -0
- package/build/cjs/src/utils/__tests__/numbers.spec.js +94 -14
- package/build/cjs/src/utils/abort.js +31 -0
- package/build/cjs/src/utils/callContract.js +4 -4
- package/build/cjs/src/utils/fees/executionFee.js +14 -11
- package/build/cjs/src/utils/fees/index.js +8 -2
- package/build/cjs/src/utils/marketKeysAndConfigs.js +8 -0
- package/build/cjs/src/utils/markets.js +7 -1
- package/build/cjs/src/utils/multicall.js +54 -17
- package/build/cjs/src/utils/numbers.js +25 -14
- package/build/cjs/src/utils/orders.js +2 -2
- package/build/cjs/src/utils/swap/__tests__/findSwapPathsBetweenTokens.spec.js +22 -0
- package/build/cjs/src/utils/swap/__tests__/mockRouting.spec.js +264 -6
- package/build/cjs/src/utils/swap/__tests__/swapRouting.spec.js +5 -4
- package/build/cjs/src/utils/swap/__tests__/swapStats.spec.js +10 -9
- package/build/cjs/src/utils/swap/buildSwapStrategy.js +20 -11
- package/build/cjs/src/utils/swap/findSwapPathsBetweenTokens.js +8 -4
- package/build/cjs/src/utils/swap/swapPath.js +6 -6
- package/build/cjs/src/utils/swap/swapRouting.js +8 -4
- package/build/cjs/src/utils/swap/swapStats.js +6 -6
- package/build/cjs/src/utils/swap/swapValues.js +17 -6
- package/build/cjs/src/utils/tokens.js +17 -1
- package/build/cjs/src/utils/trade/decrease.js +1 -1
- package/build/cjs/src/utils/trade/increase.js +3 -0
- package/build/esm/src/abis/GlvReader.js +940 -940
- package/build/esm/src/abis/IStargate.js +601 -0
- package/build/esm/src/abis/LayerZeroProvider.js +273 -46
- package/build/esm/src/abis/index.js +2 -2
- package/build/esm/src/configs/batch.js +30 -104
- package/build/esm/src/configs/chainIds.js +1 -1
- package/build/esm/src/configs/chains.js +212 -147
- package/build/esm/src/configs/contracts.js +1 -5
- package/build/esm/src/configs/dataStore.js +1 -0
- package/build/esm/src/configs/express.js +1 -1
- package/build/esm/src/configs/gasLimits.js +38 -0
- package/build/esm/src/configs/markets.js +95 -1
- package/build/esm/src/configs/multichain.js +2 -1
- package/build/esm/src/configs/oracleKeeper.js +17 -19
- package/build/esm/src/configs/tokens.js +97 -179
- package/build/esm/src/modules/markets/index.js +6 -2
- package/build/esm/src/modules/markets/markets.spec.js +5 -5
- package/build/esm/src/modules/markets/query-builders.js +8 -0
- package/build/esm/src/modules/orders/helpers.js +5 -3
- package/build/esm/src/modules/orders/helpers.spec.js +1 -1
- package/build/esm/src/modules/orders/orders.spec.js +1 -1
- package/build/esm/src/modules/positions/positions.spec.js +2 -2
- package/build/esm/src/modules/tokens/tokens.spec.js +2 -2
- package/build/esm/src/modules/trades/trades.spec.js +1 -1
- package/build/esm/src/modules/utils/utils.js +7 -6
- package/build/esm/src/prebuilt/hashedKinkModelMarketRatesKeys.json +64 -0
- package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +780 -0
- package/build/esm/src/prebuilt/hashedMarketValuesKeys.json +120 -0
- package/build/esm/src/test/mock.js +2 -0
- package/build/esm/src/types/chains.js +1 -0
- package/build/esm/src/types/orders.js +8 -2
- package/build/esm/src/types/subsquid.js +240 -0
- package/build/esm/src/types/tokens.js +6 -1
- package/build/esm/src/utils/__tests__/abort.spec.js +120 -0
- package/build/esm/src/utils/__tests__/numbers.spec.js +95 -15
- package/build/esm/src/utils/abort.js +27 -0
- package/build/esm/src/utils/callContract.js +5 -5
- package/build/esm/src/utils/fees/executionFee.js +15 -12
- package/build/esm/src/utils/fees/index.js +8 -2
- package/build/esm/src/utils/marketKeysAndConfigs.js +9 -1
- package/build/esm/src/utils/markets.js +5 -0
- package/build/esm/src/utils/multicall.js +53 -16
- package/build/esm/src/utils/numbers.js +21 -12
- package/build/esm/src/utils/orders.js +3 -3
- package/build/esm/src/utils/swap/__tests__/findSwapPathsBetweenTokens.spec.js +22 -0
- package/build/esm/src/utils/swap/__tests__/mockRouting.spec.js +264 -6
- package/build/esm/src/utils/swap/__tests__/swapRouting.spec.js +5 -4
- package/build/esm/src/utils/swap/__tests__/swapStats.spec.js +10 -9
- package/build/esm/src/utils/swap/buildSwapStrategy.js +21 -12
- package/build/esm/src/utils/swap/findSwapPathsBetweenTokens.js +8 -4
- package/build/esm/src/utils/swap/swapPath.js +6 -6
- package/build/esm/src/utils/swap/swapRouting.js +8 -4
- package/build/esm/src/utils/swap/swapStats.js +6 -6
- package/build/esm/src/utils/swap/swapValues.js +18 -7
- package/build/esm/src/utils/tokens.js +15 -1
- package/build/esm/src/utils/trade/decrease.js +2 -2
- package/build/esm/src/utils/trade/increase.js +3 -0
- package/build/types/src/abis/IStargate.d.ts +464 -0
- package/build/types/src/abis/LayerZeroProvider.d.ts +12 -0
- package/build/types/src/abis/index.d.ts +475 -99
- package/build/types/src/configs/chainIds.d.ts +1 -1
- package/build/types/src/configs/chains.d.ts +170 -27
- package/build/types/src/configs/contracts.d.ts +0 -4
- package/build/types/src/configs/dataStore.d.ts +1 -0
- package/build/types/src/configs/express.d.ts +1 -1
- package/build/types/src/configs/gasLimits.d.ts +4 -0
- package/build/types/src/configs/markets.d.ts +10 -1
- package/build/types/src/configs/oracleKeeper.d.ts +13 -0
- package/build/types/src/configs/tokens.d.ts +3 -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/markets/index.d.ts +2 -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/utils/utils.d.ts +2 -3
- package/build/types/src/types/chains.d.ts +57 -0
- package/build/types/src/types/markets.d.ts +2 -0
- package/build/types/src/types/orders.d.ts +8 -2
- package/build/types/src/types/subsquid.d.ts +5987 -5172
- package/build/types/src/types/tokens.d.ts +11 -6
- package/build/types/src/types/trade.d.ts +5 -0
- package/build/types/src/utils/__tests__/abort.spec.d.ts +1 -0
- package/build/types/src/utils/abort.d.ts +1 -0
- package/build/types/src/utils/fees/estimateOraclePriceCount.d.ts +2 -2
- package/build/types/src/utils/fees/executionFee.d.ts +6 -6
- package/build/types/src/utils/fees/index.d.ts +2 -1
- package/build/types/src/utils/marketKeysAndConfigs.d.ts +2 -0
- package/build/types/src/utils/markets.d.ts +4 -2
- package/build/types/src/utils/multicall.d.ts +4 -2
- package/build/types/src/utils/numbers.d.ts +8 -1
- package/build/types/src/utils/swap/buildSwapStrategy.d.ts +8 -3
- package/build/types/src/utils/swap/swapPath.d.ts +2 -1
- package/build/types/src/utils/swap/swapRouting.d.ts +7 -2
- package/build/types/src/utils/swap/swapStats.d.ts +3 -2
- package/build/types/src/utils/swap/swapValues.d.ts +3 -1
- package/build/types/src/utils/tokens.d.ts +3 -0
- package/package.json +17 -5
- package/build/cjs/src/abis/MultichainUtils.js +0 -133
- package/build/esm/src/abis/MultichainUtils.js +0 -131
- package/build/types/src/abis/MultichainUtils.d.ts +0 -100
|
@@ -3,7 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const vitest_1 = require("vitest");
|
|
4
4
|
const factors_1 = require("../../../configs/factors");
|
|
5
5
|
const tokens_1 = require("../../../configs/tokens");
|
|
6
|
+
const orders_1 = require("../../../types/orders");
|
|
6
7
|
const numbers_1 = require("../../numbers");
|
|
8
|
+
const swap_1 = require("..");
|
|
7
9
|
const buildMarketsAdjacencyGraph_1 = require("../buildMarketsAdjacencyGraph");
|
|
8
10
|
const findSwapPathsBetweenTokens_1 = require("../findSwapPathsBetweenTokens");
|
|
9
11
|
const swapRouting_1 = require("../swapRouting");
|
|
@@ -12,6 +14,7 @@ const marketKeys = [
|
|
|
12
14
|
"ETH-ETH-USDC",
|
|
13
15
|
"BTC-BTC-USDC",
|
|
14
16
|
"BTC-BTC-DAI",
|
|
17
|
+
"BTC-SOL-DAI",
|
|
15
18
|
"SOL-SOL-USDC",
|
|
16
19
|
"SOL-ETH-USDC",
|
|
17
20
|
"SOL-BTC-USDC",
|
|
@@ -62,7 +65,7 @@ const baseGasPrice = 1650000002n;
|
|
|
62
65
|
},
|
|
63
66
|
});
|
|
64
67
|
const tokenSwapPaths = (0, swapRouting_1.getTokenSwapPathsForTokenPair)(swapPaths, "USDC", "DAI");
|
|
65
|
-
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData,
|
|
68
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, orders_1.SwapPricingType.Swap);
|
|
66
69
|
const paths = (0, swapRouting_1.getNaiveBestMarketSwapPathsFromTokenSwapPaths)({
|
|
67
70
|
graph: marketAdjacencyGraph,
|
|
68
71
|
tokenSwapPaths,
|
|
@@ -96,7 +99,7 @@ const baseGasPrice = 1650000002n;
|
|
|
96
99
|
},
|
|
97
100
|
});
|
|
98
101
|
const tokenSwapPaths = (0, swapRouting_1.getTokenSwapPathsForTokenPair)(swapPaths, "USDC", "DAI");
|
|
99
|
-
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData,
|
|
102
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, orders_1.SwapPricingType.Swap);
|
|
100
103
|
const paths = (0, swapRouting_1.getNaiveBestMarketSwapPathsFromTokenSwapPaths)({
|
|
101
104
|
graph: marketAdjacencyGraph,
|
|
102
105
|
tokenSwapPaths,
|
|
@@ -146,7 +149,7 @@ const baseGasPrice = 1650000002n;
|
|
|
146
149
|
},
|
|
147
150
|
});
|
|
148
151
|
const tokenSwapPaths = (0, swapRouting_1.getTokenSwapPathsForTokenPair)(swapPaths, "BTC", "DAI");
|
|
149
|
-
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData,
|
|
152
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, orders_1.SwapPricingType.Swap);
|
|
150
153
|
const paths = (0, swapRouting_1.getNaiveBestMarketSwapPathsFromTokenSwapPaths)({
|
|
151
154
|
graph: marketAdjacencyGraph,
|
|
152
155
|
tokenSwapPaths,
|
|
@@ -190,7 +193,7 @@ const baseGasPrice = 1650000002n;
|
|
|
190
193
|
},
|
|
191
194
|
});
|
|
192
195
|
const tokenSwapPaths = (0, swapRouting_1.getTokenSwapPathsForTokenPair)(swapPaths, "USDC", "BTC");
|
|
193
|
-
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData,
|
|
196
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, orders_1.SwapPricingType.Swap);
|
|
194
197
|
const paths = (0, swapRouting_1.getNaiveBestMarketSwapPathsFromTokenSwapPaths)({
|
|
195
198
|
graph: marketAdjacencyGraph,
|
|
196
199
|
tokenSwapPaths,
|
|
@@ -241,7 +244,7 @@ const baseGasPrice = 1650000002n;
|
|
|
241
244
|
},
|
|
242
245
|
});
|
|
243
246
|
const tokenSwapPaths = (0, swapRouting_1.getTokenSwapPathsForTokenPair)(swapPaths, "USDC", "BTC");
|
|
244
|
-
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData,
|
|
247
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, orders_1.SwapPricingType.Swap);
|
|
245
248
|
const paths = (0, swapRouting_1.getNaiveBestMarketSwapPathsFromTokenSwapPaths)({
|
|
246
249
|
graph: marketAdjacencyGraph,
|
|
247
250
|
tokenSwapPaths,
|
|
@@ -291,7 +294,7 @@ const baseGasPrice = 1650000002n;
|
|
|
291
294
|
},
|
|
292
295
|
});
|
|
293
296
|
const tokenSwapPaths = (0, swapRouting_1.getTokenSwapPathsForTokenPair)(swapPaths, "BTC", "DAI");
|
|
294
|
-
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData,
|
|
297
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, orders_1.SwapPricingType.Swap);
|
|
295
298
|
const fakeMultiplier = 100n;
|
|
296
299
|
const networkEstimator = (0, swapRouting_1.createNaiveNetworkEstimator)({
|
|
297
300
|
chainId: 1,
|
|
@@ -433,3 +436,258 @@ const baseGasPrice = 1650000002n;
|
|
|
433
436
|
});
|
|
434
437
|
});
|
|
435
438
|
});
|
|
439
|
+
(0, vitest_1.describe)("same token arbitrage swaps", () => {
|
|
440
|
+
(0, vitest_1.it)("should yield profit (usdOut > usdIn) for USDC->USDC swap when markets have opposite imbalances", () => {
|
|
441
|
+
const USD_IN = (0, numbers_1.expandDecimals)(10000n, factors_1.USD_DECIMALS);
|
|
442
|
+
const marketsInfoData = (0, mock_1.mockMarketsInfoData)(tokensData, marketKeys, {
|
|
443
|
+
"ETH-ETH-USDC": {
|
|
444
|
+
longPoolAmount: (0, mock_1.usdToToken)(30000, tokensData.ETH),
|
|
445
|
+
shortPoolAmount: (0, mock_1.usdToToken)(170000, tokensData.USDC),
|
|
446
|
+
maxLongPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.ETH),
|
|
447
|
+
maxShortPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.USDC),
|
|
448
|
+
},
|
|
449
|
+
"SOL-ETH-USDC": {
|
|
450
|
+
longPoolAmount: (0, mock_1.usdToToken)(170000, tokensData.ETH),
|
|
451
|
+
shortPoolAmount: (0, mock_1.usdToToken)(30000, tokensData.USDC),
|
|
452
|
+
maxLongPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.ETH),
|
|
453
|
+
maxShortPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.USDC),
|
|
454
|
+
},
|
|
455
|
+
});
|
|
456
|
+
const tokenSwapPaths = (0, swapRouting_1.getTokenSwapPathsForTokenPair)(swapPaths, "USDC", "USDC");
|
|
457
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, orders_1.SwapPricingType.Swap);
|
|
458
|
+
const networkEstimator = (0, swapRouting_1.createNaiveNetworkEstimator)({
|
|
459
|
+
chainId: 1,
|
|
460
|
+
gasLimits: {
|
|
461
|
+
...baseGasLimits,
|
|
462
|
+
singleSwap: baseGasLimits.singleSwap * 3n,
|
|
463
|
+
},
|
|
464
|
+
gasPrice: baseGasPrice * 100n,
|
|
465
|
+
tokensData: {
|
|
466
|
+
...tokensData,
|
|
467
|
+
[tokens_1.NATIVE_TOKEN_ADDRESS]: {
|
|
468
|
+
...tokensData.ETH,
|
|
469
|
+
address: tokens_1.NATIVE_TOKEN_ADDRESS,
|
|
470
|
+
},
|
|
471
|
+
},
|
|
472
|
+
});
|
|
473
|
+
const paths = (0, swapRouting_1.getNaiveBestMarketSwapPathsFromTokenSwapPaths)({
|
|
474
|
+
graph: marketAdjacencyGraph,
|
|
475
|
+
tokenSwapPaths,
|
|
476
|
+
topPathsCount: 1,
|
|
477
|
+
tokenInAddress: "USDC",
|
|
478
|
+
tokenOutAddress: "USDC",
|
|
479
|
+
estimator,
|
|
480
|
+
usdIn: USD_IN,
|
|
481
|
+
networkEstimator,
|
|
482
|
+
});
|
|
483
|
+
const topPath = paths?.[0];
|
|
484
|
+
(0, vitest_1.expect)(topPath).toBeDefined();
|
|
485
|
+
(0, vitest_1.expect)(topPath?.length).toBe(2);
|
|
486
|
+
const swapPathStats = (0, swap_1.getSwapPathStats)({
|
|
487
|
+
marketsInfoData,
|
|
488
|
+
swapPath: topPath,
|
|
489
|
+
initialCollateralAddress: "USDC",
|
|
490
|
+
wrappedNativeTokenAddress: "WETH",
|
|
491
|
+
usdIn: USD_IN,
|
|
492
|
+
shouldUnwrapNativeToken: false,
|
|
493
|
+
shouldApplyPriceImpact: true,
|
|
494
|
+
swapPricingType: orders_1.SwapPricingType.Swap,
|
|
495
|
+
});
|
|
496
|
+
(0, vitest_1.expect)(swapPathStats).toBeDefined();
|
|
497
|
+
const { usdOut } = networkEstimator(swapPathStats.usdOut, topPath.length);
|
|
498
|
+
(0, vitest_1.expect)(usdOut).toBeGreaterThan(USD_IN);
|
|
499
|
+
});
|
|
500
|
+
(0, vitest_1.it)("should yield loss when markets are balanced", () => {
|
|
501
|
+
const USD_IN = (0, numbers_1.expandDecimals)(10000n, factors_1.USD_DECIMALS);
|
|
502
|
+
const marketsInfoData = (0, mock_1.mockMarketsInfoData)(tokensData, marketKeys, {
|
|
503
|
+
"ETH-ETH-USDC": {
|
|
504
|
+
longPoolAmount: (0, mock_1.usdToToken)(100000, tokensData.ETH),
|
|
505
|
+
shortPoolAmount: (0, mock_1.usdToToken)(100000, tokensData.USDC),
|
|
506
|
+
maxLongPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.ETH),
|
|
507
|
+
maxShortPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.USDC),
|
|
508
|
+
},
|
|
509
|
+
"SOL-ETH-USDC": {
|
|
510
|
+
longPoolAmount: (0, mock_1.usdToToken)(100000, tokensData.ETH),
|
|
511
|
+
shortPoolAmount: (0, mock_1.usdToToken)(100000, tokensData.USDC),
|
|
512
|
+
maxLongPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.ETH),
|
|
513
|
+
maxShortPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.USDC),
|
|
514
|
+
},
|
|
515
|
+
});
|
|
516
|
+
const tokenSwapPaths = (0, swapRouting_1.getTokenSwapPathsForTokenPair)(swapPaths, "USDC", "USDC");
|
|
517
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, orders_1.SwapPricingType.Swap);
|
|
518
|
+
const networkEstimator = (0, swapRouting_1.createNaiveNetworkEstimator)({
|
|
519
|
+
chainId: 1,
|
|
520
|
+
gasLimits: baseGasLimits,
|
|
521
|
+
gasPrice: baseGasPrice,
|
|
522
|
+
tokensData: {
|
|
523
|
+
...tokensData,
|
|
524
|
+
[tokens_1.NATIVE_TOKEN_ADDRESS]: {
|
|
525
|
+
...tokensData.ETH,
|
|
526
|
+
address: tokens_1.NATIVE_TOKEN_ADDRESS,
|
|
527
|
+
},
|
|
528
|
+
},
|
|
529
|
+
});
|
|
530
|
+
const paths = (0, swapRouting_1.getNaiveBestMarketSwapPathsFromTokenSwapPaths)({
|
|
531
|
+
graph: marketAdjacencyGraph,
|
|
532
|
+
tokenSwapPaths,
|
|
533
|
+
topPathsCount: 1,
|
|
534
|
+
tokenInAddress: "USDC",
|
|
535
|
+
tokenOutAddress: "USDC",
|
|
536
|
+
estimator,
|
|
537
|
+
usdIn: USD_IN,
|
|
538
|
+
networkEstimator,
|
|
539
|
+
});
|
|
540
|
+
const topPath = paths?.[0];
|
|
541
|
+
(0, vitest_1.expect)(topPath).toBeDefined();
|
|
542
|
+
(0, vitest_1.expect)(topPath?.length).toBeGreaterThan(0);
|
|
543
|
+
const swapPathStats = (0, swap_1.getSwapPathStats)({
|
|
544
|
+
marketsInfoData,
|
|
545
|
+
swapPath: topPath,
|
|
546
|
+
initialCollateralAddress: "USDC",
|
|
547
|
+
wrappedNativeTokenAddress: "WETH",
|
|
548
|
+
usdIn: USD_IN,
|
|
549
|
+
shouldUnwrapNativeToken: false,
|
|
550
|
+
shouldApplyPriceImpact: true,
|
|
551
|
+
swapPricingType: orders_1.SwapPricingType.Swap,
|
|
552
|
+
});
|
|
553
|
+
(0, vitest_1.expect)(swapPathStats).toBeDefined();
|
|
554
|
+
const { usdOut } = networkEstimator(swapPathStats.usdOut, topPath.length);
|
|
555
|
+
(0, vitest_1.expect)(usdOut).toBeLessThan(USD_IN);
|
|
556
|
+
});
|
|
557
|
+
(0, vitest_1.it)("should find most profitable arbitrage path", () => {
|
|
558
|
+
const USD_IN = (0, numbers_1.expandDecimals)(10000n, factors_1.USD_DECIMALS);
|
|
559
|
+
const marketsInfoData = (0, mock_1.mockMarketsInfoData)(tokensData, marketKeys, {
|
|
560
|
+
// Path 1: ETH-ETH-USDC -> SOL-ETH-USDC (moderate profit)
|
|
561
|
+
"ETH-ETH-USDC": {
|
|
562
|
+
longPoolAmount: (0, mock_1.usdToToken)(50000, tokensData.ETH),
|
|
563
|
+
shortPoolAmount: (0, mock_1.usdToToken)(150000, tokensData.USDC),
|
|
564
|
+
maxLongPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.ETH),
|
|
565
|
+
maxShortPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.USDC),
|
|
566
|
+
},
|
|
567
|
+
"SOL-ETH-USDC": {
|
|
568
|
+
longPoolAmount: (0, mock_1.usdToToken)(150000, tokensData.ETH),
|
|
569
|
+
shortPoolAmount: (0, mock_1.usdToToken)(50000, tokensData.USDC),
|
|
570
|
+
maxLongPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.ETH),
|
|
571
|
+
maxShortPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.USDC),
|
|
572
|
+
},
|
|
573
|
+
// Path 2: BTC-BTC-USDC -> SOL-BTC-USDC (higher profit)
|
|
574
|
+
"BTC-BTC-USDC": {
|
|
575
|
+
longPoolAmount: (0, mock_1.usdToToken)(30000, tokensData.BTC),
|
|
576
|
+
shortPoolAmount: (0, mock_1.usdToToken)(170000, tokensData.USDC),
|
|
577
|
+
maxLongPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.BTC),
|
|
578
|
+
maxShortPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.USDC),
|
|
579
|
+
},
|
|
580
|
+
"SOL-BTC-USDC": {
|
|
581
|
+
longPoolAmount: (0, mock_1.usdToToken)(170000, tokensData.BTC),
|
|
582
|
+
shortPoolAmount: (0, mock_1.usdToToken)(30000, tokensData.USDC),
|
|
583
|
+
maxLongPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.BTC),
|
|
584
|
+
maxShortPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.USDC),
|
|
585
|
+
},
|
|
586
|
+
});
|
|
587
|
+
const tokenSwapPaths = (0, swapRouting_1.getTokenSwapPathsForTokenPair)(swapPaths, "USDC", "USDC");
|
|
588
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, orders_1.SwapPricingType.Swap);
|
|
589
|
+
const networkEstimator = (0, swapRouting_1.createNaiveNetworkEstimator)({
|
|
590
|
+
chainId: 1,
|
|
591
|
+
gasLimits: baseGasLimits,
|
|
592
|
+
gasPrice: baseGasPrice,
|
|
593
|
+
tokensData: {
|
|
594
|
+
...tokensData,
|
|
595
|
+
[tokens_1.NATIVE_TOKEN_ADDRESS]: {
|
|
596
|
+
...tokensData.ETH,
|
|
597
|
+
address: tokens_1.NATIVE_TOKEN_ADDRESS,
|
|
598
|
+
},
|
|
599
|
+
},
|
|
600
|
+
});
|
|
601
|
+
const paths = (0, swapRouting_1.getNaiveBestMarketSwapPathsFromTokenSwapPaths)({
|
|
602
|
+
graph: marketAdjacencyGraph,
|
|
603
|
+
tokenSwapPaths,
|
|
604
|
+
topPathsCount: 1,
|
|
605
|
+
tokenInAddress: "USDC",
|
|
606
|
+
tokenOutAddress: "USDC",
|
|
607
|
+
estimator,
|
|
608
|
+
usdIn: USD_IN,
|
|
609
|
+
networkEstimator,
|
|
610
|
+
});
|
|
611
|
+
const topPath = paths?.[0];
|
|
612
|
+
(0, vitest_1.expect)(topPath).toBeDefined();
|
|
613
|
+
(0, vitest_1.expect)(topPath?.length).toBe(2);
|
|
614
|
+
const swapPathStats = (0, swap_1.getSwapPathStats)({
|
|
615
|
+
marketsInfoData,
|
|
616
|
+
swapPath: topPath,
|
|
617
|
+
initialCollateralAddress: "USDC",
|
|
618
|
+
wrappedNativeTokenAddress: "WETH",
|
|
619
|
+
usdIn: USD_IN,
|
|
620
|
+
shouldUnwrapNativeToken: false,
|
|
621
|
+
shouldApplyPriceImpact: true,
|
|
622
|
+
swapPricingType: orders_1.SwapPricingType.Swap,
|
|
623
|
+
});
|
|
624
|
+
(0, vitest_1.expect)(swapPathStats).toBeDefined();
|
|
625
|
+
const { usdOut } = networkEstimator(swapPathStats.usdOut, topPath.length);
|
|
626
|
+
(0, vitest_1.expect)(usdOut).toBeGreaterThan(USD_IN);
|
|
627
|
+
(0, vitest_1.expect)(topPath).toEqual(["SOL-BTC-USDC", "BTC-BTC-USDC"]);
|
|
628
|
+
});
|
|
629
|
+
(0, vitest_1.it)("should find longer arbitrage swap path with higher profit", () => {
|
|
630
|
+
const USD_IN = (0, numbers_1.expandDecimals)(5000n, factors_1.USD_DECIMALS);
|
|
631
|
+
const marketsInfoData = (0, mock_1.mockMarketsInfoData)(tokensData, marketKeys, {
|
|
632
|
+
// Path 1: USDC -> ETH -> USDC (2 markets, moderate profit)
|
|
633
|
+
"ETH-ETH-USDC": {
|
|
634
|
+
longPoolAmount: (0, mock_1.usdToToken)(60000, tokensData.ETH),
|
|
635
|
+
shortPoolAmount: (0, mock_1.usdToToken)(140000, tokensData.USDC),
|
|
636
|
+
maxLongPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.ETH),
|
|
637
|
+
maxShortPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.USDC),
|
|
638
|
+
},
|
|
639
|
+
"SOL-ETH-USDC": {
|
|
640
|
+
longPoolAmount: (0, mock_1.usdToToken)(140000, tokensData.ETH),
|
|
641
|
+
shortPoolAmount: (0, mock_1.usdToToken)(60000, tokensData.USDC),
|
|
642
|
+
maxLongPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.ETH),
|
|
643
|
+
maxShortPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.USDC),
|
|
644
|
+
},
|
|
645
|
+
// Path 2: USDC -> SOL -> DAI -> USDC (3 markets, higher profit)
|
|
646
|
+
"SOL-SOL-USDC": {
|
|
647
|
+
longPoolAmount: (0, mock_1.usdToToken)(150000, tokensData.SOL),
|
|
648
|
+
shortPoolAmount: (0, mock_1.usdToToken)(50000, tokensData.USDC),
|
|
649
|
+
maxLongPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.SOL),
|
|
650
|
+
maxShortPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.USDC),
|
|
651
|
+
},
|
|
652
|
+
"BTC-SOL-DAI": {
|
|
653
|
+
longPoolAmount: (0, mock_1.usdToToken)(50000, tokensData.SOL),
|
|
654
|
+
shortPoolAmount: (0, mock_1.usdToToken)(150000, tokensData.DAI),
|
|
655
|
+
maxLongPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.SOL),
|
|
656
|
+
maxShortPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.DAI),
|
|
657
|
+
},
|
|
658
|
+
"SPOT-DAI-USDC": {
|
|
659
|
+
longPoolAmount: (0, mock_1.usdToToken)(50000, tokensData.DAI),
|
|
660
|
+
shortPoolAmount: (0, mock_1.usdToToken)(150000, tokensData.USDC),
|
|
661
|
+
maxLongPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.DAI),
|
|
662
|
+
maxShortPoolAmount: (0, mock_1.usdToToken)(1000000, tokensData.USDC),
|
|
663
|
+
},
|
|
664
|
+
});
|
|
665
|
+
const tokenSwapPaths = (0, swapRouting_1.getTokenSwapPathsForTokenPair)(swapPaths, "USDC", "USDC");
|
|
666
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, orders_1.SwapPricingType.Swap);
|
|
667
|
+
const networkEstimator = (0, swapRouting_1.createNaiveNetworkEstimator)({
|
|
668
|
+
chainId: 1,
|
|
669
|
+
gasLimits: baseGasLimits,
|
|
670
|
+
gasPrice: baseGasPrice,
|
|
671
|
+
tokensData: {
|
|
672
|
+
...tokensData,
|
|
673
|
+
[tokens_1.NATIVE_TOKEN_ADDRESS]: {
|
|
674
|
+
...tokensData.ETH,
|
|
675
|
+
address: tokens_1.NATIVE_TOKEN_ADDRESS,
|
|
676
|
+
},
|
|
677
|
+
},
|
|
678
|
+
});
|
|
679
|
+
const paths = (0, swapRouting_1.getNaiveBestMarketSwapPathsFromTokenSwapPaths)({
|
|
680
|
+
graph: marketAdjacencyGraph,
|
|
681
|
+
tokenSwapPaths,
|
|
682
|
+
topPathsCount: 1,
|
|
683
|
+
tokenInAddress: "USDC",
|
|
684
|
+
tokenOutAddress: "USDC",
|
|
685
|
+
estimator,
|
|
686
|
+
usdIn: USD_IN,
|
|
687
|
+
networkEstimator,
|
|
688
|
+
});
|
|
689
|
+
const topPath = paths?.[0];
|
|
690
|
+
(0, vitest_1.expect)(topPath).toBeDefined();
|
|
691
|
+
(0, vitest_1.expect)(topPath.length).toEqual(3);
|
|
692
|
+
});
|
|
693
|
+
});
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const vitest_1 = require("vitest");
|
|
4
4
|
const factors_1 = require("../../../configs/factors");
|
|
5
|
+
const orders_1 = require("../../../types/orders");
|
|
5
6
|
const bigmath_1 = require("../../bigmath");
|
|
6
7
|
const mock_1 = require("../../../test/mock");
|
|
7
8
|
const buildMarketsAdjacencyGraph_1 = require("../buildMarketsAdjacencyGraph");
|
|
@@ -526,7 +527,7 @@ const dollar = 10n ** BigInt(factors_1.USD_DECIMALS);
|
|
|
526
527
|
isDisabled: true,
|
|
527
528
|
},
|
|
528
529
|
});
|
|
529
|
-
const estimator = (0, swapRouting_1.createSwapEstimator)(marketsInfoData,
|
|
530
|
+
const estimator = (0, swapRouting_1.createSwapEstimator)(marketsInfoData, orders_1.SwapPricingType.Swap);
|
|
530
531
|
const result = estimator({
|
|
531
532
|
marketAddress: "ETH-ETH-USDC",
|
|
532
533
|
from: "ETH",
|
|
@@ -540,7 +541,7 @@ const dollar = 10n ** BigInt(factors_1.USD_DECIMALS);
|
|
|
540
541
|
const tokensData = (0, mock_1.mockTokensData)();
|
|
541
542
|
const marketKeys = ["ETH-ETH-USDC"];
|
|
542
543
|
const marketsInfoData = (0, mock_1.mockMarketsInfoData)(tokensData, marketKeys);
|
|
543
|
-
const estimator = (0, swapRouting_1.createSwapEstimator)(marketsInfoData,
|
|
544
|
+
const estimator = (0, swapRouting_1.createSwapEstimator)(marketsInfoData, orders_1.SwapPricingType.Swap);
|
|
544
545
|
const result = estimator({
|
|
545
546
|
marketAddress: "NON-EXISTENT",
|
|
546
547
|
from: "ETH",
|
|
@@ -590,7 +591,7 @@ const dollar = 10n ** BigInt(factors_1.USD_DECIMALS);
|
|
|
590
591
|
isDisabled: true,
|
|
591
592
|
},
|
|
592
593
|
});
|
|
593
|
-
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData,
|
|
594
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, orders_1.SwapPricingType.Swap);
|
|
594
595
|
const result = estimator({
|
|
595
596
|
marketAddress: "ETH-ETH-USDC",
|
|
596
597
|
from: "ETH",
|
|
@@ -604,7 +605,7 @@ const dollar = 10n ** BigInt(factors_1.USD_DECIMALS);
|
|
|
604
605
|
const tokensData = (0, mock_1.mockTokensData)();
|
|
605
606
|
const marketKeys = ["ETH-ETH-USDC"];
|
|
606
607
|
const marketsInfoData = (0, mock_1.mockMarketsInfoData)(tokensData, marketKeys);
|
|
607
|
-
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData,
|
|
608
|
+
const estimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, orders_1.SwapPricingType.Swap);
|
|
608
609
|
const result = estimator({
|
|
609
610
|
marketAddress: "NON-EXISTENT",
|
|
610
611
|
from: "ETH",
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const vitest_1 = require("vitest");
|
|
4
4
|
const factors_1 = require("../../../configs/factors");
|
|
5
5
|
const tokens_1 = require("../../../configs/tokens");
|
|
6
|
+
const orders_1 = require("../../../types/orders");
|
|
6
7
|
const mock_1 = require("../../../test/mock");
|
|
7
8
|
const swapStats_1 = require("../swapStats");
|
|
8
9
|
const someWrappedToken = "0x0000000000000000000000000000000000000001";
|
|
@@ -255,7 +256,7 @@ const mockMarketsInfoData = {
|
|
|
255
256
|
usdIn: 100n * dollar,
|
|
256
257
|
shouldUnwrapNativeToken: false,
|
|
257
258
|
shouldApplyPriceImpact: true,
|
|
258
|
-
|
|
259
|
+
swapPricingType: orders_1.SwapPricingType.Swap,
|
|
259
260
|
});
|
|
260
261
|
(0, vitest_1.expect)(result).toBeUndefined();
|
|
261
262
|
});
|
|
@@ -268,7 +269,7 @@ const mockMarketsInfoData = {
|
|
|
268
269
|
usdIn: 100n * dollar,
|
|
269
270
|
shouldUnwrapNativeToken: false,
|
|
270
271
|
shouldApplyPriceImpact: true,
|
|
271
|
-
|
|
272
|
+
swapPricingType: orders_1.SwapPricingType.Swap,
|
|
272
273
|
});
|
|
273
274
|
(0, vitest_1.expect)(result).toBeDefined();
|
|
274
275
|
(0, vitest_1.expect)(result?.swapPath).toEqual(["ETH-WETH-USDC"]);
|
|
@@ -290,7 +291,7 @@ const mockMarketsInfoData = {
|
|
|
290
291
|
usdIn: 100n * dollar,
|
|
291
292
|
shouldUnwrapNativeToken: false,
|
|
292
293
|
shouldApplyPriceImpact: true,
|
|
293
|
-
|
|
294
|
+
swapPricingType: orders_1.SwapPricingType.Swap,
|
|
294
295
|
});
|
|
295
296
|
(0, vitest_1.expect)(result).toBeDefined();
|
|
296
297
|
(0, vitest_1.expect)(result?.swapPath).toEqual(["BTC-BTC-WETH", "BTC-BTC-USDC"]);
|
|
@@ -312,7 +313,7 @@ const mockMarketsInfoData = {
|
|
|
312
313
|
usdIn: 100n * dollar,
|
|
313
314
|
shouldUnwrapNativeToken: true,
|
|
314
315
|
shouldApplyPriceImpact: true,
|
|
315
|
-
|
|
316
|
+
swapPricingType: orders_1.SwapPricingType.Swap,
|
|
316
317
|
});
|
|
317
318
|
(0, vitest_1.expect)(result).toBeDefined();
|
|
318
319
|
(0, vitest_1.expect)(result?.tokenOutAddress).toBe(tokens_1.NATIVE_TOKEN_ADDRESS);
|
|
@@ -326,7 +327,7 @@ const mockMarketsInfoData = {
|
|
|
326
327
|
usdIn: 100n * dollar,
|
|
327
328
|
shouldUnwrapNativeToken: false,
|
|
328
329
|
shouldApplyPriceImpact: true,
|
|
329
|
-
|
|
330
|
+
swapPricingType: orders_1.SwapPricingType.Swap,
|
|
330
331
|
});
|
|
331
332
|
(0, vitest_1.expect)(result).toBeUndefined();
|
|
332
333
|
});
|
|
@@ -339,7 +340,7 @@ const mockMarketsInfoData = {
|
|
|
339
340
|
usdIn: 100n * dollar,
|
|
340
341
|
shouldUnwrapNativeToken: false,
|
|
341
342
|
shouldApplyPriceImpact: true,
|
|
342
|
-
|
|
343
|
+
swapPricingType: orders_1.SwapPricingType.Swap,
|
|
343
344
|
});
|
|
344
345
|
const resultWithoutImpact = (0, swapStats_1.getSwapPathStats)({
|
|
345
346
|
marketsInfoData: testMarketsInfoData,
|
|
@@ -349,7 +350,7 @@ const mockMarketsInfoData = {
|
|
|
349
350
|
usdIn: 100n * dollar,
|
|
350
351
|
shouldUnwrapNativeToken: false,
|
|
351
352
|
shouldApplyPriceImpact: false,
|
|
352
|
-
|
|
353
|
+
swapPricingType: orders_1.SwapPricingType.Swap,
|
|
353
354
|
});
|
|
354
355
|
if (!resultWithImpact || !resultWithoutImpact) {
|
|
355
356
|
throw new Error("Results should be defined");
|
|
@@ -365,7 +366,7 @@ const mockMarketsInfoData = {
|
|
|
365
366
|
usdIn: 100n * dollar,
|
|
366
367
|
shouldUnwrapNativeToken: false,
|
|
367
368
|
shouldApplyPriceImpact: true,
|
|
368
|
-
|
|
369
|
+
swapPricingType: orders_1.SwapPricingType.Swap,
|
|
369
370
|
});
|
|
370
371
|
(0, vitest_1.expect)(result).toBeDefined();
|
|
371
372
|
(0, vitest_1.expect)(result?.swapSteps).toHaveLength(2);
|
|
@@ -390,7 +391,7 @@ const mockMarketsInfoData = {
|
|
|
390
391
|
usdIn: 100n * dollar,
|
|
391
392
|
shouldUnwrapNativeToken: false,
|
|
392
393
|
shouldApplyPriceImpact: true,
|
|
393
|
-
|
|
394
|
+
swapPricingType: orders_1.SwapPricingType.Swap,
|
|
394
395
|
});
|
|
395
396
|
(0, vitest_1.expect)(result).toBeUndefined();
|
|
396
397
|
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.buildReverseSwapStrategy = exports.buildSwapStrategy = void 0;
|
|
4
|
+
const orders_1 = require("../../types/orders");
|
|
4
5
|
const bigmath_1 = require("../bigmath");
|
|
5
6
|
const tokens_1 = require("../tokens");
|
|
6
7
|
const externalSwapPath_1 = require("./externalSwapPath");
|
|
@@ -13,7 +14,7 @@ const swapPath_1 = require("./swapPath");
|
|
|
13
14
|
3. Check if there is a combined swap strategy and return combinedSwap if there is [combinedSwap]
|
|
14
15
|
4. Return defaultSwapStrategy (noSwap) if there is no other swap strategy [noSwap]
|
|
15
16
|
*/
|
|
16
|
-
function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chainId, swapOptimizationOrder, externalSwapQuoteParams, }) {
|
|
17
|
+
function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chainId, swapOptimizationOrder, externalSwapQuoteParams, swapPricingType = orders_1.SwapPricingType.Swap, allowSameTokenSwap, }) {
|
|
17
18
|
const priceIn = tokenIn.prices.minPrice;
|
|
18
19
|
const usdIn = (0, tokens_1.convertToUsd)(amountIn, tokenIn.decimals, priceIn);
|
|
19
20
|
if (amountIn < 0n) {
|
|
@@ -31,7 +32,11 @@ function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chain
|
|
|
31
32
|
priceOut: tokenOut.prices.maxPrice,
|
|
32
33
|
feesUsd: 0n,
|
|
33
34
|
};
|
|
34
|
-
if ((
|
|
35
|
+
if ((!allowSameTokenSwap && (0, tokens_1.getIsEquivalentTokens)(tokenIn, tokenOut)) ||
|
|
36
|
+
(0, tokens_1.getIsWrap)(tokenIn, tokenOut) ||
|
|
37
|
+
(0, tokens_1.getIsUnwrap)(tokenIn, tokenOut) ||
|
|
38
|
+
(0, tokens_1.getIsStake)(tokenIn, tokenOut) ||
|
|
39
|
+
(0, tokens_1.getIsUnstake)(tokenIn, tokenOut)) {
|
|
35
40
|
return defaultSwapStrategy;
|
|
36
41
|
}
|
|
37
42
|
const findSwapPath = (0, swapPath_1.createFindSwapPath)({
|
|
@@ -39,7 +44,7 @@ function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chain
|
|
|
39
44
|
fromTokenAddress: tokenIn.address,
|
|
40
45
|
toTokenAddress: tokenOut.address,
|
|
41
46
|
marketsInfoData,
|
|
42
|
-
|
|
47
|
+
swapPricingType,
|
|
43
48
|
});
|
|
44
49
|
const swapPathStats = findSwapPath(usdIn, { order: swapOptimizationOrder });
|
|
45
50
|
if (swapPathStats) {
|
|
@@ -63,7 +68,7 @@ function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chain
|
|
|
63
68
|
fromTokenAddress: path.outTokenAddress,
|
|
64
69
|
toTokenAddress: tokenOut.address,
|
|
65
70
|
marketsInfoData,
|
|
66
|
-
|
|
71
|
+
swapPricingType,
|
|
67
72
|
});
|
|
68
73
|
const swapPathStats = findSwapPath(usdIn);
|
|
69
74
|
return Boolean(swapPathStats);
|
|
@@ -79,7 +84,7 @@ function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chain
|
|
|
79
84
|
fromTokenAddress: suitableSwapPath.outTokenAddress,
|
|
80
85
|
toTokenAddress: tokenOut.address,
|
|
81
86
|
marketsInfoData,
|
|
82
|
-
|
|
87
|
+
swapPricingType,
|
|
83
88
|
});
|
|
84
89
|
const swapPathStatsForCombinedSwap = externalSwapQuoteForCombinedSwap
|
|
85
90
|
? findSwapPathForSuitableSwapPath(externalSwapQuoteForCombinedSwap.usdOut)
|
|
@@ -103,7 +108,7 @@ function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chain
|
|
|
103
108
|
}
|
|
104
109
|
exports.buildSwapStrategy = buildSwapStrategy;
|
|
105
110
|
// Used for getting swap amounts by to value
|
|
106
|
-
function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoData, chainId, externalSwapQuoteParams, swapOptimizationOrder, }) {
|
|
111
|
+
function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoData, chainId, externalSwapQuoteParams, swapOptimizationOrder, swapPricingType, allowSameTokenSwap, }) {
|
|
107
112
|
const priceIn = (0, tokens_1.getMidPrice)(tokenIn.prices);
|
|
108
113
|
const priceOut = (0, tokens_1.getMidPrice)(tokenOut.prices);
|
|
109
114
|
const preferredUsdOut = (0, tokens_1.convertToUsd)(amountOut, tokenOut.decimals, (0, tokens_1.getMidPrice)(tokenOut.prices));
|
|
@@ -121,7 +126,11 @@ function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoDat
|
|
|
121
126
|
priceOut,
|
|
122
127
|
feesUsd: 0n,
|
|
123
128
|
};
|
|
124
|
-
if ((
|
|
129
|
+
if ((!allowSameTokenSwap && (0, tokens_1.getIsEquivalentTokens)(tokenIn, tokenOut)) ||
|
|
130
|
+
(0, tokens_1.getIsWrap)(tokenIn, tokenOut) ||
|
|
131
|
+
(0, tokens_1.getIsUnwrap)(tokenIn, tokenOut) ||
|
|
132
|
+
(0, tokens_1.getIsStake)(tokenIn, tokenOut) ||
|
|
133
|
+
(0, tokens_1.getIsUnstake)(tokenIn, tokenOut)) {
|
|
125
134
|
return defaultSwapStrategy;
|
|
126
135
|
}
|
|
127
136
|
const findSwapPath = (0, swapPath_1.createFindSwapPath)({
|
|
@@ -129,7 +138,7 @@ function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoDat
|
|
|
129
138
|
fromTokenAddress: tokenIn.address,
|
|
130
139
|
toTokenAddress: tokenOut.address,
|
|
131
140
|
marketsInfoData,
|
|
132
|
-
|
|
141
|
+
swapPricingType,
|
|
133
142
|
});
|
|
134
143
|
const approximateSwapPathStats = findSwapPath(approximateUsdIn, { order: swapOptimizationOrder });
|
|
135
144
|
if (approximateSwapPathStats) {
|
|
@@ -165,12 +174,12 @@ function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoDat
|
|
|
165
174
|
fromTokenAddress: tokenIn.address,
|
|
166
175
|
toTokenAddress: path.inTokenAddress,
|
|
167
176
|
marketsInfoData,
|
|
168
|
-
|
|
177
|
+
swapPricingType,
|
|
169
178
|
});
|
|
170
179
|
const swapPathStats = findSwapPath(approximateUsdIn);
|
|
171
180
|
return Boolean(swapPathStats);
|
|
172
181
|
});
|
|
173
|
-
if (suitableSwapPath) {
|
|
182
|
+
if (suitableSwapPath && externalSwapQuoteParams) {
|
|
174
183
|
const approximateExternalSwapQuoteForCombinedSwap = (0, externalSwapQuoteByPath_1.getExternalSwapQuoteByPath)({
|
|
175
184
|
amountIn: approximateAmountIn,
|
|
176
185
|
externalSwapPath: suitableSwapPath,
|
|
@@ -184,7 +193,7 @@ function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoDat
|
|
|
184
193
|
fromTokenAddress: tokenIn.address,
|
|
185
194
|
toTokenAddress: suitableSwapPath.inTokenAddress,
|
|
186
195
|
marketsInfoData,
|
|
187
|
-
|
|
196
|
+
swapPricingType,
|
|
188
197
|
});
|
|
189
198
|
const approximateSwapPathStatsForCombinedSwap = findSwapPathForSuitableSwapPath(approximateExternalSwapQuoteForCombinedSwap.usdOut);
|
|
190
199
|
if (!approximateSwapPathStatsForCombinedSwap) {
|
|
@@ -10,7 +10,7 @@ function findSwapPathsBetweenTokens(graph) {
|
|
|
10
10
|
swapRoutes[tokenAAddress] = {};
|
|
11
11
|
let empty = true;
|
|
12
12
|
for (const tokenBAddress of allTokens) {
|
|
13
|
-
if (
|
|
13
|
+
if (swapRoutes[tokenBAddress]?.[tokenAAddress]) {
|
|
14
14
|
continue;
|
|
15
15
|
}
|
|
16
16
|
const result = {};
|
|
@@ -46,9 +46,13 @@ function findSwapPathsBetweenTokens(graph) {
|
|
|
46
46
|
}
|
|
47
47
|
if (currentToken === tokenBAddress) {
|
|
48
48
|
const intermediateTokenPath = tokenPath.slice(1, -1);
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
const isValidSameTokenSwap = tokenAAddress === tokenBAddress && intermediateTokenPath.length > 0;
|
|
50
|
+
const isValidDifferentTokenSwap = tokenAAddress !== tokenBAddress;
|
|
51
|
+
if (isValidSameTokenSwap || isValidDifferentTokenSwap) {
|
|
52
|
+
const pathKey = intermediateTokenPath.join(",");
|
|
53
|
+
if (!result[pathKey]) {
|
|
54
|
+
result[pathKey] = intermediateTokenPath;
|
|
55
|
+
}
|
|
52
56
|
}
|
|
53
57
|
}
|
|
54
58
|
if (tokenPath.length >= constants_1.MAX_EDGE_PATH_LENGTH + 1) {
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.createFindSwapPath = exports.getWrappedAddress = void 0;
|
|
4
4
|
const markets_1 = require("../../configs/markets");
|
|
5
5
|
const tokens_1 = require("../../configs/tokens");
|
|
6
|
+
const orders_1 = require("../../types/orders");
|
|
6
7
|
const LruCache_1 = require("../LruCache");
|
|
7
8
|
const markets_2 = require("../markets");
|
|
8
9
|
const buildMarketsAdjacencyGraph_1 = require("./buildMarketsAdjacencyGraph");
|
|
@@ -30,7 +31,7 @@ function buildMarketAdjacencyGraph(chainId, disabledMarkets) {
|
|
|
30
31
|
}
|
|
31
32
|
const FALLBACK_FIND_SWAP_PATH = () => undefined;
|
|
32
33
|
const createFindSwapPath = (params) => {
|
|
33
|
-
const { chainId, fromTokenAddress, toTokenAddress, marketsInfoData, disabledMarkets, manualPath, gasEstimationParams,
|
|
34
|
+
const { chainId, fromTokenAddress, toTokenAddress, marketsInfoData, disabledMarkets, manualPath, gasEstimationParams, swapPricingType = orders_1.SwapPricingType.Swap, } = params;
|
|
34
35
|
const wrappedFromAddress = (0, exports.getWrappedAddress)(chainId, fromTokenAddress);
|
|
35
36
|
const wrappedToAddress = (0, exports.getWrappedAddress)(chainId, toTokenAddress);
|
|
36
37
|
const wrappedToken = (0, tokens_1.getWrappedToken)(chainId);
|
|
@@ -38,20 +39,19 @@ const createFindSwapPath = (params) => {
|
|
|
38
39
|
? (0, swapRouting_1.getTokenSwapPathsForTokenPairPrebuilt)(chainId, wrappedFromAddress, wrappedToAddress)
|
|
39
40
|
: [];
|
|
40
41
|
const finalDisabledMarkets = [...(disabledMarkets ?? [])];
|
|
41
|
-
if (
|
|
42
|
+
if (swapPricingType === orders_1.SwapPricingType.AtomicSwap) {
|
|
42
43
|
const expressSwapUnavailableMarkets = Object.values(marketsInfoData ?? {})
|
|
43
44
|
.filter((market) => !(0, markets_2.getIsMarketAvailableForExpressSwaps)(market))
|
|
44
45
|
.map((market) => market.marketTokenAddress);
|
|
45
46
|
finalDisabledMarkets.push(...expressSwapUnavailableMarkets);
|
|
46
47
|
}
|
|
47
|
-
const isAtomicSwap = Boolean(isExpressFeeSwap);
|
|
48
48
|
const marketAdjacencyGraph = buildMarketAdjacencyGraph(chainId, finalDisabledMarkets);
|
|
49
49
|
const cache = {};
|
|
50
50
|
if (!marketsInfoData) {
|
|
51
51
|
return FALLBACK_FIND_SWAP_PATH;
|
|
52
52
|
}
|
|
53
53
|
const marketEdgeLiquidityGetter = (0, swapRouting_1.createMarketEdgeLiquidityGetter)(marketsInfoData);
|
|
54
|
-
const naiveEstimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData,
|
|
54
|
+
const naiveEstimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData, swapPricingType);
|
|
55
55
|
const naiveNetworkEstimator = gasEstimationParams
|
|
56
56
|
? (0, swapRouting_1.createNaiveNetworkEstimator)({
|
|
57
57
|
gasLimits: gasEstimationParams.gasLimits,
|
|
@@ -60,7 +60,7 @@ const createFindSwapPath = (params) => {
|
|
|
60
60
|
chainId,
|
|
61
61
|
})
|
|
62
62
|
: undefined;
|
|
63
|
-
const estimator = (0, swapRouting_1.createSwapEstimator)(marketsInfoData,
|
|
63
|
+
const estimator = (0, swapRouting_1.createSwapEstimator)(marketsInfoData, swapPricingType);
|
|
64
64
|
const findSwapPath = (usdIn, opts) => {
|
|
65
65
|
if (tokenSwapPaths.length === 0 || !fromTokenAddress || !wrappedFromAddress || !wrappedToAddress) {
|
|
66
66
|
return undefined;
|
|
@@ -130,7 +130,7 @@ const createFindSwapPath = (params) => {
|
|
|
130
130
|
shouldUnwrapNativeToken: toTokenAddress === tokens_1.NATIVE_TOKEN_ADDRESS,
|
|
131
131
|
shouldApplyPriceImpact: true,
|
|
132
132
|
usdIn,
|
|
133
|
-
|
|
133
|
+
swapPricingType,
|
|
134
134
|
});
|
|
135
135
|
cache[cacheKey] = result;
|
|
136
136
|
return result;
|