@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,258 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMaxSwapPathLiquidity = exports.getSwapStats = exports.getSwapPathStats = exports.getSwapPathOutputAddresses = exports.getSwapCapacityUsd = void 0;
|
|
4
|
+
const viem_1 = require("viem");
|
|
5
|
+
const tokens_1 = require("../../configs/tokens");
|
|
6
|
+
const fees_1 = require("../fees");
|
|
7
|
+
const markets_1 = require("../markets");
|
|
8
|
+
const objects_1 = require("../objects");
|
|
9
|
+
const tokens_2 = require("../tokens");
|
|
10
|
+
function getSwapCapacityUsd(marketInfo, isLong) {
|
|
11
|
+
const poolAmount = isLong ? marketInfo.longPoolAmount : marketInfo.shortPoolAmount;
|
|
12
|
+
const maxPoolAmount = isLong ? marketInfo.maxLongPoolAmount : marketInfo.maxShortPoolAmount;
|
|
13
|
+
const capacityAmount = maxPoolAmount - poolAmount;
|
|
14
|
+
const token = isLong ? marketInfo.longToken : marketInfo.shortToken;
|
|
15
|
+
const capacityUsd = (0, tokens_2.convertToUsd)(capacityAmount, token.decimals, (0, tokens_2.getMidPrice)(token.prices));
|
|
16
|
+
return capacityUsd;
|
|
17
|
+
}
|
|
18
|
+
exports.getSwapCapacityUsd = getSwapCapacityUsd;
|
|
19
|
+
function getSwapPathOutputAddresses(p) {
|
|
20
|
+
const { marketsInfoData, initialCollateralAddress, swapPath, wrappedNativeTokenAddress, shouldUnwrapNativeToken, isIncrease, } = p;
|
|
21
|
+
if (swapPath.length === 0) {
|
|
22
|
+
// Increase
|
|
23
|
+
if (isIncrease) {
|
|
24
|
+
// During increase target collateral token is always ERC20 token, it can not be native token.
|
|
25
|
+
// Thus we do not need to check if initial collateral token is wrapped token to unwrap it.
|
|
26
|
+
// So we can safely return initial collateral token address as out token address, when there is no swap path.
|
|
27
|
+
return {
|
|
28
|
+
outTokenAddress: initialCollateralAddress,
|
|
29
|
+
outMarketAddress: undefined,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
// Decrease
|
|
33
|
+
if (shouldUnwrapNativeToken && initialCollateralAddress === wrappedNativeTokenAddress) {
|
|
34
|
+
return {
|
|
35
|
+
outTokenAddress: tokens_1.NATIVE_TOKEN_ADDRESS,
|
|
36
|
+
outMarketAddress: undefined,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
outTokenAddress: initialCollateralAddress,
|
|
41
|
+
outMarketAddress: undefined,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const [firstMarketAddress, ...marketAddresses] = swapPath;
|
|
45
|
+
let outMarket = (0, objects_1.getByKey)(marketsInfoData, firstMarketAddress);
|
|
46
|
+
if (!outMarket) {
|
|
47
|
+
return {
|
|
48
|
+
outTokenAddress: undefined,
|
|
49
|
+
outMarketAddress: undefined,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
let outTokenType = (0, markets_1.getTokenPoolType)(outMarket, initialCollateralAddress);
|
|
53
|
+
let outToken = outTokenType === "long" ? outMarket.shortToken : outMarket.longToken;
|
|
54
|
+
for (const marketAddress of marketAddresses) {
|
|
55
|
+
outMarket = (0, objects_1.getByKey)(marketsInfoData, marketAddress);
|
|
56
|
+
if (!outMarket) {
|
|
57
|
+
return {
|
|
58
|
+
outTokenAddress: undefined,
|
|
59
|
+
outMarketAddress: undefined,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
outTokenType = outMarket.longTokenAddress === outToken.address ? "short" : "long";
|
|
63
|
+
outToken = outTokenType === "long" ? outMarket.longToken : outMarket.shortToken;
|
|
64
|
+
}
|
|
65
|
+
let outTokenAddress;
|
|
66
|
+
if (isIncrease) {
|
|
67
|
+
// Here swap path is not empty, this means out token came from swapping tokens,
|
|
68
|
+
// thus it can not be native token by design.
|
|
69
|
+
outTokenAddress = outToken.address;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
if (shouldUnwrapNativeToken && outToken.address === wrappedNativeTokenAddress) {
|
|
73
|
+
outTokenAddress = tokens_1.NATIVE_TOKEN_ADDRESS;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
outTokenAddress = outToken.address;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
outTokenAddress,
|
|
81
|
+
outMarketAddress: outMarket.marketTokenAddress,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
exports.getSwapPathOutputAddresses = getSwapPathOutputAddresses;
|
|
85
|
+
function getSwapPathStats(p) {
|
|
86
|
+
const { marketsInfoData, swapPath, initialCollateralAddress, usdIn, shouldUnwrapNativeToken, shouldApplyPriceImpact, wrappedNativeTokenAddress, } = p;
|
|
87
|
+
if (swapPath.length === 0) {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
const swapSteps = [];
|
|
91
|
+
let usdOut = usdIn;
|
|
92
|
+
let tokenInAddress = initialCollateralAddress;
|
|
93
|
+
let tokenOutAddress = initialCollateralAddress;
|
|
94
|
+
let totalSwapPriceImpactDeltaUsd = 0n;
|
|
95
|
+
let totalSwapFeeUsd = 0n;
|
|
96
|
+
for (let i = 0; i < swapPath.length; i++) {
|
|
97
|
+
const marketAddress = swapPath[i];
|
|
98
|
+
const marketInfo = marketsInfoData[marketAddress];
|
|
99
|
+
if (!marketInfo) {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
const nextTokenOutAddress = (0, markets_1.getOppositeCollateral)(marketInfo, tokenInAddress)?.address;
|
|
103
|
+
if (!nextTokenOutAddress) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
tokenOutAddress = nextTokenOutAddress;
|
|
107
|
+
if (i === swapPath.length - 1 && shouldUnwrapNativeToken && tokenOutAddress === wrappedNativeTokenAddress) {
|
|
108
|
+
tokenOutAddress = tokens_1.NATIVE_TOKEN_ADDRESS;
|
|
109
|
+
}
|
|
110
|
+
const swapStep = getSwapStats({
|
|
111
|
+
marketInfo,
|
|
112
|
+
tokenInAddress,
|
|
113
|
+
tokenOutAddress,
|
|
114
|
+
usdIn: usdOut,
|
|
115
|
+
shouldApplyPriceImpact,
|
|
116
|
+
});
|
|
117
|
+
tokenInAddress = swapStep.tokenOutAddress;
|
|
118
|
+
usdOut = swapStep.usdOut;
|
|
119
|
+
totalSwapPriceImpactDeltaUsd = totalSwapPriceImpactDeltaUsd + swapStep.priceImpactDeltaUsd;
|
|
120
|
+
totalSwapFeeUsd = totalSwapFeeUsd + swapStep.swapFeeUsd;
|
|
121
|
+
swapSteps.push(swapStep);
|
|
122
|
+
}
|
|
123
|
+
const lastStep = swapSteps[swapSteps.length - 1];
|
|
124
|
+
const targetMarketAddress = lastStep.marketAddress;
|
|
125
|
+
const amountOut = lastStep.amountOut;
|
|
126
|
+
const totalFeesDeltaUsd = 0n - totalSwapFeeUsd + totalSwapPriceImpactDeltaUsd;
|
|
127
|
+
return {
|
|
128
|
+
swapPath,
|
|
129
|
+
tokenInAddress: initialCollateralAddress,
|
|
130
|
+
tokenOutAddress,
|
|
131
|
+
targetMarketAddress,
|
|
132
|
+
swapSteps,
|
|
133
|
+
usdOut,
|
|
134
|
+
amountOut,
|
|
135
|
+
totalSwapFeeUsd,
|
|
136
|
+
totalSwapPriceImpactDeltaUsd,
|
|
137
|
+
totalFeesDeltaUsd,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
exports.getSwapPathStats = getSwapPathStats;
|
|
141
|
+
function getSwapStats(p) {
|
|
142
|
+
const { marketInfo, tokenInAddress, tokenOutAddress, usdIn, shouldApplyPriceImpact } = p;
|
|
143
|
+
const isWrap = tokenInAddress === tokens_1.NATIVE_TOKEN_ADDRESS;
|
|
144
|
+
const isUnwrap = tokenOutAddress === tokens_1.NATIVE_TOKEN_ADDRESS;
|
|
145
|
+
const tokenIn = (0, markets_1.getTokenPoolType)(marketInfo, tokenInAddress) === "long" ? marketInfo.longToken : marketInfo.shortToken;
|
|
146
|
+
const tokenOut = (0, markets_1.getTokenPoolType)(marketInfo, tokenOutAddress) === "long" ? marketInfo.longToken : marketInfo.shortToken;
|
|
147
|
+
const priceIn = tokenIn.prices.minPrice;
|
|
148
|
+
const priceOut = tokenOut.prices.maxPrice;
|
|
149
|
+
const amountIn = (0, tokens_2.convertToTokenAmount)(usdIn, tokenIn.decimals, priceIn);
|
|
150
|
+
let priceImpactDeltaUsd;
|
|
151
|
+
try {
|
|
152
|
+
priceImpactDeltaUsd = (0, fees_1.getPriceImpactForSwap)(marketInfo, tokenIn, tokenOut, usdIn, usdIn * -1n);
|
|
153
|
+
}
|
|
154
|
+
catch (e) {
|
|
155
|
+
// Approximate if the market would be out of capacity
|
|
156
|
+
const capacityUsd = getSwapCapacityUsd(marketInfo, (0, markets_1.getTokenPoolType)(marketInfo, tokenInAddress) === "long");
|
|
157
|
+
const swapFeeUsd = (0, fees_1.getSwapFee)(marketInfo, usdIn, false);
|
|
158
|
+
const usdInAfterFees = usdIn - swapFeeUsd;
|
|
159
|
+
const isOutCapacity = capacityUsd < usdInAfterFees;
|
|
160
|
+
return {
|
|
161
|
+
swapFeeUsd: 0n,
|
|
162
|
+
swapFeeAmount: 0n,
|
|
163
|
+
isWrap,
|
|
164
|
+
isUnwrap,
|
|
165
|
+
marketAddress: marketInfo.marketTokenAddress,
|
|
166
|
+
tokenInAddress,
|
|
167
|
+
tokenOutAddress,
|
|
168
|
+
priceImpactDeltaUsd: 0n,
|
|
169
|
+
amountIn,
|
|
170
|
+
amountInAfterFees: amountIn,
|
|
171
|
+
usdIn,
|
|
172
|
+
amountOut: 0n,
|
|
173
|
+
usdOut: 0n,
|
|
174
|
+
isOutLiquidity: true,
|
|
175
|
+
isOutCapacity,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
const swapFeeAmount = (0, fees_1.getSwapFee)(marketInfo, amountIn, priceImpactDeltaUsd > 0);
|
|
179
|
+
const swapFeeUsd = (0, fees_1.getSwapFee)(marketInfo, usdIn, priceImpactDeltaUsd > 0);
|
|
180
|
+
const amountInAfterFees = amountIn - swapFeeAmount;
|
|
181
|
+
const usdInAfterFees = usdIn - swapFeeUsd;
|
|
182
|
+
let usdOut = usdInAfterFees;
|
|
183
|
+
let amountOut = (0, tokens_2.convertToTokenAmount)(usdOut, tokenOut.decimals, priceOut);
|
|
184
|
+
let cappedImpactDeltaUsd;
|
|
185
|
+
if (priceImpactDeltaUsd > 0) {
|
|
186
|
+
const { impactDeltaAmount: positiveImpactAmountTokenOut, cappedDiffUsd } = (0, fees_1.applySwapImpactWithCap)(marketInfo, tokenOut, priceImpactDeltaUsd);
|
|
187
|
+
cappedImpactDeltaUsd = (0, tokens_2.convertToUsd)(positiveImpactAmountTokenOut, tokenOut.decimals, priceOut);
|
|
188
|
+
// https://github.com/gmx-io/gmx-synthetics/blob/3df10f1eab2734cf1b5f0a5dff12b79cbb19907d/contracts/swap/SwapUtils.sol#L290-L291
|
|
189
|
+
if (cappedDiffUsd > 0) {
|
|
190
|
+
const { impactDeltaAmount: positiveImpactAmountTokenIn } = (0, fees_1.applySwapImpactWithCap)(marketInfo, tokenIn, cappedDiffUsd);
|
|
191
|
+
if (positiveImpactAmountTokenIn > 0) {
|
|
192
|
+
cappedImpactDeltaUsd += (0, tokens_2.convertToUsd)(positiveImpactAmountTokenIn, tokenIn.decimals, priceIn);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
const { impactDeltaAmount: negativeImpactAmount } = (0, fees_1.applySwapImpactWithCap)(marketInfo, tokenIn, priceImpactDeltaUsd);
|
|
198
|
+
cappedImpactDeltaUsd = (0, tokens_2.convertToUsd)(negativeImpactAmount, tokenIn.decimals, priceIn);
|
|
199
|
+
}
|
|
200
|
+
if (shouldApplyPriceImpact) {
|
|
201
|
+
usdOut = usdOut + cappedImpactDeltaUsd;
|
|
202
|
+
}
|
|
203
|
+
if (usdOut < 0) {
|
|
204
|
+
usdOut = 0n;
|
|
205
|
+
}
|
|
206
|
+
amountOut = (0, tokens_2.convertToTokenAmount)(usdOut, tokenOut.decimals, priceOut);
|
|
207
|
+
const capacityUsd = getSwapCapacityUsd(marketInfo, (0, markets_1.getTokenPoolType)(marketInfo, tokenInAddress) === "long");
|
|
208
|
+
const isOutCapacity = capacityUsd < usdInAfterFees;
|
|
209
|
+
const liquidity = (0, markets_1.getAvailableUsdLiquidityForCollateral)(marketInfo, (0, markets_1.getTokenPoolType)(marketInfo, tokenOutAddress) === "long");
|
|
210
|
+
const isOutLiquidity = liquidity < usdOut;
|
|
211
|
+
return {
|
|
212
|
+
swapFeeUsd,
|
|
213
|
+
swapFeeAmount,
|
|
214
|
+
isWrap,
|
|
215
|
+
isUnwrap,
|
|
216
|
+
marketAddress: marketInfo.marketTokenAddress,
|
|
217
|
+
tokenInAddress,
|
|
218
|
+
tokenOutAddress,
|
|
219
|
+
priceImpactDeltaUsd: cappedImpactDeltaUsd,
|
|
220
|
+
amountIn,
|
|
221
|
+
amountInAfterFees,
|
|
222
|
+
usdIn,
|
|
223
|
+
amountOut,
|
|
224
|
+
usdOut,
|
|
225
|
+
isOutLiquidity,
|
|
226
|
+
isOutCapacity,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
exports.getSwapStats = getSwapStats;
|
|
230
|
+
function getMaxSwapPathLiquidity(p) {
|
|
231
|
+
const { marketsInfoData, swapPath, initialCollateralAddress } = p;
|
|
232
|
+
if (swapPath.length === 0) {
|
|
233
|
+
return 0n;
|
|
234
|
+
}
|
|
235
|
+
let minMarketLiquidity = viem_1.maxUint256;
|
|
236
|
+
let tokenInAddress = initialCollateralAddress;
|
|
237
|
+
for (const marketAddress of swapPath) {
|
|
238
|
+
const marketInfo = (0, objects_1.getByKey)(marketsInfoData, marketAddress);
|
|
239
|
+
if (!marketInfo) {
|
|
240
|
+
return 0n;
|
|
241
|
+
}
|
|
242
|
+
const tokenOut = (0, markets_1.getOppositeCollateral)(marketInfo, tokenInAddress);
|
|
243
|
+
if (!tokenOut) {
|
|
244
|
+
return 0n;
|
|
245
|
+
}
|
|
246
|
+
const isTokenOutLong = (0, markets_1.getTokenPoolType)(marketInfo, tokenOut.address) === "long";
|
|
247
|
+
const liquidity = (0, markets_1.getAvailableUsdLiquidityForCollateral)(marketInfo, isTokenOutLong);
|
|
248
|
+
if (liquidity < minMarketLiquidity) {
|
|
249
|
+
minMarketLiquidity = liquidity;
|
|
250
|
+
}
|
|
251
|
+
tokenInAddress = tokenOut.address;
|
|
252
|
+
}
|
|
253
|
+
if (minMarketLiquidity === viem_1.maxUint256) {
|
|
254
|
+
return 0n;
|
|
255
|
+
}
|
|
256
|
+
return minMarketLiquidity;
|
|
257
|
+
}
|
|
258
|
+
exports.getMaxSwapPathLiquidity = getMaxSwapPathLiquidity;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getSwapPathComparator = exports.getSwapAmountsByToValue = exports.getSwapAmountsByFromValue = void 0;
|
|
4
|
+
const factors_1 = require("../../configs/factors");
|
|
5
|
+
const bigmath_1 = require("../bigmath");
|
|
6
|
+
const fees_1 = require("../fees");
|
|
7
|
+
const numbers_1 = require("../numbers");
|
|
8
|
+
const tokens_1 = require("../tokens");
|
|
9
|
+
function getSwapAmountsByFromValue(p) {
|
|
10
|
+
const { tokenIn, tokenOut, amountIn, triggerRatio, isLimit, swapOptimizationOrder, findSwapPath, uiFeeFactor, allowedSwapSlippageBps, } = p;
|
|
11
|
+
const priceIn = tokenIn.prices.minPrice;
|
|
12
|
+
const priceOut = tokenOut.prices.maxPrice;
|
|
13
|
+
const usdIn = (0, tokens_1.convertToUsd)(amountIn, tokenIn.decimals, priceIn);
|
|
14
|
+
let amountOut = 0n;
|
|
15
|
+
let usdOut = 0n;
|
|
16
|
+
let minOutputAmount = 0n;
|
|
17
|
+
const defaultAmounts = {
|
|
18
|
+
amountIn,
|
|
19
|
+
usdIn,
|
|
20
|
+
amountOut,
|
|
21
|
+
usdOut,
|
|
22
|
+
minOutputAmount,
|
|
23
|
+
priceIn,
|
|
24
|
+
priceOut,
|
|
25
|
+
swapPathStats: undefined,
|
|
26
|
+
};
|
|
27
|
+
if (amountIn <= 0) {
|
|
28
|
+
return defaultAmounts;
|
|
29
|
+
}
|
|
30
|
+
if ((0, tokens_1.getIsEquivalentTokens)(tokenIn, tokenOut)) {
|
|
31
|
+
amountOut = amountIn;
|
|
32
|
+
usdOut = usdIn;
|
|
33
|
+
minOutputAmount = amountOut;
|
|
34
|
+
return {
|
|
35
|
+
amountIn,
|
|
36
|
+
usdIn,
|
|
37
|
+
amountOut,
|
|
38
|
+
usdOut,
|
|
39
|
+
minOutputAmount,
|
|
40
|
+
priceIn,
|
|
41
|
+
priceOut,
|
|
42
|
+
swapPathStats: undefined,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const swapPathStats = findSwapPath(defaultAmounts.usdIn, { order: swapOptimizationOrder });
|
|
46
|
+
const totalSwapVolume = (0, fees_1.getTotalSwapVolumeFromSwapStats)(swapPathStats?.swapSteps);
|
|
47
|
+
const swapUiFeeUsd = (0, numbers_1.applyFactor)(totalSwapVolume, uiFeeFactor);
|
|
48
|
+
const swapUiFeeAmount = (0, tokens_1.convertToTokenAmount)(swapUiFeeUsd, tokenOut.decimals, priceOut);
|
|
49
|
+
if (!swapPathStats) {
|
|
50
|
+
return defaultAmounts;
|
|
51
|
+
}
|
|
52
|
+
if (isLimit) {
|
|
53
|
+
if (!triggerRatio) {
|
|
54
|
+
return defaultAmounts;
|
|
55
|
+
}
|
|
56
|
+
amountOut = (0, tokens_1.getAmountByRatio)({
|
|
57
|
+
fromToken: tokenIn,
|
|
58
|
+
toToken: tokenOut,
|
|
59
|
+
fromTokenAmount: amountIn,
|
|
60
|
+
ratio: triggerRatio.ratio,
|
|
61
|
+
shouldInvertRatio: triggerRatio.largestToken.address === tokenOut.address,
|
|
62
|
+
allowedSwapSlippageBps,
|
|
63
|
+
});
|
|
64
|
+
usdOut = (0, tokens_1.convertToUsd)(amountOut, tokenOut.decimals, priceOut);
|
|
65
|
+
amountOut = (0, tokens_1.convertToTokenAmount)(usdOut, tokenOut.decimals, priceOut);
|
|
66
|
+
minOutputAmount = amountOut;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
usdOut = swapPathStats.usdOut - swapUiFeeUsd;
|
|
70
|
+
amountOut = swapPathStats.amountOut - swapUiFeeAmount;
|
|
71
|
+
minOutputAmount = amountOut;
|
|
72
|
+
}
|
|
73
|
+
if (amountOut < 0) {
|
|
74
|
+
amountOut = 0n;
|
|
75
|
+
usdOut = 0n;
|
|
76
|
+
minOutputAmount = 0n;
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
amountIn,
|
|
80
|
+
usdIn,
|
|
81
|
+
amountOut,
|
|
82
|
+
usdOut,
|
|
83
|
+
priceIn,
|
|
84
|
+
priceOut,
|
|
85
|
+
minOutputAmount,
|
|
86
|
+
swapPathStats,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
exports.getSwapAmountsByFromValue = getSwapAmountsByFromValue;
|
|
90
|
+
function getSwapAmountsByToValue(p) {
|
|
91
|
+
const { tokenIn, tokenOut, amountOut, triggerRatio, isLimit, findSwapPath, uiFeeFactor, swapOptimizationOrder, allowedSwapSlippageBps, } = p;
|
|
92
|
+
const priceIn = tokenIn.prices.minPrice;
|
|
93
|
+
const priceOut = tokenOut.prices.maxPrice;
|
|
94
|
+
const usdOut = (0, tokens_1.convertToUsd)(amountOut, tokenOut.decimals, priceOut);
|
|
95
|
+
const uiFeeUsd = (0, numbers_1.applyFactor)(usdOut, uiFeeFactor);
|
|
96
|
+
let minOutputAmount = amountOut;
|
|
97
|
+
let amountIn = 0n;
|
|
98
|
+
let usdIn = 0n;
|
|
99
|
+
const defaultAmounts = {
|
|
100
|
+
amountIn,
|
|
101
|
+
usdIn,
|
|
102
|
+
amountOut,
|
|
103
|
+
usdOut,
|
|
104
|
+
minOutputAmount,
|
|
105
|
+
priceIn,
|
|
106
|
+
priceOut,
|
|
107
|
+
swapPathStats: undefined,
|
|
108
|
+
};
|
|
109
|
+
if (amountOut <= 0) {
|
|
110
|
+
return defaultAmounts;
|
|
111
|
+
}
|
|
112
|
+
if ((0, tokens_1.getIsEquivalentTokens)(tokenIn, tokenOut)) {
|
|
113
|
+
amountIn = amountOut;
|
|
114
|
+
usdIn = usdOut;
|
|
115
|
+
return {
|
|
116
|
+
amountIn,
|
|
117
|
+
usdIn,
|
|
118
|
+
amountOut,
|
|
119
|
+
usdOut,
|
|
120
|
+
minOutputAmount,
|
|
121
|
+
priceIn,
|
|
122
|
+
priceOut,
|
|
123
|
+
swapPathStats: undefined,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const baseUsdIn = usdOut;
|
|
127
|
+
const swapPathStats = findSwapPath(baseUsdIn, { order: swapOptimizationOrder });
|
|
128
|
+
if (!swapPathStats) {
|
|
129
|
+
return defaultAmounts;
|
|
130
|
+
}
|
|
131
|
+
if (isLimit) {
|
|
132
|
+
if (!triggerRatio) {
|
|
133
|
+
return defaultAmounts;
|
|
134
|
+
}
|
|
135
|
+
amountIn = (0, tokens_1.getAmountByRatio)({
|
|
136
|
+
fromToken: tokenOut,
|
|
137
|
+
toToken: tokenIn,
|
|
138
|
+
fromTokenAmount: amountOut,
|
|
139
|
+
ratio: triggerRatio.ratio,
|
|
140
|
+
shouldInvertRatio: triggerRatio.largestToken.address === tokenIn.address,
|
|
141
|
+
});
|
|
142
|
+
usdIn = (0, tokens_1.convertToUsd)(amountIn, tokenIn.decimals, priceIn);
|
|
143
|
+
if (allowedSwapSlippageBps !== undefined) {
|
|
144
|
+
usdIn += bigmath_1.bigMath.mulDiv(usdIn, allowedSwapSlippageBps ?? 0n, factors_1.BASIS_POINTS_DIVISOR_BIGINT);
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
usdIn = usdIn + swapPathStats.totalSwapFeeUsd + uiFeeUsd - swapPathStats.totalSwapPriceImpactDeltaUsd;
|
|
148
|
+
}
|
|
149
|
+
amountIn = (0, tokens_1.convertToTokenAmount)(usdIn, tokenIn.decimals, priceIn);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
const adjustedUsdIn = swapPathStats.usdOut > 0 ? bigmath_1.bigMath.mulDiv(baseUsdIn, usdOut, swapPathStats.usdOut) : 0n;
|
|
153
|
+
usdIn = adjustedUsdIn + uiFeeUsd;
|
|
154
|
+
amountIn = (0, tokens_1.convertToTokenAmount)(usdIn, tokenIn.decimals, priceIn);
|
|
155
|
+
}
|
|
156
|
+
if (amountIn < 0) {
|
|
157
|
+
amountIn = 0n;
|
|
158
|
+
usdIn = 0n;
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
amountIn,
|
|
162
|
+
usdIn,
|
|
163
|
+
amountOut,
|
|
164
|
+
usdOut,
|
|
165
|
+
minOutputAmount,
|
|
166
|
+
priceIn,
|
|
167
|
+
priceOut,
|
|
168
|
+
swapPathStats,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
exports.getSwapAmountsByToValue = getSwapAmountsByToValue;
|
|
172
|
+
function getSwapPathComparator(order) {
|
|
173
|
+
return function (a, b) {
|
|
174
|
+
for (const field of order || []) {
|
|
175
|
+
const isLiquidity = field === "liquidity";
|
|
176
|
+
const aVal = isLiquidity ? a.liquidity : a.path.length;
|
|
177
|
+
const bVal = isLiquidity ? b.liquidity : b.path.length;
|
|
178
|
+
if (aVal !== bVal) {
|
|
179
|
+
if (isLiquidity) {
|
|
180
|
+
return aVal < bVal ? 1 : -1;
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
return aVal < bVal ? -1 : 1;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return 0;
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
exports.getSwapPathComparator = getSwapPathComparator;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.arbitrumSdk = exports.arbitrumSdkConfig = void 0;
|
|
4
|
+
const viem_1 = require("viem");
|
|
5
|
+
const chains_1 = require("../configs/chains");
|
|
6
|
+
const index_1 = require("../index");
|
|
7
|
+
const client = (0, viem_1.createTestClient)({
|
|
8
|
+
chain: (0, chains_1.getChain)(chains_1.ARBITRUM),
|
|
9
|
+
mode: "hardhat",
|
|
10
|
+
transport: (0, viem_1.http)(),
|
|
11
|
+
})
|
|
12
|
+
.extend(viem_1.publicActions)
|
|
13
|
+
.extend(viem_1.walletActions);
|
|
14
|
+
exports.arbitrumSdkConfig = {
|
|
15
|
+
chainId: chains_1.ARBITRUM,
|
|
16
|
+
account: "0x9f7198eb1b9Ccc0Eb7A07eD228d8FbC12963ea33",
|
|
17
|
+
oracleUrl: "https://arbitrum-api.gmxinfra.io",
|
|
18
|
+
rpcUrl: "https://arb1.arbitrum.io/rpc",
|
|
19
|
+
walletClient: client,
|
|
20
|
+
subsquidUrl: "https://gmx.squids.live/gmx-synthetics-arbitrum@e27d93/api/graphql",
|
|
21
|
+
};
|
|
22
|
+
exports.arbitrumSdk = new index_1.GmxSdk(exports.arbitrumSdkConfig);
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTokensRatioByPrice = exports.getIsUnwrap = exports.getIsWrap = exports.getAmountByRatio = exports.getTokensRatioByMinOutputAmountAndTriggerPrice = exports.getTokensRatioByAmounts = exports.getTokenData = exports.getIsEquivalentTokens = exports.getMidPrice = exports.convertToUsd = exports.convertToTokenAmount = exports.convertToContractTokenPrices = exports.convertToContractPrice = exports.parseContractPrice = void 0;
|
|
4
|
+
const factors_1 = require("../configs/factors");
|
|
5
|
+
const tokens_1 = require("../configs/tokens");
|
|
6
|
+
const bigmath_1 = require("./bigmath");
|
|
7
|
+
const numbers_1 = require("./numbers");
|
|
8
|
+
function parseContractPrice(price, tokenDecimals) {
|
|
9
|
+
return price * (0, numbers_1.expandDecimals)(1, tokenDecimals);
|
|
10
|
+
}
|
|
11
|
+
exports.parseContractPrice = parseContractPrice;
|
|
12
|
+
function convertToContractPrice(price, tokenDecimals) {
|
|
13
|
+
return price / (0, numbers_1.expandDecimals)(1, tokenDecimals);
|
|
14
|
+
}
|
|
15
|
+
exports.convertToContractPrice = convertToContractPrice;
|
|
16
|
+
function convertToContractTokenPrices(prices, tokenDecimals) {
|
|
17
|
+
return {
|
|
18
|
+
min: convertToContractPrice(prices.minPrice, tokenDecimals),
|
|
19
|
+
max: convertToContractPrice(prices.maxPrice, tokenDecimals),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
exports.convertToContractTokenPrices = convertToContractTokenPrices;
|
|
23
|
+
function convertToTokenAmount(usd, tokenDecimals, price) {
|
|
24
|
+
if (usd === undefined || typeof tokenDecimals !== "number" || price === undefined || price <= 0) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
return (usd * (0, numbers_1.expandDecimals)(1, tokenDecimals)) / price;
|
|
28
|
+
}
|
|
29
|
+
exports.convertToTokenAmount = convertToTokenAmount;
|
|
30
|
+
function convertToUsd(tokenAmount, tokenDecimals, price) {
|
|
31
|
+
if (tokenAmount == undefined || typeof tokenDecimals !== "number" || price === undefined) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
return (tokenAmount * price) / (0, numbers_1.expandDecimals)(1, tokenDecimals);
|
|
35
|
+
}
|
|
36
|
+
exports.convertToUsd = convertToUsd;
|
|
37
|
+
function getMidPrice(prices) {
|
|
38
|
+
return (prices.minPrice + prices.maxPrice) / 2n;
|
|
39
|
+
}
|
|
40
|
+
exports.getMidPrice = getMidPrice;
|
|
41
|
+
function getIsEquivalentTokens(token1, token2) {
|
|
42
|
+
if (token1.address === token2.address) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
if (token1.wrappedAddress === token2.address || token2.wrappedAddress === token1.address) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
if ((token1.isSynthetic || token2.isSynthetic) && token1.symbol === token2.symbol) {
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
exports.getIsEquivalentTokens = getIsEquivalentTokens;
|
|
54
|
+
function getTokenData(tokensData, address, convertTo) {
|
|
55
|
+
if (!address || !tokensData?.[address]) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
const token = tokensData[address];
|
|
59
|
+
if (convertTo === "wrapped" && token.isNative && token.wrappedAddress) {
|
|
60
|
+
return tokensData[token.wrappedAddress];
|
|
61
|
+
}
|
|
62
|
+
if (convertTo === "native" && token.isWrapped) {
|
|
63
|
+
return tokensData[tokens_1.NATIVE_TOKEN_ADDRESS];
|
|
64
|
+
}
|
|
65
|
+
return token;
|
|
66
|
+
}
|
|
67
|
+
exports.getTokenData = getTokenData;
|
|
68
|
+
/**
|
|
69
|
+
* Even though its not a generic function, it return the same type as the input.
|
|
70
|
+
* If `TokenData` is passed, it returns `TokenData`, if `Token` is passed, it returns `Token`.
|
|
71
|
+
*/
|
|
72
|
+
function getTokensRatioByAmounts(p) {
|
|
73
|
+
const { fromToken, toToken, fromTokenAmount, toTokenAmount } = p;
|
|
74
|
+
const adjustedFromAmount = (fromTokenAmount * numbers_1.PRECISION) / (0, numbers_1.expandDecimals)(1, fromToken.decimals);
|
|
75
|
+
const adjustedToAmount = (toTokenAmount * numbers_1.PRECISION) / (0, numbers_1.expandDecimals)(1, toToken.decimals);
|
|
76
|
+
const [smallestToken, largestToken, largestAmount, smallestAmount] = adjustedFromAmount > adjustedToAmount
|
|
77
|
+
? [fromToken, toToken, adjustedFromAmount, adjustedToAmount]
|
|
78
|
+
: [toToken, fromToken, adjustedToAmount, adjustedFromAmount];
|
|
79
|
+
const ratio = smallestAmount > 0 ? (largestAmount * numbers_1.PRECISION) / smallestAmount : 0n;
|
|
80
|
+
return { ratio, largestToken, smallestToken };
|
|
81
|
+
}
|
|
82
|
+
exports.getTokensRatioByAmounts = getTokensRatioByAmounts;
|
|
83
|
+
function getTokensRatioByMinOutputAmountAndTriggerPrice(p) {
|
|
84
|
+
const { fromToken, toToken, fromTokenAmount, toTokenAmount, triggerPrice, minOutputAmount } = p;
|
|
85
|
+
let allowedSwapSlippageBps = factors_1.DEFAULT_ALLOWED_SWAP_SLIPPAGE_BPS;
|
|
86
|
+
let smallestToken = fromToken;
|
|
87
|
+
let largestToken = toToken;
|
|
88
|
+
let largestAmount = fromTokenAmount;
|
|
89
|
+
let smallestAmount = toTokenAmount;
|
|
90
|
+
let acceptablePrice = 0n;
|
|
91
|
+
let ratio = 0n;
|
|
92
|
+
const adjustedFromAmount = (fromTokenAmount * numbers_1.PRECISION) / (0, numbers_1.expandDecimals)(1, fromToken.decimals);
|
|
93
|
+
const adjustedToAmount = (minOutputAmount * numbers_1.PRECISION) / (0, numbers_1.expandDecimals)(1, toToken.decimals);
|
|
94
|
+
const adjustedMinOutputAmount = (minOutputAmount * numbers_1.PRECISION) / (0, numbers_1.expandDecimals)(1, toToken.decimals);
|
|
95
|
+
[smallestToken, largestToken, largestAmount, smallestAmount] =
|
|
96
|
+
adjustedFromAmount > adjustedToAmount
|
|
97
|
+
? [fromToken, toToken, adjustedFromAmount, adjustedToAmount]
|
|
98
|
+
: [toToken, fromToken, adjustedToAmount, adjustedFromAmount];
|
|
99
|
+
ratio = smallestAmount > 0 ? (largestAmount * numbers_1.PRECISION) / smallestAmount : 0n;
|
|
100
|
+
if (triggerPrice === 0n) {
|
|
101
|
+
allowedSwapSlippageBps = factors_1.DEFAULT_ALLOWED_SWAP_SLIPPAGE_BPS;
|
|
102
|
+
acceptablePrice = ratio;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
const outputAtTriggerPrice = adjustedFromAmount > adjustedToAmount
|
|
106
|
+
? (adjustedFromAmount * numbers_1.PRECISION) / triggerPrice
|
|
107
|
+
: (adjustedFromAmount * triggerPrice) / numbers_1.PRECISION;
|
|
108
|
+
allowedSwapSlippageBps =
|
|
109
|
+
((outputAtTriggerPrice - adjustedMinOutputAmount) * factors_1.BASIS_POINTS_DIVISOR_BIGINT) / outputAtTriggerPrice;
|
|
110
|
+
acceptablePrice = ratio;
|
|
111
|
+
ratio = triggerPrice;
|
|
112
|
+
}
|
|
113
|
+
return { ratio, largestToken, smallestToken, allowedSwapSlippageBps, acceptablePrice };
|
|
114
|
+
}
|
|
115
|
+
exports.getTokensRatioByMinOutputAmountAndTriggerPrice = getTokensRatioByMinOutputAmountAndTriggerPrice;
|
|
116
|
+
function getAmountByRatio(p) {
|
|
117
|
+
const { fromToken, toToken, fromTokenAmount, ratio, shouldInvertRatio, allowedSwapSlippageBps } = p;
|
|
118
|
+
if (getIsEquivalentTokens(fromToken, toToken) || fromTokenAmount === 0n) {
|
|
119
|
+
return p.fromTokenAmount;
|
|
120
|
+
}
|
|
121
|
+
const _ratio = shouldInvertRatio ? (numbers_1.PRECISION * numbers_1.PRECISION) / ratio : ratio;
|
|
122
|
+
const adjustedDecimalsRatio = (0, numbers_1.adjustForDecimals)(_ratio, fromToken.decimals, toToken.decimals);
|
|
123
|
+
const amount = (p.fromTokenAmount * adjustedDecimalsRatio) / numbers_1.PRECISION;
|
|
124
|
+
const swapSlippageAmount = allowedSwapSlippageBps !== undefined
|
|
125
|
+
? bigmath_1.bigMath.mulDiv(amount, allowedSwapSlippageBps, factors_1.BASIS_POINTS_DIVISOR_BIGINT)
|
|
126
|
+
: 0n;
|
|
127
|
+
return amount - swapSlippageAmount;
|
|
128
|
+
}
|
|
129
|
+
exports.getAmountByRatio = getAmountByRatio;
|
|
130
|
+
function getIsWrap(token1, token2) {
|
|
131
|
+
return token1.isNative && token2.isWrapped;
|
|
132
|
+
}
|
|
133
|
+
exports.getIsWrap = getIsWrap;
|
|
134
|
+
function getIsUnwrap(token1, token2) {
|
|
135
|
+
return token1.isWrapped && token2.isNative;
|
|
136
|
+
}
|
|
137
|
+
exports.getIsUnwrap = getIsUnwrap;
|
|
138
|
+
function getTokensRatioByPrice(p) {
|
|
139
|
+
const { fromToken, toToken, fromPrice, toPrice } = p;
|
|
140
|
+
const [largestToken, smallestToken, largestPrice, smallestPrice] = fromPrice > toPrice ? [fromToken, toToken, fromPrice, toPrice] : [toToken, fromToken, toPrice, fromPrice];
|
|
141
|
+
const ratio = (largestPrice * numbers_1.PRECISION) / smallestPrice;
|
|
142
|
+
return { ratio, largestToken, smallestToken };
|
|
143
|
+
}
|
|
144
|
+
exports.getTokensRatioByPrice = getTokensRatioByPrice;
|