@gmx-io/sdk 1.4.1 → 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/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/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/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 +10 -4
|
@@ -3,6 +3,7 @@ import { USD_DECIMALS } from "../../../configs/factors";
|
|
|
3
3
|
import { NATIVE_TOKEN_ADDRESS } from "../../../configs/tokens";
|
|
4
4
|
import { SwapPricingType } from "../../../types/orders";
|
|
5
5
|
import { expandDecimals } from "../../numbers";
|
|
6
|
+
import { getSwapPathStats } from "..";
|
|
6
7
|
import { buildMarketsAdjacencyGraph } from "../buildMarketsAdjacencyGraph";
|
|
7
8
|
import { findSwapPathsBetweenTokens } from "../findSwapPathsBetweenTokens";
|
|
8
9
|
import { createMarketEdgeLiquidityGetter, createNaiveNetworkEstimator, createNaiveSwapEstimator, getMaxLiquidityMarketSwapPathFromTokenSwapPaths, getNaiveBestMarketSwapPathsFromTokenSwapPaths, getTokenSwapPathsForTokenPair, } from "../swapRouting";
|
|
@@ -11,6 +12,7 @@ const marketKeys = [
|
|
|
11
12
|
"ETH-ETH-USDC",
|
|
12
13
|
"BTC-BTC-USDC",
|
|
13
14
|
"BTC-BTC-DAI",
|
|
15
|
+
"BTC-SOL-DAI",
|
|
14
16
|
"SOL-SOL-USDC",
|
|
15
17
|
"SOL-ETH-USDC",
|
|
16
18
|
"SOL-BTC-USDC",
|
|
@@ -432,3 +434,258 @@ describe("getMaxLiquidityMarketSwapPathFromTokenSwapPaths", () => {
|
|
|
432
434
|
});
|
|
433
435
|
});
|
|
434
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,6 +1,6 @@
|
|
|
1
1
|
import { SwapPricingType } from "../../types/orders";
|
|
2
2
|
import { bigMath } from "../bigmath";
|
|
3
|
-
import { convertToTokenAmount, convertToUsd, getIsEquivalentTokens, getIsStake, getIsUnstake, getMidPrice, } from "../tokens";
|
|
3
|
+
import { convertToTokenAmount, convertToUsd, getIsEquivalentTokens, getIsStake, getIsUnstake, getIsUnwrap, getIsWrap, getMidPrice, } from "../tokens";
|
|
4
4
|
import { getAvailableExternalSwapPaths } from "./externalSwapPath";
|
|
5
5
|
import { getExternalSwapQuoteByPath } from "./externalSwapQuoteByPath";
|
|
6
6
|
import { createFindSwapPath } from "./swapPath";
|
|
@@ -11,7 +11,7 @@ import { createFindSwapPath } from "./swapPath";
|
|
|
11
11
|
3. Check if there is a combined swap strategy and return combinedSwap if there is [combinedSwap]
|
|
12
12
|
4. Return defaultSwapStrategy (noSwap) if there is no other swap strategy [noSwap]
|
|
13
13
|
*/
|
|
14
|
-
export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chainId, swapOptimizationOrder, externalSwapQuoteParams, swapPricingType = SwapPricingType.Swap, }) {
|
|
14
|
+
export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chainId, swapOptimizationOrder, externalSwapQuoteParams, swapPricingType = SwapPricingType.Swap, allowSameTokenSwap, }) {
|
|
15
15
|
const priceIn = tokenIn.prices.minPrice;
|
|
16
16
|
const usdIn = convertToUsd(amountIn, tokenIn.decimals, priceIn);
|
|
17
17
|
if (amountIn < 0n) {
|
|
@@ -29,7 +29,11 @@ export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData
|
|
|
29
29
|
priceOut: tokenOut.prices.maxPrice,
|
|
30
30
|
feesUsd: 0n,
|
|
31
31
|
};
|
|
32
|
-
if (
|
|
32
|
+
if ((!allowSameTokenSwap && getIsEquivalentTokens(tokenIn, tokenOut)) ||
|
|
33
|
+
getIsWrap(tokenIn, tokenOut) ||
|
|
34
|
+
getIsUnwrap(tokenIn, tokenOut) ||
|
|
35
|
+
getIsStake(tokenIn, tokenOut) ||
|
|
36
|
+
getIsUnstake(tokenIn, tokenOut)) {
|
|
33
37
|
return defaultSwapStrategy;
|
|
34
38
|
}
|
|
35
39
|
const findSwapPath = createFindSwapPath({
|
|
@@ -100,7 +104,7 @@ export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData
|
|
|
100
104
|
return defaultSwapStrategy;
|
|
101
105
|
}
|
|
102
106
|
// Used for getting swap amounts by to value
|
|
103
|
-
export function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoData, chainId, externalSwapQuoteParams, swapOptimizationOrder, swapPricingType, }) {
|
|
107
|
+
export function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoData, chainId, externalSwapQuoteParams, swapOptimizationOrder, swapPricingType, allowSameTokenSwap, }) {
|
|
104
108
|
const priceIn = getMidPrice(tokenIn.prices);
|
|
105
109
|
const priceOut = getMidPrice(tokenOut.prices);
|
|
106
110
|
const preferredUsdOut = convertToUsd(amountOut, tokenOut.decimals, getMidPrice(tokenOut.prices));
|
|
@@ -118,7 +122,11 @@ export function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, markets
|
|
|
118
122
|
priceOut,
|
|
119
123
|
feesUsd: 0n,
|
|
120
124
|
};
|
|
121
|
-
if (
|
|
125
|
+
if ((!allowSameTokenSwap && getIsEquivalentTokens(tokenIn, tokenOut)) ||
|
|
126
|
+
getIsWrap(tokenIn, tokenOut) ||
|
|
127
|
+
getIsUnwrap(tokenIn, tokenOut) ||
|
|
128
|
+
getIsStake(tokenIn, tokenOut) ||
|
|
129
|
+
getIsUnstake(tokenIn, tokenOut)) {
|
|
122
130
|
return defaultSwapStrategy;
|
|
123
131
|
}
|
|
124
132
|
const findSwapPath = createFindSwapPath({
|
|
@@ -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) {
|
|
@@ -3,10 +3,10 @@ import { SwapPricingType } from "../../types/orders";
|
|
|
3
3
|
import { bigMath } from "../bigmath";
|
|
4
4
|
import { getTotalSwapVolumeFromSwapStats } from "../fees";
|
|
5
5
|
import { applyFactor } from "../numbers";
|
|
6
|
-
import { convertToTokenAmount, convertToUsd, getAmountByRatio, getIsEquivalentTokens, getIsStake, getIsUnstake, } from "../tokens";
|
|
6
|
+
import { convertToTokenAmount, convertToUsd, getAmountByRatio, getIsEquivalentTokens, getIsStake, getIsUnstake, getIsUnwrap, getIsWrap, } from "../tokens";
|
|
7
7
|
import { buildReverseSwapStrategy, buildSwapStrategy } from "./buildSwapStrategy";
|
|
8
8
|
export function getSwapAmountsByFromValue(p) {
|
|
9
|
-
const { tokenIn, tokenOut, amountIn, triggerRatio, isLimit, swapOptimizationOrder, uiFeeFactor, allowedSwapSlippageBps, marketsInfoData, chainId, externalSwapQuoteParams, } = p;
|
|
9
|
+
const { tokenIn, tokenOut, amountIn, triggerRatio, isLimit, swapOptimizationOrder, uiFeeFactor, allowedSwapSlippageBps, marketsInfoData, chainId, externalSwapQuoteParams, allowSameTokenSwap, } = p;
|
|
10
10
|
if (!externalSwapQuoteParams) {
|
|
11
11
|
return getSwapAmountsByFromValueDefault(p);
|
|
12
12
|
}
|
|
@@ -19,6 +19,7 @@ export function getSwapAmountsByFromValue(p) {
|
|
|
19
19
|
swapOptimizationOrder,
|
|
20
20
|
externalSwapQuoteParams,
|
|
21
21
|
swapPricingType: SwapPricingType.Swap,
|
|
22
|
+
allowSameTokenSwap,
|
|
22
23
|
});
|
|
23
24
|
const swapPathStats = swapStrategy.swapPathStats;
|
|
24
25
|
const totalSwapVolume = getTotalSwapVolumeFromSwapStats(swapPathStats?.swapSteps);
|
|
@@ -75,7 +76,7 @@ export function getSwapAmountsByFromValue(p) {
|
|
|
75
76
|
};
|
|
76
77
|
}
|
|
77
78
|
export function getSwapAmountsByToValue(p) {
|
|
78
|
-
const { tokenIn, tokenOut, amountOut, triggerRatio, isLimit, uiFeeFactor, swapOptimizationOrder, allowedSwapSlippageBps, marketsInfoData, chainId, externalSwapQuoteParams, } = p;
|
|
79
|
+
const { tokenIn, tokenOut, amountOut, triggerRatio, isLimit, uiFeeFactor, swapOptimizationOrder, allowedSwapSlippageBps, marketsInfoData, chainId, externalSwapQuoteParams, allowSameTokenSwap, } = p;
|
|
79
80
|
if (!externalSwapQuoteParams) {
|
|
80
81
|
return getSwapAmountsByToValueDefault(p);
|
|
81
82
|
}
|
|
@@ -88,6 +89,7 @@ export function getSwapAmountsByToValue(p) {
|
|
|
88
89
|
externalSwapQuoteParams,
|
|
89
90
|
swapOptimizationOrder,
|
|
90
91
|
swapPricingType: SwapPricingType.Swap,
|
|
92
|
+
allowSameTokenSwap,
|
|
91
93
|
});
|
|
92
94
|
const swapStrategy = buildSwapStrategy({
|
|
93
95
|
amountIn: swapStrategyReverse.amountIn,
|
|
@@ -98,6 +100,7 @@ export function getSwapAmountsByToValue(p) {
|
|
|
98
100
|
swapOptimizationOrder,
|
|
99
101
|
externalSwapQuoteParams,
|
|
100
102
|
swapPricingType: SwapPricingType.Swap,
|
|
103
|
+
allowSameTokenSwap,
|
|
101
104
|
});
|
|
102
105
|
const uiFeeUsd = applyFactor(swapStrategy.usdIn, uiFeeFactor);
|
|
103
106
|
const defaultAmounts = {
|
|
@@ -154,7 +157,7 @@ export function getSwapAmountsByToValue(p) {
|
|
|
154
157
|
};
|
|
155
158
|
}
|
|
156
159
|
function getSwapAmountsByFromValueDefault(p) {
|
|
157
|
-
const { tokenIn, tokenOut, amountIn, triggerRatio, isLimit, swapOptimizationOrder, findSwapPath, uiFeeFactor, allowedSwapSlippageBps, } = p;
|
|
160
|
+
const { tokenIn, tokenOut, amountIn, triggerRatio, isLimit, swapOptimizationOrder, findSwapPath, uiFeeFactor, allowedSwapSlippageBps, allowSameTokenSwap, } = p;
|
|
158
161
|
const priceIn = tokenIn.prices.minPrice;
|
|
159
162
|
const priceOut = tokenOut.prices.maxPrice;
|
|
160
163
|
const usdIn = convertToUsd(amountIn, tokenIn.decimals, priceIn);
|
|
@@ -186,7 +189,9 @@ function getSwapAmountsByFromValueDefault(p) {
|
|
|
186
189
|
if (amountIn <= 0) {
|
|
187
190
|
return defaultAmounts;
|
|
188
191
|
}
|
|
189
|
-
if (getIsEquivalentTokens(tokenIn, tokenOut))
|
|
192
|
+
if ((!allowSameTokenSwap && getIsEquivalentTokens(tokenIn, tokenOut)) ||
|
|
193
|
+
getIsWrap(tokenIn, tokenOut) ||
|
|
194
|
+
getIsUnwrap(tokenIn, tokenOut)) {
|
|
190
195
|
amountOut = amountIn;
|
|
191
196
|
usdOut = usdIn;
|
|
192
197
|
minOutputAmount = amountOut;
|
|
@@ -273,7 +278,7 @@ function getSwapAmountsByFromValueDefault(p) {
|
|
|
273
278
|
};
|
|
274
279
|
}
|
|
275
280
|
function getSwapAmountsByToValueDefault(p) {
|
|
276
|
-
const { tokenIn, tokenOut, amountOut, triggerRatio, isLimit, findSwapPath, uiFeeFactor, swapOptimizationOrder, allowedSwapSlippageBps, } = p;
|
|
281
|
+
const { tokenIn, tokenOut, amountOut, triggerRatio, isLimit, findSwapPath, uiFeeFactor, swapOptimizationOrder, allowedSwapSlippageBps, allowSameTokenSwap, } = p;
|
|
277
282
|
const priceIn = tokenIn.prices.minPrice;
|
|
278
283
|
const priceOut = tokenOut.prices.maxPrice;
|
|
279
284
|
const usdOut = convertToUsd(amountOut, tokenOut.decimals, priceOut);
|
|
@@ -306,7 +311,9 @@ function getSwapAmountsByToValueDefault(p) {
|
|
|
306
311
|
if (amountOut <= 0) {
|
|
307
312
|
return defaultAmounts;
|
|
308
313
|
}
|
|
309
|
-
if (getIsEquivalentTokens(tokenIn, tokenOut))
|
|
314
|
+
if ((!allowSameTokenSwap && getIsEquivalentTokens(tokenIn, tokenOut)) ||
|
|
315
|
+
getIsWrap(tokenIn, tokenOut) ||
|
|
316
|
+
getIsUnwrap(tokenIn, tokenOut)) {
|
|
310
317
|
amountIn = amountOut;
|
|
311
318
|
usdIn = usdOut;
|
|
312
319
|
const swapStrategy = {
|
|
@@ -99,6 +99,7 @@ export function getIncreasePositionAmounts(p) {
|
|
|
99
99
|
marketsInfoData,
|
|
100
100
|
chainId,
|
|
101
101
|
externalSwapQuoteParams,
|
|
102
|
+
allowSameTokenSwap: false,
|
|
102
103
|
});
|
|
103
104
|
values.swapStrategy = swapAmounts.swapStrategy;
|
|
104
105
|
}
|
|
@@ -171,6 +172,7 @@ export function getIncreasePositionAmounts(p) {
|
|
|
171
172
|
marketsInfoData,
|
|
172
173
|
chainId,
|
|
173
174
|
externalSwapQuoteParams,
|
|
175
|
+
allowSameTokenSwap: false,
|
|
174
176
|
});
|
|
175
177
|
values.swapStrategy = swapAmounts.swapStrategy;
|
|
176
178
|
}
|
|
@@ -212,6 +214,7 @@ export function getIncreasePositionAmounts(p) {
|
|
|
212
214
|
marketsInfoData,
|
|
213
215
|
chainId,
|
|
214
216
|
externalSwapQuoteParams,
|
|
217
|
+
allowSameTokenSwap: false,
|
|
215
218
|
});
|
|
216
219
|
values.swapStrategy = swapAmounts.swapStrategy;
|
|
217
220
|
}
|
|
@@ -23,8 +23,6 @@ export type Token = {
|
|
|
23
23
|
visualMultiplier?: number;
|
|
24
24
|
visualPrefix?: string;
|
|
25
25
|
wrappedAddress?: string;
|
|
26
|
-
coingeckoUrl?: string;
|
|
27
|
-
coingeckoSymbol?: string;
|
|
28
26
|
metamaskSymbol?: string;
|
|
29
27
|
explorerSymbol?: string;
|
|
30
28
|
explorerUrl?: string;
|
|
@@ -46,7 +44,6 @@ export type Token = {
|
|
|
46
44
|
isPlatformToken?: boolean;
|
|
47
45
|
isPlatformTradingToken?: boolean;
|
|
48
46
|
isStaking?: boolean;
|
|
49
|
-
shouldResetAllowance?: boolean;
|
|
50
47
|
};
|
|
51
48
|
/**
|
|
52
49
|
* @deprecated V1 TokenInfo struct
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { AbiId } from "../abis";
|
|
2
2
|
import type { GmxSdk } from "index";
|
|
3
3
|
export declare const MAX_TIMEOUT = 40000;
|
|
4
|
-
export declare const MAX_CALLS_PER_BATCH =
|
|
5
|
-
export declare const MAX_CONCURRENT_BATCHES =
|
|
4
|
+
export declare const MAX_CALLS_PER_BATCH = 1000;
|
|
5
|
+
export declare const MAX_CONCURRENT_BATCHES = 1;
|
|
6
6
|
export type MulticallProviderUrls = {
|
|
7
7
|
primary: string;
|
|
8
8
|
secondary: string;
|
|
@@ -3,7 +3,7 @@ import { SwapPricingType } from "../../types/orders";
|
|
|
3
3
|
import { SwapStrategyForSwapOrders } from "../../types/swapStrategy";
|
|
4
4
|
import { TokenData } from "../../types/tokens";
|
|
5
5
|
import { ExternalSwapQuoteParams, SwapOptimizationOrderArray } from "../../types/trade";
|
|
6
|
-
export declare function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chainId, swapOptimizationOrder, externalSwapQuoteParams, swapPricingType, }: {
|
|
6
|
+
export declare function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chainId, swapOptimizationOrder, externalSwapQuoteParams, swapPricingType, allowSameTokenSwap, }: {
|
|
7
7
|
chainId: number;
|
|
8
8
|
amountIn: bigint;
|
|
9
9
|
tokenIn: TokenData;
|
|
@@ -12,8 +12,9 @@ export declare function buildSwapStrategy({ amountIn, tokenIn, tokenOut, markets
|
|
|
12
12
|
swapOptimizationOrder: SwapOptimizationOrderArray | undefined;
|
|
13
13
|
externalSwapQuoteParams: ExternalSwapQuoteParams;
|
|
14
14
|
swapPricingType: SwapPricingType;
|
|
15
|
+
allowSameTokenSwap: boolean;
|
|
15
16
|
}): SwapStrategyForSwapOrders;
|
|
16
|
-
export declare function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoData, chainId, externalSwapQuoteParams, swapOptimizationOrder, swapPricingType, }: {
|
|
17
|
+
export declare function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoData, chainId, externalSwapQuoteParams, swapOptimizationOrder, swapPricingType, allowSameTokenSwap, }: {
|
|
17
18
|
chainId: number;
|
|
18
19
|
amountOut: bigint;
|
|
19
20
|
tokenIn: TokenData;
|
|
@@ -22,4 +23,5 @@ export declare function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut,
|
|
|
22
23
|
externalSwapQuoteParams: ExternalSwapQuoteParams | undefined;
|
|
23
24
|
swapOptimizationOrder: SwapOptimizationOrderArray | undefined;
|
|
24
25
|
swapPricingType: SwapPricingType;
|
|
26
|
+
allowSameTokenSwap: boolean;
|
|
25
27
|
}): SwapStrategyForSwapOrders;
|
|
@@ -15,6 +15,7 @@ export declare function getSwapAmountsByFromValue(p: {
|
|
|
15
15
|
chainId: number;
|
|
16
16
|
externalSwapQuoteParams: ExternalSwapQuoteParams | undefined;
|
|
17
17
|
findSwapPath: FindSwapPath;
|
|
18
|
+
allowSameTokenSwap: boolean;
|
|
18
19
|
}): SwapAmounts;
|
|
19
20
|
export declare function getSwapAmountsByToValue(p: {
|
|
20
21
|
tokenIn: TokenData;
|
|
@@ -29,5 +30,6 @@ export declare function getSwapAmountsByToValue(p: {
|
|
|
29
30
|
chainId: number;
|
|
30
31
|
externalSwapQuoteParams: ExternalSwapQuoteParams | undefined;
|
|
31
32
|
findSwapPath: FindSwapPath;
|
|
33
|
+
allowSameTokenSwap: boolean;
|
|
32
34
|
}): SwapAmounts;
|
|
33
35
|
export declare function getSwapPathComparator(order?: SwapOptimizationOrderArray | undefined): (a: SwapRoute, b: SwapRoute) => 1 | 0 | -1;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gmx-io/sdk",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -58,8 +58,14 @@
|
|
|
58
58
|
"build:esm": "tsc -p tsconfig.esm.json && echo '{\"type\": \"module\"}' > build/esm/src/package.json",
|
|
59
59
|
"build:types": "tsc -p tsconfig.json --emitDeclarationOnly",
|
|
60
60
|
"build:subsquid": "./node_modules/@graphql-codegen/cli/cjs/bin.js -c subsquid-codegen.ts && npx prettier --write src/types/subsquid.ts",
|
|
61
|
-
"tscheck": "tsc -p tsconfig.json --noEmit",
|
|
61
|
+
"tscheck": "tsc -p tsconfig.json --noEmit --incremental",
|
|
62
|
+
"tscheck:ci": "tsc -p tsconfig.json --noEmit",
|
|
63
|
+
"lint": "cd .. && node node_modules/.bin/eslint sdk --fix --cache --cache-location .eslintcache",
|
|
64
|
+
"lint:ci": "cd .. && node node_modules/.bin/eslint sdk --max-warnings=0",
|
|
62
65
|
"clean": "rm -rf build/**/*.tsbuildinfo",
|
|
66
|
+
"clean:cache": "rm -rf ../.eslintcache ../tsconfig.tsbuildinfo build/**/*.tsbuildinfo",
|
|
67
|
+
"clean:lint": "rm -rf ../.eslintcache",
|
|
68
|
+
"clean:ts": "rm -rf ../tsconfig.tsbuildinfo build/**/*.tsbuildinfo",
|
|
63
69
|
"build": "rm -rf build && yarn build:subsquid && yarn build:types && yarn build:cjs && yarn build:esm && yarn clean"
|
|
64
70
|
},
|
|
65
71
|
"publishConfig": {
|
|
@@ -81,7 +87,7 @@
|
|
|
81
87
|
"viem": "^2.37.1"
|
|
82
88
|
},
|
|
83
89
|
"devDependencies": {
|
|
84
|
-
"@babel/plugin-
|
|
90
|
+
"@babel/plugin-transform-private-property-in-object": "^7.25.9",
|
|
85
91
|
"@graphql-codegen/cli": "5.0.6",
|
|
86
92
|
"@graphql-codegen/client-preset": "4.8.1",
|
|
87
93
|
"@graphql-codegen/typescript": "4.1.6",
|
|
@@ -93,7 +99,7 @@
|
|
|
93
99
|
"tsx": "4.19.0",
|
|
94
100
|
"typescript-transform-paths": "3.5.1",
|
|
95
101
|
"viem": "2.39.0",
|
|
96
|
-
"vitest": "3.0.
|
|
102
|
+
"vitest": "^3.0.5"
|
|
97
103
|
},
|
|
98
104
|
"files": [
|
|
99
105
|
"build"
|