@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
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { isTradingEnabled, PoolType, } from '../../core/types';
|
|
2
|
+
import { TradingLimitsService } from './TradingLimitsService';
|
|
3
|
+
import { BIPOOL_MANAGER_ABI, BREAKERBOX_ABI, FPMM_ABI } from '../../core/abis';
|
|
4
|
+
import { getContractAddress, tryGetContractAddress } from '../../core/constants';
|
|
5
|
+
import { multicall } from '../../utils/multicall';
|
|
6
|
+
/**
|
|
7
|
+
* Service for checking trading status and circuit breaker state in the Mento protocol.
|
|
8
|
+
* Provides methods to query whether trading is enabled for specific rate feeds,
|
|
9
|
+
* token pairs, or routes. Also integrates trading limit checks.
|
|
10
|
+
*/
|
|
11
|
+
export class TradingService {
|
|
12
|
+
constructor(publicClient, chainId, routeService) {
|
|
13
|
+
this.publicClient = publicClient;
|
|
14
|
+
this.chainId = chainId;
|
|
15
|
+
this.routeService = routeService;
|
|
16
|
+
this.tradingLimitsService = new TradingLimitsService(publicClient, chainId);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Returns the current trading mode for a given rate feed.
|
|
20
|
+
*
|
|
21
|
+
* The BreakerBox uses a bitmask approach where 0 means bidirectional trading
|
|
22
|
+
* is enabled, and any non-zero value means trading is suspended.
|
|
23
|
+
*
|
|
24
|
+
* @param rateFeedId - The address of the rate feed
|
|
25
|
+
* @returns The raw trading mode value from BreakerBox (0 = enabled, non-zero = suspended)
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const mode = await tradingService.getRateFeedTradingMode(rateFeedId)
|
|
30
|
+
* if (mode === TradingMode.BIDIRECTIONAL) {
|
|
31
|
+
* console.log('Trading is enabled')
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
async getRateFeedTradingMode(rateFeedId) {
|
|
36
|
+
const breakerBoxAddr = getContractAddress(this.chainId, 'BreakerBox');
|
|
37
|
+
const mode = await this.publicClient.readContract({
|
|
38
|
+
address: breakerBoxAddr,
|
|
39
|
+
abi: BREAKERBOX_ABI,
|
|
40
|
+
functionName: 'getRateFeedTradingMode',
|
|
41
|
+
args: [rateFeedId],
|
|
42
|
+
});
|
|
43
|
+
return Number(mode);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Checks if a trading pair is currently tradable.
|
|
47
|
+
* For multi-hop routes (e.g., CELO → USDm → USDT), checks that ALL
|
|
48
|
+
* intermediate rate feeds are in BIDIRECTIONAL mode.
|
|
49
|
+
*
|
|
50
|
+
* @param tokenIn - Input token address
|
|
51
|
+
* @param tokenOut - Output token address
|
|
52
|
+
* @returns true if the pair is tradable (all rate feeds BIDIRECTIONAL), false otherwise
|
|
53
|
+
* @throws {RouteNotFoundError} If no route exists between the token pair
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const isTradable = await tradingService.isPairTradable(USDm, CELO)
|
|
58
|
+
* if (!isTradable) {
|
|
59
|
+
* console.log('Trading is currently suspended for this pair')
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
async isPairTradable(tokenIn, tokenOut) {
|
|
64
|
+
const route = await this.routeService.findRoute(tokenIn, tokenOut);
|
|
65
|
+
return this.isRouteTradable(route);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Checks if a route is currently tradable.
|
|
69
|
+
* Verifies that all pools in the route's path have their rate feeds
|
|
70
|
+
* in BIDIRECTIONAL mode.
|
|
71
|
+
*
|
|
72
|
+
* @param route - The route to check
|
|
73
|
+
* @returns true if all pools in the route are tradable, false otherwise
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const route = await routeService.findRoute(tokenIn, tokenOut)
|
|
78
|
+
* const isRouteTradable = await tradingService.isRouteTradable(route)
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
async isRouteTradable(route) {
|
|
82
|
+
const tradingModes = await this.getTradingModesForPools(route.path);
|
|
83
|
+
// All rate feeds must have trading enabled for the route to be tradable
|
|
84
|
+
return tradingModes.every((tradingMode) => isTradingEnabled(tradingMode));
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Get trading limits for a pool.
|
|
88
|
+
*
|
|
89
|
+
* @param pool - The pool to get trading limits for
|
|
90
|
+
* @returns Array of TradingLimit objects with maxIn/maxOut/until
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* const limits = await tradingService.getPoolTradingLimits(pool)
|
|
95
|
+
* limits.forEach(limit => {
|
|
96
|
+
* console.log(`${limit.asset}: maxIn=${limit.maxIn}, maxOut=${limit.maxOut}`)
|
|
97
|
+
* })
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
async getPoolTradingLimits(pool) {
|
|
101
|
+
return this.tradingLimitsService.getPoolTradingLimits(pool);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Get complete tradability status for a pool.
|
|
105
|
+
* Returns separate flags for circuit breaker and trading limits,
|
|
106
|
+
* allowing frontends to show different messages for each condition.
|
|
107
|
+
*
|
|
108
|
+
* @param pool - The pool to check
|
|
109
|
+
* @returns PoolTradabilityStatus with tradable, circuitBreakerOk, limitsOk, and limits
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const status = await tradingService.getPoolTradabilityStatus(pool)
|
|
114
|
+
* if (!status.circuitBreakerOk) {
|
|
115
|
+
* showModal("Trading temporarily suspended (circuit breaker)")
|
|
116
|
+
* } else if (!status.limitsOk) {
|
|
117
|
+
* showModal("Trading limit reached", status.limits)
|
|
118
|
+
* }
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
async getPoolTradabilityStatus(pool) {
|
|
122
|
+
const [[tradingMode], limits] = await Promise.all([
|
|
123
|
+
this.getTradingModesForPools([pool]),
|
|
124
|
+
this.tradingLimitsService.getPoolTradingLimits(pool),
|
|
125
|
+
]);
|
|
126
|
+
const circuitBreakerOk = isTradingEnabled(tradingMode);
|
|
127
|
+
// Limits are OK if no limits configured OR all limits have capacity
|
|
128
|
+
const limitsOk = limits.length === 0 || limits.every((l) => l.maxIn > 0n && l.maxOut > 0n);
|
|
129
|
+
return {
|
|
130
|
+
tradable: circuitBreakerOk && limitsOk,
|
|
131
|
+
circuitBreakerOk,
|
|
132
|
+
tradingMode,
|
|
133
|
+
limitsOk,
|
|
134
|
+
limits,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Get the reference rate feed ID for a pool.
|
|
139
|
+
* FPMM pools expose this via referenceRateFeedID() directly.
|
|
140
|
+
* Virtual pools wrap a BiPoolManager exchange; the rate feed is read from
|
|
141
|
+
* BiPoolManager.getPoolExchange(exchangeId).config.referenceRateFeedID.
|
|
142
|
+
*
|
|
143
|
+
* @param pool - The pool to get rate feed ID for
|
|
144
|
+
* @returns The rate feed ID address
|
|
145
|
+
*/
|
|
146
|
+
async getPoolRateFeedId(pool) {
|
|
147
|
+
const [rateFeedId] = await this.getPoolRateFeedIds([pool]);
|
|
148
|
+
return rateFeedId;
|
|
149
|
+
}
|
|
150
|
+
async getTradingModesForPools(pools) {
|
|
151
|
+
const rateFeedIds = await this.getPoolRateFeedIds(pools);
|
|
152
|
+
return this.getTradingModesForRateFeeds(rateFeedIds);
|
|
153
|
+
}
|
|
154
|
+
async getPoolRateFeedIds(pools) {
|
|
155
|
+
if (pools.length === 0) {
|
|
156
|
+
return [];
|
|
157
|
+
}
|
|
158
|
+
const biPoolManagerAddr = pools.some((pool) => pool.poolType === PoolType.Virtual)
|
|
159
|
+
? tryGetContractAddress(this.chainId, 'BiPoolManager')
|
|
160
|
+
: undefined;
|
|
161
|
+
const contracts = pools.map((pool) => {
|
|
162
|
+
if (pool.poolType === PoolType.Virtual) {
|
|
163
|
+
if (!pool.exchangeId) {
|
|
164
|
+
throw new Error(`Virtual pool ${pool.poolAddr} is missing exchangeId`);
|
|
165
|
+
}
|
|
166
|
+
if (!biPoolManagerAddr) {
|
|
167
|
+
throw new Error(`BiPoolManager address not configured for chain ID ${this.chainId}; cannot resolve rate feed for virtual pool ${pool.poolAddr}`);
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
address: biPoolManagerAddr,
|
|
171
|
+
abi: BIPOOL_MANAGER_ABI,
|
|
172
|
+
functionName: 'getPoolExchange',
|
|
173
|
+
args: [pool.exchangeId],
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
address: pool.poolAddr,
|
|
178
|
+
abi: FPMM_ABI,
|
|
179
|
+
functionName: 'referenceRateFeedID',
|
|
180
|
+
args: [],
|
|
181
|
+
};
|
|
182
|
+
});
|
|
183
|
+
const results = await multicall(this.publicClient, contracts, { allowFailure: false });
|
|
184
|
+
return results.map((result, index) => {
|
|
185
|
+
if (result.status === 'failure') {
|
|
186
|
+
throw result.error;
|
|
187
|
+
}
|
|
188
|
+
if (pools[index].poolType === PoolType.Virtual) {
|
|
189
|
+
const exchange = result.result;
|
|
190
|
+
return exchange.config.referenceRateFeedID;
|
|
191
|
+
}
|
|
192
|
+
return result.result;
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
async getTradingModesForRateFeeds(rateFeedIds) {
|
|
196
|
+
if (rateFeedIds.length === 0) {
|
|
197
|
+
return [];
|
|
198
|
+
}
|
|
199
|
+
const breakerBoxAddr = getContractAddress(this.chainId, 'BreakerBox');
|
|
200
|
+
const uniqueRateFeeds = Array.from(new Map(rateFeedIds.map((rateFeedId) => [rateFeedId.toLowerCase(), rateFeedId])).values());
|
|
201
|
+
const results = await multicall(this.publicClient, uniqueRateFeeds.map((rateFeedId) => ({
|
|
202
|
+
address: breakerBoxAddr,
|
|
203
|
+
abi: BREAKERBOX_ABI,
|
|
204
|
+
functionName: 'getRateFeedTradingMode',
|
|
205
|
+
args: [rateFeedId],
|
|
206
|
+
})), { allowFailure: false });
|
|
207
|
+
const tradingModes = new Map();
|
|
208
|
+
for (const [index, result] of results.entries()) {
|
|
209
|
+
if (result.status === 'failure') {
|
|
210
|
+
throw result.error;
|
|
211
|
+
}
|
|
212
|
+
tradingModes.set(uniqueRateFeeds[index].toLowerCase(), Number(result.result));
|
|
213
|
+
}
|
|
214
|
+
return rateFeedIds.map((rateFeedId) => {
|
|
215
|
+
const tradingMode = tradingModes.get(rateFeedId.toLowerCase());
|
|
216
|
+
if (tradingMode === undefined) {
|
|
217
|
+
throw new Error(`Trading mode not found for rate feed ${rateFeedId}`);
|
|
218
|
+
}
|
|
219
|
+
return tradingMode;
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { celo } from 'viem/chains';
|
|
2
|
+
import { defineChain } from 'viem';
|
|
3
|
+
import { ChainId } from '../core/constants/chainId';
|
|
4
|
+
// Celo Sepolia chain definition (not available in viem/chains yet)
|
|
5
|
+
const celoSepolia = defineChain({
|
|
6
|
+
id: 11142220,
|
|
7
|
+
name: 'Celo Sepolia',
|
|
8
|
+
nativeCurrency: {
|
|
9
|
+
decimals: 18,
|
|
10
|
+
name: 'CELO',
|
|
11
|
+
symbol: 'CELO',
|
|
12
|
+
},
|
|
13
|
+
rpcUrls: {
|
|
14
|
+
default: {
|
|
15
|
+
http: ['https://forno.celo-sepolia.celo-testnet.org'],
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
blockExplorers: {
|
|
19
|
+
default: {
|
|
20
|
+
name: 'Celo Explorer',
|
|
21
|
+
url: 'https://sepolia.celoscan.io',
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
testnet: true,
|
|
25
|
+
});
|
|
26
|
+
const monadTestnet = defineChain({
|
|
27
|
+
id: 10143,
|
|
28
|
+
name: 'Monad Testnet',
|
|
29
|
+
nativeCurrency: {
|
|
30
|
+
decimals: 18,
|
|
31
|
+
name: 'MON',
|
|
32
|
+
symbol: 'MON',
|
|
33
|
+
},
|
|
34
|
+
rpcUrls: {
|
|
35
|
+
default: {
|
|
36
|
+
http: ['https://testnet-rpc.monad.xyz'],
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
blockExplorers: {
|
|
40
|
+
default: {
|
|
41
|
+
name: 'Monad Testnet Explorer',
|
|
42
|
+
url: 'https://testnet.monadexplorer.com',
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
contracts: {
|
|
46
|
+
multicall3: {
|
|
47
|
+
address: '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
48
|
+
blockCreated: 251449,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
testnet: true,
|
|
52
|
+
});
|
|
53
|
+
const monad = defineChain({
|
|
54
|
+
id: 143,
|
|
55
|
+
name: 'Monad',
|
|
56
|
+
nativeCurrency: {
|
|
57
|
+
decimals: 18,
|
|
58
|
+
name: 'MON',
|
|
59
|
+
symbol: 'MON',
|
|
60
|
+
},
|
|
61
|
+
rpcUrls: {
|
|
62
|
+
default: {
|
|
63
|
+
http: ['https://rpc.monad.xyz'],
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
blockExplorers: {
|
|
67
|
+
default: {
|
|
68
|
+
name: 'Monad Explorer',
|
|
69
|
+
url: 'https://monadvision.com',
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
contracts: {
|
|
73
|
+
multicall3: {
|
|
74
|
+
address: '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
75
|
+
blockCreated: 9248132,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
/**
|
|
80
|
+
* Get the default RPC URL for a given chain ID
|
|
81
|
+
* @param chainId - The chain ID
|
|
82
|
+
* @returns The default RPC URL for the chain
|
|
83
|
+
* @throws Error if chain ID is not supported
|
|
84
|
+
*/
|
|
85
|
+
export function getDefaultRpcUrl(chainId) {
|
|
86
|
+
switch (chainId) {
|
|
87
|
+
case ChainId.CELO:
|
|
88
|
+
return 'https://forno.celo.org';
|
|
89
|
+
case ChainId.CELO_SEPOLIA:
|
|
90
|
+
return 'https://forno.celo-sepolia.celo-testnet.org';
|
|
91
|
+
case ChainId.MONAD_TESTNET:
|
|
92
|
+
return 'https://testnet-rpc.monad.xyz';
|
|
93
|
+
case ChainId.MONAD:
|
|
94
|
+
return 'https://rpc.monad.xyz';
|
|
95
|
+
default:
|
|
96
|
+
throw new Error(`Unsupported chain ID: ${chainId}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Get the viem chain configuration for a given chain ID
|
|
101
|
+
* @param chainId - The chain ID
|
|
102
|
+
* @returns The viem chain configuration
|
|
103
|
+
* @throws Error if chain ID is not supported
|
|
104
|
+
*/
|
|
105
|
+
export function getChainConfig(chainId) {
|
|
106
|
+
switch (chainId) {
|
|
107
|
+
case ChainId.CELO:
|
|
108
|
+
return celo;
|
|
109
|
+
case ChainId.CELO_SEPOLIA:
|
|
110
|
+
return celoSepolia;
|
|
111
|
+
case ChainId.MONAD_TESTNET:
|
|
112
|
+
return monadTestnet;
|
|
113
|
+
case ChainId.MONAD:
|
|
114
|
+
return monad;
|
|
115
|
+
default:
|
|
116
|
+
throw new Error(`Unsupported chain ID: ${chainId}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { PoolType } from '../core/types';
|
|
2
|
+
import { FPMM_ABI } from '../core/abis';
|
|
3
|
+
import { VIRTUAL_POOL_ABI } from '../core/abis/virtualPool';
|
|
4
|
+
import { multicall } from './multicall';
|
|
5
|
+
/**
|
|
6
|
+
* Calculate cost percentage for a pool based on its type
|
|
7
|
+
* Returns cost as a percentage (e.g., 0.5 = 0.5%)
|
|
8
|
+
*
|
|
9
|
+
* @param pool - The pool to calculate cost for
|
|
10
|
+
* @param publicClient - Viem public client for RPC calls
|
|
11
|
+
* @returns Cost percentage for the pool
|
|
12
|
+
*/
|
|
13
|
+
export async function getPoolCostPercent(pool, publicClient) {
|
|
14
|
+
if (pool.poolType === PoolType.FPMM) {
|
|
15
|
+
return getFPMMCostPercent(pool.poolAddr, publicClient);
|
|
16
|
+
}
|
|
17
|
+
else if (pool.poolType === PoolType.Virtual) {
|
|
18
|
+
return getVirtualPoolCostPercent(pool.poolAddr, publicClient);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
throw new Error('Invalid pool type');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Calculate cost for FPMM pools
|
|
26
|
+
* FPMM pools use lpFee + protocolFee in basis points (10000 = 100%)
|
|
27
|
+
*/
|
|
28
|
+
async function getFPMMCostPercent(poolAddress, publicClient) {
|
|
29
|
+
const results = await multicall(publicClient, [
|
|
30
|
+
{ address: poolAddress, abi: FPMM_ABI, functionName: 'lpFee' },
|
|
31
|
+
{ address: poolAddress, abi: FPMM_ABI, functionName: 'protocolFee' },
|
|
32
|
+
]);
|
|
33
|
+
if (results[0].status === 'failure' || results[1].status === 'failure') {
|
|
34
|
+
throw new Error(`Failed to read fees for pool ${poolAddress}`);
|
|
35
|
+
}
|
|
36
|
+
const lpFee = results[0].result;
|
|
37
|
+
const protocolFee = results[1].result;
|
|
38
|
+
// Convert from basis points to percentage using BigInt arithmetic to avoid precision loss
|
|
39
|
+
const totalBasisPoints = lpFee + protocolFee;
|
|
40
|
+
const scaled = totalBasisPoints * 1000000n;
|
|
41
|
+
return Number(scaled / 100n) / 1e6;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Calculate cost for Virtual pools
|
|
45
|
+
*/
|
|
46
|
+
async function getVirtualPoolCostPercent(poolAddress, publicClient) {
|
|
47
|
+
const results = await multicall(publicClient, [
|
|
48
|
+
{ address: poolAddress, abi: VIRTUAL_POOL_ABI, functionName: 'protocolFee' },
|
|
49
|
+
]);
|
|
50
|
+
if (results[0].status === 'failure') {
|
|
51
|
+
throw new Error(`Failed to read protocolFee for pool ${poolAddress}`);
|
|
52
|
+
}
|
|
53
|
+
// Convert from basis points to percentage using BigInt arithmetic to avoid precision loss
|
|
54
|
+
const scaled = results[0].result * 1000000n;
|
|
55
|
+
return Number(scaled / 100n) / 1e6;
|
|
56
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a deadline timestamp from a duration in minutes.
|
|
3
|
+
*
|
|
4
|
+
* @param minutes - Number of minutes from now
|
|
5
|
+
* @returns Unix timestamp as bigint suitable for router contract deadline parameter
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { deadlineFromMinutes } from '@mento-protocol/mento-sdk'
|
|
10
|
+
*
|
|
11
|
+
* // 5 minute deadline
|
|
12
|
+
* const deadline = deadlineFromMinutes(5)
|
|
13
|
+
*
|
|
14
|
+
* const swap = await mento.swap.buildSwapParams(
|
|
15
|
+
* tokenIn, tokenOut, amountIn, recipient,
|
|
16
|
+
* { slippageTolerance: 0.5, deadline }
|
|
17
|
+
* )
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export function deadlineFromMinutes(minutes) {
|
|
21
|
+
return BigInt(Date.now()) / 1000n + BigInt(minutes * 60);
|
|
22
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './retry';
|
|
2
|
+
export * from './chainConfig';
|
|
3
|
+
export * from './routes';
|
|
4
|
+
export * from './sortUtils';
|
|
5
|
+
export * from './pathEncoder';
|
|
6
|
+
export * from './tokens';
|
|
7
|
+
export * from './rateFeed';
|
|
8
|
+
export * from './tradingLimits';
|
|
9
|
+
export * from './deadline';
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standard Multicall3 contract address, deployed at the same address on all EVM chains.
|
|
3
|
+
* https://www.multicall3.com/
|
|
4
|
+
*/
|
|
5
|
+
export const MULTICALL3_ADDRESS = '0xcA11bde05977b3631167028862bE2a173976CA11';
|
|
6
|
+
/**
|
|
7
|
+
* Wrapper around viem's multicall that explicitly provides the Multicall3 address.
|
|
8
|
+
* This ensures multicall works even when the PublicClient was created without a `chain` config.
|
|
9
|
+
*/
|
|
10
|
+
export async function multicall(publicClient, contracts, options = {}) {
|
|
11
|
+
const { allowFailure = true, batchSize } = options;
|
|
12
|
+
const client = publicClient;
|
|
13
|
+
if (typeof client.multicall !== 'function') {
|
|
14
|
+
if (typeof client.readContract !== 'function') {
|
|
15
|
+
throw new Error('Public client does not support multicall or readContract');
|
|
16
|
+
}
|
|
17
|
+
return Promise.all(contracts.map(async (contract) => {
|
|
18
|
+
try {
|
|
19
|
+
const result = await client.readContract({
|
|
20
|
+
...contract,
|
|
21
|
+
args: contract.args ?? [],
|
|
22
|
+
});
|
|
23
|
+
return { status: 'success', result };
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
if (!allowFailure) {
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
status: 'failure',
|
|
31
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}));
|
|
35
|
+
}
|
|
36
|
+
const raw = await client.multicall({
|
|
37
|
+
allowFailure,
|
|
38
|
+
batchSize,
|
|
39
|
+
contracts: contracts,
|
|
40
|
+
multicallAddress: MULTICALL3_ADDRESS,
|
|
41
|
+
});
|
|
42
|
+
const results = raw;
|
|
43
|
+
if (!allowFailure) {
|
|
44
|
+
return results.map((result) => ({ status: 'success', result }));
|
|
45
|
+
}
|
|
46
|
+
return results;
|
|
47
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a route path to the format expected by the Router contract.
|
|
3
|
+
* Used by both QuoteService (getAmountsOut) and SwapService (swapTokensForTokens).
|
|
4
|
+
*
|
|
5
|
+
* @param path - The route path (array of pools)
|
|
6
|
+
* @param tokenIn - The input token address (determines swap direction)
|
|
7
|
+
* @param _tokenOut - The output token address (unused but kept for API clarity)
|
|
8
|
+
* @returns Array of RouterRoute objects for the contract call
|
|
9
|
+
* @throws {Error} If path is empty, too long, or contains invalid pools
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const route = await routeService.findRoute(USDm, CELO)
|
|
14
|
+
* const routerRoutes = encodeRoutePath(route.path, USDm, CELO)
|
|
15
|
+
* // routerRoutes can now be passed to Router.getAmountsOut or Router.swapTokensForTokens
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export function encodeRoutePath(path, tokenIn, _tokenOut) {
|
|
19
|
+
// Validate path is not empty
|
|
20
|
+
if (!path || path.length === 0) {
|
|
21
|
+
throw new Error('Internal error: Route path is empty. This should not happen - routes are validated before encoding.');
|
|
22
|
+
}
|
|
23
|
+
// Validate all pools have required structure
|
|
24
|
+
for (let i = 0; i < path.length; i++) {
|
|
25
|
+
const pool = path[i];
|
|
26
|
+
if (!pool.token0 || !pool.token1 || !pool.factoryAddr) {
|
|
27
|
+
throw new Error(`Invalid pool structure at index ${i}: missing required fields. ` +
|
|
28
|
+
`Pool must have token0, token1, and factoryAddr. ` +
|
|
29
|
+
`Got: ${JSON.stringify(pool)}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const routes = [];
|
|
33
|
+
const tokenInLower = tokenIn.toLowerCase();
|
|
34
|
+
// Check if we need to reverse the path
|
|
35
|
+
// The path is stored in canonical order, but we may need to traverse it backwards
|
|
36
|
+
const firstPool = path[0];
|
|
37
|
+
const startsWithTokenIn = firstPool.token0.toLowerCase() === tokenInLower || firstPool.token1.toLowerCase() === tokenInLower;
|
|
38
|
+
// If tokenIn isn't in the first pool, reverse the path
|
|
39
|
+
const orderedPath = startsWithTokenIn ? path : [...path].reverse();
|
|
40
|
+
let currentTokenIn = tokenInLower;
|
|
41
|
+
for (const pool of orderedPath) {
|
|
42
|
+
const token0 = pool.token0.toLowerCase();
|
|
43
|
+
const token1 = pool.token1.toLowerCase();
|
|
44
|
+
// Determine direction: which token is the input for this hop?
|
|
45
|
+
let from;
|
|
46
|
+
let to;
|
|
47
|
+
if (currentTokenIn === token0) {
|
|
48
|
+
from = pool.token0;
|
|
49
|
+
to = pool.token1;
|
|
50
|
+
}
|
|
51
|
+
else if (currentTokenIn === token1) {
|
|
52
|
+
from = pool.token1;
|
|
53
|
+
to = pool.token0;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
throw new Error(`Route encoding error: Token ${currentTokenIn} not found in pool ${pool.poolAddr}. ` +
|
|
57
|
+
`Pool contains tokens: ${token0}, ${token1}. ` +
|
|
58
|
+
`This indicates the route path is invalid or tokens don't form a connected path.`);
|
|
59
|
+
}
|
|
60
|
+
routes.push({
|
|
61
|
+
from,
|
|
62
|
+
to,
|
|
63
|
+
factory: pool.factoryAddr,
|
|
64
|
+
});
|
|
65
|
+
// The output of this hop becomes the input of the next hop
|
|
66
|
+
currentTokenIn = to.toLowerCase();
|
|
67
|
+
}
|
|
68
|
+
return routes;
|
|
69
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { keccak256, toBytes } from 'viem';
|
|
2
|
+
/**
|
|
3
|
+
* Computes the rate feed ID from a rate feed identifier string.
|
|
4
|
+
* Follows the Solidity formula: address(uint160(uint256(keccak256(abi.encodePacked(rateFeed)))))
|
|
5
|
+
*
|
|
6
|
+
* @param rateFeed - The rate feed identifier string (e.g., "EURUSD", "relayed:COPUSD")
|
|
7
|
+
* @returns The computed rate feed address as a hex string
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const rateFeedId = toRateFeedId('EURUSD')
|
|
12
|
+
* // Returns the computed address for the EURUSD rate feed
|
|
13
|
+
*
|
|
14
|
+
* const relayedRateFeedId = toRateFeedId('relayed:COPUSD')
|
|
15
|
+
* // Returns the computed address for the relayed COPUSD rate feed
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export function toRateFeedId(rateFeed) {
|
|
19
|
+
const hashedBytes = keccak256(toBytes(rateFeed));
|
|
20
|
+
const hashAsBigInt = BigInt(hashedBytes);
|
|
21
|
+
const maskedToUint160 = hashAsBigInt & ((1n << 160n) - 1n);
|
|
22
|
+
return `0x${maskedToUint160.toString(16).padStart(40, '0')}`;
|
|
23
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retry an operation a number of times with exponential backoff.
|
|
3
|
+
* @param operation - The operation to retry
|
|
4
|
+
* @param options - The retry options
|
|
5
|
+
* @returns The result of the operation
|
|
6
|
+
*/
|
|
7
|
+
export async function retryOperation(operation, options = {}) {
|
|
8
|
+
const maxAttempts = options.maxAttempts ?? 3;
|
|
9
|
+
const delayMs = options.initialDelayMs ?? 1000;
|
|
10
|
+
let lastError;
|
|
11
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
12
|
+
try {
|
|
13
|
+
return await operation();
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
lastError = error;
|
|
17
|
+
if (attempt === maxAttempts)
|
|
18
|
+
break;
|
|
19
|
+
// Exponential backoff
|
|
20
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs * attempt));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
throw new Error(`Operation failed after ${maxAttempts} attempts: ${lastError?.message}`);
|
|
24
|
+
}
|