@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,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createFindSwapPath = exports.getWrappedAddress = void 0;
|
|
4
|
+
const markets_1 = require("../../configs/markets");
|
|
5
|
+
const tokens_1 = require("../../configs/tokens");
|
|
6
|
+
const LruCache_1 = require("../LruCache");
|
|
7
|
+
const buildMarketsAdjacencyGraph_1 = require("./buildMarketsAdjacencyGraph");
|
|
8
|
+
const swapRouting_1 = require("./swapRouting");
|
|
9
|
+
const swapStats_1 = require("./swapStats");
|
|
10
|
+
const getWrappedAddress = (chainId, address) => {
|
|
11
|
+
return address ? (0, tokens_1.convertTokenAddress)(chainId, address, "wrapped") : undefined;
|
|
12
|
+
};
|
|
13
|
+
exports.getWrappedAddress = getWrappedAddress;
|
|
14
|
+
const DEBUG_MARKET_ADJACENCY_GRAPH_CACHE = new LruCache_1.LRUCache(100);
|
|
15
|
+
function buildDebugMarketAdjacencyGraph(chainId, debugSwapMarketsConfig) {
|
|
16
|
+
if (!debugSwapMarketsConfig?.disabledSwapMarkets?.length) {
|
|
17
|
+
return (0, swapRouting_1.getMarketAdjacencyGraph)(chainId);
|
|
18
|
+
}
|
|
19
|
+
const cacheKey = `${chainId}-${JSON.stringify(debugSwapMarketsConfig)}`;
|
|
20
|
+
const cachedGraph = DEBUG_MARKET_ADJACENCY_GRAPH_CACHE.get(cacheKey);
|
|
21
|
+
if (cachedGraph) {
|
|
22
|
+
return cachedGraph;
|
|
23
|
+
}
|
|
24
|
+
const disabledMarketAddresses = debugSwapMarketsConfig.disabledSwapMarkets;
|
|
25
|
+
const strippedMarkets = Object.fromEntries(Object.entries(markets_1.MARKETS[chainId]).filter(([marketAddress]) => !disabledMarketAddresses.includes(marketAddress)));
|
|
26
|
+
const graph = (0, buildMarketsAdjacencyGraph_1.buildMarketsAdjacencyGraph)(strippedMarkets);
|
|
27
|
+
DEBUG_MARKET_ADJACENCY_GRAPH_CACHE.set(cacheKey, graph);
|
|
28
|
+
return graph;
|
|
29
|
+
}
|
|
30
|
+
const FALLBACK_FIND_SWAP_PATH = () => undefined;
|
|
31
|
+
const createFindSwapPath = (params) => {
|
|
32
|
+
const { chainId, fromTokenAddress, toTokenAddress, marketsInfoData, debugSwapMarketsConfig, gasEstimationParams } = params;
|
|
33
|
+
const wrappedFromAddress = (0, exports.getWrappedAddress)(chainId, fromTokenAddress);
|
|
34
|
+
const wrappedToAddress = (0, exports.getWrappedAddress)(chainId, toTokenAddress);
|
|
35
|
+
const wrappedToken = (0, tokens_1.getWrappedToken)(chainId);
|
|
36
|
+
const tokenSwapPaths = wrappedFromAddress && wrappedToAddress
|
|
37
|
+
? (0, swapRouting_1.getTokenSwapPathsForTokenPairPrebuilt)(chainId, wrappedFromAddress, wrappedToAddress)
|
|
38
|
+
: [];
|
|
39
|
+
const marketAdjacencyGraph = debugSwapMarketsConfig?.disabledSwapMarkets?.length
|
|
40
|
+
? buildDebugMarketAdjacencyGraph(chainId, debugSwapMarketsConfig)
|
|
41
|
+
: (0, swapRouting_1.getMarketAdjacencyGraph)(chainId);
|
|
42
|
+
const cache = {};
|
|
43
|
+
if (!marketsInfoData) {
|
|
44
|
+
return FALLBACK_FIND_SWAP_PATH;
|
|
45
|
+
}
|
|
46
|
+
const marketEdgeLiquidityGetter = (0, swapRouting_1.createMarketEdgeLiquidityGetter)(marketsInfoData);
|
|
47
|
+
const naiveEstimator = (0, swapRouting_1.createNaiveSwapEstimator)(marketsInfoData);
|
|
48
|
+
const naiveNetworkEstimator = gasEstimationParams
|
|
49
|
+
? (0, swapRouting_1.createNaiveNetworkEstimator)({
|
|
50
|
+
gasLimits: gasEstimationParams.gasLimits,
|
|
51
|
+
tokensData: gasEstimationParams.tokensData,
|
|
52
|
+
gasPrice: gasEstimationParams.gasPrice,
|
|
53
|
+
chainId,
|
|
54
|
+
})
|
|
55
|
+
: undefined;
|
|
56
|
+
const estimator = (0, swapRouting_1.createSwapEstimator)(marketsInfoData);
|
|
57
|
+
const findSwapPath = (usdIn, opts) => {
|
|
58
|
+
if (tokenSwapPaths.length === 0 || !fromTokenAddress || !wrappedFromAddress || !wrappedToAddress) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
const cacheKey = `${usdIn}-${opts?.order?.join("-") || "none"}`;
|
|
62
|
+
if (cache[cacheKey]) {
|
|
63
|
+
return cache[cacheKey];
|
|
64
|
+
}
|
|
65
|
+
let swapPath = undefined;
|
|
66
|
+
if (debugSwapMarketsConfig?.manualPath !== undefined) {
|
|
67
|
+
swapPath = debugSwapMarketsConfig.manualPath;
|
|
68
|
+
}
|
|
69
|
+
else if (opts?.order || usdIn === 0n) {
|
|
70
|
+
const primaryOrder = opts?.order?.at(0) === "length" ? "length" : "liquidity";
|
|
71
|
+
if (!marketEdgeLiquidityGetter) {
|
|
72
|
+
swapPath = undefined;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
let applicableTokenSwapPaths = tokenSwapPaths;
|
|
76
|
+
if (primaryOrder === "length") {
|
|
77
|
+
const shortestLength = Math.min(...tokenSwapPaths.map((path) => path.length));
|
|
78
|
+
applicableTokenSwapPaths = tokenSwapPaths.filter((path) => path.length === shortestLength);
|
|
79
|
+
}
|
|
80
|
+
const maxLiquidityPathInfo = (0, swapRouting_1.getMaxLiquidityMarketSwapPathFromTokenSwapPaths)({
|
|
81
|
+
graph: marketAdjacencyGraph,
|
|
82
|
+
tokenSwapPaths: applicableTokenSwapPaths,
|
|
83
|
+
tokenInAddress: wrappedFromAddress,
|
|
84
|
+
tokenOutAddress: wrappedToAddress,
|
|
85
|
+
getLiquidity: marketEdgeLiquidityGetter,
|
|
86
|
+
});
|
|
87
|
+
if (maxLiquidityPathInfo) {
|
|
88
|
+
swapPath = maxLiquidityPathInfo.path;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
if (naiveEstimator) {
|
|
94
|
+
const naiveSwapRoutes = (0, swapRouting_1.getNaiveBestMarketSwapPathsFromTokenSwapPaths)({
|
|
95
|
+
graph: marketAdjacencyGraph,
|
|
96
|
+
tokenSwapPaths,
|
|
97
|
+
usdIn,
|
|
98
|
+
tokenInAddress: wrappedFromAddress,
|
|
99
|
+
tokenOutAddress: wrappedToAddress,
|
|
100
|
+
estimator: naiveEstimator,
|
|
101
|
+
networkEstimator: naiveNetworkEstimator,
|
|
102
|
+
});
|
|
103
|
+
if (naiveSwapRoutes?.length) {
|
|
104
|
+
const edges = naiveSwapRoutes.map((path) => (0, swapRouting_1.marketRouteToMarketEdges)(path, wrappedFromAddress, marketsInfoData));
|
|
105
|
+
swapPath = (0, swapRouting_1.getBestSwapPath)({
|
|
106
|
+
routes: edges,
|
|
107
|
+
usdIn,
|
|
108
|
+
estimator,
|
|
109
|
+
networkEstimator: naiveNetworkEstimator,
|
|
110
|
+
})?.map((edge) => edge.marketAddress);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (!swapPath) {
|
|
115
|
+
cache[cacheKey] = undefined;
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
let result = (0, swapStats_1.getSwapPathStats)({
|
|
119
|
+
marketsInfoData,
|
|
120
|
+
swapPath,
|
|
121
|
+
initialCollateralAddress: fromTokenAddress,
|
|
122
|
+
wrappedNativeTokenAddress: wrappedToken.address,
|
|
123
|
+
shouldUnwrapNativeToken: toTokenAddress === tokens_1.NATIVE_TOKEN_ADDRESS,
|
|
124
|
+
shouldApplyPriceImpact: true,
|
|
125
|
+
usdIn,
|
|
126
|
+
});
|
|
127
|
+
cache[cacheKey] = result;
|
|
128
|
+
return result;
|
|
129
|
+
};
|
|
130
|
+
return findSwapPath;
|
|
131
|
+
};
|
|
132
|
+
exports.createFindSwapPath = createFindSwapPath;
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMaxLiquidityMarketForTokenEdge = exports.getMaxLiquidityMarketSwapPathFromTokenSwapPaths = exports.findAllReachableTokens = exports.getMarketAdjacencyGraph = exports.getTokenSwapPathsForTokenPairPrebuilt = exports.getTokenSwapPathsForTokenPair = exports.marketRouteToMarketEdges = exports.getBestMarketForTokenEdge = exports.getMarketsForTokenPair = exports.getNaiveBestMarketSwapPathsFromTokenSwapPaths = exports.getBestSwapPath = exports.createNaiveNetworkEstimator = exports.createNaiveSwapEstimator = exports.createMarketEdgeLiquidityGetter = exports.createSwapEstimator = void 0;
|
|
4
|
+
const viem_1 = require("viem");
|
|
5
|
+
const bigmath_1 = require("../bigmath");
|
|
6
|
+
const fees_1 = require("../fees");
|
|
7
|
+
const getNaiveEstimatedGasBySwapCount_1 = require("../fees/getNaiveEstimatedGasBySwapCount");
|
|
8
|
+
const markets_1 = require("../markets");
|
|
9
|
+
const numbers_1 = require("../numbers");
|
|
10
|
+
const objects_1 = require("../objects");
|
|
11
|
+
const constants_1 = require("./constants");
|
|
12
|
+
const preparedSwapData_1 = require("./preparedSwapData");
|
|
13
|
+
const swapStats_1 = require("./swapStats");
|
|
14
|
+
const createSwapEstimator = (marketsInfoData) => {
|
|
15
|
+
return (e, usdIn) => {
|
|
16
|
+
const marketInfo = marketsInfoData[e.marketAddress];
|
|
17
|
+
if (!marketInfo || marketInfo.isDisabled) {
|
|
18
|
+
return {
|
|
19
|
+
usdOut: 0n,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const swapStats = (0, swapStats_1.getSwapStats)({
|
|
23
|
+
marketInfo,
|
|
24
|
+
usdIn,
|
|
25
|
+
tokenInAddress: e.from,
|
|
26
|
+
tokenOutAddress: e.to,
|
|
27
|
+
shouldApplyPriceImpact: true,
|
|
28
|
+
});
|
|
29
|
+
const isOutLiquidity = swapStats?.isOutLiquidity;
|
|
30
|
+
const isOutCapacity = swapStats?.isOutCapacity;
|
|
31
|
+
const usdOut = swapStats?.usdOut;
|
|
32
|
+
if (usdOut === undefined || isOutLiquidity || isOutCapacity) {
|
|
33
|
+
return {
|
|
34
|
+
usdOut: 0n,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
usdOut,
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
exports.createSwapEstimator = createSwapEstimator;
|
|
43
|
+
const createMarketEdgeLiquidityGetter = (marketsInfoData) => {
|
|
44
|
+
return (e) => {
|
|
45
|
+
const marketInfo = (0, objects_1.getByKey)(marketsInfoData, e.marketAddress);
|
|
46
|
+
if (!marketInfo || marketInfo.isDisabled) {
|
|
47
|
+
return 0n;
|
|
48
|
+
}
|
|
49
|
+
const isTokenOutLong = (0, markets_1.getTokenPoolType)(marketInfo, e.to) === "long";
|
|
50
|
+
const liquidity = (0, markets_1.getAvailableUsdLiquidityForCollateral)(marketInfo, isTokenOutLong);
|
|
51
|
+
return liquidity;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
exports.createMarketEdgeLiquidityGetter = createMarketEdgeLiquidityGetter;
|
|
55
|
+
const createNaiveSwapEstimator = (marketsInfoData) => {
|
|
56
|
+
return (e, usdIn) => {
|
|
57
|
+
let marketInfo = marketsInfoData[e.marketAddress];
|
|
58
|
+
if (marketInfo === undefined || marketInfo.isDisabled) {
|
|
59
|
+
return { swapYield: 0 };
|
|
60
|
+
}
|
|
61
|
+
const swapStats = (0, swapStats_1.getSwapStats)({
|
|
62
|
+
marketInfo,
|
|
63
|
+
usdIn,
|
|
64
|
+
tokenInAddress: e.from,
|
|
65
|
+
tokenOutAddress: e.to,
|
|
66
|
+
shouldApplyPriceImpact: true,
|
|
67
|
+
});
|
|
68
|
+
const usdOut = swapStats?.usdOut;
|
|
69
|
+
if (usdOut === undefined || usdOut === 0n || swapStats.isOutCapacity || swapStats.isOutLiquidity) {
|
|
70
|
+
return { swapYield: 0 };
|
|
71
|
+
}
|
|
72
|
+
const swapYield = (0, numbers_1.bigintToNumber)((usdOut * numbers_1.PRECISION) / usdIn, numbers_1.PRECISION_DECIMALS);
|
|
73
|
+
return { swapYield };
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
exports.createNaiveSwapEstimator = createNaiveSwapEstimator;
|
|
77
|
+
const createNaiveNetworkEstimator = ({ gasLimits, tokensData, gasPrice, chainId, }) => {
|
|
78
|
+
return (usdIn, swapsCount) => {
|
|
79
|
+
const estimatedGas = (0, getNaiveEstimatedGasBySwapCount_1.getNaiveEstimatedGasBySwapCount)(gasLimits.singleSwap, swapsCount);
|
|
80
|
+
if (estimatedGas === null || estimatedGas === undefined)
|
|
81
|
+
return { networkYield: 1.0, usdOut: usdIn };
|
|
82
|
+
const oraclePriceCount = (0, fees_1.estimateOrderOraclePriceCount)(swapsCount);
|
|
83
|
+
const feeUsd = (0, fees_1.getExecutionFee)(chainId, gasLimits, tokensData, estimatedGas, gasPrice, oraclePriceCount)?.feeUsd;
|
|
84
|
+
if (feeUsd === undefined)
|
|
85
|
+
return { networkYield: 1.0, usdOut: usdIn };
|
|
86
|
+
const networkYield = (0, numbers_1.bigintToNumber)(bigmath_1.bigMath.mulDiv(usdIn, numbers_1.PRECISION, usdIn + feeUsd), numbers_1.PRECISION_DECIMALS);
|
|
87
|
+
return { networkYield, usdOut: usdIn - feeUsd };
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
exports.createNaiveNetworkEstimator = createNaiveNetworkEstimator;
|
|
91
|
+
function getBestSwapPath({ routes, usdIn, estimator, networkEstimator, }) {
|
|
92
|
+
if (routes.length === 0) {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
let bestRoute = routes[0];
|
|
96
|
+
let bestUsdOut = 0n;
|
|
97
|
+
for (const route of routes) {
|
|
98
|
+
try {
|
|
99
|
+
let pathUsdOut = route.reduce((prevUsdOut, edge) => {
|
|
100
|
+
const { usdOut } = estimator(edge, prevUsdOut);
|
|
101
|
+
return usdOut;
|
|
102
|
+
}, usdIn);
|
|
103
|
+
if (networkEstimator) {
|
|
104
|
+
const { usdOut } = networkEstimator(pathUsdOut, route.length);
|
|
105
|
+
pathUsdOut = usdOut;
|
|
106
|
+
}
|
|
107
|
+
if (pathUsdOut > bestUsdOut) {
|
|
108
|
+
bestRoute = route;
|
|
109
|
+
bestUsdOut = pathUsdOut;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
catch (e) {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return bestRoute;
|
|
117
|
+
}
|
|
118
|
+
exports.getBestSwapPath = getBestSwapPath;
|
|
119
|
+
function getNaiveBestMarketSwapPathsFromTokenSwapPaths({ graph, tokenSwapPaths, usdIn, tokenInAddress, tokenOutAddress, estimator, topPathsCount = constants_1.DEFAULT_NAIVE_TOP_PATHS_COUNT, networkEstimator, }) {
|
|
120
|
+
// This seems to be true, because for any path if we have performed swaps to and from token
|
|
121
|
+
// The best markets sequence is the same
|
|
122
|
+
const cachedBestMarketForTokenEdge = {};
|
|
123
|
+
const calculatedCache = {};
|
|
124
|
+
const topPaths = [];
|
|
125
|
+
const networkYieldCache = {};
|
|
126
|
+
for (const pathType of tokenSwapPaths) {
|
|
127
|
+
const marketPath = [];
|
|
128
|
+
let pathTypeSwapYield = 1;
|
|
129
|
+
let bad = false;
|
|
130
|
+
// Just how many times we have swapped from token A to token B
|
|
131
|
+
const tokenSwapCounter = {};
|
|
132
|
+
for (let hopIndex = 0; hopIndex <= pathType.length; hopIndex++) {
|
|
133
|
+
const tokenHopFromAddress = hopIndex === 0 ? tokenInAddress : pathType[hopIndex - 1];
|
|
134
|
+
const tokenHopToAddress = hopIndex === pathType.length ? tokenOutAddress : pathType[hopIndex];
|
|
135
|
+
// prevTokenAddress -> tokenAddress
|
|
136
|
+
// get all markets for prevTokenAddress -> tokenAddress
|
|
137
|
+
const marketAddresses = getMarketsForTokenPair(graph, tokenHopFromAddress, tokenHopToAddress);
|
|
138
|
+
if (marketAddresses.length === 0) {
|
|
139
|
+
bad = true;
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
const tokenSwapCount = tokenSwapCounter[tokenHopFromAddress]?.[tokenHopToAddress] || 0;
|
|
143
|
+
const key = `${tokenHopFromAddress}-${tokenHopToAddress}-${tokenSwapCount}`;
|
|
144
|
+
let bestMarketInfo = cachedBestMarketForTokenEdge[key];
|
|
145
|
+
if (!bestMarketInfo) {
|
|
146
|
+
calculatedCache[tokenHopFromAddress] = calculatedCache[tokenHopFromAddress] || {};
|
|
147
|
+
calculatedCache[tokenHopFromAddress][tokenHopToAddress] =
|
|
148
|
+
calculatedCache[tokenHopFromAddress][tokenHopToAddress] || {};
|
|
149
|
+
bestMarketInfo = getBestMarketForTokenEdge({
|
|
150
|
+
marketAddresses,
|
|
151
|
+
usdIn,
|
|
152
|
+
tokenInAddress: tokenHopFromAddress,
|
|
153
|
+
tokenOutAddress: tokenHopToAddress,
|
|
154
|
+
estimator,
|
|
155
|
+
marketPath,
|
|
156
|
+
calculatedCache: calculatedCache[tokenHopFromAddress][tokenHopToAddress],
|
|
157
|
+
});
|
|
158
|
+
if (!bestMarketInfo) {
|
|
159
|
+
bad = true;
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
cachedBestMarketForTokenEdge[key] = bestMarketInfo;
|
|
163
|
+
}
|
|
164
|
+
if (bestMarketInfo.swapYield === 0) {
|
|
165
|
+
bad = true;
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
pathTypeSwapYield *= bestMarketInfo.swapYield;
|
|
169
|
+
marketPath.push(bestMarketInfo.marketAddress);
|
|
170
|
+
tokenSwapCounter[tokenHopFromAddress] = tokenSwapCounter[tokenHopFromAddress] || {};
|
|
171
|
+
tokenSwapCounter[tokenHopFromAddress][tokenHopToAddress] =
|
|
172
|
+
(tokenSwapCounter[tokenHopFromAddress][tokenHopToAddress] || 0) + 1;
|
|
173
|
+
}
|
|
174
|
+
if (bad) {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (topPaths.length < topPathsCount) {
|
|
178
|
+
topPaths.push({ marketPath: marketPath, swapYield: pathTypeSwapYield });
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
let adjustedPathTypeSwapYield = pathTypeSwapYield;
|
|
182
|
+
if (networkEstimator) {
|
|
183
|
+
let networkYield = networkYieldCache[marketPath.length];
|
|
184
|
+
if (networkYield === undefined) {
|
|
185
|
+
networkYield = networkEstimator(usdIn, marketPath.length).networkYield;
|
|
186
|
+
networkYieldCache[marketPath.length] = networkYield;
|
|
187
|
+
}
|
|
188
|
+
adjustedPathTypeSwapYield = adjustedPathTypeSwapYield * networkYield;
|
|
189
|
+
}
|
|
190
|
+
// if yield is greater than any of the top paths, replace the one with the lowest yield
|
|
191
|
+
let minSwapYield = topPaths[0].swapYield;
|
|
192
|
+
let minSwapYieldIndex = 0;
|
|
193
|
+
for (let i = 1; i < topPaths.length; i++) {
|
|
194
|
+
if (topPaths[i].swapYield < minSwapYield) {
|
|
195
|
+
minSwapYield = topPaths[i].swapYield;
|
|
196
|
+
minSwapYieldIndex = i;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (adjustedPathTypeSwapYield > minSwapYield) {
|
|
200
|
+
topPaths[minSwapYieldIndex] = { marketPath: marketPath, swapYield: adjustedPathTypeSwapYield };
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return topPaths.map((p) => p.marketPath);
|
|
205
|
+
}
|
|
206
|
+
exports.getNaiveBestMarketSwapPathsFromTokenSwapPaths = getNaiveBestMarketSwapPathsFromTokenSwapPaths;
|
|
207
|
+
function getMarketsForTokenPair(graph, tokenAAddress, tokenBAddress) {
|
|
208
|
+
if (graph[tokenAAddress]?.[tokenBAddress]) {
|
|
209
|
+
return graph[tokenAAddress][tokenBAddress];
|
|
210
|
+
}
|
|
211
|
+
if (graph[tokenBAddress]?.[tokenAAddress]) {
|
|
212
|
+
return graph[tokenBAddress][tokenAAddress];
|
|
213
|
+
}
|
|
214
|
+
return [];
|
|
215
|
+
}
|
|
216
|
+
exports.getMarketsForTokenPair = getMarketsForTokenPair;
|
|
217
|
+
function getBestMarketForTokenEdge({ marketAddresses, usdIn, tokenInAddress, tokenOutAddress, estimator, marketPath, calculatedCache, }) {
|
|
218
|
+
let bestMarketAddress = marketAddresses[0];
|
|
219
|
+
let bestYield = 0;
|
|
220
|
+
let found = false;
|
|
221
|
+
for (const marketAddress of marketAddresses) {
|
|
222
|
+
if (marketPath && marketPath.includes(marketAddress)) {
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
let swapYield = undefined;
|
|
226
|
+
const key = marketAddress;
|
|
227
|
+
if (calculatedCache) {
|
|
228
|
+
swapYield = calculatedCache[key];
|
|
229
|
+
}
|
|
230
|
+
if (swapYield === undefined) {
|
|
231
|
+
swapYield = estimator({
|
|
232
|
+
marketAddress,
|
|
233
|
+
from: tokenInAddress,
|
|
234
|
+
to: tokenOutAddress,
|
|
235
|
+
}, usdIn).swapYield;
|
|
236
|
+
if (calculatedCache) {
|
|
237
|
+
calculatedCache[key] = swapYield;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (swapYield > bestYield) {
|
|
241
|
+
bestYield = swapYield;
|
|
242
|
+
bestMarketAddress = marketAddress;
|
|
243
|
+
found = true;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (!found) {
|
|
247
|
+
return undefined;
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
marketAddress: bestMarketAddress,
|
|
251
|
+
swapYield: bestYield,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
exports.getBestMarketForTokenEdge = getBestMarketForTokenEdge;
|
|
255
|
+
function marketRouteToMarketEdges(marketPath, from, marketsInfoData) {
|
|
256
|
+
let edges = [];
|
|
257
|
+
for (let i = 0; i < marketPath.length; i++) {
|
|
258
|
+
const currentFrom = i === 0 ? from : edges[i - 1].to;
|
|
259
|
+
const currentTo = marketsInfoData[marketPath[i]].longTokenAddress === currentFrom
|
|
260
|
+
? marketsInfoData[marketPath[i]].shortTokenAddress
|
|
261
|
+
: marketsInfoData[marketPath[i]].longTokenAddress;
|
|
262
|
+
edges.push({ from: currentFrom, to: currentTo, marketAddress: marketPath[i] });
|
|
263
|
+
}
|
|
264
|
+
return edges;
|
|
265
|
+
}
|
|
266
|
+
exports.marketRouteToMarketEdges = marketRouteToMarketEdges;
|
|
267
|
+
function getTokenSwapPathsForTokenPair(tokenSwapPaths, tokenAAddress, tokenBAddress) {
|
|
268
|
+
if (tokenSwapPaths[tokenAAddress]?.[tokenBAddress]) {
|
|
269
|
+
return tokenSwapPaths[tokenAAddress][tokenBAddress];
|
|
270
|
+
}
|
|
271
|
+
if (tokenSwapPaths[tokenBAddress]?.[tokenAAddress]) {
|
|
272
|
+
return tokenSwapPaths[tokenBAddress][tokenAAddress].map((route) => [...route].reverse());
|
|
273
|
+
}
|
|
274
|
+
return [];
|
|
275
|
+
}
|
|
276
|
+
exports.getTokenSwapPathsForTokenPair = getTokenSwapPathsForTokenPair;
|
|
277
|
+
function getTokenSwapPathsForTokenPairPrebuilt(chainId, from, to) {
|
|
278
|
+
return getTokenSwapPathsForTokenPair(preparedSwapData_1.TOKEN_SWAP_PATHS[chainId], from, to);
|
|
279
|
+
}
|
|
280
|
+
exports.getTokenSwapPathsForTokenPairPrebuilt = getTokenSwapPathsForTokenPairPrebuilt;
|
|
281
|
+
function getMarketAdjacencyGraph(chainId) {
|
|
282
|
+
return preparedSwapData_1.MARKETS_ADJACENCY_GRAPH[chainId];
|
|
283
|
+
}
|
|
284
|
+
exports.getMarketAdjacencyGraph = getMarketAdjacencyGraph;
|
|
285
|
+
function findAllReachableTokens(chainId, from) {
|
|
286
|
+
return preparedSwapData_1.REACHABLE_TOKENS[chainId][from];
|
|
287
|
+
}
|
|
288
|
+
exports.findAllReachableTokens = findAllReachableTokens;
|
|
289
|
+
function getMaxLiquidityMarketSwapPathFromTokenSwapPaths({ graph, tokenSwapPaths, tokenInAddress, tokenOutAddress, getLiquidity, }) {
|
|
290
|
+
// go through all edges and find best yield market for it
|
|
291
|
+
const cachedMaxLiquidityMarketForTokenEdge = {};
|
|
292
|
+
let bestMarketPath = undefined;
|
|
293
|
+
let bestLiquidity = 0n;
|
|
294
|
+
for (const pathType of tokenSwapPaths) {
|
|
295
|
+
let bad = false;
|
|
296
|
+
let bestMarketPathForPathType = [];
|
|
297
|
+
let pathTypeBestLiquidity = viem_1.maxUint256;
|
|
298
|
+
for (let hopIndex = 0; hopIndex <= pathType.length; hopIndex++) {
|
|
299
|
+
const tokenFromAddress = hopIndex === 0 ? tokenInAddress : pathType[hopIndex - 1];
|
|
300
|
+
const tokenToAddress = hopIndex === pathType.length ? tokenOutAddress : pathType[hopIndex];
|
|
301
|
+
// prevTokenAddress -> tokenAddress
|
|
302
|
+
// get all markets for prevTokenAddress -> tokenAddress
|
|
303
|
+
const markets = getMarketsForTokenPair(graph, tokenFromAddress, tokenToAddress);
|
|
304
|
+
if (markets.length === 0) {
|
|
305
|
+
bad = true;
|
|
306
|
+
break;
|
|
307
|
+
}
|
|
308
|
+
let bestMarketInfo = cachedMaxLiquidityMarketForTokenEdge[tokenFromAddress]?.[tokenToAddress];
|
|
309
|
+
if (!bestMarketInfo) {
|
|
310
|
+
bestMarketInfo = getMaxLiquidityMarketForTokenEdge({
|
|
311
|
+
markets,
|
|
312
|
+
tokenInAddress,
|
|
313
|
+
tokenOutAddress,
|
|
314
|
+
getLiquidity,
|
|
315
|
+
});
|
|
316
|
+
cachedMaxLiquidityMarketForTokenEdge[tokenFromAddress] =
|
|
317
|
+
cachedMaxLiquidityMarketForTokenEdge[tokenFromAddress] || {};
|
|
318
|
+
cachedMaxLiquidityMarketForTokenEdge[tokenFromAddress][tokenToAddress] = bestMarketInfo;
|
|
319
|
+
}
|
|
320
|
+
bestMarketPathForPathType.push(bestMarketInfo.marketAddress);
|
|
321
|
+
if (bestMarketInfo.liquidity < pathTypeBestLiquidity) {
|
|
322
|
+
pathTypeBestLiquidity = bestMarketInfo.liquidity;
|
|
323
|
+
}
|
|
324
|
+
if (pathTypeBestLiquidity < bestLiquidity) {
|
|
325
|
+
bad = true;
|
|
326
|
+
break;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
if (bad) {
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
if (pathTypeBestLiquidity > bestLiquidity) {
|
|
333
|
+
bestLiquidity = pathTypeBestLiquidity;
|
|
334
|
+
bestMarketPath = bestMarketPathForPathType;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return bestMarketPath ? { path: bestMarketPath, liquidity: bestLiquidity } : undefined;
|
|
338
|
+
}
|
|
339
|
+
exports.getMaxLiquidityMarketSwapPathFromTokenSwapPaths = getMaxLiquidityMarketSwapPathFromTokenSwapPaths;
|
|
340
|
+
function getMaxLiquidityMarketForTokenEdge({ markets, tokenInAddress, tokenOutAddress, getLiquidity, }) {
|
|
341
|
+
let bestMarketAddress = markets[0];
|
|
342
|
+
let bestLiquidity = 0n;
|
|
343
|
+
for (const market of markets) {
|
|
344
|
+
const liquidity = getLiquidity({
|
|
345
|
+
marketAddress: market,
|
|
346
|
+
from: tokenInAddress,
|
|
347
|
+
to: tokenOutAddress,
|
|
348
|
+
});
|
|
349
|
+
if (liquidity > bestLiquidity) {
|
|
350
|
+
bestLiquidity = liquidity;
|
|
351
|
+
bestMarketAddress = market;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return {
|
|
355
|
+
marketAddress: bestMarketAddress,
|
|
356
|
+
liquidity: bestLiquidity,
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
exports.getMaxLiquidityMarketForTokenEdge = getMaxLiquidityMarketForTokenEdge;
|