@gmx-io/sdk 1.4.1 → 2.0.0-alpha-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/src/configs/dataStore.js +6 -2
- package/build/cjs/src/configs/markets.js +35 -0
- package/build/cjs/src/configs/tokens.js +48 -169
- package/build/cjs/src/modules/orders/helpers.js +2 -0
- package/build/cjs/src/prebuilt/hashedKinkModelMarketRatesKeys.json +40 -0
- package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +285 -0
- package/build/cjs/src/prebuilt/hashedMarketValuesKeys.json +75 -0
- package/build/cjs/src/utils/multicall.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 +257 -0
- package/build/cjs/src/utils/swap/buildSwapStrategy.js +12 -4
- package/build/cjs/src/utils/swap/findSwapPathsBetweenTokens.js +8 -4
- package/build/cjs/src/utils/swap/swapValues.js +13 -6
- package/build/cjs/src/utils/trade/increase.js +3 -0
- package/build/esm/src/configs/dataStore.js +3 -0
- package/build/esm/src/configs/markets.js +35 -0
- package/build/esm/src/configs/tokens.js +48 -169
- package/build/esm/src/modules/orders/helpers.js +2 -0
- package/build/esm/src/prebuilt/hashedKinkModelMarketRatesKeys.json +40 -0
- package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +285 -0
- package/build/esm/src/prebuilt/hashedMarketValuesKeys.json +75 -0
- package/build/esm/src/utils/multicall.js +2 -2
- package/build/esm/src/utils/swap/__tests__/findSwapPathsBetweenTokens.spec.js +22 -0
- package/build/esm/src/utils/swap/__tests__/mockRouting.spec.js +257 -0
- package/build/esm/src/utils/swap/buildSwapStrategy.js +13 -5
- package/build/esm/src/utils/swap/findSwapPathsBetweenTokens.js +8 -4
- package/build/esm/src/utils/swap/swapValues.js +14 -7
- package/build/esm/src/utils/trade/increase.js +3 -0
- package/build/types/src/configs/dataStore.d.ts +1 -0
- package/build/types/src/modules/markets/query-builders.d.ts +1 -1
- package/build/types/src/types/markets.d.ts +1 -0
- package/build/types/src/types/subsquid.d.ts +13 -0
- package/build/types/src/types/tokens.d.ts +0 -3
- package/build/types/src/utils/multicall.d.ts +2 -2
- package/build/types/src/utils/swap/buildSwapStrategy.d.ts +4 -2
- package/build/types/src/utils/swap/swapValues.d.ts +2 -0
- package/package.json +14 -6
|
@@ -5,6 +5,7 @@ const factors_1 = require("../../../configs/factors");
|
|
|
5
5
|
const tokens_1 = require("../../../configs/tokens");
|
|
6
6
|
const orders_1 = require("../../../types/orders");
|
|
7
7
|
const numbers_1 = require("../../numbers");
|
|
8
|
+
const swap_1 = require("..");
|
|
8
9
|
const buildMarketsAdjacencyGraph_1 = require("../buildMarketsAdjacencyGraph");
|
|
9
10
|
const findSwapPathsBetweenTokens_1 = require("../findSwapPathsBetweenTokens");
|
|
10
11
|
const swapRouting_1 = require("../swapRouting");
|
|
@@ -13,6 +14,7 @@ const marketKeys = [
|
|
|
13
14
|
"ETH-ETH-USDC",
|
|
14
15
|
"BTC-BTC-USDC",
|
|
15
16
|
"BTC-BTC-DAI",
|
|
17
|
+
"BTC-SOL-DAI",
|
|
16
18
|
"SOL-SOL-USDC",
|
|
17
19
|
"SOL-ETH-USDC",
|
|
18
20
|
"SOL-BTC-USDC",
|
|
@@ -434,3 +436,258 @@ const baseGasPrice = 1650000002n;
|
|
|
434
436
|
});
|
|
435
437
|
});
|
|
436
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
|
+
});
|
|
@@ -14,7 +14,7 @@ const swapPath_1 = require("./swapPath");
|
|
|
14
14
|
3. Check if there is a combined swap strategy and return combinedSwap if there is [combinedSwap]
|
|
15
15
|
4. Return defaultSwapStrategy (noSwap) if there is no other swap strategy [noSwap]
|
|
16
16
|
*/
|
|
17
|
-
function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chainId, swapOptimizationOrder, externalSwapQuoteParams, swapPricingType = orders_1.SwapPricingType.Swap, }) {
|
|
17
|
+
function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chainId, swapOptimizationOrder, externalSwapQuoteParams, swapPricingType = orders_1.SwapPricingType.Swap, allowSameTokenSwap, }) {
|
|
18
18
|
const priceIn = tokenIn.prices.minPrice;
|
|
19
19
|
const usdIn = (0, tokens_1.convertToUsd)(amountIn, tokenIn.decimals, priceIn);
|
|
20
20
|
if (amountIn < 0n) {
|
|
@@ -32,7 +32,11 @@ function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chain
|
|
|
32
32
|
priceOut: tokenOut.prices.maxPrice,
|
|
33
33
|
feesUsd: 0n,
|
|
34
34
|
};
|
|
35
|
-
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)) {
|
|
36
40
|
return defaultSwapStrategy;
|
|
37
41
|
}
|
|
38
42
|
const findSwapPath = (0, swapPath_1.createFindSwapPath)({
|
|
@@ -104,7 +108,7 @@ function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chain
|
|
|
104
108
|
}
|
|
105
109
|
exports.buildSwapStrategy = buildSwapStrategy;
|
|
106
110
|
// Used for getting swap amounts by to value
|
|
107
|
-
function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoData, chainId, externalSwapQuoteParams, swapOptimizationOrder, swapPricingType, }) {
|
|
111
|
+
function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoData, chainId, externalSwapQuoteParams, swapOptimizationOrder, swapPricingType, allowSameTokenSwap, }) {
|
|
108
112
|
const priceIn = (0, tokens_1.getMidPrice)(tokenIn.prices);
|
|
109
113
|
const priceOut = (0, tokens_1.getMidPrice)(tokenOut.prices);
|
|
110
114
|
const preferredUsdOut = (0, tokens_1.convertToUsd)(amountOut, tokenOut.decimals, (0, tokens_1.getMidPrice)(tokenOut.prices));
|
|
@@ -122,7 +126,11 @@ function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoDat
|
|
|
122
126
|
priceOut,
|
|
123
127
|
feesUsd: 0n,
|
|
124
128
|
};
|
|
125
|
-
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)) {
|
|
126
134
|
return defaultSwapStrategy;
|
|
127
135
|
}
|
|
128
136
|
const findSwapPath = (0, swapPath_1.createFindSwapPath)({
|
|
@@ -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) {
|
|
@@ -9,7 +9,7 @@ const numbers_1 = require("../numbers");
|
|
|
9
9
|
const tokens_1 = require("../tokens");
|
|
10
10
|
const buildSwapStrategy_1 = require("./buildSwapStrategy");
|
|
11
11
|
function getSwapAmountsByFromValue(p) {
|
|
12
|
-
const { tokenIn, tokenOut, amountIn, triggerRatio, isLimit, swapOptimizationOrder, uiFeeFactor, allowedSwapSlippageBps, marketsInfoData, chainId, externalSwapQuoteParams, } = p;
|
|
12
|
+
const { tokenIn, tokenOut, amountIn, triggerRatio, isLimit, swapOptimizationOrder, uiFeeFactor, allowedSwapSlippageBps, marketsInfoData, chainId, externalSwapQuoteParams, allowSameTokenSwap, } = p;
|
|
13
13
|
if (!externalSwapQuoteParams) {
|
|
14
14
|
return getSwapAmountsByFromValueDefault(p);
|
|
15
15
|
}
|
|
@@ -22,6 +22,7 @@ function getSwapAmountsByFromValue(p) {
|
|
|
22
22
|
swapOptimizationOrder,
|
|
23
23
|
externalSwapQuoteParams,
|
|
24
24
|
swapPricingType: orders_1.SwapPricingType.Swap,
|
|
25
|
+
allowSameTokenSwap,
|
|
25
26
|
});
|
|
26
27
|
const swapPathStats = swapStrategy.swapPathStats;
|
|
27
28
|
const totalSwapVolume = (0, fees_1.getTotalSwapVolumeFromSwapStats)(swapPathStats?.swapSteps);
|
|
@@ -79,7 +80,7 @@ function getSwapAmountsByFromValue(p) {
|
|
|
79
80
|
}
|
|
80
81
|
exports.getSwapAmountsByFromValue = getSwapAmountsByFromValue;
|
|
81
82
|
function getSwapAmountsByToValue(p) {
|
|
82
|
-
const { tokenIn, tokenOut, amountOut, triggerRatio, isLimit, uiFeeFactor, swapOptimizationOrder, allowedSwapSlippageBps, marketsInfoData, chainId, externalSwapQuoteParams, } = p;
|
|
83
|
+
const { tokenIn, tokenOut, amountOut, triggerRatio, isLimit, uiFeeFactor, swapOptimizationOrder, allowedSwapSlippageBps, marketsInfoData, chainId, externalSwapQuoteParams, allowSameTokenSwap, } = p;
|
|
83
84
|
if (!externalSwapQuoteParams) {
|
|
84
85
|
return getSwapAmountsByToValueDefault(p);
|
|
85
86
|
}
|
|
@@ -92,6 +93,7 @@ function getSwapAmountsByToValue(p) {
|
|
|
92
93
|
externalSwapQuoteParams,
|
|
93
94
|
swapOptimizationOrder,
|
|
94
95
|
swapPricingType: orders_1.SwapPricingType.Swap,
|
|
96
|
+
allowSameTokenSwap,
|
|
95
97
|
});
|
|
96
98
|
const swapStrategy = (0, buildSwapStrategy_1.buildSwapStrategy)({
|
|
97
99
|
amountIn: swapStrategyReverse.amountIn,
|
|
@@ -102,6 +104,7 @@ function getSwapAmountsByToValue(p) {
|
|
|
102
104
|
swapOptimizationOrder,
|
|
103
105
|
externalSwapQuoteParams,
|
|
104
106
|
swapPricingType: orders_1.SwapPricingType.Swap,
|
|
107
|
+
allowSameTokenSwap,
|
|
105
108
|
});
|
|
106
109
|
const uiFeeUsd = (0, numbers_1.applyFactor)(swapStrategy.usdIn, uiFeeFactor);
|
|
107
110
|
const defaultAmounts = {
|
|
@@ -159,7 +162,7 @@ function getSwapAmountsByToValue(p) {
|
|
|
159
162
|
}
|
|
160
163
|
exports.getSwapAmountsByToValue = getSwapAmountsByToValue;
|
|
161
164
|
function getSwapAmountsByFromValueDefault(p) {
|
|
162
|
-
const { tokenIn, tokenOut, amountIn, triggerRatio, isLimit, swapOptimizationOrder, findSwapPath, uiFeeFactor, allowedSwapSlippageBps, } = p;
|
|
165
|
+
const { tokenIn, tokenOut, amountIn, triggerRatio, isLimit, swapOptimizationOrder, findSwapPath, uiFeeFactor, allowedSwapSlippageBps, allowSameTokenSwap, } = p;
|
|
163
166
|
const priceIn = tokenIn.prices.minPrice;
|
|
164
167
|
const priceOut = tokenOut.prices.maxPrice;
|
|
165
168
|
const usdIn = (0, tokens_1.convertToUsd)(amountIn, tokenIn.decimals, priceIn);
|
|
@@ -191,7 +194,9 @@ function getSwapAmountsByFromValueDefault(p) {
|
|
|
191
194
|
if (amountIn <= 0) {
|
|
192
195
|
return defaultAmounts;
|
|
193
196
|
}
|
|
194
|
-
if ((0, tokens_1.getIsEquivalentTokens)(tokenIn, tokenOut))
|
|
197
|
+
if ((!allowSameTokenSwap && (0, tokens_1.getIsEquivalentTokens)(tokenIn, tokenOut)) ||
|
|
198
|
+
(0, tokens_1.getIsWrap)(tokenIn, tokenOut) ||
|
|
199
|
+
(0, tokens_1.getIsUnwrap)(tokenIn, tokenOut)) {
|
|
195
200
|
amountOut = amountIn;
|
|
196
201
|
usdOut = usdIn;
|
|
197
202
|
minOutputAmount = amountOut;
|
|
@@ -278,7 +283,7 @@ function getSwapAmountsByFromValueDefault(p) {
|
|
|
278
283
|
};
|
|
279
284
|
}
|
|
280
285
|
function getSwapAmountsByToValueDefault(p) {
|
|
281
|
-
const { tokenIn, tokenOut, amountOut, triggerRatio, isLimit, findSwapPath, uiFeeFactor, swapOptimizationOrder, allowedSwapSlippageBps, } = p;
|
|
286
|
+
const { tokenIn, tokenOut, amountOut, triggerRatio, isLimit, findSwapPath, uiFeeFactor, swapOptimizationOrder, allowedSwapSlippageBps, allowSameTokenSwap, } = p;
|
|
282
287
|
const priceIn = tokenIn.prices.minPrice;
|
|
283
288
|
const priceOut = tokenOut.prices.maxPrice;
|
|
284
289
|
const usdOut = (0, tokens_1.convertToUsd)(amountOut, tokenOut.decimals, priceOut);
|
|
@@ -311,7 +316,9 @@ function getSwapAmountsByToValueDefault(p) {
|
|
|
311
316
|
if (amountOut <= 0) {
|
|
312
317
|
return defaultAmounts;
|
|
313
318
|
}
|
|
314
|
-
if ((0, tokens_1.getIsEquivalentTokens)(tokenIn, tokenOut))
|
|
319
|
+
if ((!allowSameTokenSwap && (0, tokens_1.getIsEquivalentTokens)(tokenIn, tokenOut)) ||
|
|
320
|
+
(0, tokens_1.getIsWrap)(tokenIn, tokenOut) ||
|
|
321
|
+
(0, tokens_1.getIsUnwrap)(tokenIn, tokenOut)) {
|
|
315
322
|
amountIn = amountOut;
|
|
316
323
|
usdIn = usdOut;
|
|
317
324
|
const swapStrategy = {
|
|
@@ -102,6 +102,7 @@ function getIncreasePositionAmounts(p) {
|
|
|
102
102
|
marketsInfoData,
|
|
103
103
|
chainId,
|
|
104
104
|
externalSwapQuoteParams,
|
|
105
|
+
allowSameTokenSwap: false,
|
|
105
106
|
});
|
|
106
107
|
values.swapStrategy = swapAmounts.swapStrategy;
|
|
107
108
|
}
|
|
@@ -174,6 +175,7 @@ function getIncreasePositionAmounts(p) {
|
|
|
174
175
|
marketsInfoData,
|
|
175
176
|
chainId,
|
|
176
177
|
externalSwapQuoteParams,
|
|
178
|
+
allowSameTokenSwap: false,
|
|
177
179
|
});
|
|
178
180
|
values.swapStrategy = swapAmounts.swapStrategy;
|
|
179
181
|
}
|
|
@@ -215,6 +217,7 @@ function getIncreasePositionAmounts(p) {
|
|
|
215
217
|
marketsInfoData,
|
|
216
218
|
chainId,
|
|
217
219
|
externalSwapQuoteParams,
|
|
220
|
+
allowSameTokenSwap: false,
|
|
218
221
|
});
|
|
219
222
|
values.swapStrategy = swapAmounts.swapStrategy;
|
|
220
223
|
}
|
|
@@ -150,6 +150,9 @@ export function swapFeeFactorKey(market, forPositiveImpact) {
|
|
|
150
150
|
export function atomicSwapFeeFactorKey(market) {
|
|
151
151
|
return hashData(["bytes32", "address"], [ATOMIC_SWAP_FEE_FACTOR_KEY, market]);
|
|
152
152
|
}
|
|
153
|
+
export function withdrawalFeeFactorKey(market, forBalanceWasImproved) {
|
|
154
|
+
return hashData(["bytes32", "address", "bool"], [WITHDRAWAL_FEE_FACTOR_KEY, market, forBalanceWasImproved]);
|
|
155
|
+
}
|
|
153
156
|
export function openInterestKey(market, collateralToken, isLong) {
|
|
154
157
|
return hashData(["bytes32", "address", "address", "bool"], [OPEN_INTEREST_KEY, market, collateralToken, isLong]);
|
|
155
158
|
}
|
|
@@ -831,6 +831,41 @@ export const MARKETS = {
|
|
|
831
831
|
longTokenAddress: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
832
832
|
shortTokenAddress: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
833
833
|
},
|
|
834
|
+
// AR/USD [WBTC-USDC]
|
|
835
|
+
"0xfa19f7d23a475575BD0270AE7de4FC6852442945": {
|
|
836
|
+
marketTokenAddress: "0xfa19f7d23a475575BD0270AE7de4FC6852442945",
|
|
837
|
+
indexTokenAddress: "0x4b9a2b862E1a30e6E844c991D31Dc6387c9d65D5",
|
|
838
|
+
longTokenAddress: "0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f",
|
|
839
|
+
shortTokenAddress: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
840
|
+
},
|
|
841
|
+
// DASH/USD [WBTC-USDC]
|
|
842
|
+
"0x728Ff0679c89267434D6EF1824c8C8eED4aC3DBC": {
|
|
843
|
+
marketTokenAddress: "0x728Ff0679c89267434D6EF1824c8C8eED4aC3DBC",
|
|
844
|
+
indexTokenAddress: "0x426FE015a5F4E6Ad4E6497ad79265D07EA9F121e",
|
|
845
|
+
longTokenAddress: "0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f",
|
|
846
|
+
shortTokenAddress: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
847
|
+
},
|
|
848
|
+
// JTO/USD [WBTC-USDC]
|
|
849
|
+
"0x3B4689d69516b9D4b1aaf7545c6fC4d3ED70b70b": {
|
|
850
|
+
marketTokenAddress: "0x3B4689d69516b9D4b1aaf7545c6fC4d3ED70b70b",
|
|
851
|
+
indexTokenAddress: "0xF01D2AA2f3f903A93D57c4Bf672C8096DE485FA7",
|
|
852
|
+
longTokenAddress: "0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f",
|
|
853
|
+
shortTokenAddress: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
854
|
+
},
|
|
855
|
+
// SYRUP/USD [WETH-USDC]
|
|
856
|
+
"0x8965e821c7C8c09c6eB3Cb9cCf7Eb6f386441EA2": {
|
|
857
|
+
marketTokenAddress: "0x8965e821c7C8c09c6eB3Cb9cCf7Eb6f386441EA2",
|
|
858
|
+
indexTokenAddress: "0x9759C297fb6C91e252c7292cECa30a509558E5De",
|
|
859
|
+
longTokenAddress: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
860
|
+
shortTokenAddress: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
861
|
+
},
|
|
862
|
+
// CHZ/USD [WETH-USDC]
|
|
863
|
+
"0x3600592DdEd7e6E0B05029DfB637fFc5A85D6f6B": {
|
|
864
|
+
marketTokenAddress: "0x3600592DdEd7e6E0B05029DfB637fFc5A85D6f6B",
|
|
865
|
+
indexTokenAddress: "0x5dB4692926C8ceebF6Da0995358Bbc438F3fd80C",
|
|
866
|
+
longTokenAddress: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
867
|
+
shortTokenAddress: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
868
|
+
},
|
|
834
869
|
},
|
|
835
870
|
[AVALANCHE]: {
|
|
836
871
|
// BTC/USD [BTC-USDC]
|