@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
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import { USD_DECIMALS } from "../../../configs/factors";
|
|
3
3
|
import { NATIVE_TOKEN_ADDRESS } from "../../../configs/tokens";
|
|
4
|
+
import { SwapPricingType } from "../../../types/orders";
|
|
4
5
|
import { expandDecimals } from "../../numbers";
|
|
6
|
+
import { getSwapPathStats } from "..";
|
|
5
7
|
import { buildMarketsAdjacencyGraph } from "../buildMarketsAdjacencyGraph";
|
|
6
8
|
import { findSwapPathsBetweenTokens } from "../findSwapPathsBetweenTokens";
|
|
7
9
|
import { createMarketEdgeLiquidityGetter, createNaiveNetworkEstimator, createNaiveSwapEstimator, getMaxLiquidityMarketSwapPathFromTokenSwapPaths, getNaiveBestMarketSwapPathsFromTokenSwapPaths, getTokenSwapPathsForTokenPair, } from "../swapRouting";
|
|
@@ -10,6 +12,7 @@ const marketKeys = [
|
|
|
10
12
|
"ETH-ETH-USDC",
|
|
11
13
|
"BTC-BTC-USDC",
|
|
12
14
|
"BTC-BTC-DAI",
|
|
15
|
+
"BTC-SOL-DAI",
|
|
13
16
|
"SOL-SOL-USDC",
|
|
14
17
|
"SOL-ETH-USDC",
|
|
15
18
|
"SOL-BTC-USDC",
|
|
@@ -60,7 +63,7 @@ describe("mockRouting", () => {
|
|
|
60
63
|
},
|
|
61
64
|
});
|
|
62
65
|
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "DAI");
|
|
63
|
-
const estimator = createNaiveSwapEstimator(marketsInfoData,
|
|
66
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData, SwapPricingType.Swap);
|
|
64
67
|
const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
|
|
65
68
|
graph: marketAdjacencyGraph,
|
|
66
69
|
tokenSwapPaths,
|
|
@@ -94,7 +97,7 @@ describe("mockRouting", () => {
|
|
|
94
97
|
},
|
|
95
98
|
});
|
|
96
99
|
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "DAI");
|
|
97
|
-
const estimator = createNaiveSwapEstimator(marketsInfoData,
|
|
100
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData, SwapPricingType.Swap);
|
|
98
101
|
const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
|
|
99
102
|
graph: marketAdjacencyGraph,
|
|
100
103
|
tokenSwapPaths,
|
|
@@ -144,7 +147,7 @@ describe("mockRouting", () => {
|
|
|
144
147
|
},
|
|
145
148
|
});
|
|
146
149
|
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "BTC", "DAI");
|
|
147
|
-
const estimator = createNaiveSwapEstimator(marketsInfoData,
|
|
150
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData, SwapPricingType.Swap);
|
|
148
151
|
const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
|
|
149
152
|
graph: marketAdjacencyGraph,
|
|
150
153
|
tokenSwapPaths,
|
|
@@ -188,7 +191,7 @@ describe("mockRouting", () => {
|
|
|
188
191
|
},
|
|
189
192
|
});
|
|
190
193
|
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "BTC");
|
|
191
|
-
const estimator = createNaiveSwapEstimator(marketsInfoData,
|
|
194
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData, SwapPricingType.Swap);
|
|
192
195
|
const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
|
|
193
196
|
graph: marketAdjacencyGraph,
|
|
194
197
|
tokenSwapPaths,
|
|
@@ -239,7 +242,7 @@ describe("mockRouting", () => {
|
|
|
239
242
|
},
|
|
240
243
|
});
|
|
241
244
|
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "BTC");
|
|
242
|
-
const estimator = createNaiveSwapEstimator(marketsInfoData,
|
|
245
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData, SwapPricingType.Swap);
|
|
243
246
|
const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
|
|
244
247
|
graph: marketAdjacencyGraph,
|
|
245
248
|
tokenSwapPaths,
|
|
@@ -289,7 +292,7 @@ describe("mockRouting", () => {
|
|
|
289
292
|
},
|
|
290
293
|
});
|
|
291
294
|
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "BTC", "DAI");
|
|
292
|
-
const estimator = createNaiveSwapEstimator(marketsInfoData,
|
|
295
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData, SwapPricingType.Swap);
|
|
293
296
|
const fakeMultiplier = 100n;
|
|
294
297
|
const networkEstimator = createNaiveNetworkEstimator({
|
|
295
298
|
chainId: 1,
|
|
@@ -431,3 +434,258 @@ describe("getMaxLiquidityMarketSwapPathFromTokenSwapPaths", () => {
|
|
|
431
434
|
});
|
|
432
435
|
});
|
|
433
436
|
});
|
|
437
|
+
describe("same token arbitrage swaps", () => {
|
|
438
|
+
it("should yield profit (usdOut > usdIn) for USDC->USDC swap when markets have opposite imbalances", () => {
|
|
439
|
+
const USD_IN = expandDecimals(10000n, USD_DECIMALS);
|
|
440
|
+
const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
|
|
441
|
+
"ETH-ETH-USDC": {
|
|
442
|
+
longPoolAmount: usdToToken(30000, tokensData.ETH),
|
|
443
|
+
shortPoolAmount: usdToToken(170000, tokensData.USDC),
|
|
444
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.ETH),
|
|
445
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
446
|
+
},
|
|
447
|
+
"SOL-ETH-USDC": {
|
|
448
|
+
longPoolAmount: usdToToken(170000, tokensData.ETH),
|
|
449
|
+
shortPoolAmount: usdToToken(30000, tokensData.USDC),
|
|
450
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.ETH),
|
|
451
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
452
|
+
},
|
|
453
|
+
});
|
|
454
|
+
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "USDC");
|
|
455
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData, SwapPricingType.Swap);
|
|
456
|
+
const networkEstimator = createNaiveNetworkEstimator({
|
|
457
|
+
chainId: 1,
|
|
458
|
+
gasLimits: {
|
|
459
|
+
...baseGasLimits,
|
|
460
|
+
singleSwap: baseGasLimits.singleSwap * 3n,
|
|
461
|
+
},
|
|
462
|
+
gasPrice: baseGasPrice * 100n,
|
|
463
|
+
tokensData: {
|
|
464
|
+
...tokensData,
|
|
465
|
+
[NATIVE_TOKEN_ADDRESS]: {
|
|
466
|
+
...tokensData.ETH,
|
|
467
|
+
address: NATIVE_TOKEN_ADDRESS,
|
|
468
|
+
},
|
|
469
|
+
},
|
|
470
|
+
});
|
|
471
|
+
const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
|
|
472
|
+
graph: marketAdjacencyGraph,
|
|
473
|
+
tokenSwapPaths,
|
|
474
|
+
topPathsCount: 1,
|
|
475
|
+
tokenInAddress: "USDC",
|
|
476
|
+
tokenOutAddress: "USDC",
|
|
477
|
+
estimator,
|
|
478
|
+
usdIn: USD_IN,
|
|
479
|
+
networkEstimator,
|
|
480
|
+
});
|
|
481
|
+
const topPath = paths?.[0];
|
|
482
|
+
expect(topPath).toBeDefined();
|
|
483
|
+
expect(topPath?.length).toBe(2);
|
|
484
|
+
const swapPathStats = getSwapPathStats({
|
|
485
|
+
marketsInfoData,
|
|
486
|
+
swapPath: topPath,
|
|
487
|
+
initialCollateralAddress: "USDC",
|
|
488
|
+
wrappedNativeTokenAddress: "WETH",
|
|
489
|
+
usdIn: USD_IN,
|
|
490
|
+
shouldUnwrapNativeToken: false,
|
|
491
|
+
shouldApplyPriceImpact: true,
|
|
492
|
+
swapPricingType: SwapPricingType.Swap,
|
|
493
|
+
});
|
|
494
|
+
expect(swapPathStats).toBeDefined();
|
|
495
|
+
const { usdOut } = networkEstimator(swapPathStats.usdOut, topPath.length);
|
|
496
|
+
expect(usdOut).toBeGreaterThan(USD_IN);
|
|
497
|
+
});
|
|
498
|
+
it("should yield loss when markets are balanced", () => {
|
|
499
|
+
const USD_IN = expandDecimals(10000n, USD_DECIMALS);
|
|
500
|
+
const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
|
|
501
|
+
"ETH-ETH-USDC": {
|
|
502
|
+
longPoolAmount: usdToToken(100000, tokensData.ETH),
|
|
503
|
+
shortPoolAmount: usdToToken(100000, tokensData.USDC),
|
|
504
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.ETH),
|
|
505
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
506
|
+
},
|
|
507
|
+
"SOL-ETH-USDC": {
|
|
508
|
+
longPoolAmount: usdToToken(100000, tokensData.ETH),
|
|
509
|
+
shortPoolAmount: usdToToken(100000, tokensData.USDC),
|
|
510
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.ETH),
|
|
511
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
512
|
+
},
|
|
513
|
+
});
|
|
514
|
+
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "USDC");
|
|
515
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData, SwapPricingType.Swap);
|
|
516
|
+
const networkEstimator = createNaiveNetworkEstimator({
|
|
517
|
+
chainId: 1,
|
|
518
|
+
gasLimits: baseGasLimits,
|
|
519
|
+
gasPrice: baseGasPrice,
|
|
520
|
+
tokensData: {
|
|
521
|
+
...tokensData,
|
|
522
|
+
[NATIVE_TOKEN_ADDRESS]: {
|
|
523
|
+
...tokensData.ETH,
|
|
524
|
+
address: NATIVE_TOKEN_ADDRESS,
|
|
525
|
+
},
|
|
526
|
+
},
|
|
527
|
+
});
|
|
528
|
+
const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
|
|
529
|
+
graph: marketAdjacencyGraph,
|
|
530
|
+
tokenSwapPaths,
|
|
531
|
+
topPathsCount: 1,
|
|
532
|
+
tokenInAddress: "USDC",
|
|
533
|
+
tokenOutAddress: "USDC",
|
|
534
|
+
estimator,
|
|
535
|
+
usdIn: USD_IN,
|
|
536
|
+
networkEstimator,
|
|
537
|
+
});
|
|
538
|
+
const topPath = paths?.[0];
|
|
539
|
+
expect(topPath).toBeDefined();
|
|
540
|
+
expect(topPath?.length).toBeGreaterThan(0);
|
|
541
|
+
const swapPathStats = getSwapPathStats({
|
|
542
|
+
marketsInfoData,
|
|
543
|
+
swapPath: topPath,
|
|
544
|
+
initialCollateralAddress: "USDC",
|
|
545
|
+
wrappedNativeTokenAddress: "WETH",
|
|
546
|
+
usdIn: USD_IN,
|
|
547
|
+
shouldUnwrapNativeToken: false,
|
|
548
|
+
shouldApplyPriceImpact: true,
|
|
549
|
+
swapPricingType: SwapPricingType.Swap,
|
|
550
|
+
});
|
|
551
|
+
expect(swapPathStats).toBeDefined();
|
|
552
|
+
const { usdOut } = networkEstimator(swapPathStats.usdOut, topPath.length);
|
|
553
|
+
expect(usdOut).toBeLessThan(USD_IN);
|
|
554
|
+
});
|
|
555
|
+
it("should find most profitable arbitrage path", () => {
|
|
556
|
+
const USD_IN = expandDecimals(10000n, USD_DECIMALS);
|
|
557
|
+
const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
|
|
558
|
+
// Path 1: ETH-ETH-USDC -> SOL-ETH-USDC (moderate profit)
|
|
559
|
+
"ETH-ETH-USDC": {
|
|
560
|
+
longPoolAmount: usdToToken(50000, tokensData.ETH),
|
|
561
|
+
shortPoolAmount: usdToToken(150000, tokensData.USDC),
|
|
562
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.ETH),
|
|
563
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
564
|
+
},
|
|
565
|
+
"SOL-ETH-USDC": {
|
|
566
|
+
longPoolAmount: usdToToken(150000, tokensData.ETH),
|
|
567
|
+
shortPoolAmount: usdToToken(50000, tokensData.USDC),
|
|
568
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.ETH),
|
|
569
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
570
|
+
},
|
|
571
|
+
// Path 2: BTC-BTC-USDC -> SOL-BTC-USDC (higher profit)
|
|
572
|
+
"BTC-BTC-USDC": {
|
|
573
|
+
longPoolAmount: usdToToken(30000, tokensData.BTC),
|
|
574
|
+
shortPoolAmount: usdToToken(170000, tokensData.USDC),
|
|
575
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.BTC),
|
|
576
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
577
|
+
},
|
|
578
|
+
"SOL-BTC-USDC": {
|
|
579
|
+
longPoolAmount: usdToToken(170000, tokensData.BTC),
|
|
580
|
+
shortPoolAmount: usdToToken(30000, tokensData.USDC),
|
|
581
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.BTC),
|
|
582
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
583
|
+
},
|
|
584
|
+
});
|
|
585
|
+
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "USDC");
|
|
586
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData, SwapPricingType.Swap);
|
|
587
|
+
const networkEstimator = createNaiveNetworkEstimator({
|
|
588
|
+
chainId: 1,
|
|
589
|
+
gasLimits: baseGasLimits,
|
|
590
|
+
gasPrice: baseGasPrice,
|
|
591
|
+
tokensData: {
|
|
592
|
+
...tokensData,
|
|
593
|
+
[NATIVE_TOKEN_ADDRESS]: {
|
|
594
|
+
...tokensData.ETH,
|
|
595
|
+
address: NATIVE_TOKEN_ADDRESS,
|
|
596
|
+
},
|
|
597
|
+
},
|
|
598
|
+
});
|
|
599
|
+
const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
|
|
600
|
+
graph: marketAdjacencyGraph,
|
|
601
|
+
tokenSwapPaths,
|
|
602
|
+
topPathsCount: 1,
|
|
603
|
+
tokenInAddress: "USDC",
|
|
604
|
+
tokenOutAddress: "USDC",
|
|
605
|
+
estimator,
|
|
606
|
+
usdIn: USD_IN,
|
|
607
|
+
networkEstimator,
|
|
608
|
+
});
|
|
609
|
+
const topPath = paths?.[0];
|
|
610
|
+
expect(topPath).toBeDefined();
|
|
611
|
+
expect(topPath?.length).toBe(2);
|
|
612
|
+
const swapPathStats = getSwapPathStats({
|
|
613
|
+
marketsInfoData,
|
|
614
|
+
swapPath: topPath,
|
|
615
|
+
initialCollateralAddress: "USDC",
|
|
616
|
+
wrappedNativeTokenAddress: "WETH",
|
|
617
|
+
usdIn: USD_IN,
|
|
618
|
+
shouldUnwrapNativeToken: false,
|
|
619
|
+
shouldApplyPriceImpact: true,
|
|
620
|
+
swapPricingType: SwapPricingType.Swap,
|
|
621
|
+
});
|
|
622
|
+
expect(swapPathStats).toBeDefined();
|
|
623
|
+
const { usdOut } = networkEstimator(swapPathStats.usdOut, topPath.length);
|
|
624
|
+
expect(usdOut).toBeGreaterThan(USD_IN);
|
|
625
|
+
expect(topPath).toEqual(["SOL-BTC-USDC", "BTC-BTC-USDC"]);
|
|
626
|
+
});
|
|
627
|
+
it("should find longer arbitrage swap path with higher profit", () => {
|
|
628
|
+
const USD_IN = expandDecimals(5000n, USD_DECIMALS);
|
|
629
|
+
const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
|
|
630
|
+
// Path 1: USDC -> ETH -> USDC (2 markets, moderate profit)
|
|
631
|
+
"ETH-ETH-USDC": {
|
|
632
|
+
longPoolAmount: usdToToken(60000, tokensData.ETH),
|
|
633
|
+
shortPoolAmount: usdToToken(140000, tokensData.USDC),
|
|
634
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.ETH),
|
|
635
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
636
|
+
},
|
|
637
|
+
"SOL-ETH-USDC": {
|
|
638
|
+
longPoolAmount: usdToToken(140000, tokensData.ETH),
|
|
639
|
+
shortPoolAmount: usdToToken(60000, tokensData.USDC),
|
|
640
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.ETH),
|
|
641
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
642
|
+
},
|
|
643
|
+
// Path 2: USDC -> SOL -> DAI -> USDC (3 markets, higher profit)
|
|
644
|
+
"SOL-SOL-USDC": {
|
|
645
|
+
longPoolAmount: usdToToken(150000, tokensData.SOL),
|
|
646
|
+
shortPoolAmount: usdToToken(50000, tokensData.USDC),
|
|
647
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.SOL),
|
|
648
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
649
|
+
},
|
|
650
|
+
"BTC-SOL-DAI": {
|
|
651
|
+
longPoolAmount: usdToToken(50000, tokensData.SOL),
|
|
652
|
+
shortPoolAmount: usdToToken(150000, tokensData.DAI),
|
|
653
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.SOL),
|
|
654
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.DAI),
|
|
655
|
+
},
|
|
656
|
+
"SPOT-DAI-USDC": {
|
|
657
|
+
longPoolAmount: usdToToken(50000, tokensData.DAI),
|
|
658
|
+
shortPoolAmount: usdToToken(150000, tokensData.USDC),
|
|
659
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.DAI),
|
|
660
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
661
|
+
},
|
|
662
|
+
});
|
|
663
|
+
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "USDC");
|
|
664
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData, SwapPricingType.Swap);
|
|
665
|
+
const networkEstimator = createNaiveNetworkEstimator({
|
|
666
|
+
chainId: 1,
|
|
667
|
+
gasLimits: baseGasLimits,
|
|
668
|
+
gasPrice: baseGasPrice,
|
|
669
|
+
tokensData: {
|
|
670
|
+
...tokensData,
|
|
671
|
+
[NATIVE_TOKEN_ADDRESS]: {
|
|
672
|
+
...tokensData.ETH,
|
|
673
|
+
address: NATIVE_TOKEN_ADDRESS,
|
|
674
|
+
},
|
|
675
|
+
},
|
|
676
|
+
});
|
|
677
|
+
const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
|
|
678
|
+
graph: marketAdjacencyGraph,
|
|
679
|
+
tokenSwapPaths,
|
|
680
|
+
topPathsCount: 1,
|
|
681
|
+
tokenInAddress: "USDC",
|
|
682
|
+
tokenOutAddress: "USDC",
|
|
683
|
+
estimator,
|
|
684
|
+
usdIn: USD_IN,
|
|
685
|
+
networkEstimator,
|
|
686
|
+
});
|
|
687
|
+
const topPath = paths?.[0];
|
|
688
|
+
expect(topPath).toBeDefined();
|
|
689
|
+
expect(topPath.length).toEqual(3);
|
|
690
|
+
});
|
|
691
|
+
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import { USD_DECIMALS } from "../../../configs/factors";
|
|
3
|
+
import { SwapPricingType } from "../../../types/orders";
|
|
3
4
|
import { bigMath } from "../../bigmath";
|
|
4
5
|
import { mockMarketsInfoData, mockTokensData } from "../../../test/mock";
|
|
5
6
|
import { buildMarketsAdjacencyGraph } from "../buildMarketsAdjacencyGraph";
|
|
@@ -524,7 +525,7 @@ describe("createSwapEstimator", () => {
|
|
|
524
525
|
isDisabled: true,
|
|
525
526
|
},
|
|
526
527
|
});
|
|
527
|
-
const estimator = createSwapEstimator(marketsInfoData,
|
|
528
|
+
const estimator = createSwapEstimator(marketsInfoData, SwapPricingType.Swap);
|
|
528
529
|
const result = estimator({
|
|
529
530
|
marketAddress: "ETH-ETH-USDC",
|
|
530
531
|
from: "ETH",
|
|
@@ -538,7 +539,7 @@ describe("createSwapEstimator", () => {
|
|
|
538
539
|
const tokensData = mockTokensData();
|
|
539
540
|
const marketKeys = ["ETH-ETH-USDC"];
|
|
540
541
|
const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys);
|
|
541
|
-
const estimator = createSwapEstimator(marketsInfoData,
|
|
542
|
+
const estimator = createSwapEstimator(marketsInfoData, SwapPricingType.Swap);
|
|
542
543
|
const result = estimator({
|
|
543
544
|
marketAddress: "NON-EXISTENT",
|
|
544
545
|
from: "ETH",
|
|
@@ -588,7 +589,7 @@ describe("createNaiveSwapEstimator", () => {
|
|
|
588
589
|
isDisabled: true,
|
|
589
590
|
},
|
|
590
591
|
});
|
|
591
|
-
const estimator = createNaiveSwapEstimator(marketsInfoData,
|
|
592
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData, SwapPricingType.Swap);
|
|
592
593
|
const result = estimator({
|
|
593
594
|
marketAddress: "ETH-ETH-USDC",
|
|
594
595
|
from: "ETH",
|
|
@@ -602,7 +603,7 @@ describe("createNaiveSwapEstimator", () => {
|
|
|
602
603
|
const tokensData = mockTokensData();
|
|
603
604
|
const marketKeys = ["ETH-ETH-USDC"];
|
|
604
605
|
const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys);
|
|
605
|
-
const estimator = createNaiveSwapEstimator(marketsInfoData,
|
|
606
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData, SwapPricingType.Swap);
|
|
606
607
|
const result = estimator({
|
|
607
608
|
marketAddress: "NON-EXISTENT",
|
|
608
609
|
from: "ETH",
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import { USD_DECIMALS } from "../../../configs/factors";
|
|
3
3
|
import { NATIVE_TOKEN_ADDRESS } from "../../../configs/tokens";
|
|
4
|
+
import { SwapPricingType } from "../../../types/orders";
|
|
4
5
|
import { mockMarketsInfoData as createMockMarketsInfoData, mockTokensData, usdToToken } from "../../../test/mock";
|
|
5
6
|
import { getSwapPathOutputAddresses, getSwapPathStats } from "../swapStats";
|
|
6
7
|
const someWrappedToken = "0x0000000000000000000000000000000000000001";
|
|
@@ -253,7 +254,7 @@ describe("getSwapPathStats", () => {
|
|
|
253
254
|
usdIn: 100n * dollar,
|
|
254
255
|
shouldUnwrapNativeToken: false,
|
|
255
256
|
shouldApplyPriceImpact: true,
|
|
256
|
-
|
|
257
|
+
swapPricingType: SwapPricingType.Swap,
|
|
257
258
|
});
|
|
258
259
|
expect(result).toBeUndefined();
|
|
259
260
|
});
|
|
@@ -266,7 +267,7 @@ describe("getSwapPathStats", () => {
|
|
|
266
267
|
usdIn: 100n * dollar,
|
|
267
268
|
shouldUnwrapNativeToken: false,
|
|
268
269
|
shouldApplyPriceImpact: true,
|
|
269
|
-
|
|
270
|
+
swapPricingType: SwapPricingType.Swap,
|
|
270
271
|
});
|
|
271
272
|
expect(result).toBeDefined();
|
|
272
273
|
expect(result?.swapPath).toEqual(["ETH-WETH-USDC"]);
|
|
@@ -288,7 +289,7 @@ describe("getSwapPathStats", () => {
|
|
|
288
289
|
usdIn: 100n * dollar,
|
|
289
290
|
shouldUnwrapNativeToken: false,
|
|
290
291
|
shouldApplyPriceImpact: true,
|
|
291
|
-
|
|
292
|
+
swapPricingType: SwapPricingType.Swap,
|
|
292
293
|
});
|
|
293
294
|
expect(result).toBeDefined();
|
|
294
295
|
expect(result?.swapPath).toEqual(["BTC-BTC-WETH", "BTC-BTC-USDC"]);
|
|
@@ -310,7 +311,7 @@ describe("getSwapPathStats", () => {
|
|
|
310
311
|
usdIn: 100n * dollar,
|
|
311
312
|
shouldUnwrapNativeToken: true,
|
|
312
313
|
shouldApplyPriceImpact: true,
|
|
313
|
-
|
|
314
|
+
swapPricingType: SwapPricingType.Swap,
|
|
314
315
|
});
|
|
315
316
|
expect(result).toBeDefined();
|
|
316
317
|
expect(result?.tokenOutAddress).toBe(NATIVE_TOKEN_ADDRESS);
|
|
@@ -324,7 +325,7 @@ describe("getSwapPathStats", () => {
|
|
|
324
325
|
usdIn: 100n * dollar,
|
|
325
326
|
shouldUnwrapNativeToken: false,
|
|
326
327
|
shouldApplyPriceImpact: true,
|
|
327
|
-
|
|
328
|
+
swapPricingType: SwapPricingType.Swap,
|
|
328
329
|
});
|
|
329
330
|
expect(result).toBeUndefined();
|
|
330
331
|
});
|
|
@@ -337,7 +338,7 @@ describe("getSwapPathStats", () => {
|
|
|
337
338
|
usdIn: 100n * dollar,
|
|
338
339
|
shouldUnwrapNativeToken: false,
|
|
339
340
|
shouldApplyPriceImpact: true,
|
|
340
|
-
|
|
341
|
+
swapPricingType: SwapPricingType.Swap,
|
|
341
342
|
});
|
|
342
343
|
const resultWithoutImpact = getSwapPathStats({
|
|
343
344
|
marketsInfoData: testMarketsInfoData,
|
|
@@ -347,7 +348,7 @@ describe("getSwapPathStats", () => {
|
|
|
347
348
|
usdIn: 100n * dollar,
|
|
348
349
|
shouldUnwrapNativeToken: false,
|
|
349
350
|
shouldApplyPriceImpact: false,
|
|
350
|
-
|
|
351
|
+
swapPricingType: SwapPricingType.Swap,
|
|
351
352
|
});
|
|
352
353
|
if (!resultWithImpact || !resultWithoutImpact) {
|
|
353
354
|
throw new Error("Results should be defined");
|
|
@@ -363,7 +364,7 @@ describe("getSwapPathStats", () => {
|
|
|
363
364
|
usdIn: 100n * dollar,
|
|
364
365
|
shouldUnwrapNativeToken: false,
|
|
365
366
|
shouldApplyPriceImpact: true,
|
|
366
|
-
|
|
367
|
+
swapPricingType: SwapPricingType.Swap,
|
|
367
368
|
});
|
|
368
369
|
expect(result).toBeDefined();
|
|
369
370
|
expect(result?.swapSteps).toHaveLength(2);
|
|
@@ -388,7 +389,7 @@ describe("getSwapPathStats", () => {
|
|
|
388
389
|
usdIn: 100n * dollar,
|
|
389
390
|
shouldUnwrapNativeToken: false,
|
|
390
391
|
shouldApplyPriceImpact: true,
|
|
391
|
-
|
|
392
|
+
swapPricingType: SwapPricingType.Swap,
|
|
392
393
|
});
|
|
393
394
|
expect(result).toBeUndefined();
|
|
394
395
|
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { SwapPricingType } from "../../types/orders";
|
|
1
2
|
import { bigMath } from "../bigmath";
|
|
2
|
-
import { convertToTokenAmount, convertToUsd, getIsEquivalentTokens, getIsStake, getIsUnstake, getMidPrice, } from "../tokens";
|
|
3
|
+
import { convertToTokenAmount, convertToUsd, getIsEquivalentTokens, getIsStake, getIsUnstake, getIsUnwrap, getIsWrap, getMidPrice, } from "../tokens";
|
|
3
4
|
import { getAvailableExternalSwapPaths } from "./externalSwapPath";
|
|
4
5
|
import { getExternalSwapQuoteByPath } from "./externalSwapQuoteByPath";
|
|
5
6
|
import { createFindSwapPath } from "./swapPath";
|
|
@@ -10,7 +11,7 @@ import { createFindSwapPath } from "./swapPath";
|
|
|
10
11
|
3. Check if there is a combined swap strategy and return combinedSwap if there is [combinedSwap]
|
|
11
12
|
4. Return defaultSwapStrategy (noSwap) if there is no other swap strategy [noSwap]
|
|
12
13
|
*/
|
|
13
|
-
export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chainId, swapOptimizationOrder, externalSwapQuoteParams, }) {
|
|
14
|
+
export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chainId, swapOptimizationOrder, externalSwapQuoteParams, swapPricingType = SwapPricingType.Swap, allowSameTokenSwap, }) {
|
|
14
15
|
const priceIn = tokenIn.prices.minPrice;
|
|
15
16
|
const usdIn = convertToUsd(amountIn, tokenIn.decimals, priceIn);
|
|
16
17
|
if (amountIn < 0n) {
|
|
@@ -28,7 +29,11 @@ export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData
|
|
|
28
29
|
priceOut: tokenOut.prices.maxPrice,
|
|
29
30
|
feesUsd: 0n,
|
|
30
31
|
};
|
|
31
|
-
if (
|
|
32
|
+
if ((!allowSameTokenSwap && getIsEquivalentTokens(tokenIn, tokenOut)) ||
|
|
33
|
+
getIsWrap(tokenIn, tokenOut) ||
|
|
34
|
+
getIsUnwrap(tokenIn, tokenOut) ||
|
|
35
|
+
getIsStake(tokenIn, tokenOut) ||
|
|
36
|
+
getIsUnstake(tokenIn, tokenOut)) {
|
|
32
37
|
return defaultSwapStrategy;
|
|
33
38
|
}
|
|
34
39
|
const findSwapPath = createFindSwapPath({
|
|
@@ -36,7 +41,7 @@ export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData
|
|
|
36
41
|
fromTokenAddress: tokenIn.address,
|
|
37
42
|
toTokenAddress: tokenOut.address,
|
|
38
43
|
marketsInfoData,
|
|
39
|
-
|
|
44
|
+
swapPricingType,
|
|
40
45
|
});
|
|
41
46
|
const swapPathStats = findSwapPath(usdIn, { order: swapOptimizationOrder });
|
|
42
47
|
if (swapPathStats) {
|
|
@@ -60,7 +65,7 @@ export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData
|
|
|
60
65
|
fromTokenAddress: path.outTokenAddress,
|
|
61
66
|
toTokenAddress: tokenOut.address,
|
|
62
67
|
marketsInfoData,
|
|
63
|
-
|
|
68
|
+
swapPricingType,
|
|
64
69
|
});
|
|
65
70
|
const swapPathStats = findSwapPath(usdIn);
|
|
66
71
|
return Boolean(swapPathStats);
|
|
@@ -76,7 +81,7 @@ export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData
|
|
|
76
81
|
fromTokenAddress: suitableSwapPath.outTokenAddress,
|
|
77
82
|
toTokenAddress: tokenOut.address,
|
|
78
83
|
marketsInfoData,
|
|
79
|
-
|
|
84
|
+
swapPricingType,
|
|
80
85
|
});
|
|
81
86
|
const swapPathStatsForCombinedSwap = externalSwapQuoteForCombinedSwap
|
|
82
87
|
? findSwapPathForSuitableSwapPath(externalSwapQuoteForCombinedSwap.usdOut)
|
|
@@ -99,7 +104,7 @@ export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData
|
|
|
99
104
|
return defaultSwapStrategy;
|
|
100
105
|
}
|
|
101
106
|
// Used for getting swap amounts by to value
|
|
102
|
-
export function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoData, chainId, externalSwapQuoteParams, swapOptimizationOrder, }) {
|
|
107
|
+
export function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoData, chainId, externalSwapQuoteParams, swapOptimizationOrder, swapPricingType, allowSameTokenSwap, }) {
|
|
103
108
|
const priceIn = getMidPrice(tokenIn.prices);
|
|
104
109
|
const priceOut = getMidPrice(tokenOut.prices);
|
|
105
110
|
const preferredUsdOut = convertToUsd(amountOut, tokenOut.decimals, getMidPrice(tokenOut.prices));
|
|
@@ -117,7 +122,11 @@ export function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, markets
|
|
|
117
122
|
priceOut,
|
|
118
123
|
feesUsd: 0n,
|
|
119
124
|
};
|
|
120
|
-
if (
|
|
125
|
+
if ((!allowSameTokenSwap && getIsEquivalentTokens(tokenIn, tokenOut)) ||
|
|
126
|
+
getIsWrap(tokenIn, tokenOut) ||
|
|
127
|
+
getIsUnwrap(tokenIn, tokenOut) ||
|
|
128
|
+
getIsStake(tokenIn, tokenOut) ||
|
|
129
|
+
getIsUnstake(tokenIn, tokenOut)) {
|
|
121
130
|
return defaultSwapStrategy;
|
|
122
131
|
}
|
|
123
132
|
const findSwapPath = createFindSwapPath({
|
|
@@ -125,7 +134,7 @@ export function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, markets
|
|
|
125
134
|
fromTokenAddress: tokenIn.address,
|
|
126
135
|
toTokenAddress: tokenOut.address,
|
|
127
136
|
marketsInfoData,
|
|
128
|
-
|
|
137
|
+
swapPricingType,
|
|
129
138
|
});
|
|
130
139
|
const approximateSwapPathStats = findSwapPath(approximateUsdIn, { order: swapOptimizationOrder });
|
|
131
140
|
if (approximateSwapPathStats) {
|
|
@@ -161,12 +170,12 @@ export function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, markets
|
|
|
161
170
|
fromTokenAddress: tokenIn.address,
|
|
162
171
|
toTokenAddress: path.inTokenAddress,
|
|
163
172
|
marketsInfoData,
|
|
164
|
-
|
|
173
|
+
swapPricingType,
|
|
165
174
|
});
|
|
166
175
|
const swapPathStats = findSwapPath(approximateUsdIn);
|
|
167
176
|
return Boolean(swapPathStats);
|
|
168
177
|
});
|
|
169
|
-
if (suitableSwapPath) {
|
|
178
|
+
if (suitableSwapPath && externalSwapQuoteParams) {
|
|
170
179
|
const approximateExternalSwapQuoteForCombinedSwap = getExternalSwapQuoteByPath({
|
|
171
180
|
amountIn: approximateAmountIn,
|
|
172
181
|
externalSwapPath: suitableSwapPath,
|
|
@@ -180,7 +189,7 @@ export function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, markets
|
|
|
180
189
|
fromTokenAddress: tokenIn.address,
|
|
181
190
|
toTokenAddress: suitableSwapPath.inTokenAddress,
|
|
182
191
|
marketsInfoData,
|
|
183
|
-
|
|
192
|
+
swapPricingType,
|
|
184
193
|
});
|
|
185
194
|
const approximateSwapPathStatsForCombinedSwap = findSwapPathForSuitableSwapPath(approximateExternalSwapQuoteForCombinedSwap.usdOut);
|
|
186
195
|
if (!approximateSwapPathStatsForCombinedSwap) {
|
|
@@ -7,7 +7,7 @@ export function findSwapPathsBetweenTokens(graph) {
|
|
|
7
7
|
swapRoutes[tokenAAddress] = {};
|
|
8
8
|
let empty = true;
|
|
9
9
|
for (const tokenBAddress of allTokens) {
|
|
10
|
-
if (
|
|
10
|
+
if (swapRoutes[tokenBAddress]?.[tokenAAddress]) {
|
|
11
11
|
continue;
|
|
12
12
|
}
|
|
13
13
|
const result = {};
|
|
@@ -43,9 +43,13 @@ export function findSwapPathsBetweenTokens(graph) {
|
|
|
43
43
|
}
|
|
44
44
|
if (currentToken === tokenBAddress) {
|
|
45
45
|
const intermediateTokenPath = tokenPath.slice(1, -1);
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
const isValidSameTokenSwap = tokenAAddress === tokenBAddress && intermediateTokenPath.length > 0;
|
|
47
|
+
const isValidDifferentTokenSwap = tokenAAddress !== tokenBAddress;
|
|
48
|
+
if (isValidSameTokenSwap || isValidDifferentTokenSwap) {
|
|
49
|
+
const pathKey = intermediateTokenPath.join(",");
|
|
50
|
+
if (!result[pathKey]) {
|
|
51
|
+
result[pathKey] = intermediateTokenPath;
|
|
52
|
+
}
|
|
49
53
|
}
|
|
50
54
|
}
|
|
51
55
|
if (tokenPath.length >= MAX_EDGE_PATH_LENGTH + 1) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { MARKETS } from "../../configs/markets";
|
|
2
2
|
import { convertTokenAddress, getWrappedToken, NATIVE_TOKEN_ADDRESS } from "../../configs/tokens";
|
|
3
|
+
import { SwapPricingType } from "../../types/orders";
|
|
3
4
|
import { LRUCache } from "../LruCache";
|
|
4
5
|
import { getIsMarketAvailableForExpressSwaps } from "../markets";
|
|
5
6
|
import { buildMarketsAdjacencyGraph } from "./buildMarketsAdjacencyGraph";
|
|
@@ -26,7 +27,7 @@ function buildMarketAdjacencyGraph(chainId, disabledMarkets) {
|
|
|
26
27
|
}
|
|
27
28
|
const FALLBACK_FIND_SWAP_PATH = () => undefined;
|
|
28
29
|
export const createFindSwapPath = (params) => {
|
|
29
|
-
const { chainId, fromTokenAddress, toTokenAddress, marketsInfoData, disabledMarkets, manualPath, gasEstimationParams,
|
|
30
|
+
const { chainId, fromTokenAddress, toTokenAddress, marketsInfoData, disabledMarkets, manualPath, gasEstimationParams, swapPricingType = SwapPricingType.Swap, } = params;
|
|
30
31
|
const wrappedFromAddress = getWrappedAddress(chainId, fromTokenAddress);
|
|
31
32
|
const wrappedToAddress = getWrappedAddress(chainId, toTokenAddress);
|
|
32
33
|
const wrappedToken = getWrappedToken(chainId);
|
|
@@ -34,20 +35,19 @@ export const createFindSwapPath = (params) => {
|
|
|
34
35
|
? getTokenSwapPathsForTokenPairPrebuilt(chainId, wrappedFromAddress, wrappedToAddress)
|
|
35
36
|
: [];
|
|
36
37
|
const finalDisabledMarkets = [...(disabledMarkets ?? [])];
|
|
37
|
-
if (
|
|
38
|
+
if (swapPricingType === SwapPricingType.AtomicSwap) {
|
|
38
39
|
const expressSwapUnavailableMarkets = Object.values(marketsInfoData ?? {})
|
|
39
40
|
.filter((market) => !getIsMarketAvailableForExpressSwaps(market))
|
|
40
41
|
.map((market) => market.marketTokenAddress);
|
|
41
42
|
finalDisabledMarkets.push(...expressSwapUnavailableMarkets);
|
|
42
43
|
}
|
|
43
|
-
const isAtomicSwap = Boolean(isExpressFeeSwap);
|
|
44
44
|
const marketAdjacencyGraph = buildMarketAdjacencyGraph(chainId, finalDisabledMarkets);
|
|
45
45
|
const cache = {};
|
|
46
46
|
if (!marketsInfoData) {
|
|
47
47
|
return FALLBACK_FIND_SWAP_PATH;
|
|
48
48
|
}
|
|
49
49
|
const marketEdgeLiquidityGetter = createMarketEdgeLiquidityGetter(marketsInfoData);
|
|
50
|
-
const naiveEstimator = createNaiveSwapEstimator(marketsInfoData,
|
|
50
|
+
const naiveEstimator = createNaiveSwapEstimator(marketsInfoData, swapPricingType);
|
|
51
51
|
const naiveNetworkEstimator = gasEstimationParams
|
|
52
52
|
? createNaiveNetworkEstimator({
|
|
53
53
|
gasLimits: gasEstimationParams.gasLimits,
|
|
@@ -56,7 +56,7 @@ export const createFindSwapPath = (params) => {
|
|
|
56
56
|
chainId,
|
|
57
57
|
})
|
|
58
58
|
: undefined;
|
|
59
|
-
const estimator = createSwapEstimator(marketsInfoData,
|
|
59
|
+
const estimator = createSwapEstimator(marketsInfoData, swapPricingType);
|
|
60
60
|
const findSwapPath = (usdIn, opts) => {
|
|
61
61
|
if (tokenSwapPaths.length === 0 || !fromTokenAddress || !wrappedFromAddress || !wrappedToAddress) {
|
|
62
62
|
return undefined;
|
|
@@ -126,7 +126,7 @@ export const createFindSwapPath = (params) => {
|
|
|
126
126
|
shouldUnwrapNativeToken: toTokenAddress === NATIVE_TOKEN_ADDRESS,
|
|
127
127
|
shouldApplyPriceImpact: true,
|
|
128
128
|
usdIn,
|
|
129
|
-
|
|
129
|
+
swapPricingType,
|
|
130
130
|
});
|
|
131
131
|
cache[cacheKey] = result;
|
|
132
132
|
return result;
|