@gmx-io/sdk 0.2.0 → 1.0.0-alpha.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/README.md +28 -1
- package/build/cjs/scripts/prebuild/index.js +11 -0
- package/build/cjs/scripts/prebuild/prebuildKinkModelMarketRatesKeys.js +51 -0
- package/build/cjs/scripts/prebuild/prebuildMarketConfigKeys.js +25 -0
- package/build/cjs/scripts/prebuild/prebuildMarketValuesKeys.js +25 -0
- package/build/cjs/src/abis/CustomErrors.json +3254 -0
- package/build/cjs/src/abis/DataStore.json +1440 -0
- package/build/cjs/src/abis/ERC721.json +442 -0
- package/build/cjs/src/abis/EventEmitter.json +2065 -0
- package/build/cjs/src/abis/ExchangeRouter.json +1801 -0
- package/build/cjs/src/abis/GMT.json +522 -0
- package/build/cjs/src/abis/GlpManager.json +591 -0
- package/build/cjs/src/abis/GlvReader.json +1414 -0
- package/build/cjs/src/abis/GlvRouter.json +706 -0
- package/build/cjs/src/abis/GmxMigrator.json +657 -0
- package/build/cjs/src/abis/GovToken.json +1121 -0
- package/build/cjs/src/abis/MintableBaseToken.json +696 -0
- package/build/cjs/src/abis/Multicall.json +333 -0
- package/build/cjs/src/abis/OrderBook.json +1997 -0
- package/build/cjs/src/abis/OrderBookReader.json +111 -0
- package/build/cjs/src/abis/OrderExecutor.json +122 -0
- package/build/cjs/src/abis/PositionManager.json +1165 -0
- package/build/cjs/src/abis/PositionRouter.json +2030 -0
- package/build/cjs/src/abis/Reader.json +355 -0
- package/build/cjs/src/abis/ReaderV2.json +630 -0
- package/build/cjs/src/abis/ReferralStorage.json +507 -0
- package/build/cjs/src/abis/RewardReader.json +86 -0
- package/build/cjs/src/abis/RewardRouter.json +1024 -0
- package/build/cjs/src/abis/RewardTracker.json +918 -0
- package/build/cjs/src/abis/Router-v2.json +605 -0
- package/build/cjs/src/abis/Router.json +605 -0
- package/build/cjs/src/abis/SubaccountRouter.json +658 -0
- package/build/cjs/src/abis/SyntheticsReader.json +4706 -0
- package/build/cjs/src/abis/SyntheticsRouter.json +77 -0
- package/build/cjs/src/abis/Timelock.json +1737 -0
- package/build/cjs/src/abis/Token.json +345 -0
- package/build/cjs/src/abis/Treasury.json +411 -0
- package/build/cjs/src/abis/UniPool.json +89 -0
- package/build/cjs/src/abis/UniswapV2.json +1 -0
- package/build/cjs/src/abis/Vault.json +3220 -0
- package/build/cjs/src/abis/VaultReader.json +87 -0
- package/build/cjs/src/abis/VaultV2.json +3019 -0
- package/build/cjs/src/abis/VaultV2b.json +3220 -0
- package/build/cjs/src/abis/Vester.json +1031 -0
- package/build/cjs/src/abis/WETH.json +320 -0
- package/build/cjs/src/abis/YieldFarm.json +599 -0
- package/build/cjs/src/abis/YieldToken.json +560 -0
- package/build/cjs/src/abis/index.js +94 -0
- package/build/cjs/src/configs/__tests__/markets.spec.js +32 -0
- package/build/cjs/src/configs/__tests__/tokens.spec.js +40 -0
- package/build/cjs/src/configs/batch.js +42 -0
- package/build/cjs/src/configs/chains.js +98 -0
- package/build/cjs/src/configs/contracts.js +248 -0
- package/build/cjs/src/configs/dataStore.js +348 -0
- package/build/cjs/src/configs/factors.js +14 -0
- package/build/cjs/src/configs/markets.js +840 -0
- package/build/cjs/src/configs/oracleKeeper.js +45 -0
- package/build/cjs/src/configs/tokens.js +1635 -0
- package/build/cjs/src/index.js +75 -0
- package/build/cjs/src/modules/accounts/accounts.js +39 -0
- package/build/cjs/src/modules/accounts/accounts.spec.js +10 -0
- package/build/cjs/src/modules/base.js +19 -0
- package/build/cjs/src/modules/markets/index.js +334 -0
- package/build/cjs/src/modules/markets/markets.spec.js +40 -0
- package/build/cjs/src/modules/markets/query-builders.js +385 -0
- package/build/cjs/src/modules/markets/types.js +2 -0
- package/build/cjs/src/modules/oracle.js +45 -0
- package/build/cjs/src/modules/orders/helpers.js +225 -0
- package/build/cjs/src/modules/orders/helpers.spec.js +131 -0
- package/build/cjs/src/modules/orders/orders.js +293 -0
- package/build/cjs/src/modules/orders/orders.spec.js +19 -0
- package/build/cjs/src/modules/orders/transactions/cancelOrdersTxn.js +20 -0
- package/build/cjs/src/modules/orders/transactions/createDecreaseOrderTxn.js +104 -0
- package/build/cjs/src/modules/orders/transactions/createIncreaseOrderTxn.js +174 -0
- package/build/cjs/src/modules/orders/transactions/createSwapOrderTxn.js +84 -0
- package/build/cjs/src/modules/orders/transactions/createWrapOrUnwrapTxn.js +17 -0
- package/build/cjs/src/modules/orders/transactions/updateOrderTxn.js +51 -0
- package/build/cjs/src/modules/orders/utils.js +157 -0
- package/build/cjs/src/modules/positions/positions.js +501 -0
- package/build/cjs/src/modules/positions/positions.spec.js +28 -0
- package/build/cjs/src/modules/tokens/tokens.js +135 -0
- package/build/cjs/src/modules/tokens/tokens.spec.js +29 -0
- package/build/cjs/src/modules/trades/trades.js +266 -0
- package/build/cjs/src/modules/trades/trades.spec.js +17 -0
- package/build/cjs/src/modules/utils/utils.js +211 -0
- package/build/cjs/src/package.json +1 -0
- package/build/cjs/src/prebuilt/hashedKinkModelMarketRatesKeys.json +944 -0
- package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +5858 -0
- package/build/cjs/src/prebuilt/hashedMarketValuesKeys.json +1763 -0
- package/build/cjs/src/prebuilt/index.js +15 -0
- package/build/cjs/src/test/mock.js +246 -0
- package/build/cjs/src/types/common.js +2 -0
- package/build/cjs/src/types/fees.js +2 -0
- package/build/cjs/src/types/markets.js +2 -0
- package/build/cjs/src/types/orders.js +38 -0
- package/build/cjs/src/types/positions.js +2 -0
- package/build/cjs/src/types/referrals.js +12 -0
- package/build/cjs/src/types/sdk.js +2 -0
- package/build/cjs/src/types/sidecarOrders.js +2 -0
- package/build/cjs/src/types/tokens.js +2 -0
- package/build/cjs/src/types/trade.js +25 -0
- package/build/cjs/src/types/tradeHistory.js +11 -0
- package/build/cjs/src/utils/LruCache.js +49 -0
- package/build/cjs/src/utils/__tests__/LruCache.spec.js +45 -0
- package/build/cjs/src/utils/__tests__/bigmath.spec.js +40 -0
- package/build/cjs/src/utils/__tests__/fees.spec.js +64 -0
- package/build/cjs/src/utils/__tests__/hash.spec.js +43 -0
- package/build/cjs/src/utils/__tests__/markets.spec.js +267 -0
- package/build/cjs/src/utils/__tests__/numbers.spec.js +83 -0
- package/build/cjs/src/utils/__tests__/objects.spec.js +139 -0
- package/build/cjs/src/utils/__tests__/positions.spec.js +180 -0
- package/build/cjs/src/utils/__tests__/prices.spec.js +69 -0
- package/build/cjs/src/utils/__tests__/referrals.spec.js +20 -0
- package/build/cjs/src/utils/__tests__/subgraph.spec.js +161 -0
- package/build/cjs/src/utils/__tests__/tokens.spec.js +115 -0
- package/build/cjs/src/utils/__tests__/trade.spec.js +38 -0
- package/build/cjs/src/utils/bigmath.js +34 -0
- package/build/cjs/src/utils/buildUrl.js +12 -0
- package/build/cjs/src/utils/callContract.js +143 -0
- package/build/cjs/src/utils/common.js +14 -0
- package/build/cjs/src/utils/contracts.js +93 -0
- package/build/cjs/src/utils/fees/estimateOraclePriceCount.js +28 -0
- package/build/cjs/src/utils/fees/executionFee.js +113 -0
- package/build/cjs/src/utils/fees/executionFee.spec.js +66 -0
- package/build/cjs/src/utils/fees/getNaiveEstimatedGasBySwapCount.js +8 -0
- package/build/cjs/src/utils/fees/index.js +128 -0
- package/build/cjs/src/utils/fees/priceImpact.js +300 -0
- package/build/cjs/src/utils/graphqlFetcher.js +24 -0
- package/build/cjs/src/utils/hash.js +41 -0
- package/build/cjs/src/utils/marketKeysAndConfigs.js +262 -0
- package/build/cjs/src/utils/markets.js +169 -0
- package/build/cjs/src/utils/multicall.js +125 -0
- package/build/cjs/src/utils/numbers.js +73 -0
- package/build/cjs/src/utils/objects.js +47 -0
- package/build/cjs/src/utils/orders.js +210 -0
- package/build/cjs/src/utils/positions.js +145 -0
- package/build/cjs/src/utils/prices.js +141 -0
- package/build/cjs/src/utils/referrals.js +34 -0
- package/build/cjs/src/utils/simulateExecuteOrder.js +143 -0
- package/build/cjs/src/utils/subgraph.js +86 -0
- package/build/cjs/src/utils/swap/__tests__/buildMarketsAdjacencyGraph.spec.js +118 -0
- package/build/cjs/src/utils/swap/__tests__/findReachableTokens.spec.js +118 -0
- package/build/cjs/src/utils/swap/__tests__/findSwapPathsBetweenTokens.spec.js +150 -0
- package/build/cjs/src/utils/swap/__tests__/mockRouting.spec.js +429 -0
- package/build/cjs/src/utils/swap/__tests__/swapRouting.spec.js +617 -0
- package/build/cjs/src/utils/swap/__tests__/swapStats.spec.js +388 -0
- package/build/cjs/src/utils/swap/buildMarketsAdjacencyGraph.js +25 -0
- package/build/cjs/src/utils/swap/constants.js +5 -0
- package/build/cjs/src/utils/swap/findReachableTokens.js +37 -0
- package/build/cjs/src/utils/swap/findSwapPathsBetweenTokens.js +75 -0
- package/build/cjs/src/utils/swap/index.js +18 -0
- package/build/cjs/src/utils/swap/preparedSwapData.js +28 -0
- package/build/cjs/src/utils/swap/swapPath.js +132 -0
- package/build/cjs/src/utils/swap/swapRouting.js +359 -0
- package/build/cjs/src/utils/swap/swapStats.js +258 -0
- package/build/cjs/src/utils/swap/swapValues.js +190 -0
- package/build/cjs/src/utils/testUtil.js +22 -0
- package/build/cjs/src/utils/tokens.js +144 -0
- package/build/cjs/src/utils/trade/amounts.js +317 -0
- package/build/cjs/src/utils/trade/index.js +17 -0
- package/build/cjs/src/utils/trade/trade.js +61 -0
- package/build/cjs/src/utils/tradeHistory.js +137 -0
- package/build/cjs/tsconfig.cjs.tsbuildinfo +1 -0
- package/build/esm/src/abis/CustomErrors.json +3254 -0
- package/build/esm/src/abis/DataStore.json +1440 -0
- package/build/esm/src/abis/ERC721.json +442 -0
- package/build/esm/src/abis/EventEmitter.json +2065 -0
- package/build/esm/src/abis/ExchangeRouter.json +1801 -0
- package/build/esm/src/abis/GMT.json +522 -0
- package/build/esm/src/abis/GlpManager.json +591 -0
- package/build/esm/src/abis/GlvReader.json +1414 -0
- package/build/esm/src/abis/GlvRouter.json +706 -0
- package/build/esm/src/abis/GmxMigrator.json +657 -0
- package/build/esm/src/abis/GovToken.json +1121 -0
- package/build/esm/src/abis/MintableBaseToken.json +696 -0
- package/build/esm/src/abis/Multicall.json +333 -0
- package/build/esm/src/abis/OrderBook.json +1997 -0
- package/build/esm/src/abis/OrderBookReader.json +111 -0
- package/build/esm/src/abis/OrderExecutor.json +122 -0
- package/build/esm/src/abis/PositionManager.json +1165 -0
- package/build/esm/src/abis/PositionRouter.json +2030 -0
- package/build/esm/src/abis/Reader.json +355 -0
- package/build/esm/src/abis/ReaderV2.json +630 -0
- package/build/esm/src/abis/ReferralStorage.json +507 -0
- package/build/esm/src/abis/RewardReader.json +86 -0
- package/build/esm/src/abis/RewardRouter.json +1024 -0
- package/build/esm/src/abis/RewardTracker.json +918 -0
- package/build/esm/src/abis/Router-v2.json +605 -0
- package/build/esm/src/abis/Router.json +605 -0
- package/build/esm/src/abis/SubaccountRouter.json +658 -0
- package/build/esm/src/abis/SyntheticsReader.json +4706 -0
- package/build/esm/src/abis/SyntheticsRouter.json +77 -0
- package/build/esm/src/abis/Timelock.json +1737 -0
- package/build/esm/src/abis/Token.json +345 -0
- package/build/esm/src/abis/Treasury.json +411 -0
- package/build/esm/src/abis/UniPool.json +89 -0
- package/build/esm/src/abis/UniswapV2.json +1 -0
- package/build/esm/src/abis/Vault.json +3220 -0
- package/build/esm/src/abis/VaultReader.json +87 -0
- package/build/esm/src/abis/VaultV2.json +3019 -0
- package/build/esm/src/abis/VaultV2b.json +3220 -0
- package/build/esm/src/abis/Vester.json +1031 -0
- package/build/esm/src/abis/WETH.json +320 -0
- package/build/esm/src/abis/YieldFarm.json +599 -0
- package/build/esm/src/abis/YieldToken.json +560 -0
- package/build/esm/src/abis/index.js +88 -0
- package/build/esm/src/configs/__tests__/markets.spec.js +30 -0
- package/build/esm/src/configs/__tests__/tokens.spec.js +38 -0
- package/build/{src → esm/src}/configs/chains.js +1 -1
- package/build/{src → esm/src}/configs/contracts.js +10 -4
- package/build/{src → esm/src}/configs/factors.js +3 -1
- package/build/{src → esm/src}/configs/markets.js +92 -0
- package/build/esm/src/configs/oracleKeeper.js +36 -0
- package/build/{src → esm/src}/configs/tokens.js +154 -13
- package/build/{src → esm/src}/modules/accounts/accounts.js +1 -2
- package/build/{src → esm/src}/modules/markets/index.js +1 -2
- package/build/{src → esm/src}/modules/markets/query-builders.js +8 -10
- package/build/esm/src/modules/orders/helpers.js +220 -0
- package/build/esm/src/modules/orders/helpers.spec.js +106 -0
- package/build/{src → esm/src}/modules/orders/orders.js +20 -2
- package/build/{src → esm/src}/modules/orders/transactions/cancelOrdersTxn.js +3 -3
- package/build/{src → esm/src}/modules/orders/transactions/createDecreaseOrderTxn.js +5 -5
- package/build/{src → esm/src}/modules/orders/transactions/createIncreaseOrderTxn.js +8 -7
- package/build/{src → esm/src}/modules/orders/transactions/createSwapOrderTxn.js +5 -5
- package/build/{src → esm/src}/modules/orders/transactions/createWrapOrUnwrapTxn.js +3 -3
- package/build/{src → esm/src}/modules/orders/transactions/updateOrderTxn.js +4 -4
- package/build/{src → esm/src}/modules/orders/utils.js +4 -6
- package/build/{src → esm/src}/modules/positions/positions.js +11 -14
- package/build/{src → esm/src}/modules/tokens/tokens.js +4 -10
- package/build/{src → esm/src}/modules/trades/trades.js +27 -155
- package/build/{src → esm/src}/modules/utils/utils.js +27 -3
- package/build/esm/src/package.json +1 -0
- package/build/esm/src/prebuilt/hashedKinkModelMarketRatesKeys.json +944 -0
- package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +5858 -0
- package/build/esm/src/prebuilt/hashedMarketValuesKeys.json +1763 -0
- package/build/{src → esm/src}/prebuilt/index.js +3 -7
- package/build/esm/src/test/mock.js +238 -0
- package/build/{src → esm/src}/types/orders.js +2 -0
- package/build/{src → esm/src}/types/trade.js +5 -0
- package/build/{src → esm/src}/utils/__tests__/markets.spec.js +4 -4
- package/build/esm/src/utils/__tests__/objects.spec.js +137 -0
- package/build/{src → esm/src}/utils/__tests__/prices.spec.js +16 -8
- package/build/{src → esm/src}/utils/__tests__/subgraph.spec.js +0 -4
- package/build/{src → esm/src}/utils/__tests__/tokens.spec.js +33 -3
- package/build/{src → esm/src}/utils/bigmath.js +3 -0
- package/build/{src → esm/src}/utils/fees/executionFee.js +3 -1
- package/build/esm/src/utils/fees/getNaiveEstimatedGasBySwapCount.js +4 -0
- package/build/{src → esm/src}/utils/fees/priceImpact.js +1 -1
- package/build/{src → esm/src}/utils/markets.js +0 -3
- package/build/{src → esm/src}/utils/multicall.js +5 -3
- package/build/{src → esm/src}/utils/numbers.js +5 -1
- package/build/esm/src/utils/objects.js +36 -0
- package/build/{src → esm/src}/utils/orders.js +42 -13
- package/build/esm/src/utils/prices.js +132 -0
- package/build/{src → esm/src}/utils/simulateExecuteOrder.js +14 -10
- package/build/{src → esm/src}/utils/subgraph.js +16 -6
- package/build/esm/src/utils/swap/__tests__/buildMarketsAdjacencyGraph.spec.js +116 -0
- package/build/esm/src/utils/swap/__tests__/findReachableTokens.spec.js +116 -0
- package/build/esm/src/utils/swap/__tests__/findSwapPathsBetweenTokens.spec.js +148 -0
- package/build/esm/src/utils/swap/__tests__/mockRouting.spec.js +427 -0
- package/build/esm/src/utils/swap/__tests__/swapRouting.spec.js +615 -0
- package/build/esm/src/utils/swap/__tests__/swapStats.spec.js +386 -0
- package/build/esm/src/utils/swap/buildMarketsAdjacencyGraph.js +21 -0
- package/build/esm/src/utils/swap/constants.js +2 -0
- package/build/esm/src/utils/swap/findReachableTokens.js +33 -0
- package/build/esm/src/utils/swap/findSwapPathsBetweenTokens.js +71 -0
- package/build/esm/src/utils/swap/index.js +2 -0
- package/build/esm/src/utils/swap/preparedSwapData.js +23 -0
- package/build/esm/src/utils/swap/swapPath.js +127 -0
- package/build/esm/src/utils/swap/swapRouting.js +341 -0
- package/build/{src/utils → esm/src/utils/swap}/swapStats.js +49 -7
- package/build/esm/src/utils/swap/swapValues.js +184 -0
- package/build/{src → esm/src}/utils/testUtil.js +1 -2
- package/build/esm/src/utils/tokens.js +127 -0
- package/build/esm/src/utils/trade/amounts.js +310 -0
- package/build/esm/src/utils/trade/index.js +1 -0
- package/build/{src/utils → esm/src/utils/trade}/trade.js +26 -4
- package/build/esm/src/utils/tradeHistory.js +132 -0
- package/build/esm/tsconfig.esm.tsbuildinfo +1 -0
- package/build/src/abis/index.d.ts +23 -0
- package/build/src/configs/__tests__/markets.spec.d.ts +1 -0
- package/build/src/configs/__tests__/tokens.spec.d.ts +1 -0
- package/build/src/configs/chains.d.ts +7 -22
- package/build/src/configs/contracts.d.ts +1 -220
- package/build/src/configs/factors.d.ts +3 -1
- package/build/src/configs/markets.d.ts +2 -1
- package/build/src/configs/oracleKeeper.d.ts +3 -0
- package/build/src/configs/tokens.d.ts +4 -4
- package/build/src/modules/markets/query-builders.d.ts +5 -4
- package/build/src/modules/oracle.d.ts +7 -3
- package/build/src/modules/orders/helpers.d.ts +50 -0
- package/build/src/modules/orders/helpers.spec.d.ts +1 -0
- package/build/src/modules/orders/orders.d.ts +6 -1
- package/build/src/modules/orders/transactions/createDecreaseOrderTxn.d.ts +1 -1
- package/build/src/modules/orders/transactions/createSwapOrderTxn.d.ts +1 -0
- package/build/src/modules/orders/transactions/updateOrderTxn.d.ts +2 -2
- package/build/src/modules/orders/utils.d.ts +5 -200
- package/build/src/modules/positions/positions.d.ts +1 -1
- package/build/src/modules/trades/trades.d.ts +2 -3
- package/build/src/modules/utils/utils.d.ts +4 -2
- package/build/src/prebuilt/index.d.ts +6 -2
- package/build/src/test/mock.d.ts +14 -0
- package/build/src/types/fees.d.ts +6 -0
- package/build/src/types/orders.d.ts +4 -3
- package/build/src/types/sdk.d.ts +4 -3
- package/build/src/types/tokens.d.ts +5 -0
- package/build/src/types/trade.d.ts +84 -9
- package/build/src/types/tradeHistory.d.ts +3 -3
- package/build/src/utils/bigmath.d.ts +1 -0
- package/build/src/utils/fees/executionFee.d.ts +1 -1
- package/build/src/utils/fees/getNaiveEstimatedGasBySwapCount.d.ts +2 -0
- package/build/src/utils/hash.d.ts +1 -1
- package/build/src/utils/multicall.d.ts +2 -1
- package/build/src/utils/numbers.d.ts +2 -0
- package/build/src/utils/objects.d.ts +6 -0
- package/build/src/utils/orders.d.ts +1 -0
- package/build/src/utils/positions.d.ts +1 -1
- package/build/src/utils/prices.d.ts +35 -1
- package/build/src/utils/subgraph.d.ts +7 -5
- package/build/src/utils/swap/__tests__/buildMarketsAdjacencyGraph.spec.d.ts +1 -0
- package/build/src/utils/swap/__tests__/findReachableTokens.spec.d.ts +1 -0
- package/build/src/utils/swap/__tests__/findSwapPathsBetweenTokens.spec.d.ts +1 -0
- package/build/src/utils/swap/__tests__/mockRouting.spec.d.ts +1 -0
- package/build/src/utils/swap/__tests__/swapRouting.spec.d.ts +1 -0
- package/build/src/utils/swap/__tests__/swapStats.spec.d.ts +1 -0
- package/build/src/utils/swap/buildMarketsAdjacencyGraph.d.ts +11 -0
- package/build/src/utils/swap/constants.d.ts +2 -0
- package/build/src/utils/swap/findReachableTokens.d.ts +2 -0
- package/build/src/utils/swap/findSwapPathsBetweenTokens.d.ts +3 -0
- package/build/src/utils/swap/index.d.ts +2 -0
- package/build/src/utils/swap/preparedSwapData.d.ts +14 -0
- package/build/src/utils/swap/swapPath.d.ts +23 -0
- package/build/src/utils/swap/swapRouting.d.ts +67 -0
- package/build/src/utils/{swapStats.d.ts → swap/swapStats.d.ts} +7 -2
- package/build/src/utils/swap/swapValues.d.ts +26 -0
- package/build/src/utils/testUtil.d.ts +2 -14560
- package/build/src/utils/tokens.d.ts +25 -1
- package/build/src/utils/trade/amounts.d.ts +70 -0
- package/build/src/utils/trade/index.d.ts +1 -0
- package/build/src/utils/{trade.d.ts → trade/trade.d.ts} +2 -1
- package/build/src/utils/tradeHistory.d.ts +7 -0
- package/package.json +43 -12
- package/build/src/abis/CustomErrors.json +0 -3254
- package/build/src/abis/DataStore.json +0 -1440
- package/build/src/abis/ERC721.json +0 -442
- package/build/src/abis/EventEmitter.json +0 -2065
- package/build/src/abis/ExchangeRouter.json +0 -1801
- package/build/src/abis/GMT.json +0 -522
- package/build/src/abis/GlpManager.json +0 -592
- package/build/src/abis/GlvReader.json +0 -1415
- package/build/src/abis/GlvRouter.json +0 -707
- package/build/src/abis/GmxMigrator.json +0 -657
- package/build/src/abis/GovToken.json +0 -1121
- package/build/src/abis/MintableBaseToken.json +0 -696
- package/build/src/abis/Multicall.json +0 -333
- package/build/src/abis/OrderBook.json +0 -1997
- package/build/src/abis/OrderBookReader.json +0 -111
- package/build/src/abis/OrderExecutor.json +0 -122
- package/build/src/abis/PositionManager.json +0 -1165
- package/build/src/abis/PositionRouter.json +0 -2030
- package/build/src/abis/Reader.json +0 -355
- package/build/src/abis/ReaderV2.json +0 -630
- package/build/src/abis/ReferralStorage.json +0 -507
- package/build/src/abis/RewardReader.json +0 -86
- package/build/src/abis/RewardRouter.json +0 -1024
- package/build/src/abis/RewardTracker.json +0 -918
- package/build/src/abis/Router-v2.json +0 -605
- package/build/src/abis/Router.json +0 -605
- package/build/src/abis/SubaccountRouter.json +0 -658
- package/build/src/abis/SyntheticsReader.json +0 -4709
- package/build/src/abis/SyntheticsRouter.json +0 -77
- package/build/src/abis/Timelock.json +0 -1737
- package/build/src/abis/Token.json +0 -345
- package/build/src/abis/Treasury.json +0 -411
- package/build/src/abis/UniPool.json +0 -89
- package/build/src/abis/UniswapV2.json +0 -1
- package/build/src/abis/Vault.json +0 -3220
- package/build/src/abis/VaultReader.json +0 -87
- package/build/src/abis/VaultV2.json +0 -3019
- package/build/src/abis/VaultV2b.json +0 -3220
- package/build/src/abis/Vester.json +0 -1031
- package/build/src/abis/WETH.json +0 -320
- package/build/src/abis/YieldFarm.json +0 -599
- package/build/src/abis/YieldToken.json +0 -560
- package/build/src/prebuilt/hashedKinkModelMarketRatesKeys.json +0 -840
- package/build/src/prebuilt/hashedMarketConfigKeys.json +0 -5208
- package/build/src/prebuilt/hashedMarketValuesKeys.json +0 -1568
- package/build/src/prebuilt/index.ts +0 -45
- package/build/src/utils/__tests__/objects.spec.js +0 -59
- package/build/src/utils/objects.js +0 -13
- package/build/src/utils/prices.js +0 -25
- package/build/src/utils/tokens.js +0 -68
- package/build/tsconfig.tsbuildinfo +0 -1
- package/build/{scripts → esm/scripts}/prebuild/index.js +2 -2
- package/build/{scripts → esm/scripts}/prebuild/prebuildKinkModelMarketRatesKeys.js +1 -1
- package/build/{scripts → esm/scripts}/prebuild/prebuildMarketConfigKeys.js +1 -1
- package/build/{scripts → esm/scripts}/prebuild/prebuildMarketValuesKeys.js +0 -0
- package/build/{src → esm/src}/configs/batch.js +0 -0
- package/build/{src → esm/src}/configs/dataStore.js +0 -0
- package/build/{src → esm/src}/index.js +2 -2
- package/build/{src → esm/src}/modules/accounts/accounts.spec.js +0 -0
- package/build/{src → esm/src}/modules/base.js +0 -0
- package/build/{src → esm/src}/modules/markets/markets.spec.js +2 -2
- package/build/{src → esm/src}/modules/markets/types.js +0 -0
- package/build/{src → esm/src}/modules/oracle.js +0 -0
- package/build/{src → esm/src}/modules/orders/orders.spec.js +0 -0
- package/build/{src → esm/src}/modules/positions/positions.spec.js +1 -1
- package/build/{src → esm/src}/modules/tokens/tokens.spec.js +1 -1
- package/build/{src → esm/src}/modules/trades/trades.spec.js +0 -0
- package/build/{src → esm/src}/types/common.js +0 -0
- package/build/{src → esm/src}/types/fees.js +0 -0
- package/build/{src → esm/src}/types/markets.js +0 -0
- package/build/{src → esm/src}/types/positions.js +0 -0
- package/build/{src → esm/src}/types/referrals.js +0 -0
- package/build/{src → esm/src}/types/sdk.js +0 -0
- package/build/{src → esm/src}/types/sidecarOrders.js +0 -0
- package/build/{src → esm/src}/types/tokens.js +0 -0
- package/build/{src → esm/src}/types/tradeHistory.js +0 -0
- package/build/{src → esm/src}/utils/LruCache.js +0 -0
- package/build/{src → esm/src}/utils/__tests__/LruCache.spec.js +0 -0
- package/build/{src → esm/src}/utils/__tests__/bigmath.spec.js +1 -1
- package/build/{src → esm/src}/utils/__tests__/fees.spec.js +1 -1
- package/build/{src → esm/src}/utils/__tests__/hash.spec.js +1 -1
- package/build/{src → esm/src}/utils/__tests__/numbers.spec.js +0 -0
- package/build/{src → esm/src}/utils/__tests__/positions.spec.js +0 -0
- package/build/{src → esm/src}/utils/__tests__/referrals.spec.js +1 -1
- package/build/{src → esm/src}/utils/__tests__/trade.spec.js +0 -0
- package/build/{src → esm/src}/utils/buildUrl.js +0 -0
- package/build/{src → esm/src}/utils/callContract.js +0 -0
- package/build/{src → esm/src}/utils/common.js +0 -0
- package/build/{src → esm/src}/utils/contracts.js +0 -0
- package/build/{src → esm/src}/utils/fees/estimateOraclePriceCount.js +0 -0
- package/build/{src → esm/src}/utils/fees/executionFee.spec.js +2 -2
- package/build/{src → esm/src}/utils/fees/index.js +0 -0
- package/build/{src → esm/src}/utils/graphqlFetcher.js +0 -0
- package/build/{src → esm/src}/utils/hash.js +0 -0
- package/build/{src → esm/src}/utils/marketKeysAndConfigs.js +1 -1
- package/build/{src → esm/src}/utils/positions.js +3 -3
- /package/build/{src → esm/src}/utils/referrals.js +0 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { findSwapPathsBetweenTokens } from "../findSwapPathsBetweenTokens";
|
|
3
|
+
describe("findSwapPathsBetweenTokens", () => {
|
|
4
|
+
it("should find direct swap routes between tokens", () => {
|
|
5
|
+
const graph = {
|
|
6
|
+
ETH: {
|
|
7
|
+
USDC: ["ETH [ETH-USDC]"],
|
|
8
|
+
},
|
|
9
|
+
USDC: {
|
|
10
|
+
ETH: ["ETH [ETH-USDC]"],
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
const result = findSwapPathsBetweenTokens(graph);
|
|
14
|
+
expect(result).toEqual({
|
|
15
|
+
ETH: {
|
|
16
|
+
USDC: [[]],
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
it("should find multi-hop swap routes", () => {
|
|
21
|
+
const graph = {
|
|
22
|
+
ETH: {
|
|
23
|
+
USDC: ["ETH [ETH-USDC]"],
|
|
24
|
+
BTC: ["BTC [BTC-ETH]"],
|
|
25
|
+
},
|
|
26
|
+
USDC: {
|
|
27
|
+
ETH: ["ETH [ETH-USDC]"],
|
|
28
|
+
BTC: ["BTC [BTC-USDC]"],
|
|
29
|
+
},
|
|
30
|
+
BTC: {
|
|
31
|
+
ETH: ["BTC [BTC-ETH]"],
|
|
32
|
+
USDC: ["BTC [BTC-USDC]"],
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
const result = findSwapPathsBetweenTokens(graph);
|
|
36
|
+
expect(result).toEqual({
|
|
37
|
+
BTC: {
|
|
38
|
+
ETH: [[], ["USDC"]],
|
|
39
|
+
USDC: [[], ["ETH"]],
|
|
40
|
+
},
|
|
41
|
+
ETH: {
|
|
42
|
+
USDC: [[], ["BTC"]],
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
it("should handle multiple markets between same tokens", () => {
|
|
47
|
+
const graph = {
|
|
48
|
+
ETH: {
|
|
49
|
+
USDC: ["ETH [ETH-USDC]", "ETH [ETH-USDC-2]"],
|
|
50
|
+
},
|
|
51
|
+
USDC: {
|
|
52
|
+
ETH: ["ETH [ETH-USDC]", "ETH [ETH-USDC-2]"],
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
const result = findSwapPathsBetweenTokens(graph);
|
|
56
|
+
expect(result).toEqual({
|
|
57
|
+
ETH: {
|
|
58
|
+
USDC: [[], ["USDC", "ETH"]],
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
it("should not include self-loops", () => {
|
|
63
|
+
const graph = {
|
|
64
|
+
ETH: {
|
|
65
|
+
ETH: ["ETH [ETH-ETH]"],
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
const result = findSwapPathsBetweenTokens(graph);
|
|
69
|
+
expect(result).toEqual({});
|
|
70
|
+
});
|
|
71
|
+
it("should handle empty graph", () => {
|
|
72
|
+
const graph = {};
|
|
73
|
+
const result = findSwapPathsBetweenTokens(graph);
|
|
74
|
+
expect(result).toEqual({});
|
|
75
|
+
});
|
|
76
|
+
it("should handle isolated tokens", () => {
|
|
77
|
+
const graph = {
|
|
78
|
+
ETH: {},
|
|
79
|
+
USDC: {},
|
|
80
|
+
};
|
|
81
|
+
const result = findSwapPathsBetweenTokens(graph);
|
|
82
|
+
expect(result).toEqual({});
|
|
83
|
+
});
|
|
84
|
+
it("should respect MAX_EDGE_PATH_LENGTH limit", () => {
|
|
85
|
+
const graph = {
|
|
86
|
+
ETH: {
|
|
87
|
+
USDC: ["ETH [ETH-USDC]"],
|
|
88
|
+
},
|
|
89
|
+
USDC: {
|
|
90
|
+
ETH: ["ETH [ETH-USDC]"],
|
|
91
|
+
BTC: ["BTC [BTC-USDC]"],
|
|
92
|
+
},
|
|
93
|
+
BTC: {
|
|
94
|
+
USDC: ["BTC [BTC-USDC]"],
|
|
95
|
+
WBTC: ["WBTC [WBTC-BTC]"],
|
|
96
|
+
},
|
|
97
|
+
WBTC: {
|
|
98
|
+
BTC: ["WBTC [WBTC-BTC]"],
|
|
99
|
+
USDT: ["USDT [USDT-WBTC]"],
|
|
100
|
+
},
|
|
101
|
+
USDT: {
|
|
102
|
+
WBTC: ["USDT [USDT-WBTC]"],
|
|
103
|
+
DAI: ["DAI [DAI-USDT]"],
|
|
104
|
+
},
|
|
105
|
+
DAI: {
|
|
106
|
+
USDT: ["DAI [DAI-USDT]"],
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
const result = findSwapPathsBetweenTokens(graph);
|
|
110
|
+
// Assuming MAX_EDGE_PATH_LENGTH is 3, we shouldn't see paths longer than 3 hops
|
|
111
|
+
// So node path length should be less than or equal to 2
|
|
112
|
+
for (const tokenA in result) {
|
|
113
|
+
for (const tokenB in result[tokenA]) {
|
|
114
|
+
for (const path of result[tokenA][tokenB]) {
|
|
115
|
+
expect(path.length).toBeLessThanOrEqual(2);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
it("should find swap routes through common node pairs", () => {
|
|
121
|
+
// A - USDC - B
|
|
122
|
+
// ^
|
|
123
|
+
// |
|
|
124
|
+
// 2 markets
|
|
125
|
+
const graph = {
|
|
126
|
+
A: {
|
|
127
|
+
USDC: ["A [A-USDC]", "A2 [A-USDC]"],
|
|
128
|
+
},
|
|
129
|
+
B: {
|
|
130
|
+
USDC: ["B [B-USDC]"],
|
|
131
|
+
},
|
|
132
|
+
USDC: {
|
|
133
|
+
A: ["A [A-USDC]", "A2 [A-USDC]"],
|
|
134
|
+
B: ["B [B-USDC]"],
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
const result = findSwapPathsBetweenTokens(graph);
|
|
138
|
+
expect(result).toEqual({
|
|
139
|
+
A: {
|
|
140
|
+
B: [["USDC"]],
|
|
141
|
+
USDC: [[], ["USDC", "A"]],
|
|
142
|
+
},
|
|
143
|
+
B: {
|
|
144
|
+
USDC: [[], ["USDC", "A"]],
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
});
|
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { USD_DECIMALS } from "../../../configs/factors";
|
|
3
|
+
import { NATIVE_TOKEN_ADDRESS } from "../../../configs/tokens";
|
|
4
|
+
import { expandDecimals } from "../../numbers";
|
|
5
|
+
import { buildMarketsAdjacencyGraph } from "../buildMarketsAdjacencyGraph";
|
|
6
|
+
import { findSwapPathsBetweenTokens } from "../findSwapPathsBetweenTokens";
|
|
7
|
+
import { createMarketEdgeLiquidityGetter, createNaiveNetworkEstimator, createNaiveSwapEstimator, getMaxLiquidityMarketSwapPathFromTokenSwapPaths, getNaiveBestMarketSwapPathsFromTokenSwapPaths, getTokenSwapPathsForTokenPair, } from "../swapRouting";
|
|
8
|
+
import { mockMarketsInfoData, mockTokensData, usdToToken } from "../../../test/mock";
|
|
9
|
+
const marketKeys = [
|
|
10
|
+
"ETH-ETH-USDC",
|
|
11
|
+
"BTC-BTC-USDC",
|
|
12
|
+
"BTC-BTC-DAI",
|
|
13
|
+
"SOL-SOL-USDC",
|
|
14
|
+
"SOL-ETH-USDC",
|
|
15
|
+
"SOL-BTC-USDC",
|
|
16
|
+
"SPOT-DAI-USDC",
|
|
17
|
+
"SPOT-USDC-DAI",
|
|
18
|
+
];
|
|
19
|
+
const tokensData = mockTokensData();
|
|
20
|
+
const baseMarketsInfoData = mockMarketsInfoData(tokensData, marketKeys);
|
|
21
|
+
const marketAdjacencyGraph = buildMarketsAdjacencyGraph(baseMarketsInfoData);
|
|
22
|
+
const swapPaths = findSwapPathsBetweenTokens(marketAdjacencyGraph);
|
|
23
|
+
const baseGasLimits = {
|
|
24
|
+
decreaseOrder: 4000000n,
|
|
25
|
+
depositToken: 1800000n,
|
|
26
|
+
estimatedFeeMultiplierFactor: 1000000000000000000000000000000n,
|
|
27
|
+
estimatedGasFeeBaseAmount: 600000n,
|
|
28
|
+
estimatedGasFeePerOraclePrice: 250000n,
|
|
29
|
+
glvDepositGasLimit: 2000000n,
|
|
30
|
+
glvPerMarketGasLimit: 100000n,
|
|
31
|
+
glvWithdrawalGasLimit: 2000000n,
|
|
32
|
+
increaseOrder: 4000000n,
|
|
33
|
+
shift: 2500000n,
|
|
34
|
+
singleSwap: 1000000n,
|
|
35
|
+
swapOrder: 3000000n,
|
|
36
|
+
withdrawalMultiToken: 1500000n,
|
|
37
|
+
};
|
|
38
|
+
const baseGasPrice = 1650000002n;
|
|
39
|
+
describe("mockRouting", () => {
|
|
40
|
+
it("selects SPOT [USDC-DAI] path for USDC->DAI swap when pool has excess of DAI", () => {
|
|
41
|
+
const allPoolsBalanced = Object.fromEntries(marketKeys.map((marketKey) => [
|
|
42
|
+
marketKey,
|
|
43
|
+
{
|
|
44
|
+
marketKey,
|
|
45
|
+
longPoolAmount: usdToToken(1000, baseMarketsInfoData[marketKey].longToken),
|
|
46
|
+
shortPoolAmount: usdToToken(1000, baseMarketsInfoData[marketKey].shortToken),
|
|
47
|
+
},
|
|
48
|
+
]));
|
|
49
|
+
const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
|
|
50
|
+
...allPoolsBalanced,
|
|
51
|
+
"SPOT-USDC-DAI": {
|
|
52
|
+
longPoolAmount: usdToToken(600, tokensData.USDC),
|
|
53
|
+
shortPoolAmount: usdToToken(1100, tokensData.DAI),
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "DAI");
|
|
57
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData);
|
|
58
|
+
const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
|
|
59
|
+
graph: marketAdjacencyGraph,
|
|
60
|
+
tokenSwapPaths,
|
|
61
|
+
topPathsCount: 1,
|
|
62
|
+
tokenInAddress: "USDC",
|
|
63
|
+
tokenOutAddress: "DAI",
|
|
64
|
+
estimator,
|
|
65
|
+
usdIn: expandDecimals(100n, USD_DECIMALS),
|
|
66
|
+
});
|
|
67
|
+
const topPath = paths?.[0];
|
|
68
|
+
expect(topPath).toBeDefined();
|
|
69
|
+
expect(topPath).toEqual(["SPOT-USDC-DAI"]);
|
|
70
|
+
});
|
|
71
|
+
it("selects SPOT [DAI-USDC] path for USDC->DAI swap when pool has excess of DAI", () => {
|
|
72
|
+
const allPoolsBalanced = Object.fromEntries(marketKeys.map((marketKey) => [
|
|
73
|
+
marketKey,
|
|
74
|
+
{
|
|
75
|
+
longPoolAmount: usdToToken(100000, baseMarketsInfoData[marketKey].longToken),
|
|
76
|
+
shortPoolAmount: usdToToken(100000, baseMarketsInfoData[marketKey].shortToken),
|
|
77
|
+
maxLongPoolAmount: usdToToken(1000000, baseMarketsInfoData[marketKey].longToken),
|
|
78
|
+
maxShortPoolAmount: usdToToken(1000000, baseMarketsInfoData[marketKey].shortToken),
|
|
79
|
+
},
|
|
80
|
+
]));
|
|
81
|
+
const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
|
|
82
|
+
...allPoolsBalanced,
|
|
83
|
+
"SPOT-DAI-USDC": {
|
|
84
|
+
longPoolAmount: usdToToken(110000, tokensData.DAI),
|
|
85
|
+
shortPoolAmount: usdToToken(60000, tokensData.USDC),
|
|
86
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.DAI),
|
|
87
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "DAI");
|
|
91
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData);
|
|
92
|
+
const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
|
|
93
|
+
graph: marketAdjacencyGraph,
|
|
94
|
+
tokenSwapPaths,
|
|
95
|
+
topPathsCount: 1,
|
|
96
|
+
tokenInAddress: "USDC",
|
|
97
|
+
tokenOutAddress: "DAI",
|
|
98
|
+
estimator,
|
|
99
|
+
usdIn: expandDecimals(100n, USD_DECIMALS),
|
|
100
|
+
});
|
|
101
|
+
const topPath = paths?.[0];
|
|
102
|
+
expect(topPath).toBeDefined();
|
|
103
|
+
expect(topPath).toEqual(["SPOT-DAI-USDC"]);
|
|
104
|
+
});
|
|
105
|
+
it("routes through BTC [BTC-USDC] -> SOL [BTC-USDC] -> BTC [BTC-DAI] for optimal pricing", () => {
|
|
106
|
+
const allPoolsBalanced = Object.fromEntries(marketKeys.map((marketKey) => [
|
|
107
|
+
marketKey,
|
|
108
|
+
{
|
|
109
|
+
longPoolAmount: usdToToken(100000, baseMarketsInfoData[marketKey].longToken),
|
|
110
|
+
maxLongPoolAmount: usdToToken(1000000, baseMarketsInfoData[marketKey].longToken),
|
|
111
|
+
shortPoolAmount: usdToToken(100000, baseMarketsInfoData[marketKey].shortToken),
|
|
112
|
+
maxShortPoolAmount: usdToToken(1000000, baseMarketsInfoData[marketKey].shortToken),
|
|
113
|
+
},
|
|
114
|
+
]));
|
|
115
|
+
const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
|
|
116
|
+
...allPoolsBalanced,
|
|
117
|
+
// Desired swaps BTC -> USDC, USDC -> BTC, BTC -> DAI
|
|
118
|
+
// 1st desired swap BTC -> USDC, profitable
|
|
119
|
+
"BTC-BTC-USDC": {
|
|
120
|
+
longPoolAmount: usdToToken(60000, tokensData.BTC),
|
|
121
|
+
shortPoolAmount: usdToToken(120000, tokensData.USDC),
|
|
122
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.BTC),
|
|
123
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
124
|
+
},
|
|
125
|
+
// 2nd desired swap USDC -> BTC, back to BTC but profitable
|
|
126
|
+
"SOL-BTC-USDC": {
|
|
127
|
+
longPoolAmount: usdToToken(120000, tokensData.BTC),
|
|
128
|
+
shortPoolAmount: usdToToken(60000, tokensData.USDC),
|
|
129
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.BTC),
|
|
130
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
131
|
+
},
|
|
132
|
+
// 3d desired swap BTC -> DAI, just balanced
|
|
133
|
+
"BTC-BTC-DAI": {
|
|
134
|
+
longPoolAmount: usdToToken(100000, tokensData.BTC),
|
|
135
|
+
shortPoolAmount: usdToToken(100000, tokensData.DAI),
|
|
136
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.BTC),
|
|
137
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.DAI),
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "BTC", "DAI");
|
|
141
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData);
|
|
142
|
+
const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
|
|
143
|
+
graph: marketAdjacencyGraph,
|
|
144
|
+
tokenSwapPaths,
|
|
145
|
+
topPathsCount: 1,
|
|
146
|
+
tokenInAddress: "BTC",
|
|
147
|
+
tokenOutAddress: "DAI",
|
|
148
|
+
estimator,
|
|
149
|
+
usdIn: expandDecimals(30000n, USD_DECIMALS),
|
|
150
|
+
});
|
|
151
|
+
const topPath = paths?.[0];
|
|
152
|
+
expect(topPath).toBeDefined();
|
|
153
|
+
expect(topPath).toEqual(["BTC-BTC-USDC", "SOL-BTC-USDC", "BTC-BTC-DAI"]);
|
|
154
|
+
});
|
|
155
|
+
it("does not route USDC-BTC through BTC [BTC-USDC] -> SOL [BTC-USDC] -> BTC [BTC-USDC] for imbalanced liquidity, because of duplicate BTC-BTC-USDC", () => {
|
|
156
|
+
const allPoolsBalanced = Object.fromEntries(marketKeys.map((marketKey) => [
|
|
157
|
+
marketKey,
|
|
158
|
+
{
|
|
159
|
+
longPoolAmount: usdToToken(100000, baseMarketsInfoData[marketKey].longToken),
|
|
160
|
+
shortPoolAmount: usdToToken(100000, baseMarketsInfoData[marketKey].shortToken),
|
|
161
|
+
maxLongPoolAmount: usdToToken(1000000, baseMarketsInfoData[marketKey].longToken),
|
|
162
|
+
maxShortPoolAmount: usdToToken(1000000, baseMarketsInfoData[marketKey].shortToken),
|
|
163
|
+
},
|
|
164
|
+
]));
|
|
165
|
+
const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
|
|
166
|
+
...allPoolsBalanced,
|
|
167
|
+
// Desired swap USDC -> BTC -> USDC -> BTC, but first and third steps are through BTC-BTC-USDC
|
|
168
|
+
// 1st and 3rd desired swap USDC -> BTC, profitable
|
|
169
|
+
"BTC-BTC-USDC": {
|
|
170
|
+
// Huge imbalance
|
|
171
|
+
longPoolAmount: usdToToken(120000, tokensData.BTC),
|
|
172
|
+
shortPoolAmount: usdToToken(20000, tokensData.USDC),
|
|
173
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.BTC),
|
|
174
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
175
|
+
},
|
|
176
|
+
// 2nd desired swap BTC -> USDC, back to BTC but profitable
|
|
177
|
+
"SOL-BTC-USDC": {
|
|
178
|
+
longPoolAmount: usdToToken(70000, tokensData.BTC),
|
|
179
|
+
shortPoolAmount: usdToToken(100000, tokensData.USDC),
|
|
180
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.BTC),
|
|
181
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "BTC");
|
|
185
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData);
|
|
186
|
+
const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
|
|
187
|
+
graph: marketAdjacencyGraph,
|
|
188
|
+
tokenSwapPaths,
|
|
189
|
+
topPathsCount: 1,
|
|
190
|
+
tokenInAddress: "USDC",
|
|
191
|
+
tokenOutAddress: "BTC",
|
|
192
|
+
estimator,
|
|
193
|
+
usdIn: expandDecimals(30000n, USD_DECIMALS),
|
|
194
|
+
});
|
|
195
|
+
const topPath = paths?.[0];
|
|
196
|
+
expect(topPath).toBeDefined();
|
|
197
|
+
expect(topPath).toEqual(["BTC-BTC-USDC"]);
|
|
198
|
+
});
|
|
199
|
+
it("selects BTC [BTC-USDC] direct path when impact factors penalize multi-hop routes", () => {
|
|
200
|
+
const allPoolsBalanced = Object.fromEntries(marketKeys.map((marketKey) => [
|
|
201
|
+
marketKey,
|
|
202
|
+
{
|
|
203
|
+
// ensure non relevant markets have enough liquidity, so that test includes all relevant markets but chooses desired
|
|
204
|
+
longPoolAmount: usdToToken(500000, baseMarketsInfoData[marketKey].longToken),
|
|
205
|
+
shortPoolAmount: usdToToken(500000, baseMarketsInfoData[marketKey].shortToken),
|
|
206
|
+
maxLongPoolAmount: usdToToken(1000000, baseMarketsInfoData[marketKey].longToken),
|
|
207
|
+
maxShortPoolAmount: usdToToken(1000000, baseMarketsInfoData[marketKey].shortToken),
|
|
208
|
+
},
|
|
209
|
+
]));
|
|
210
|
+
const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
|
|
211
|
+
...allPoolsBalanced,
|
|
212
|
+
// Desired swap USDC -> BTC. even with huge imbalance because second swap through BTC-BTC-USDC would be bad
|
|
213
|
+
// 1st and 3rd desired swap USDC -> BTC, profitable
|
|
214
|
+
"BTC-BTC-USDC": {
|
|
215
|
+
// Huge imbalance
|
|
216
|
+
longPoolAmount: usdToToken(150000, tokensData.BTC),
|
|
217
|
+
shortPoolAmount: usdToToken(20000, tokensData.USDC),
|
|
218
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.BTC),
|
|
219
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
220
|
+
swapImpactFactorPositive: expandDecimals(1, 23),
|
|
221
|
+
// Punish super hard for worsening price impact
|
|
222
|
+
swapImpactFactorNegative: expandDecimals(3, 23),
|
|
223
|
+
},
|
|
224
|
+
// trap step to trick algorithm into picking 3 step path
|
|
225
|
+
"SOL-BTC-USDC": {
|
|
226
|
+
longPoolAmount: usdToToken(80000, tokensData.BTC),
|
|
227
|
+
shortPoolAmount: usdToToken(100000, tokensData.USDC),
|
|
228
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.BTC),
|
|
229
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
230
|
+
swapImpactFactorPositive: expandDecimals(1, 23),
|
|
231
|
+
// Punish super hard for worsening price impact
|
|
232
|
+
swapImpactFactorNegative: expandDecimals(3, 23),
|
|
233
|
+
},
|
|
234
|
+
});
|
|
235
|
+
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "BTC");
|
|
236
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData);
|
|
237
|
+
const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
|
|
238
|
+
graph: marketAdjacencyGraph,
|
|
239
|
+
tokenSwapPaths,
|
|
240
|
+
topPathsCount: 1,
|
|
241
|
+
tokenInAddress: "USDC",
|
|
242
|
+
tokenOutAddress: "BTC",
|
|
243
|
+
estimator,
|
|
244
|
+
usdIn: expandDecimals(70000n, USD_DECIMALS),
|
|
245
|
+
});
|
|
246
|
+
const topPath = paths?.[0];
|
|
247
|
+
expect(topPath).toBeDefined();
|
|
248
|
+
expect(topPath).toEqual(["BTC-BTC-USDC"]);
|
|
249
|
+
});
|
|
250
|
+
it("selects BTC [BTC-DAI] direct path when high gas fees outweigh multi-hop benefits", () => {
|
|
251
|
+
const allPoolsBalanced = Object.fromEntries(marketKeys.map((marketKey) => [
|
|
252
|
+
marketKey,
|
|
253
|
+
{
|
|
254
|
+
longPoolAmount: usdToToken(100000, baseMarketsInfoData[marketKey].longToken),
|
|
255
|
+
maxLongPoolAmount: usdToToken(1000000, baseMarketsInfoData[marketKey].longToken),
|
|
256
|
+
shortPoolAmount: usdToToken(100000, baseMarketsInfoData[marketKey].shortToken),
|
|
257
|
+
maxShortPoolAmount: usdToToken(1000000, baseMarketsInfoData[marketKey].shortToken),
|
|
258
|
+
},
|
|
259
|
+
]));
|
|
260
|
+
const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
|
|
261
|
+
...allPoolsBalanced,
|
|
262
|
+
// Desired swaps BTC -> USDC, USDC -> BTC, BTC -> DAI
|
|
263
|
+
// 1st desired swap BTC -> USDC, profitable
|
|
264
|
+
"BTC-BTC-USDC": {
|
|
265
|
+
longPoolAmount: usdToToken(60000, tokensData.BTC),
|
|
266
|
+
shortPoolAmount: usdToToken(120000, tokensData.USDC),
|
|
267
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.BTC),
|
|
268
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
269
|
+
},
|
|
270
|
+
// 2nd desired swap USDC -> BTC, back to BTC but profitable
|
|
271
|
+
"SOL-BTC-USDC": {
|
|
272
|
+
longPoolAmount: usdToToken(120000, tokensData.BTC),
|
|
273
|
+
shortPoolAmount: usdToToken(60000, tokensData.USDC),
|
|
274
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.BTC),
|
|
275
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.USDC),
|
|
276
|
+
},
|
|
277
|
+
// 3d desired swap BTC -> DAI, just balanced
|
|
278
|
+
"BTC-BTC-DAI": {
|
|
279
|
+
longPoolAmount: usdToToken(100000, tokensData.BTC),
|
|
280
|
+
shortPoolAmount: usdToToken(100000, tokensData.DAI),
|
|
281
|
+
maxLongPoolAmount: usdToToken(1000000, tokensData.BTC),
|
|
282
|
+
maxShortPoolAmount: usdToToken(1000000, tokensData.DAI),
|
|
283
|
+
},
|
|
284
|
+
});
|
|
285
|
+
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "BTC", "DAI");
|
|
286
|
+
const estimator = createNaiveSwapEstimator(marketsInfoData);
|
|
287
|
+
const fakeMultiplier = 100n;
|
|
288
|
+
const networkEstimator = createNaiveNetworkEstimator({
|
|
289
|
+
chainId: 1,
|
|
290
|
+
gasLimits: {
|
|
291
|
+
...baseGasLimits,
|
|
292
|
+
singleSwap: baseGasLimits.singleSwap * fakeMultiplier,
|
|
293
|
+
},
|
|
294
|
+
gasPrice: baseGasPrice * fakeMultiplier,
|
|
295
|
+
tokensData: {
|
|
296
|
+
...tokensData,
|
|
297
|
+
[NATIVE_TOKEN_ADDRESS]: {
|
|
298
|
+
...tokensData.ETH,
|
|
299
|
+
address: NATIVE_TOKEN_ADDRESS,
|
|
300
|
+
},
|
|
301
|
+
},
|
|
302
|
+
});
|
|
303
|
+
const paths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
|
|
304
|
+
graph: marketAdjacencyGraph,
|
|
305
|
+
tokenSwapPaths,
|
|
306
|
+
topPathsCount: 1,
|
|
307
|
+
tokenInAddress: "BTC",
|
|
308
|
+
tokenOutAddress: "DAI",
|
|
309
|
+
estimator,
|
|
310
|
+
usdIn: expandDecimals(30000n, USD_DECIMALS),
|
|
311
|
+
networkEstimator,
|
|
312
|
+
});
|
|
313
|
+
const topPath = paths?.[0];
|
|
314
|
+
expect(topPath).toBeDefined();
|
|
315
|
+
expect(topPath).toEqual(["BTC-BTC-DAI"]);
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
describe("getMaxLiquidityMarketSwapPathFromTokenSwapPaths", () => {
|
|
319
|
+
it("selects SPOT [USDC-DAI] path when it has highest liquidity", () => {
|
|
320
|
+
const allPoolsBalanced = Object.fromEntries(marketKeys.map((marketKey) => [
|
|
321
|
+
marketKey,
|
|
322
|
+
{
|
|
323
|
+
marketKey,
|
|
324
|
+
longPoolAmount: usdToToken(1000, baseMarketsInfoData[marketKey].longToken),
|
|
325
|
+
shortPoolAmount: usdToToken(1000, baseMarketsInfoData[marketKey].shortToken),
|
|
326
|
+
},
|
|
327
|
+
]));
|
|
328
|
+
const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
|
|
329
|
+
...allPoolsBalanced,
|
|
330
|
+
"SPOT-USDC-DAI": {
|
|
331
|
+
longPoolAmount: usdToToken(2000, tokensData.USDC),
|
|
332
|
+
shortPoolAmount: usdToToken(2000, tokensData.DAI),
|
|
333
|
+
},
|
|
334
|
+
"SPOT-DAI-USDC": {
|
|
335
|
+
longPoolAmount: usdToToken(1000, tokensData.USDC),
|
|
336
|
+
shortPoolAmount: usdToToken(1000, tokensData.DAI),
|
|
337
|
+
},
|
|
338
|
+
});
|
|
339
|
+
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "USDC", "DAI");
|
|
340
|
+
const result = getMaxLiquidityMarketSwapPathFromTokenSwapPaths({
|
|
341
|
+
graph: marketAdjacencyGraph,
|
|
342
|
+
tokenSwapPaths,
|
|
343
|
+
tokenInAddress: "USDC",
|
|
344
|
+
tokenOutAddress: "DAI",
|
|
345
|
+
getLiquidity: createMarketEdgeLiquidityGetter(marketsInfoData),
|
|
346
|
+
});
|
|
347
|
+
expect(result).toEqual({
|
|
348
|
+
path: ["SPOT-USDC-DAI"],
|
|
349
|
+
liquidity: usdToToken(2000, tokensData.DAI),
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
it("selects multi-hop path when intermediate markets have higher liquidity", () => {
|
|
353
|
+
const allPoolsBalanced = Object.fromEntries(marketKeys.map((marketKey) => [
|
|
354
|
+
marketKey,
|
|
355
|
+
{
|
|
356
|
+
marketKey,
|
|
357
|
+
longPoolAmount: usdToToken(1000, baseMarketsInfoData[marketKey].longToken),
|
|
358
|
+
shortPoolAmount: usdToToken(1000, baseMarketsInfoData[marketKey].shortToken),
|
|
359
|
+
},
|
|
360
|
+
]));
|
|
361
|
+
const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
|
|
362
|
+
...allPoolsBalanced,
|
|
363
|
+
"BTC-BTC-USDC": {
|
|
364
|
+
longPoolAmount: usdToToken(3000, tokensData.BTC),
|
|
365
|
+
shortPoolAmount: usdToToken(3000, tokensData.USDC),
|
|
366
|
+
},
|
|
367
|
+
"SPOT-USDC-DAI": {
|
|
368
|
+
longPoolAmount: usdToToken(3000, tokensData.USDC),
|
|
369
|
+
shortPoolAmount: usdToToken(3000, tokensData.DAI),
|
|
370
|
+
},
|
|
371
|
+
"BTC-BTC-DAI": {
|
|
372
|
+
longPoolAmount: usdToToken(1000, tokensData.BTC),
|
|
373
|
+
shortPoolAmount: usdToToken(1000, tokensData.DAI),
|
|
374
|
+
},
|
|
375
|
+
});
|
|
376
|
+
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "BTC", "DAI");
|
|
377
|
+
const result = getMaxLiquidityMarketSwapPathFromTokenSwapPaths({
|
|
378
|
+
graph: marketAdjacencyGraph,
|
|
379
|
+
tokenSwapPaths,
|
|
380
|
+
tokenInAddress: "BTC",
|
|
381
|
+
tokenOutAddress: "DAI",
|
|
382
|
+
getLiquidity: createMarketEdgeLiquidityGetter(marketsInfoData),
|
|
383
|
+
});
|
|
384
|
+
expect(result).toEqual({
|
|
385
|
+
path: ["BTC-BTC-USDC", "SPOT-USDC-DAI"],
|
|
386
|
+
liquidity: 2000000000000000000000000000000000n,
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
it("selects direct path when multi-hop path has lower minimum liquidity", () => {
|
|
390
|
+
const allPoolsBalanced = Object.fromEntries(marketKeys.map((marketKey) => [
|
|
391
|
+
marketKey,
|
|
392
|
+
{
|
|
393
|
+
marketKey,
|
|
394
|
+
longPoolAmount: usdToToken(1000, baseMarketsInfoData[marketKey].longToken),
|
|
395
|
+
shortPoolAmount: usdToToken(1000, baseMarketsInfoData[marketKey].shortToken),
|
|
396
|
+
},
|
|
397
|
+
]));
|
|
398
|
+
const marketsInfoData = mockMarketsInfoData(tokensData, marketKeys, {
|
|
399
|
+
...allPoolsBalanced,
|
|
400
|
+
"BTC-BTC-USDC": {
|
|
401
|
+
longPoolAmount: usdToToken(1000, tokensData.BTC),
|
|
402
|
+
shortPoolAmount: usdToToken(100, tokensData.USDC),
|
|
403
|
+
},
|
|
404
|
+
"SPOT-USDC-DAI": {
|
|
405
|
+
longPoolAmount: usdToToken(1000, tokensData.USDC),
|
|
406
|
+
shortPoolAmount: usdToToken(1000, tokensData.DAI),
|
|
407
|
+
},
|
|
408
|
+
"BTC-BTC-DAI": {
|
|
409
|
+
longPoolAmount: usdToToken(1000000, tokensData.BTC),
|
|
410
|
+
shortPoolAmount: usdToToken(1000000, tokensData.DAI),
|
|
411
|
+
},
|
|
412
|
+
});
|
|
413
|
+
const tokenSwapPaths = getTokenSwapPathsForTokenPair(swapPaths, "BTC", "DAI");
|
|
414
|
+
const result = getMaxLiquidityMarketSwapPathFromTokenSwapPaths({
|
|
415
|
+
graph: marketAdjacencyGraph,
|
|
416
|
+
tokenSwapPaths,
|
|
417
|
+
tokenInAddress: "BTC",
|
|
418
|
+
tokenOutAddress: "DAI",
|
|
419
|
+
getLiquidity: createMarketEdgeLiquidityGetter(marketsInfoData),
|
|
420
|
+
});
|
|
421
|
+
// Should choose direct path because multi-hop path's minimum liquidity (500) is less than direct path (2000)
|
|
422
|
+
expect(result).toEqual({
|
|
423
|
+
path: ["BTC-BTC-DAI"],
|
|
424
|
+
liquidity: 999000000000000000000000000000000000n,
|
|
425
|
+
});
|
|
426
|
+
});
|
|
427
|
+
});
|