@mento-protocol/mento-sdk 3.2.3 → 3.2.5
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/dist/cache/routes.d.ts +13 -0
- package/dist/cache/routes.js +14089 -0
- package/dist/cache/tokens.d.ts +68 -0
- package/dist/cache/tokens.js +460 -0
- package/dist/core/abis/activePool.d.ts +2 -0
- package/dist/core/abis/activePool.js +14 -0
- package/dist/core/abis/addressesRegistry.d.ts +2 -0
- package/dist/core/abis/addressesRegistry.js +26 -0
- package/dist/core/abis/bipoolmanager.d.ts +34 -0
- package/dist/core/abis/bipoolmanager.js +72 -0
- package/dist/core/abis/borrowerOperations.d.ts +9 -0
- package/dist/core/abis/borrowerOperations.js +89 -0
- package/dist/core/abis/breakerbox.d.ts +13 -0
- package/dist/core/abis/breakerbox.js +8 -0
- package/dist/core/abis/broker.d.ts +2 -0
- package/dist/core/abis/broker.js +9 -0
- package/dist/core/abis/erc20.d.ts +9 -0
- package/dist/core/abis/erc20.js +21 -0
- package/dist/core/abis/fpmm.d.ts +270 -0
- package/dist/core/abis/fpmm.js +49 -0
- package/dist/core/abis/fpmmFactory.d.ts +85 -0
- package/dist/core/abis/fpmmFactory.js +26 -0
- package/dist/core/abis/hintHelpers.d.ts +2 -0
- package/dist/core/abis/hintHelpers.js +14 -0
- package/dist/core/abis/index.d.ts +22 -0
- package/dist/core/abis/index.js +38 -0
- package/dist/core/abis/liquidityStrategy.d.ts +132 -0
- package/dist/core/abis/liquidityStrategy.js +10 -0
- package/dist/core/abis/multiTroveGetter.d.ts +8 -0
- package/dist/core/abis/multiTroveGetter.js +15 -0
- package/dist/core/abis/priceFeed.d.ts +7 -0
- package/dist/core/abis/priceFeed.js +16 -0
- package/dist/core/abis/pricingmodule.d.ts +2 -0
- package/dist/core/abis/pricingmodule.js +6 -0
- package/dist/core/abis/reserve.d.ts +3 -0
- package/dist/core/abis/reserve.js +18 -0
- package/dist/core/abis/router.d.ts +521 -0
- package/dist/core/abis/router.js +45 -0
- package/dist/core/abis/sortedTroves.d.ts +2 -0
- package/dist/core/abis/sortedTroves.js +15 -0
- package/dist/core/abis/systemParams.d.ts +2 -0
- package/dist/core/abis/systemParams.js +14 -0
- package/dist/core/abis/troveManager.d.ts +2 -0
- package/dist/core/abis/troveManager.js +27 -0
- package/dist/core/abis/troveNFT.d.ts +2 -0
- package/dist/core/abis/troveNFT.js +9 -0
- package/dist/core/abis/virtualPool.d.ts +50 -0
- package/dist/core/abis/virtualPool.js +11 -0
- package/dist/core/abis/virtualPoolFactory.d.ts +59 -0
- package/dist/core/abis/virtualPoolFactory.js +17 -0
- package/dist/core/constants/addresses.d.ts +18 -0
- package/dist/core/constants/addresses.js +113 -0
- package/dist/core/constants/borrowConstants.d.ts +10 -0
- package/dist/core/constants/borrowConstants.js +16 -0
- package/dist/core/constants/borrowRegistries.d.ts +7 -0
- package/dist/core/constants/borrowRegistries.js +30 -0
- package/dist/core/constants/chainId.d.ts +7 -0
- package/dist/{cjs/enums → core/constants}/chainId.js +4 -1
- package/dist/core/constants/contractNames.d.ts +21 -0
- package/dist/core/constants/contractNames.js +24 -0
- package/dist/core/constants/index.d.ts +6 -0
- package/dist/core/constants/index.js +22 -0
- package/dist/core/errors/base.d.ts +8 -0
- package/dist/core/errors/base.js +17 -0
- package/dist/core/errors/index.d.ts +4 -0
- package/dist/{cjs/constants → core/errors}/index.js +4 -4
- package/dist/core/errors/oracle.d.ts +9 -0
- package/dist/core/errors/oracle.js +15 -0
- package/dist/core/errors/router.d.ts +14 -0
- package/dist/core/errors/router.js +24 -0
- package/dist/core/types/borrow.d.ts +87 -0
- package/dist/{cjs/interfaces/tradingLimitsConfig.js → core/types/borrow.js} +1 -0
- package/dist/core/types/contractAddresses.d.ts +42 -0
- package/dist/{cjs → core}/types/contractAddresses.js +1 -0
- package/dist/core/types/index.d.ts +10 -0
- package/dist/core/types/index.js +26 -0
- package/dist/core/types/liquidity.d.ts +194 -0
- package/dist/core/types/liquidity.js +3 -0
- package/dist/core/types/pool.d.ts +208 -0
- package/dist/core/types/pool.js +14 -0
- package/dist/core/types/provider.d.ts +45 -0
- package/dist/{cjs/interfaces/tradingLimitsState.js → core/types/provider.js} +1 -0
- package/dist/core/types/route.d.ts +62 -0
- package/dist/{cjs/interfaces/IChainClient.js → core/types/route.js} +1 -0
- package/dist/core/types/token.d.ts +21 -0
- package/dist/{cjs/interfaces/tradingLimit.js → core/types/token.js} +1 -0
- package/dist/core/types/tradingLimits.d.ts +91 -0
- package/dist/core/types/tradingLimits.js +3 -0
- package/dist/core/types/tradingMode.d.ts +24 -0
- package/dist/core/types/tradingMode.js +31 -0
- package/dist/core/types/transaction.d.ts +45 -0
- package/dist/core/types/transaction.js +3 -0
- package/dist/esm/cache/routes.js +14084 -0
- package/dist/esm/cache/tokens.js +452 -0
- package/dist/esm/core/abis/activePool.js +10 -0
- package/dist/esm/core/abis/addressesRegistry.js +22 -0
- package/dist/esm/core/abis/bipoolmanager.js +68 -0
- package/dist/esm/core/abis/borrowerOperations.js +85 -0
- package/dist/esm/core/abis/breakerbox.js +4 -0
- package/dist/esm/core/abis/broker.js +5 -0
- package/dist/esm/core/abis/erc20.js +17 -0
- package/dist/esm/core/abis/fpmm.js +45 -0
- package/dist/esm/core/abis/fpmmFactory.js +22 -0
- package/dist/esm/core/abis/hintHelpers.js +10 -0
- package/dist/esm/core/abis/index.js +21 -0
- package/dist/esm/core/abis/liquidityStrategy.js +6 -0
- package/dist/esm/core/abis/multiTroveGetter.js +11 -0
- package/dist/esm/core/abis/priceFeed.js +12 -0
- package/dist/esm/core/abis/pricingmodule.js +2 -0
- package/dist/esm/core/abis/reserve.js +14 -0
- package/dist/esm/core/abis/router.js +41 -0
- package/dist/esm/core/abis/sortedTroves.js +11 -0
- package/dist/esm/core/abis/systemParams.js +10 -0
- package/dist/esm/core/abis/troveManager.js +23 -0
- package/dist/esm/core/abis/troveNFT.js +5 -0
- package/dist/esm/core/abis/virtualPool.js +7 -0
- package/dist/esm/core/abis/virtualPoolFactory.js +13 -0
- package/dist/esm/core/constants/addresses.js +107 -0
- package/dist/esm/core/constants/borrowConstants.js +12 -0
- package/dist/esm/core/constants/borrowRegistries.js +25 -0
- package/dist/esm/{enums → core/constants}/chainId.js +2 -0
- package/dist/esm/core/constants/contractNames.js +20 -0
- package/dist/esm/core/constants/index.js +5 -0
- package/dist/esm/core/errors/base.js +12 -0
- package/dist/esm/core/errors/index.js +3 -0
- package/dist/esm/core/errors/oracle.js +10 -0
- package/dist/esm/core/errors/router.js +18 -0
- package/dist/esm/core/types/index.js +9 -0
- package/dist/esm/core/types/pool.js +10 -0
- package/dist/esm/core/types/tradingLimits.js +1 -0
- package/dist/esm/core/types/tradingMode.js +26 -0
- package/dist/esm/core/types/transaction.js +1 -0
- package/dist/esm/index.js +138 -6
- package/dist/esm/package.json +1 -0
- package/dist/esm/services/borrow/BorrowService.js +455 -0
- package/dist/esm/services/borrow/borrowHelpers.js +3 -0
- package/dist/esm/services/borrow/borrowMath.js +127 -0
- package/dist/esm/services/borrow/index.js +3 -0
- package/dist/esm/services/borrow/internal/borrowApprovalService.js +48 -0
- package/dist/esm/services/borrow/internal/borrowContextStore.js +35 -0
- package/dist/esm/services/borrow/internal/borrowErc20.js +38 -0
- package/dist/esm/services/borrow/internal/borrowHints.js +27 -0
- package/dist/esm/services/borrow/internal/borrowPositionParser.js +82 -0
- package/dist/esm/services/borrow/internal/borrowReadService.js +271 -0
- package/dist/esm/services/borrow/internal/borrowRegistryReader.js +108 -0
- package/dist/esm/services/borrow/internal/borrowTransactionService.js +271 -0
- package/dist/esm/services/borrow/internal/borrowTypes.js +1 -0
- package/dist/esm/services/borrow/internal/borrowValidation.js +89 -0
- package/dist/esm/services/index.js +8 -0
- package/dist/esm/services/liquidity/LiquidityService.js +163 -0
- package/dist/esm/services/liquidity/basicLiquidity.js +162 -0
- package/dist/esm/services/liquidity/index.js +1 -0
- package/dist/esm/services/liquidity/liquidityHelpers.js +95 -0
- package/dist/esm/services/liquidity/rebalance.js +59 -0
- package/dist/esm/services/liquidity/zapHelpers.js +120 -0
- package/dist/esm/services/liquidity/zapIn.js +112 -0
- package/dist/esm/services/liquidity/zapOut.js +248 -0
- package/dist/esm/services/pools/PoolService.js +204 -0
- package/dist/esm/services/pools/index.js +1 -0
- package/dist/esm/services/pools/poolDetails.js +209 -0
- package/dist/esm/services/pools/poolDiscovery.js +112 -0
- package/dist/esm/services/pools/rebalancePreview.js +181 -0
- package/dist/esm/services/quotes/QuoteService.js +85 -0
- package/dist/esm/services/quotes/index.js +1 -0
- package/dist/esm/services/routes/RouteService.js +268 -0
- package/dist/esm/services/routes/index.js +1 -0
- package/dist/esm/services/swap/SwapService.js +247 -0
- package/dist/esm/services/swap/index.js +1 -0
- package/dist/esm/services/tokens/index.js +1 -0
- package/dist/esm/services/tokens/tokenService.js +285 -0
- package/dist/esm/services/trading/TradingLimitsService.js +154 -0
- package/dist/esm/services/trading/TradingService.js +222 -0
- package/dist/esm/services/trading/index.js +2 -0
- package/dist/esm/utils/chainConfig.js +118 -0
- package/dist/esm/utils/costUtils.js +56 -0
- package/dist/esm/utils/deadline.js +22 -0
- package/dist/esm/utils/index.js +9 -0
- package/dist/esm/utils/multicall.js +47 -0
- package/dist/esm/utils/pathEncoder.js +69 -0
- package/dist/esm/utils/rateFeed.js +23 -0
- package/dist/esm/utils/retry.js +24 -0
- package/dist/esm/{routeUtils.js → utils/routeUtils.js} +115 -116
- package/dist/esm/utils/routes.js +2 -0
- package/dist/esm/utils/sortUtils.js +33 -0
- package/dist/esm/utils/tokens.js +2 -0
- package/dist/esm/utils/tradingLimits.js +163 -0
- package/dist/esm/utils/validation.js +30 -0
- package/dist/index.d.ts +101 -0
- package/dist/index.js +158 -0
- package/dist/services/borrow/BorrowService.d.ts +381 -0
- package/dist/services/borrow/BorrowService.js +460 -0
- package/dist/services/borrow/borrowHelpers.d.ts +4 -0
- package/dist/services/borrow/borrowHelpers.js +13 -0
- package/dist/services/borrow/borrowMath.d.ts +21 -0
- package/dist/services/borrow/borrowMath.js +137 -0
- package/dist/services/borrow/index.d.ts +4 -0
- package/dist/{cjs/interfaces → services/borrow}/index.js +4 -4
- package/dist/services/borrow/internal/borrowApprovalService.d.ts +14 -0
- package/dist/services/borrow/internal/borrowApprovalService.js +53 -0
- package/dist/services/borrow/internal/borrowContextStore.d.ts +11 -0
- package/dist/services/borrow/internal/borrowContextStore.js +40 -0
- package/dist/services/borrow/internal/borrowErc20.d.ts +5 -0
- package/dist/services/borrow/internal/borrowErc20.js +43 -0
- package/dist/services/borrow/internal/borrowHints.d.ts +7 -0
- package/dist/services/borrow/internal/borrowHints.js +31 -0
- package/dist/services/borrow/internal/borrowPositionParser.d.ts +4 -0
- package/dist/services/borrow/internal/borrowPositionParser.js +87 -0
- package/dist/services/borrow/internal/borrowReadService.d.ts +31 -0
- package/dist/services/borrow/internal/borrowReadService.js +276 -0
- package/dist/services/borrow/internal/borrowRegistryReader.d.ts +5 -0
- package/dist/services/borrow/internal/borrowRegistryReader.js +113 -0
- package/dist/services/borrow/internal/borrowTransactionService.d.ts +23 -0
- package/dist/services/borrow/internal/borrowTransactionService.js +276 -0
- package/dist/services/borrow/internal/borrowTypes.d.ts +15 -0
- package/dist/services/borrow/internal/borrowTypes.js +3 -0
- package/dist/services/borrow/internal/borrowValidation.d.ts +14 -0
- package/dist/services/borrow/internal/borrowValidation.js +104 -0
- package/dist/services/index.d.ts +9 -0
- package/dist/{cjs → services}/index.js +9 -7
- package/dist/services/liquidity/LiquidityService.d.ts +139 -0
- package/dist/services/liquidity/LiquidityService.js +168 -0
- package/dist/services/liquidity/basicLiquidity.d.ts +11 -0
- package/dist/services/liquidity/basicLiquidity.js +172 -0
- package/dist/services/liquidity/index.d.ts +2 -0
- package/dist/{cjs/types → services/liquidity}/index.js +2 -2
- package/dist/services/liquidity/liquidityHelpers.d.ts +19 -0
- package/dist/services/liquidity/liquidityHelpers.js +104 -0
- package/dist/services/liquidity/rebalance.d.ts +6 -0
- package/dist/services/liquidity/rebalance.js +64 -0
- package/dist/services/liquidity/zapHelpers.d.ts +66 -0
- package/dist/services/liquidity/zapHelpers.js +129 -0
- package/dist/services/liquidity/zapIn.d.ts +18 -0
- package/dist/services/liquidity/zapIn.js +119 -0
- package/dist/services/liquidity/zapOut.d.ts +9 -0
- package/dist/services/liquidity/zapOut.js +255 -0
- package/dist/services/pools/PoolService.d.ts +69 -0
- package/dist/services/pools/PoolService.js +209 -0
- package/dist/services/pools/index.d.ts +2 -0
- package/dist/{cjs/enums → services/pools}/index.js +2 -3
- package/dist/services/pools/poolDetails.d.ts +13 -0
- package/dist/services/pools/poolDetails.js +216 -0
- package/dist/services/pools/poolDiscovery.d.ts +12 -0
- package/dist/services/pools/poolDiscovery.js +117 -0
- package/dist/services/pools/rebalancePreview.d.ts +5 -0
- package/dist/services/pools/rebalancePreview.js +186 -0
- package/dist/services/quotes/QuoteService.d.ts +51 -0
- package/dist/services/quotes/QuoteService.js +91 -0
- package/dist/services/quotes/index.d.ts +2 -0
- package/dist/services/quotes/index.js +18 -0
- package/dist/services/routes/RouteService.d.ts +117 -0
- package/dist/services/routes/RouteService.js +306 -0
- package/dist/services/routes/index.d.ts +2 -0
- package/dist/services/routes/index.js +18 -0
- package/dist/services/swap/SwapService.d.ts +198 -0
- package/dist/services/swap/SwapService.js +252 -0
- package/dist/services/swap/index.d.ts +2 -0
- package/dist/services/swap/index.js +18 -0
- package/dist/services/tokens/index.d.ts +2 -0
- package/dist/services/tokens/index.js +18 -0
- package/dist/services/tokens/tokenService.d.ts +55 -0
- package/dist/services/tokens/tokenService.js +290 -0
- package/dist/services/trading/TradingLimitsService.d.ts +38 -0
- package/dist/services/trading/TradingLimitsService.js +159 -0
- package/dist/services/trading/TradingService.d.ts +115 -0
- package/dist/services/trading/TradingService.js +227 -0
- package/dist/services/trading/index.d.ts +3 -0
- package/dist/services/trading/index.js +19 -0
- package/dist/utils/chainConfig.d.ts +16 -0
- package/dist/utils/chainConfig.js +123 -0
- package/dist/utils/costUtils.d.ts +12 -0
- package/dist/utils/costUtils.js +60 -0
- package/dist/utils/deadline.d.ts +21 -0
- package/dist/utils/deadline.js +26 -0
- package/dist/utils/index.d.ts +10 -0
- package/dist/utils/index.js +26 -0
- package/dist/utils/multicall.d.ts +30 -0
- package/dist/utils/multicall.js +52 -0
- package/dist/utils/pathEncoder.d.ts +34 -0
- package/dist/utils/pathEncoder.js +73 -0
- package/dist/utils/rateFeed.d.ts +18 -0
- package/dist/utils/rateFeed.js +27 -0
- package/dist/utils/retry.d.ts +12 -0
- package/dist/utils/retry.js +28 -0
- package/dist/{esm → utils}/routeUtils.d.ts +84 -93
- package/dist/{cjs → utils}/routeUtils.js +123 -124
- package/dist/utils/routes.d.ts +3 -0
- package/dist/utils/routes.js +8 -0
- package/dist/utils/sortUtils.d.ts +24 -0
- package/dist/utils/sortUtils.js +39 -0
- package/dist/utils/tokens.d.ts +2 -0
- package/dist/utils/tokens.js +13 -0
- package/dist/utils/tradingLimits.d.ts +41 -0
- package/dist/utils/tradingLimits.js +171 -0
- package/dist/utils/validation.d.ts +19 -0
- package/dist/utils/validation.js +34 -0
- package/package.json +1 -1
- package/dist/cjs/ChainClient.d.ts +0 -9
- package/dist/cjs/ChainClient.js +0 -58
- package/dist/cjs/TestChainClient.d.ts +0 -7
- package/dist/cjs/TestChainClient.js +0 -41
- package/dist/cjs/constants/addresses.d.ts +0 -4
- package/dist/cjs/constants/addresses.js +0 -59
- package/dist/cjs/constants/currencies.d.ts +0 -5
- package/dist/cjs/constants/currencies.js +0 -29
- package/dist/cjs/constants/index.d.ts +0 -4
- package/dist/cjs/constants/tokens.11142220.d.ts +0 -2
- package/dist/cjs/constants/tokens.11142220.js +0 -128
- package/dist/cjs/constants/tokens.42220.d.ts +0 -2
- package/dist/cjs/constants/tokens.42220.js +0 -128
- package/dist/cjs/constants/tokens.d.ts +0 -65
- package/dist/cjs/constants/tokens.js +0 -170
- package/dist/cjs/constants/tradablePairs.11142220.d.ts +0 -2
- package/dist/cjs/constants/tradablePairs.11142220.js +0 -7391
- package/dist/cjs/constants/tradablePairs.42220.d.ts +0 -2
- package/dist/cjs/constants/tradablePairs.42220.js +0 -7391
- package/dist/cjs/constants/tradablePairs.d.ts +0 -16
- package/dist/cjs/constants/tradablePairs.js +0 -53
- package/dist/cjs/enums/chainId.d.ts +0 -4
- package/dist/cjs/enums/index.d.ts +0 -3
- package/dist/cjs/enums/proposalState.d.ts +0 -10
- package/dist/cjs/enums/proposalState.js +0 -14
- package/dist/cjs/enums/tradingMode.d.ts +0 -17
- package/dist/cjs/enums/tradingMode.js +0 -21
- package/dist/cjs/governance.d.ts +0 -62
- package/dist/cjs/governance.js +0 -151
- package/dist/cjs/index.d.ts +0 -7
- package/dist/cjs/interfaces/IChainClient.d.ts +0 -6
- package/dist/cjs/interfaces/index.d.ts +0 -4
- package/dist/cjs/interfaces/tradingLimit.d.ts +0 -7
- package/dist/cjs/interfaces/tradingLimitsConfig.d.ts +0 -10
- package/dist/cjs/interfaces/tradingLimitsState.d.ts +0 -9
- package/dist/cjs/limits.d.ts +0 -33
- package/dist/cjs/limits.js +0 -130
- package/dist/cjs/mento.d.ts +0 -287
- package/dist/cjs/mento.js +0 -699
- package/dist/cjs/routeUtils.d.ts +0 -304
- package/dist/cjs/types/contractAddressMap.d.ts +0 -4
- package/dist/cjs/types/contractAddressMap.js +0 -2
- package/dist/cjs/types/contractAddresses.d.ts +0 -23
- package/dist/cjs/types/index.d.ts +0 -2
- package/dist/cjs/utils.d.ts +0 -80
- package/dist/cjs/utils.js +0 -177
- package/dist/esm/ChainClient.d.ts +0 -9
- package/dist/esm/ChainClient.js +0 -54
- package/dist/esm/TestChainClient.d.ts +0 -7
- package/dist/esm/TestChainClient.js +0 -37
- package/dist/esm/constants/addresses.d.ts +0 -4
- package/dist/esm/constants/addresses.js +0 -55
- package/dist/esm/constants/currencies.d.ts +0 -5
- package/dist/esm/constants/currencies.js +0 -26
- package/dist/esm/constants/index.d.ts +0 -4
- package/dist/esm/constants/index.js +0 -4
- package/dist/esm/constants/tokens.11142220.d.ts +0 -2
- package/dist/esm/constants/tokens.11142220.js +0 -125
- package/dist/esm/constants/tokens.42220.d.ts +0 -2
- package/dist/esm/constants/tokens.42220.js +0 -125
- package/dist/esm/constants/tokens.d.ts +0 -65
- package/dist/esm/constants/tokens.js +0 -142
- package/dist/esm/constants/tradablePairs.11142220.d.ts +0 -2
- package/dist/esm/constants/tradablePairs.11142220.js +0 -7388
- package/dist/esm/constants/tradablePairs.42220.d.ts +0 -2
- package/dist/esm/constants/tradablePairs.42220.js +0 -7388
- package/dist/esm/constants/tradablePairs.d.ts +0 -16
- package/dist/esm/constants/tradablePairs.js +0 -26
- package/dist/esm/enums/chainId.d.ts +0 -4
- package/dist/esm/enums/index.d.ts +0 -3
- package/dist/esm/enums/index.js +0 -3
- package/dist/esm/enums/proposalState.d.ts +0 -10
- package/dist/esm/enums/proposalState.js +0 -11
- package/dist/esm/enums/tradingMode.d.ts +0 -17
- package/dist/esm/enums/tradingMode.js +0 -18
- package/dist/esm/governance.d.ts +0 -62
- package/dist/esm/governance.js +0 -147
- package/dist/esm/index.d.ts +0 -7
- package/dist/esm/interfaces/IChainClient.d.ts +0 -6
- package/dist/esm/interfaces/index.d.ts +0 -4
- package/dist/esm/interfaces/index.js +0 -4
- package/dist/esm/interfaces/tradingLimit.d.ts +0 -7
- package/dist/esm/interfaces/tradingLimitsConfig.d.ts +0 -10
- package/dist/esm/interfaces/tradingLimitsState.d.ts +0 -9
- package/dist/esm/limits.d.ts +0 -33
- package/dist/esm/limits.js +0 -123
- package/dist/esm/mento.d.ts +0 -287
- package/dist/esm/mento.js +0 -671
- package/dist/esm/types/contractAddressMap.d.ts +0 -4
- package/dist/esm/types/contractAddresses.d.ts +0 -23
- package/dist/esm/types/index.d.ts +0 -2
- package/dist/esm/types/index.js +0 -2
- package/dist/esm/utils.d.ts +0 -80
- package/dist/esm/utils.js +0 -162
- /package/dist/esm/{interfaces/IChainClient.js → core/types/borrow.js} +0 -0
- /package/dist/esm/{types → core/types}/contractAddresses.js +0 -0
- /package/dist/esm/{interfaces/tradingLimit.js → core/types/liquidity.js} +0 -0
- /package/dist/esm/{interfaces/tradingLimitsConfig.js → core/types/provider.js} +0 -0
- /package/dist/esm/{interfaces/tradingLimitsState.js → core/types/route.js} +0 -0
- /package/dist/esm/{types/contractAddressMap.js → core/types/token.js} +0 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { canonicalSymbolKey } from './sortUtils';
|
|
1
2
|
/**
|
|
2
3
|
* Builds the connectivity data structures needed for route generation.
|
|
3
4
|
*
|
|
@@ -8,23 +9,23 @@
|
|
|
8
9
|
*
|
|
9
10
|
* ```
|
|
10
11
|
* Input: TradablePairs = [
|
|
11
|
-
* { id: '
|
|
12
|
-
* { id: 'CELO-
|
|
12
|
+
* { id: 'USDm-CELO', assets: [USDm, CELO], path: [exchange1_CELO_USDm] },
|
|
13
|
+
* { id: 'CELO-EURm', assets: [CELO, EURm], path: [exchange2_CELO_EURm] }
|
|
13
14
|
* ]
|
|
14
15
|
*
|
|
15
16
|
* Step 1 - Build addrToSymbol map:
|
|
16
|
-
*
|
|
17
|
+
* USDm.address → 'USDm'
|
|
17
18
|
* CELO.address → 'CELO'
|
|
18
|
-
*
|
|
19
|
+
* EURm.address → 'EURm'
|
|
19
20
|
*
|
|
20
21
|
* Step 2 - Build directPathMap (sorted alphabetically for consistency):
|
|
21
|
-
* 'CELO_addr-
|
|
22
|
-
* 'CELO_addr-
|
|
22
|
+
* 'CELO_addr-EURm_addr' → exchange2_CELO_EURm
|
|
23
|
+
* 'CELO_addr-USDm_addr' → exchange1_CELO_USDm
|
|
23
24
|
*
|
|
24
25
|
* Step 3 - Build bidirectional tokenGraph:
|
|
25
|
-
*
|
|
26
|
-
* CELO.address → Set([
|
|
27
|
-
*
|
|
26
|
+
* USDm.address → Set([CELO.address])
|
|
27
|
+
* CELO.address → Set([USDm.address, EURm.address])
|
|
28
|
+
* EURm.address → Set([CELO.address])
|
|
28
29
|
* ```
|
|
29
30
|
*
|
|
30
31
|
* **Result**: We can now efficiently answer:
|
|
@@ -32,59 +33,57 @@
|
|
|
32
33
|
* - "What exchange connects tokens X and Y?" → directPathMap.get(sortedAddressPairKey)
|
|
33
34
|
* - "What tokens can I reach from token X?" → tokenGraph.get(X)
|
|
34
35
|
*
|
|
35
|
-
* @param
|
|
36
|
+
* @param directRoutes - Array of direct trading pairs
|
|
36
37
|
* @returns Connectivity data structure for efficient route generation
|
|
37
38
|
*
|
|
38
39
|
* @example
|
|
39
40
|
* ```typescript
|
|
40
41
|
* const directPairs = [
|
|
41
|
-
* { id: '
|
|
42
|
-
* { id: 'CELO-
|
|
42
|
+
* { id: 'USDm-CELO', assets: [USDm, CELO], path: [exchange1] },
|
|
43
|
+
* { id: 'CELO-EURm', assets: [CELO, EURm], path: [exchange2] }
|
|
43
44
|
* ]
|
|
44
45
|
*
|
|
45
46
|
* const connectivityData = buildConnectivityStructures(directPairs)
|
|
46
47
|
*
|
|
47
48
|
* // Now we can efficiently find routes:
|
|
48
|
-
* // 1. Check if
|
|
49
|
-
* // 2. Check if CELO connects to
|
|
50
|
-
* // 3. Get exchange details: connectivityData.directPathMap.get('CELO_addr-
|
|
51
|
-
* // Result: Found route
|
|
49
|
+
* // 1. Check if USDm connects to anything: connectivityData.tokenGraph.get(USDm.address) → [CELO.address]
|
|
50
|
+
* // 2. Check if CELO connects to EURm: connectivityData.tokenGraph.get(CELO.address) → [USDm.address, EURm.address] ✓
|
|
51
|
+
* // 3. Get exchange details: connectivityData.directPathMap.get('CELO_addr-EURm_addr') → exchange2_CELO_EURm
|
|
52
|
+
* // Result: Found route USDm → CELO → EURm with exchange details
|
|
52
53
|
* ```
|
|
53
54
|
*/
|
|
54
|
-
export function buildConnectivityStructures(
|
|
55
|
+
export function buildConnectivityStructures(directRoutes) {
|
|
55
56
|
const addrToSymbol = new Map();
|
|
56
|
-
const
|
|
57
|
+
const directRouteMap = new Map();
|
|
57
58
|
const tokenGraph = new Map();
|
|
58
|
-
for (const
|
|
59
|
-
const [
|
|
59
|
+
for (const route of directRoutes) {
|
|
60
|
+
const [tokenA, tokenB] = route.tokens;
|
|
60
61
|
// Build address-to-symbol map for quick symbol lookups
|
|
61
|
-
addrToSymbol.set(
|
|
62
|
-
addrToSymbol.set(
|
|
62
|
+
addrToSymbol.set(tokenA.address, tokenA.symbol);
|
|
63
|
+
addrToSymbol.set(tokenB.address, tokenB.symbol);
|
|
63
64
|
// Build direct path map (sorted addresses as key for consistency)
|
|
64
65
|
// for quick lookup of exchange details for any token pair
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
.
|
|
68
|
-
if (!directPathMap.has(sortedAddresses)) {
|
|
69
|
-
directPathMap.set(sortedAddresses, pair.path[0]);
|
|
66
|
+
const routeId = canonicalSymbolKey(tokenA.symbol, tokenB.symbol);
|
|
67
|
+
if (!directRouteMap.has(routeId)) {
|
|
68
|
+
directRouteMap.set(routeId, route.path[0]);
|
|
70
69
|
}
|
|
71
70
|
// Build bidirectional connectivity graph for route traversal
|
|
72
71
|
// Each token can reach its directly connected tokens
|
|
73
|
-
if (!tokenGraph.has(
|
|
74
|
-
tokenGraph.set(
|
|
75
|
-
if (!tokenGraph.has(
|
|
76
|
-
tokenGraph.set(
|
|
77
|
-
tokenGraph.get(
|
|
78
|
-
tokenGraph.get(
|
|
72
|
+
if (!tokenGraph.has(tokenA.address))
|
|
73
|
+
tokenGraph.set(tokenA.address, new Set());
|
|
74
|
+
if (!tokenGraph.has(tokenB.address))
|
|
75
|
+
tokenGraph.set(tokenB.address, new Set());
|
|
76
|
+
tokenGraph.get(tokenA.address).add(tokenB.address);
|
|
77
|
+
tokenGraph.get(tokenB.address).add(tokenA.address);
|
|
79
78
|
}
|
|
80
|
-
return { addrToSymbol,
|
|
79
|
+
return { addrToSymbol, directRouteMap, tokenGraph, directRoutes };
|
|
81
80
|
}
|
|
82
81
|
/**
|
|
83
82
|
* Generates all possible routes (direct + two-hop) using connectivity data.
|
|
84
83
|
*
|
|
85
84
|
* This function implements a route discovery algorithm that:
|
|
86
85
|
*
|
|
87
|
-
* 1. **Adds all direct
|
|
86
|
+
* 1. **Adds all direct routes** (single-hop routes)
|
|
88
87
|
* 2. **Discovers two-hop routes** using graph traversal:
|
|
89
88
|
* - For each token A, find its neighbors (tokens directly connected)
|
|
90
89
|
* - For each neighbor B, find B's neighbors
|
|
@@ -92,64 +91,64 @@ export function buildConnectivityStructures(directPairs) {
|
|
|
92
91
|
*
|
|
93
92
|
* **Route Deduplication**: Multiple routes between the same token pair
|
|
94
93
|
* are collected in arrays, allowing the selection algorithm to choose
|
|
95
|
-
* the best one based on
|
|
94
|
+
* the best one based on cost data or heuristics.
|
|
96
95
|
*
|
|
97
|
-
* **Canonical
|
|
98
|
-
* (e.g., '
|
|
96
|
+
* **Canonical Route IDs**: All routes use alphabetically sorted symbols
|
|
97
|
+
* (e.g., 'EURm-USDm' not 'USDm-EURm') for consistent identification.
|
|
99
98
|
*
|
|
100
99
|
* @param connectivityData - The connectivity data from buildConnectivityStructures()
|
|
101
|
-
* @returns Map of
|
|
100
|
+
* @returns Map of route ID -> array of possible routes for that token pair
|
|
102
101
|
*
|
|
103
102
|
* @example
|
|
104
103
|
* ```typescript
|
|
105
|
-
* // Given direct
|
|
104
|
+
* // Given direct routes: USDm-CELO, CELO-EURm, USDm-USDC
|
|
106
105
|
* const allRoutes = generateAllRoutes(connectivityData)
|
|
107
106
|
*
|
|
108
107
|
* // Results might include:
|
|
109
|
-
* // '
|
|
110
|
-
* // '
|
|
111
|
-
* // { path: [
|
|
112
|
-
* // { path: [
|
|
108
|
+
* // 'USDm-CELO' -> [{ path: [USDm->CELO] }] // direct route
|
|
109
|
+
* // 'EURm-USDm' -> [
|
|
110
|
+
* // { path: [USDm->USDC, USDC->EURm] } // two-hop via USDC
|
|
111
|
+
* // { path: [USDm->CELO, CELO->EURm] } // two-hop via CELO
|
|
113
112
|
* // ]
|
|
114
113
|
* ```
|
|
115
114
|
*/
|
|
116
115
|
export function generateAllRoutes(connectivityData) {
|
|
117
|
-
const { addrToSymbol,
|
|
116
|
+
const { addrToSymbol, directRouteMap, tokenGraph, directRoutes } = connectivityData;
|
|
118
117
|
const allRoutes = new Map();
|
|
119
118
|
// Step 1: Add all direct pairs (single-hop routes)
|
|
120
|
-
for (const
|
|
121
|
-
if (!allRoutes.has(
|
|
122
|
-
allRoutes.set(
|
|
119
|
+
for (const route of directRoutes) {
|
|
120
|
+
if (!allRoutes.has(route.id)) {
|
|
121
|
+
allRoutes.set(route.id, []);
|
|
123
122
|
}
|
|
124
|
-
allRoutes.get(
|
|
123
|
+
allRoutes.get(route.id).push(route);
|
|
125
124
|
}
|
|
126
|
-
// Step 2: Generate two-hop
|
|
125
|
+
// Step 2: Generate two-hop routes using graph traversal
|
|
127
126
|
// Algorithm: For each token, explore all paths of length 2
|
|
128
|
-
// OUTER LOOP: "For each starting token..." (e.g.,
|
|
127
|
+
// OUTER LOOP: "For each starting token..." (e.g., USDm, CELO, EURm, etc.)
|
|
129
128
|
for (const [start, neighbors] of tokenGraph.entries()) {
|
|
130
129
|
// MIDDLE LOOP: "Where can I go from the starting token?" (first hop)
|
|
131
|
-
// Example: If start =
|
|
130
|
+
// Example: If start = USDm, neighbors might be [CELO, USDC, KESm]
|
|
132
131
|
for (const intermediate of neighbors) {
|
|
133
132
|
// Get all tokens reachable from this intermediate token (second hop destinations)
|
|
134
133
|
const secondHopNeighbors = tokenGraph.get(intermediate);
|
|
135
134
|
if (!secondHopNeighbors)
|
|
136
135
|
continue;
|
|
137
136
|
// INNER LOOP: "From the intermediate token, where can I go?" (second hop)
|
|
138
|
-
// Example: If intermediate = CELO, secondHopNeighbors might be [
|
|
137
|
+
// Example: If intermediate = CELO, secondHopNeighbors might be [USDm, EURm, BRLm]
|
|
139
138
|
for (const end of secondHopNeighbors) {
|
|
140
|
-
// Skip circular routes like
|
|
139
|
+
// Skip circular routes like USDm → CELO → USDm (pointless)
|
|
141
140
|
if (end === start)
|
|
142
141
|
continue;
|
|
143
142
|
// At this point we have a potential route: start → intermediate → end
|
|
144
|
-
// Example:
|
|
143
|
+
// Example: USDm → CELO → EURm
|
|
145
144
|
// Try to create a valid two-hop trading pair from this route
|
|
146
|
-
const
|
|
145
|
+
const twoHopRoute = createTwoHopRoute(start, intermediate, end, addrToSymbol, directRouteMap);
|
|
147
146
|
// If we successfully created the pair, add it to our collection
|
|
148
|
-
if (
|
|
149
|
-
if (!allRoutes.has(
|
|
150
|
-
allRoutes.set(
|
|
147
|
+
if (twoHopRoute) {
|
|
148
|
+
if (!allRoutes.has(twoHopRoute.id)) {
|
|
149
|
+
allRoutes.set(twoHopRoute.id, []);
|
|
151
150
|
}
|
|
152
|
-
allRoutes.get(
|
|
151
|
+
allRoutes.get(twoHopRoute.id).push(twoHopRoute);
|
|
153
152
|
}
|
|
154
153
|
}
|
|
155
154
|
}
|
|
@@ -179,52 +178,52 @@ export function generateAllRoutes(connectivityData) {
|
|
|
179
178
|
*
|
|
180
179
|
* @example
|
|
181
180
|
* ```typescript
|
|
182
|
-
* // Create route:
|
|
181
|
+
* // Create route: USDm -> CELO -> EURm
|
|
183
182
|
* const pair = createTwoHopPair(
|
|
184
|
-
* '0x765D...', //
|
|
183
|
+
* '0x765D...', // USDm address
|
|
185
184
|
* '0x471E...', // CELO address
|
|
186
|
-
* '0xD876...', //
|
|
185
|
+
* '0xD876...', // EURm address
|
|
187
186
|
* addrToSymbol,
|
|
188
187
|
* directPathMap
|
|
189
188
|
* )
|
|
190
189
|
*
|
|
191
190
|
* // Result:
|
|
192
191
|
* // {
|
|
193
|
-
* // id: '
|
|
194
|
-
* // assets: [
|
|
192
|
+
* // id: 'EURm-USDm', // alphabetical order
|
|
193
|
+
* // assets: [EURm, USDm], // alphabetical order
|
|
195
194
|
* // path: [ // actual routing path
|
|
196
|
-
* // {
|
|
197
|
-
* // { CELO->
|
|
195
|
+
* // { USDm->CELO exchange },
|
|
196
|
+
* // { CELO->EURm exchange }
|
|
198
197
|
* // ]
|
|
199
198
|
* // }
|
|
200
199
|
* ```
|
|
201
200
|
*/
|
|
202
|
-
export function
|
|
203
|
-
// Validate that both
|
|
204
|
-
const startSymbol = addrToSymbol.get(
|
|
205
|
-
const
|
|
206
|
-
|
|
201
|
+
export function createTwoHopRoute(startAddr, intermediateAddr, endAddr, addrToSymbol, directRouteMap) {
|
|
202
|
+
// Validate that both all tokens exist in our address-to-symbol map
|
|
203
|
+
const startSymbol = addrToSymbol.get(startAddr);
|
|
204
|
+
const intermediateSymbol = addrToSymbol.get(intermediateAddr);
|
|
205
|
+
const endSymbol = addrToSymbol.get(endAddr);
|
|
206
|
+
if (!startSymbol || !intermediateSymbol || !endSymbol)
|
|
207
207
|
return null;
|
|
208
|
-
// Create Asset objects from address and symbol
|
|
209
|
-
const startAsset = { address: startToken, symbol: startSymbol };
|
|
210
|
-
const endAsset = { address: endToken, symbol: endSymbol };
|
|
211
208
|
// Find exchange hops for both segments of the two-hop route
|
|
212
209
|
// Keys are sorted token addresses for consistent lookup
|
|
213
|
-
const hop1Key =
|
|
214
|
-
const hop2Key =
|
|
215
|
-
const hop1 =
|
|
216
|
-
const hop2 =
|
|
210
|
+
const hop1Key = canonicalSymbolKey(startSymbol, intermediateSymbol);
|
|
211
|
+
const hop2Key = canonicalSymbolKey(intermediateSymbol, endSymbol);
|
|
212
|
+
const hop1 = directRouteMap.get(hop1Key);
|
|
213
|
+
const hop2 = directRouteMap.get(hop2Key);
|
|
217
214
|
// If either hop doesn't exist, this route is invalid
|
|
218
215
|
if (!hop1 || !hop2)
|
|
219
216
|
return null;
|
|
220
217
|
// Create canonical pair structure (alphabetical symbol ordering)
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
const
|
|
218
|
+
const routeId = canonicalSymbolKey(startSymbol, endSymbol);
|
|
219
|
+
// Create Token objects from address and symbol
|
|
220
|
+
const startToken = { address: startAddr, symbol: startSymbol };
|
|
221
|
+
const endToken = { address: endAddr, symbol: endSymbol };
|
|
222
|
+
// Token array follows alphabetical ordering for consistency
|
|
223
|
+
const tokens = startSymbol <= endSymbol ? [startToken, endToken] : [endToken, startToken];
|
|
225
224
|
return {
|
|
226
|
-
id:
|
|
227
|
-
|
|
225
|
+
id: routeId,
|
|
226
|
+
tokens,
|
|
228
227
|
path: [hop1, hop2], // Preserves actual routing path for execution
|
|
229
228
|
};
|
|
230
229
|
}
|
|
@@ -249,58 +248,58 @@ export function createTwoHopPair(startToken, intermediateToken, endToken, addrTo
|
|
|
249
248
|
*
|
|
250
249
|
* @example
|
|
251
250
|
* ```typescript
|
|
252
|
-
* // Multiple routes for
|
|
251
|
+
* // Multiple routes for USDm-EURm pair
|
|
253
252
|
* const candidates = new Map([
|
|
254
|
-
* ['
|
|
255
|
-
* { path: [
|
|
256
|
-
* { path: [
|
|
257
|
-
* { path: [
|
|
253
|
+
* ['EURm-USDm', [
|
|
254
|
+
* { path: [USDm->CELO->EURm], costData: { totalCostPercent: 0.5 } },
|
|
255
|
+
* { path: [USDm->BRLm->EURm], costData: { totalCostPercent: 0.3 } },
|
|
256
|
+
* { path: [USDm->EURm] } // direct route, no cost data
|
|
258
257
|
* ]]
|
|
259
258
|
* ])
|
|
260
259
|
*
|
|
261
260
|
* const optimal = selectOptimalRoutes(candidates, false, assetMap)
|
|
262
|
-
* // Returns the
|
|
261
|
+
* // Returns the USDm->BRLm->EURm route (lowest cost: 0.3%)
|
|
263
262
|
* ```
|
|
264
263
|
*/
|
|
265
264
|
export function selectOptimalRoutes(allRoutes, returnAllRoutes, addrToSymbol) {
|
|
266
265
|
const result = new Map();
|
|
267
|
-
for (const [
|
|
266
|
+
for (const [routeId, routes] of allRoutes) {
|
|
268
267
|
if (routes.length === 1) {
|
|
269
268
|
// Only one route available - use it directly
|
|
270
|
-
result.set(
|
|
269
|
+
result.set(routeId, routes[0]);
|
|
271
270
|
}
|
|
272
271
|
else if (returnAllRoutes) {
|
|
273
272
|
// Return all routes with unique keys (used for cache generation)
|
|
274
273
|
routes.forEach((route, index) => {
|
|
275
|
-
result.set(`${
|
|
274
|
+
result.set(`${routeId}_${index}`, route);
|
|
276
275
|
});
|
|
277
276
|
}
|
|
278
277
|
else {
|
|
279
278
|
// Multiple routes - select the best one using optimization logic
|
|
280
279
|
const bestRoute = selectBestRoute(routes, addrToSymbol);
|
|
281
|
-
result.set(
|
|
280
|
+
result.set(routeId, bestRoute);
|
|
282
281
|
}
|
|
283
282
|
}
|
|
284
283
|
return Array.from(result.values());
|
|
285
284
|
}
|
|
286
285
|
/**
|
|
287
|
-
* Selects the best route from candidates using
|
|
286
|
+
* Selects the best route from candidates using cost data or fallback heuristics.
|
|
288
287
|
*
|
|
289
288
|
* This function implements a sophisticated route selection algorithm with
|
|
290
289
|
* multiple optimization tiers:
|
|
291
290
|
*
|
|
292
|
-
* **Tier 1 -
|
|
293
|
-
* - Use routes with
|
|
294
|
-
* - Select route with lowest `
|
|
291
|
+
* **Tier 1 - Cost-Based Optimization** (Preferred):
|
|
292
|
+
* - Use routes with cost data (actual cost information)
|
|
293
|
+
* - Select route with lowest `totalCostPercent`
|
|
295
294
|
* - This provides the most cost-efficient trading
|
|
296
295
|
*
|
|
297
296
|
* **Tier 2 - Direct Route Preference** (Fallback):
|
|
298
|
-
* - If no
|
|
297
|
+
* - If no cost data available, prefer direct (single-hop) routes
|
|
299
298
|
* - Direct routes have lower execution risk and gas costs
|
|
300
299
|
*
|
|
301
300
|
* **Tier 3 - Major Stablecoin Preference** (Final Fallback):
|
|
302
301
|
* - For two-hop routes, prefer those going through major stablecoins
|
|
303
|
-
* - Major FX currencies like
|
|
302
|
+
* - Major FX currencies like USDm and EURm typically have better liquidity
|
|
304
303
|
*
|
|
305
304
|
* **Tier 4 - First Available** (Last Resort):
|
|
306
305
|
* - If no other heuristics apply, use the first route found
|
|
@@ -312,29 +311,27 @@ export function selectOptimalRoutes(allRoutes, returnAllRoutes, addrToSymbol) {
|
|
|
312
311
|
* @example
|
|
313
312
|
* ```typescript
|
|
314
313
|
* const candidates = [
|
|
315
|
-
* { path: [A->B->C],
|
|
316
|
-
* { path: [A->D->C],
|
|
317
|
-
* { path: [A->C] }, // direct route, no
|
|
314
|
+
* { path: [A->B->C], costData: { totalCostPercent: 0.8 } },
|
|
315
|
+
* { path: [A->D->C], costData: { totalCostPercent: 0.4 } }, // Winner: lowest cost
|
|
316
|
+
* { path: [A->C] }, // direct route, no cost data
|
|
318
317
|
* ]
|
|
319
318
|
*
|
|
320
319
|
* const best = selectBestRoute(candidates, assetMap)
|
|
321
|
-
* // Returns the A->D->C route (0.4%
|
|
320
|
+
* // Returns the A->D->C route (0.4% cost)
|
|
322
321
|
* ```
|
|
323
322
|
*/
|
|
324
323
|
export function selectBestRoute(candidates, addrToSymbol) {
|
|
325
|
-
// Tier 1: Prefer routes with
|
|
326
|
-
const
|
|
327
|
-
if (
|
|
328
|
-
return
|
|
329
|
-
? current
|
|
330
|
-
: best);
|
|
324
|
+
// Tier 1: Prefer routes with cost data (lowest cost wins)
|
|
325
|
+
const candidatesWithCost = candidates.filter(hasCostData);
|
|
326
|
+
if (candidatesWithCost.length > 0) {
|
|
327
|
+
return candidatesWithCost.reduce((best, current) => current.costData.totalCostPercent < best.costData.totalCostPercent ? current : best);
|
|
331
328
|
}
|
|
332
329
|
// Tier 2: Prefer direct routes (single-hop, lower risk)
|
|
333
330
|
const directRoute = candidates.find((c) => c.path.length === 1);
|
|
334
331
|
if (directRoute)
|
|
335
332
|
return directRoute;
|
|
336
333
|
// Tier 3: Prefer routes through major stablecoins (better liquidity)
|
|
337
|
-
const stablecoins = ['
|
|
334
|
+
const stablecoins = ['USDm', 'EURm', 'USDC', 'USDT'];
|
|
338
335
|
const routeWithStablecoin = candidates.find((candidate) => {
|
|
339
336
|
const intermediateToken = getIntermediateToken(candidate);
|
|
340
337
|
if (!intermediateToken)
|
|
@@ -352,11 +349,13 @@ export function selectBestRoute(candidates, addrToSymbol) {
|
|
|
352
349
|
export function getIntermediateToken(route) {
|
|
353
350
|
// Find the common token between the two hops
|
|
354
351
|
const [hop1, hop2] = route.path;
|
|
355
|
-
|
|
352
|
+
const hop1Tokens = [hop1.token0, hop1.token1];
|
|
353
|
+
const hop2Tokens = [hop2.token0, hop2.token1];
|
|
354
|
+
return hop1Tokens.find((addr) => hop2Tokens.includes(addr));
|
|
356
355
|
}
|
|
357
356
|
/**
|
|
358
|
-
* Type guard to check if a Route has
|
|
357
|
+
* Type guard to check if a Route has cost data.
|
|
359
358
|
*/
|
|
360
|
-
export function
|
|
361
|
-
return '
|
|
359
|
+
export function hasCostData(pair) {
|
|
360
|
+
return 'costData' in pair && pair.costData !== undefined;
|
|
362
361
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sorts two token addresses to match the smart contract's _sortTokens behavior.
|
|
3
|
+
* Compares addresses as numeric values (BigInt), matching Solidity's address comparison.
|
|
4
|
+
*
|
|
5
|
+
* Solidity reference:
|
|
6
|
+
* ```solidity
|
|
7
|
+
* function _sortTokens(address a, address b) private pure returns (address, address) {
|
|
8
|
+
* return (a < b) ? (a, b) : (b, a);
|
|
9
|
+
* }
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export function sortTokenAddresses(tokenA, tokenB) {
|
|
13
|
+
return BigInt(tokenA) < BigInt(tokenB)
|
|
14
|
+
? [tokenA, tokenB]
|
|
15
|
+
: [tokenB, tokenA];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Creates a canonical route ID from two addresses.
|
|
19
|
+
* Uses numeric address comparison to match contract behavior.
|
|
20
|
+
*/
|
|
21
|
+
export function canonicalAddressKey(addressA, addressB, separator = '-') {
|
|
22
|
+
const [first, second] = sortTokenAddresses(addressA, addressB);
|
|
23
|
+
return `${first}${separator}${second}`;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Creates a canonical pair key from two symbols.
|
|
27
|
+
* Uses standard string comparison for human-readable IDs.
|
|
28
|
+
*/
|
|
29
|
+
export function canonicalSymbolKey(symbolA, symbolB, separator = '-') {
|
|
30
|
+
return symbolA < symbolB
|
|
31
|
+
? `${symbolA}${separator}${symbolB}`
|
|
32
|
+
: `${symbolB}${separator}${symbolA}`;
|
|
33
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { pad, hexToBigInt, numberToHex } from 'viem';
|
|
2
|
+
/** V2 trading limits use 15 decimals internally */
|
|
3
|
+
const V2_INTERNAL_DECIMALS = 15;
|
|
4
|
+
/** V2 L0 window is fixed at 5 minutes */
|
|
5
|
+
const V2_L0_TIMESTEP = 5 * 60; // 300 seconds
|
|
6
|
+
/** V2 L1 window is fixed at 1 day */
|
|
7
|
+
const V2_L1_TIMESTEP = 24 * 60 * 60; // 86400 seconds
|
|
8
|
+
/** Far future timestamp for global limits (year 2030) */
|
|
9
|
+
const TIMESTAMP_FAR_FUTURE = 1893456000;
|
|
10
|
+
/**
|
|
11
|
+
* Compute limit ID for Broker (V1) trading limits.
|
|
12
|
+
* limitId = exchangeId XOR bytes32(uint256(uint160(token)))
|
|
13
|
+
*
|
|
14
|
+
* @param exchangeId - The exchange ID (bytes32)
|
|
15
|
+
* @param token - The token address
|
|
16
|
+
* @returns The limit ID as hex string
|
|
17
|
+
*/
|
|
18
|
+
export function computeLimitId(exchangeId, token) {
|
|
19
|
+
// Pad token address to 32 bytes (left-padded with zeros)
|
|
20
|
+
const tokenBytes32 = pad(token, { size: 32 });
|
|
21
|
+
// Convert both to BigInt for XOR operation
|
|
22
|
+
const exchangeIdBigInt = hexToBigInt(exchangeId);
|
|
23
|
+
const tokenBigInt = hexToBigInt(tokenBytes32);
|
|
24
|
+
// XOR operation
|
|
25
|
+
const limitIdBigInt = exchangeIdBigInt ^ tokenBigInt;
|
|
26
|
+
// Convert back to hex with proper padding
|
|
27
|
+
return pad(numberToHex(limitIdBigInt), { size: 32 });
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Calculate trading limits from V1 config and state (Broker/Virtual pools).
|
|
31
|
+
* Returns human-friendly TradingLimit objects with maxIn/maxOut/until.
|
|
32
|
+
*
|
|
33
|
+
* @param config - V1 trading limits configuration
|
|
34
|
+
* @param state - V1 trading limits state
|
|
35
|
+
* @param asset - Token address
|
|
36
|
+
* @param tokenDecimals - Token decimals for consumer reference
|
|
37
|
+
* @returns Array of TradingLimit objects
|
|
38
|
+
*/
|
|
39
|
+
export function calculateTradingLimitsV1(config, state, asset, tokenDecimals) {
|
|
40
|
+
const nowEpoch = Math.floor(Date.now() / 1000);
|
|
41
|
+
const limits = [];
|
|
42
|
+
// Check if L0 is enabled (bit 0 of flags)
|
|
43
|
+
const isL0Enabled = (config.flags & 0x01) !== 0;
|
|
44
|
+
// Check if L1 is enabled (bit 1 of flags)
|
|
45
|
+
const isL1Enabled = (config.flags & 0x02) !== 0;
|
|
46
|
+
// Check if LG is enabled (bit 2 of flags)
|
|
47
|
+
const isLGEnabled = (config.flags & 0x04) !== 0;
|
|
48
|
+
// Reset netflows if time windows have passed
|
|
49
|
+
const isL0Outdated = isL0Enabled && nowEpoch > state.lastUpdated0 + config.timestep0;
|
|
50
|
+
const isL1Outdated = isL1Enabled && nowEpoch > state.lastUpdated1 + config.timestep1;
|
|
51
|
+
const effectiveNetflow0 = isL0Outdated ? 0n : state.netflow0;
|
|
52
|
+
const effectiveNetflow1 = isL1Outdated ? 0n : state.netflow1;
|
|
53
|
+
const effectiveLastUpdated0 = isL0Outdated ? nowEpoch : state.lastUpdated0;
|
|
54
|
+
const effectiveLastUpdated1 = isL1Outdated ? nowEpoch : state.lastUpdated1;
|
|
55
|
+
// Add L0 limit if configured
|
|
56
|
+
if (isL0Enabled && config.limit0 > 0n) {
|
|
57
|
+
limits.push({
|
|
58
|
+
asset,
|
|
59
|
+
maxIn: config.limit0 - effectiveNetflow0,
|
|
60
|
+
maxOut: config.limit0 + effectiveNetflow0,
|
|
61
|
+
until: effectiveLastUpdated0 + config.timestep0,
|
|
62
|
+
decimals: tokenDecimals,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
// Add L1 limit if configured
|
|
66
|
+
if (isL1Enabled && config.limit1 > 0n) {
|
|
67
|
+
limits.push({
|
|
68
|
+
asset,
|
|
69
|
+
maxIn: config.limit1 - effectiveNetflow1,
|
|
70
|
+
maxOut: config.limit1 + effectiveNetflow1,
|
|
71
|
+
until: effectiveLastUpdated1 + config.timestep1,
|
|
72
|
+
decimals: tokenDecimals,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
// Add LG (global) limit if configured
|
|
76
|
+
if (isLGEnabled && config.limitGlobal > 0n) {
|
|
77
|
+
limits.push({
|
|
78
|
+
asset,
|
|
79
|
+
maxIn: config.limitGlobal - state.netflowGlobal,
|
|
80
|
+
maxOut: config.limitGlobal + state.netflowGlobal,
|
|
81
|
+
until: TIMESTAMP_FAR_FUTURE,
|
|
82
|
+
decimals: tokenDecimals,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
// Apply cascading restrictions: limits with larger timeframes restrict smaller ones
|
|
86
|
+
// e.g., if maxIn is 0 in LG, it should also be 0 in L1 and L0
|
|
87
|
+
for (let i = limits.length - 1; i > 0; i--) {
|
|
88
|
+
if (limits[i - 1].maxIn > limits[i].maxIn) {
|
|
89
|
+
limits[i - 1] = { ...limits[i - 1], maxIn: limits[i].maxIn };
|
|
90
|
+
}
|
|
91
|
+
if (limits[i - 1].maxOut > limits[i].maxOut) {
|
|
92
|
+
limits[i - 1] = { ...limits[i - 1], maxOut: limits[i].maxOut };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return limits;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Calculate trading limits from V2 config and state (FPMM pools).
|
|
99
|
+
* V2 uses fixed timeframes: 5 min for L0, 1 day for L1.
|
|
100
|
+
* Values are stored with 15 decimals precision.
|
|
101
|
+
*
|
|
102
|
+
* @param config - V2 trading limits configuration
|
|
103
|
+
* @param state - V2 trading limits state
|
|
104
|
+
* @param asset - Token address
|
|
105
|
+
* @returns Array of TradingLimit objects
|
|
106
|
+
*/
|
|
107
|
+
export function calculateTradingLimitsV2(config, state, asset) {
|
|
108
|
+
const nowEpoch = Math.floor(Date.now() / 1000);
|
|
109
|
+
const limits = [];
|
|
110
|
+
// Check if L0 is configured (limit0 > 0)
|
|
111
|
+
const isL0Enabled = config.limit0 > 0n;
|
|
112
|
+
// Check if L1 is configured (limit1 > 0)
|
|
113
|
+
const isL1Enabled = config.limit1 > 0n;
|
|
114
|
+
// Reset netflows if time windows have passed
|
|
115
|
+
const isL0Outdated = isL0Enabled && nowEpoch > state.lastUpdated0 + V2_L0_TIMESTEP;
|
|
116
|
+
const isL1Outdated = isL1Enabled && nowEpoch > state.lastUpdated1 + V2_L1_TIMESTEP;
|
|
117
|
+
const effectiveNetflow0 = isL0Outdated ? 0n : state.netflow0;
|
|
118
|
+
const effectiveNetflow1 = isL1Outdated ? 0n : state.netflow1;
|
|
119
|
+
const effectiveLastUpdated0 = isL0Outdated ? nowEpoch : state.lastUpdated0;
|
|
120
|
+
const effectiveLastUpdated1 = isL1Outdated ? nowEpoch : state.lastUpdated1;
|
|
121
|
+
// Add L0 limit if configured
|
|
122
|
+
if (isL0Enabled) {
|
|
123
|
+
limits.push({
|
|
124
|
+
asset,
|
|
125
|
+
maxIn: config.limit0 - effectiveNetflow0,
|
|
126
|
+
maxOut: config.limit0 + effectiveNetflow0,
|
|
127
|
+
until: effectiveLastUpdated0 + V2_L0_TIMESTEP,
|
|
128
|
+
decimals: V2_INTERNAL_DECIMALS, // V2 returns 15 decimal values
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
// Add L1 limit if configured
|
|
132
|
+
if (isL1Enabled) {
|
|
133
|
+
limits.push({
|
|
134
|
+
asset,
|
|
135
|
+
maxIn: config.limit1 - effectiveNetflow1,
|
|
136
|
+
maxOut: config.limit1 + effectiveNetflow1,
|
|
137
|
+
until: effectiveLastUpdated1 + V2_L1_TIMESTEP,
|
|
138
|
+
decimals: V2_INTERNAL_DECIMALS, // V2 returns 15 decimal values
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
// Apply cascading restrictions
|
|
142
|
+
for (let i = limits.length - 1; i > 0; i--) {
|
|
143
|
+
if (limits[i - 1].maxIn > limits[i].maxIn) {
|
|
144
|
+
limits[i - 1] = { ...limits[i - 1], maxIn: limits[i].maxIn };
|
|
145
|
+
}
|
|
146
|
+
if (limits[i - 1].maxOut > limits[i].maxOut) {
|
|
147
|
+
limits[i - 1] = { ...limits[i - 1], maxOut: limits[i].maxOut };
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return limits;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Check if any trading limits are configured (flags > 0 for V1, or limit0/limit1 > 0 for V2)
|
|
154
|
+
*/
|
|
155
|
+
export function hasConfiguredLimitsV1(config) {
|
|
156
|
+
return config.flags > 0;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Check if any trading limits are configured for V2
|
|
160
|
+
*/
|
|
161
|
+
export function hasConfiguredLimitsV2(config) {
|
|
162
|
+
return config.limit0 > 0n || config.limit1 > 0n;
|
|
163
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { isAddress } from 'viem';
|
|
2
|
+
/**
|
|
3
|
+
* Validates an Ethereum address format using viem's isAddress utility
|
|
4
|
+
*
|
|
5
|
+
* @param address - The address string to validate
|
|
6
|
+
* @param paramName - Optional parameter name for better error messages
|
|
7
|
+
* @returns True if valid (type guard for `0x${string}`)
|
|
8
|
+
* @throws Error if address is invalid
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* validateAddress('0x765DE816845861e75A25fCA122bb6898B8B1282a')
|
|
13
|
+
* // Returns true
|
|
14
|
+
*
|
|
15
|
+
* validateAddress('invalid', 'tokenIn')
|
|
16
|
+
* // Throws: "Invalid address for tokenIn: invalid"
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export function validateAddress(address, paramName) {
|
|
20
|
+
if (typeof address !== 'string') {
|
|
21
|
+
const param = paramName ? ` for ${paramName}` : '';
|
|
22
|
+
throw new Error(`Address${param} must be a string, got ${typeof address}`);
|
|
23
|
+
}
|
|
24
|
+
// Use viem's isAddress with strict: false to allow both checksummed and non-checksummed addresses
|
|
25
|
+
if (!isAddress(address, { strict: false })) {
|
|
26
|
+
const param = paramName ? ` for ${paramName}` : '';
|
|
27
|
+
throw new Error(`Invalid address${param}: ${address}. Expected 0x followed by 40 hex characters`);
|
|
28
|
+
}
|
|
29
|
+
return true;
|
|
30
|
+
}
|