@guru-fund/sdk 0.1.0
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/LICENSE +21 -0
- package/README.md +226 -0
- package/dist/GuruProtocol.d.ts +44 -0
- package/dist/GuruProtocol.js +75 -0
- package/dist/addresses.d.ts +40 -0
- package/dist/addresses.js +111 -0
- package/dist/constants.d.ts +22 -0
- package/dist/constants.js +23 -0
- package/dist/helpers/FundDataFetcher.d.ts +24 -0
- package/dist/helpers/FundDataFetcher.js +66 -0
- package/dist/helpers/ReceiptParser.d.ts +23 -0
- package/dist/helpers/ReceiptParser.js +59 -0
- package/dist/helpers/Token.d.ts +18 -0
- package/dist/helpers/Token.js +43 -0
- package/dist/helpers/compareAddresses.d.ts +1 -0
- package/dist/helpers/compareAddresses.js +15 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +3 -0
- package/dist/quotes/quoteDeposit.d.ts +41 -0
- package/dist/quotes/quoteDeposit.js +186 -0
- package/dist/quotes/quoteHarvest.d.ts +34 -0
- package/dist/quotes/quoteHarvest.js +105 -0
- package/dist/quotes/quoteRebalance.d.ts +51 -0
- package/dist/quotes/quoteRebalance.js +201 -0
- package/dist/quotes/quoteTrade.d.ts +18 -0
- package/dist/quotes/quoteTrade.js +51 -0
- package/dist/quotes/quoteWithdrawal.d.ts +41 -0
- package/dist/quotes/quoteWithdrawal.js +113 -0
- package/dist/router/constants.d.ts +18 -0
- package/dist/router/constants.js +25 -0
- package/dist/router/finalizeRoute.d.ts +16 -0
- package/dist/router/finalizeRoute.js +29 -0
- package/dist/router/getFallbackRoutes.d.ts +10 -0
- package/dist/router/getFallbackRoutes.js +91 -0
- package/dist/router/getUniswapV4Route.d.ts +42 -0
- package/dist/router/getUniswapV4Route.js +324 -0
- package/dist/router/getVeloraRoute.d.ts +10 -0
- package/dist/router/getVeloraRoute.js +68 -0
- package/dist/router/helpers.d.ts +11 -0
- package/dist/router/helpers.js +30 -0
- package/dist/router/index.d.ts +22 -0
- package/dist/router/index.js +135 -0
- package/dist/router/pathCache.d.ts +16 -0
- package/dist/router/pathCache.js +116 -0
- package/dist/router/poolHelper.d.ts +126 -0
- package/dist/router/poolHelper.js +807 -0
- package/dist/router/quoteWethTrade.d.ts +14 -0
- package/dist/router/quoteWethTrade.js +125 -0
- package/dist/router/simulation.d.ts +43 -0
- package/dist/router/simulation.js +70 -0
- package/dist/router/types.d.ts +149 -0
- package/dist/router/types.js +1 -0
- package/dist/router/v4PoolDiscovery.d.ts +30 -0
- package/dist/router/v4PoolDiscovery.js +147 -0
- package/dist/router/velora.d.ts +42 -0
- package/dist/router/velora.js +279 -0
- package/dist/schemas/index.d.ts +6 -0
- package/dist/schemas/index.js +6 -0
- package/dist/schemas/primitives.d.ts +3 -0
- package/dist/schemas/primitives.js +9 -0
- package/dist/schemas/quoteDeposit.d.ts +23 -0
- package/dist/schemas/quoteDeposit.js +10 -0
- package/dist/schemas/quoteHarvest.d.ts +17 -0
- package/dist/schemas/quoteHarvest.js +8 -0
- package/dist/schemas/quoteRebalance.d.ts +29 -0
- package/dist/schemas/quoteRebalance.js +10 -0
- package/dist/schemas/quoteTrade.d.ts +20 -0
- package/dist/schemas/quoteTrade.js +9 -0
- package/dist/schemas/quoteWithdrawal.d.ts +23 -0
- package/dist/schemas/quoteWithdrawal.js +10 -0
- package/dist/txBuilders/buildDepositTx.d.ts +17 -0
- package/dist/txBuilders/buildDepositTx.js +18 -0
- package/dist/txBuilders/buildHarvestTx.d.ts +16 -0
- package/dist/txBuilders/buildHarvestTx.js +17 -0
- package/dist/txBuilders/buildTradeTx.d.ts +9 -0
- package/dist/txBuilders/buildTradeTx.js +9 -0
- package/dist/txBuilders/buildWithdrawTx.d.ts +16 -0
- package/dist/txBuilders/buildWithdrawTx.js +17 -0
- package/dist/txBuilders/index.d.ts +8 -0
- package/dist/txBuilders/index.js +4 -0
- package/dist/typechain/common.d.ts +50 -0
- package/dist/typechain/common.js +1 -0
- package/dist/typechain/factories/include/IPancakeQuoterV2__factory.d.ts +225 -0
- package/dist/typechain/factories/include/IPancakeQuoterV2__factory.js +295 -0
- package/dist/typechain/factories/include/ISwapRouter02__factory.d.ts +826 -0
- package/dist/typechain/factories/include/ISwapRouter02__factory.js +1073 -0
- package/dist/typechain/factories/include/index.d.ts +2 -0
- package/dist/typechain/factories/include/index.js +2 -0
- package/dist/typechain/factories/index.d.ts +2 -0
- package/dist/typechain/factories/index.js +2 -0
- package/dist/typechain/factories/out/Adapter__factory.d.ts +31 -0
- package/dist/typechain/factories/out/Adapter__factory.js +43 -0
- package/dist/typechain/factories/out/AerodromeV2Adapter__factory.d.ts +179 -0
- package/dist/typechain/factories/out/AerodromeV2Adapter__factory.js +237 -0
- package/dist/typechain/factories/out/AerodromeV3Adapter__factory.d.ts +162 -0
- package/dist/typechain/factories/out/AerodromeV3Adapter__factory.js +215 -0
- package/dist/typechain/factories/out/AssetManager.sol/LedgerAssetManager__factory.d.ts +1032 -0
- package/dist/typechain/factories/out/AssetManager.sol/LedgerAssetManager__factory.js +1357 -0
- package/dist/typechain/factories/out/AssetManager.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/AssetManager.sol/index.js +1 -0
- package/dist/typechain/factories/out/Bookkeeper.sol/LedgerBookkeeper__factory.d.ts +950 -0
- package/dist/typechain/factories/out/Bookkeeper.sol/LedgerBookkeeper__factory.js +1250 -0
- package/dist/typechain/factories/out/Bookkeeper.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/Bookkeeper.sol/index.js +1 -0
- package/dist/typechain/factories/out/Checkpoint.sol/CheckpointController__factory.d.ts +33 -0
- package/dist/typechain/factories/out/Checkpoint.sol/CheckpointController__factory.js +47 -0
- package/dist/typechain/factories/out/Checkpoint.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/Checkpoint.sol/index.js +1 -0
- package/dist/typechain/factories/out/Closure.sol/ClosureController__factory.d.ts +459 -0
- package/dist/typechain/factories/out/Closure.sol/ClosureController__factory.js +603 -0
- package/dist/typechain/factories/out/Closure.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/Closure.sol/index.js +1 -0
- package/dist/typechain/factories/out/ContextBuilder__factory.d.ts +671 -0
- package/dist/typechain/factories/out/ContextBuilder__factory.js +870 -0
- package/dist/typechain/factories/out/Control.sol/Controllable__factory.d.ts +107 -0
- package/dist/typechain/factories/out/Control.sol/Controllable__factory.js +141 -0
- package/dist/typechain/factories/out/Control.sol/Controller__factory.d.ts +21 -0
- package/dist/typechain/factories/out/Control.sol/Controller__factory.js +30 -0
- package/dist/typechain/factories/out/Control.sol/index.d.ts +2 -0
- package/dist/typechain/factories/out/Control.sol/index.js +2 -0
- package/dist/typechain/factories/out/Controller.sol/ClosureDelegate__factory.d.ts +57 -0
- package/dist/typechain/factories/out/Controller.sol/ClosureDelegate__factory.js +77 -0
- package/dist/typechain/factories/out/Controller.sol/CreationDelegate__factory.d.ts +58 -0
- package/dist/typechain/factories/out/Controller.sol/CreationDelegate__factory.js +77 -0
- package/dist/typechain/factories/out/Controller.sol/DepositDelegate__factory.d.ts +55 -0
- package/dist/typechain/factories/out/Controller.sol/DepositDelegate__factory.js +74 -0
- package/dist/typechain/factories/out/Controller.sol/FundController__factory.d.ts +703 -0
- package/dist/typechain/factories/out/Controller.sol/FundController__factory.js +911 -0
- package/dist/typechain/factories/out/Controller.sol/HarvestDelegate__factory.d.ts +234 -0
- package/dist/typechain/factories/out/Controller.sol/HarvestDelegate__factory.js +303 -0
- package/dist/typechain/factories/out/Controller.sol/TradeDelegate__factory.d.ts +61 -0
- package/dist/typechain/factories/out/Controller.sol/TradeDelegate__factory.js +81 -0
- package/dist/typechain/factories/out/Controller.sol/TransferDelegate__factory.d.ts +29 -0
- package/dist/typechain/factories/out/Controller.sol/TransferDelegate__factory.js +40 -0
- package/dist/typechain/factories/out/Controller.sol/WithdrawalDelegate__factory.d.ts +51 -0
- package/dist/typechain/factories/out/Controller.sol/WithdrawalDelegate__factory.js +69 -0
- package/dist/typechain/factories/out/Controller.sol/index.d.ts +8 -0
- package/dist/typechain/factories/out/Controller.sol/index.js +8 -0
- package/dist/typechain/factories/out/Cooldown.sol/LedgerCooldown__factory.d.ts +747 -0
- package/dist/typechain/factories/out/Cooldown.sol/LedgerCooldown__factory.js +982 -0
- package/dist/typechain/factories/out/Cooldown.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/Cooldown.sol/index.js +1 -0
- package/dist/typechain/factories/out/Creation.sol/CreationController__factory.d.ts +172 -0
- package/dist/typechain/factories/out/Creation.sol/CreationController__factory.js +225 -0
- package/dist/typechain/factories/out/Creation.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/Creation.sol/index.js +1 -0
- package/dist/typechain/factories/out/Deposit.sol/DepositController__factory.d.ts +162 -0
- package/dist/typechain/factories/out/Deposit.sol/DepositController__factory.js +223 -0
- package/dist/typechain/factories/out/Deposit.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/Deposit.sol/index.js +1 -0
- package/dist/typechain/factories/out/Error__factory.d.ts +318 -0
- package/dist/typechain/factories/out/Error__factory.js +423 -0
- package/dist/typechain/factories/out/Harvest.sol/HarvestController__factory.d.ts +401 -0
- package/dist/typechain/factories/out/Harvest.sol/HarvestController__factory.js +527 -0
- package/dist/typechain/factories/out/Harvest.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/Harvest.sol/index.js +1 -0
- package/dist/typechain/factories/out/IAerodromeRouter__factory.d.ts +85 -0
- package/dist/typechain/factories/out/IAerodromeRouter__factory.js +113 -0
- package/dist/typechain/factories/out/IAerodromeSwapRouter__factory.d.ts +89 -0
- package/dist/typechain/factories/out/IAerodromeSwapRouter__factory.js +119 -0
- package/dist/typechain/factories/out/IUniswapV4.sol/IPermit2__factory.d.ts +29 -0
- package/dist/typechain/factories/out/IUniswapV4.sol/IPermit2__factory.js +40 -0
- package/dist/typechain/factories/out/IUniswapV4.sol/IUniversalRouter__factory.d.ts +25 -0
- package/dist/typechain/factories/out/IUniswapV4.sol/IUniversalRouter__factory.js +35 -0
- package/dist/typechain/factories/out/IUniswapV4.sol/index.d.ts +2 -0
- package/dist/typechain/factories/out/IUniswapV4.sol/index.js +2 -0
- package/dist/typechain/factories/out/IV0Fund__factory.d.ts +129 -0
- package/dist/typechain/factories/out/IV0Fund__factory.js +173 -0
- package/dist/typechain/factories/out/IV3SwapRouter__factory.d.ts +38 -0
- package/dist/typechain/factories/out/IV3SwapRouter__factory.js +53 -0
- package/dist/typechain/factories/out/Initializer.sol/LedgerInitializer__factory.d.ts +655 -0
- package/dist/typechain/factories/out/Initializer.sol/LedgerInitializer__factory.js +859 -0
- package/dist/typechain/factories/out/Initializer.sol/VaultInitializer__factory.d.ts +141 -0
- package/dist/typechain/factories/out/Initializer.sol/VaultInitializer__factory.js +185 -0
- package/dist/typechain/factories/out/Initializer.sol/index.d.ts +2 -0
- package/dist/typechain/factories/out/Initializer.sol/index.js +2 -0
- package/dist/typechain/factories/out/Ledger.sol/FundLedger__factory.d.ts +1502 -0
- package/dist/typechain/factories/out/Ledger.sol/FundLedger__factory.js +1959 -0
- package/dist/typechain/factories/out/Ledger.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/Ledger.sol/index.js +1 -0
- package/dist/typechain/factories/out/LotusP2P__factory.d.ts +847 -0
- package/dist/typechain/factories/out/LotusP2P__factory.js +1095 -0
- package/dist/typechain/factories/out/MulticallStatic__factory.d.ts +29 -0
- package/dist/typechain/factories/out/MulticallStatic__factory.js +42 -0
- package/dist/typechain/factories/out/Operator.sol/VaultOperator__factory.d.ts +235 -0
- package/dist/typechain/factories/out/Operator.sol/VaultOperator__factory.js +308 -0
- package/dist/typechain/factories/out/Operator.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/Operator.sol/index.js +1 -0
- package/dist/typechain/factories/out/P2PAdapter__factory.d.ts +216 -0
- package/dist/typechain/factories/out/P2PAdapter__factory.js +287 -0
- package/dist/typechain/factories/out/ProtocolMigrationExtension__factory.d.ts +1084 -0
- package/dist/typechain/factories/out/ProtocolMigrationExtension__factory.js +1409 -0
- package/dist/typechain/factories/out/ProtocolProvider__factory.d.ts +21 -0
- package/dist/typechain/factories/out/ProtocolProvider__factory.js +30 -0
- package/dist/typechain/factories/out/Protocol__factory.d.ts +1095 -0
- package/dist/typechain/factories/out/Protocol__factory.js +1424 -0
- package/dist/typechain/factories/out/Registrar.sol/LedgerRegistrar__factory.d.ts +1375 -0
- package/dist/typechain/factories/out/Registrar.sol/LedgerRegistrar__factory.js +1791 -0
- package/dist/typechain/factories/out/Registrar.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/Registrar.sol/index.js +1 -0
- package/dist/typechain/factories/out/Settings.sol/SettingsController__factory.d.ts +143 -0
- package/dist/typechain/factories/out/Settings.sol/SettingsController__factory.js +188 -0
- package/dist/typechain/factories/out/Settings.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/Settings.sol/index.js +1 -0
- package/dist/typechain/factories/out/Shares.sol/ICheckpointController__factory.d.ts +21 -0
- package/dist/typechain/factories/out/Shares.sol/ICheckpointController__factory.js +31 -0
- package/dist/typechain/factories/out/Shares.sol/ITransferController__factory.d.ts +25 -0
- package/dist/typechain/factories/out/Shares.sol/ITransferController__factory.js +35 -0
- package/dist/typechain/factories/out/Shares.sol/LedgerShares__factory.d.ts +829 -0
- package/dist/typechain/factories/out/Shares.sol/LedgerShares__factory.js +1089 -0
- package/dist/typechain/factories/out/Shares.sol/index.d.ts +3 -0
- package/dist/typechain/factories/out/Shares.sol/index.js +3 -0
- package/dist/typechain/factories/out/SignatureVerifier__factory.d.ts +171 -0
- package/dist/typechain/factories/out/SignatureVerifier__factory.js +224 -0
- package/dist/typechain/factories/out/Storage.sol/LedgerStorage__factory.d.ts +527 -0
- package/dist/typechain/factories/out/Storage.sol/LedgerStorage__factory.js +695 -0
- package/dist/typechain/factories/out/Storage.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/Storage.sol/index.js +1 -0
- package/dist/typechain/factories/out/Trade.sol/TradeController__factory.d.ts +200 -0
- package/dist/typechain/factories/out/Trade.sol/TradeController__factory.js +262 -0
- package/dist/typechain/factories/out/Trade.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/Trade.sol/index.js +1 -0
- package/dist/typechain/factories/out/Transfer.sol/TransferController__factory.d.ts +100 -0
- package/dist/typechain/factories/out/Transfer.sol/TransferController__factory.js +141 -0
- package/dist/typechain/factories/out/Transfer.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/Transfer.sol/index.js +1 -0
- package/dist/typechain/factories/out/UniswapV2Adapter__factory.d.ts +162 -0
- package/dist/typechain/factories/out/UniswapV2Adapter__factory.js +215 -0
- package/dist/typechain/factories/out/UniswapV3Adapter__factory.d.ts +162 -0
- package/dist/typechain/factories/out/UniswapV3Adapter__factory.js +215 -0
- package/dist/typechain/factories/out/UniswapV3SwapRouter02Adapter__factory.d.ts +162 -0
- package/dist/typechain/factories/out/UniswapV3SwapRouter02Adapter__factory.js +215 -0
- package/dist/typechain/factories/out/UniswapV4Adapter__factory.d.ts +203 -0
- package/dist/typechain/factories/out/UniswapV4Adapter__factory.js +268 -0
- package/dist/typechain/factories/out/V0ToLotusMigrator__factory.d.ts +216 -0
- package/dist/typechain/factories/out/V0ToLotusMigrator__factory.js +284 -0
- package/dist/typechain/factories/out/Vault.sol/FundVault__factory.d.ts +258 -0
- package/dist/typechain/factories/out/Vault.sol/FundVault__factory.js +339 -0
- package/dist/typechain/factories/out/Vault.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/Vault.sol/index.js +1 -0
- package/dist/typechain/factories/out/Withdrawal.sol/WithdrawalController__factory.d.ts +285 -0
- package/dist/typechain/factories/out/Withdrawal.sol/WithdrawalController__factory.js +377 -0
- package/dist/typechain/factories/out/Withdrawal.sol/index.d.ts +1 -0
- package/dist/typechain/factories/out/Withdrawal.sol/index.js +1 -0
- package/dist/typechain/factories/out/index.d.ts +43 -0
- package/dist/typechain/factories/out/index.js +43 -0
- package/dist/typechain/include/IPancakeQuoterV2.d.ts +223 -0
- package/dist/typechain/include/IPancakeQuoterV2.js +1 -0
- package/dist/typechain/include/ISwapRouter02.d.ts +682 -0
- package/dist/typechain/include/ISwapRouter02.js +1 -0
- package/dist/typechain/include/index.d.ts +2 -0
- package/dist/typechain/include/index.js +1 -0
- package/dist/typechain/index.d.ts +119 -0
- package/dist/typechain/index.js +58 -0
- package/dist/typechain/out/Adapter.d.ts +29 -0
- package/dist/typechain/out/Adapter.js +1 -0
- package/dist/typechain/out/AerodromeV2Adapter.d.ts +106 -0
- package/dist/typechain/out/AerodromeV2Adapter.js +1 -0
- package/dist/typechain/out/AerodromeV3Adapter.d.ts +87 -0
- package/dist/typechain/out/AerodromeV3Adapter.js +1 -0
- package/dist/typechain/out/AssetManager.sol/LedgerAssetManager.d.ts +604 -0
- package/dist/typechain/out/AssetManager.sol/LedgerAssetManager.js +1 -0
- package/dist/typechain/out/AssetManager.sol/index.d.ts +1 -0
- package/dist/typechain/out/AssetManager.sol/index.js +1 -0
- package/dist/typechain/out/Bookkeeper.sol/LedgerBookkeeper.d.ts +548 -0
- package/dist/typechain/out/Bookkeeper.sol/LedgerBookkeeper.js +1 -0
- package/dist/typechain/out/Bookkeeper.sol/index.d.ts +1 -0
- package/dist/typechain/out/Bookkeeper.sol/index.js +1 -0
- package/dist/typechain/out/Checkpoint.sol/CheckpointController.d.ts +29 -0
- package/dist/typechain/out/Checkpoint.sol/CheckpointController.js +1 -0
- package/dist/typechain/out/Checkpoint.sol/index.d.ts +1 -0
- package/dist/typechain/out/Checkpoint.sol/index.js +1 -0
- package/dist/typechain/out/Closure.sol/ClosureController.d.ts +273 -0
- package/dist/typechain/out/Closure.sol/ClosureController.js +1 -0
- package/dist/typechain/out/Closure.sol/index.d.ts +1 -0
- package/dist/typechain/out/Closure.sol/index.js +1 -0
- package/dist/typechain/out/ContextBuilder.d.ts +403 -0
- package/dist/typechain/out/ContextBuilder.js +1 -0
- package/dist/typechain/out/Control.sol/Controllable.d.ts +97 -0
- package/dist/typechain/out/Control.sol/Controllable.js +1 -0
- package/dist/typechain/out/Control.sol/Controller.d.ts +25 -0
- package/dist/typechain/out/Control.sol/Controller.js +1 -0
- package/dist/typechain/out/Control.sol/index.d.ts +2 -0
- package/dist/typechain/out/Control.sol/index.js +1 -0
- package/dist/typechain/out/Controller.sol/ClosureDelegate.d.ts +61 -0
- package/dist/typechain/out/Controller.sol/ClosureDelegate.js +1 -0
- package/dist/typechain/out/Controller.sol/CreationDelegate.d.ts +61 -0
- package/dist/typechain/out/Controller.sol/CreationDelegate.js +1 -0
- package/dist/typechain/out/Controller.sol/DepositDelegate.d.ts +56 -0
- package/dist/typechain/out/Controller.sol/DepositDelegate.js +1 -0
- package/dist/typechain/out/Controller.sol/FundController.d.ts +464 -0
- package/dist/typechain/out/Controller.sol/FundController.js +1 -0
- package/dist/typechain/out/Controller.sol/HarvestDelegate.d.ts +163 -0
- package/dist/typechain/out/Controller.sol/HarvestDelegate.js +1 -0
- package/dist/typechain/out/Controller.sol/TradeDelegate.d.ts +67 -0
- package/dist/typechain/out/Controller.sol/TradeDelegate.js +1 -0
- package/dist/typechain/out/Controller.sol/TransferDelegate.d.ts +37 -0
- package/dist/typechain/out/Controller.sol/TransferDelegate.js +1 -0
- package/dist/typechain/out/Controller.sol/WithdrawalDelegate.d.ts +53 -0
- package/dist/typechain/out/Controller.sol/WithdrawalDelegate.js +1 -0
- package/dist/typechain/out/Controller.sol/index.d.ts +8 -0
- package/dist/typechain/out/Controller.sol/index.js +1 -0
- package/dist/typechain/out/Cooldown.sol/LedgerCooldown.d.ts +446 -0
- package/dist/typechain/out/Cooldown.sol/LedgerCooldown.js +1 -0
- package/dist/typechain/out/Cooldown.sol/index.d.ts +1 -0
- package/dist/typechain/out/Cooldown.sol/index.js +1 -0
- package/dist/typechain/out/Creation.sol/CreationController.d.ts +102 -0
- package/dist/typechain/out/Creation.sol/CreationController.js +1 -0
- package/dist/typechain/out/Creation.sol/index.d.ts +1 -0
- package/dist/typechain/out/Creation.sol/index.js +1 -0
- package/dist/typechain/out/Deposit.sol/DepositController.d.ts +68 -0
- package/dist/typechain/out/Deposit.sol/DepositController.js +1 -0
- package/dist/typechain/out/Deposit.sol/index.d.ts +1 -0
- package/dist/typechain/out/Deposit.sol/index.js +1 -0
- package/dist/typechain/out/Error.d.ts +20 -0
- package/dist/typechain/out/Error.js +1 -0
- package/dist/typechain/out/Harvest.sol/HarvestController.d.ts +241 -0
- package/dist/typechain/out/Harvest.sol/HarvestController.js +1 -0
- package/dist/typechain/out/Harvest.sol/index.d.ts +1 -0
- package/dist/typechain/out/Harvest.sol/index.js +1 -0
- package/dist/typechain/out/IAerodromeRouter.d.ts +80 -0
- package/dist/typechain/out/IAerodromeRouter.js +1 -0
- package/dist/typechain/out/IAerodromeSwapRouter.d.ts +96 -0
- package/dist/typechain/out/IAerodromeSwapRouter.js +1 -0
- package/dist/typechain/out/IUniswapV4.sol/IPermit2.d.ts +39 -0
- package/dist/typechain/out/IUniswapV4.sol/IPermit2.js +1 -0
- package/dist/typechain/out/IUniswapV4.sol/IUniversalRouter.d.ts +37 -0
- package/dist/typechain/out/IUniswapV4.sol/IUniversalRouter.js +1 -0
- package/dist/typechain/out/IUniswapV4.sol/index.d.ts +2 -0
- package/dist/typechain/out/IUniswapV4.sol/index.js +1 -0
- package/dist/typechain/out/IV0Fund.d.ts +69 -0
- package/dist/typechain/out/IV0Fund.js +1 -0
- package/dist/typechain/out/IV3SwapRouter.d.ts +52 -0
- package/dist/typechain/out/IV3SwapRouter.js +1 -0
- package/dist/typechain/out/Initializer.sol/LedgerInitializer.d.ts +379 -0
- package/dist/typechain/out/Initializer.sol/LedgerInitializer.js +1 -0
- package/dist/typechain/out/Initializer.sol/VaultInitializer.d.ts +129 -0
- package/dist/typechain/out/Initializer.sol/VaultInitializer.js +1 -0
- package/dist/typechain/out/Initializer.sol/index.d.ts +2 -0
- package/dist/typechain/out/Initializer.sol/index.js +1 -0
- package/dist/typechain/out/Ledger.sol/FundLedger.d.ts +926 -0
- package/dist/typechain/out/Ledger.sol/FundLedger.js +1 -0
- package/dist/typechain/out/Ledger.sol/index.d.ts +1 -0
- package/dist/typechain/out/Ledger.sol/index.js +1 -0
- package/dist/typechain/out/LotusP2P.d.ts +610 -0
- package/dist/typechain/out/LotusP2P.js +1 -0
- package/dist/typechain/out/MulticallStatic.d.ts +25 -0
- package/dist/typechain/out/MulticallStatic.js +1 -0
- package/dist/typechain/out/Operator.sol/VaultOperator.d.ts +197 -0
- package/dist/typechain/out/Operator.sol/VaultOperator.js +1 -0
- package/dist/typechain/out/Operator.sol/index.d.ts +1 -0
- package/dist/typechain/out/Operator.sol/index.js +1 -0
- package/dist/typechain/out/P2PAdapter.d.ts +100 -0
- package/dist/typechain/out/P2PAdapter.js +1 -0
- package/dist/typechain/out/Protocol.d.ts +815 -0
- package/dist/typechain/out/Protocol.js +1 -0
- package/dist/typechain/out/ProtocolMigrationExtension.d.ts +806 -0
- package/dist/typechain/out/ProtocolMigrationExtension.js +1 -0
- package/dist/typechain/out/ProtocolProvider.d.ts +25 -0
- package/dist/typechain/out/ProtocolProvider.js +1 -0
- package/dist/typechain/out/Registrar.sol/LedgerRegistrar.d.ts +866 -0
- package/dist/typechain/out/Registrar.sol/LedgerRegistrar.js +1 -0
- package/dist/typechain/out/Registrar.sol/index.d.ts +1 -0
- package/dist/typechain/out/Registrar.sol/index.js +1 -0
- package/dist/typechain/out/Settings.sol/SettingsController.d.ts +97 -0
- package/dist/typechain/out/Settings.sol/SettingsController.js +1 -0
- package/dist/typechain/out/Settings.sol/index.d.ts +1 -0
- package/dist/typechain/out/Settings.sol/index.js +1 -0
- package/dist/typechain/out/Shares.sol/ICheckpointController.d.ts +29 -0
- package/dist/typechain/out/Shares.sol/ICheckpointController.js +1 -0
- package/dist/typechain/out/Shares.sol/ITransferController.d.ts +37 -0
- package/dist/typechain/out/Shares.sol/ITransferController.js +1 -0
- package/dist/typechain/out/Shares.sol/LedgerShares.d.ts +494 -0
- package/dist/typechain/out/Shares.sol/LedgerShares.js +1 -0
- package/dist/typechain/out/Shares.sol/index.d.ts +3 -0
- package/dist/typechain/out/Shares.sol/index.js +1 -0
- package/dist/typechain/out/SignatureVerifier.d.ts +139 -0
- package/dist/typechain/out/SignatureVerifier.js +1 -0
- package/dist/typechain/out/Storage.sol/LedgerStorage.d.ts +281 -0
- package/dist/typechain/out/Storage.sol/LedgerStorage.js +1 -0
- package/dist/typechain/out/Storage.sol/index.d.ts +1 -0
- package/dist/typechain/out/Storage.sol/index.js +1 -0
- package/dist/typechain/out/Trade.sol/TradeController.d.ts +137 -0
- package/dist/typechain/out/Trade.sol/TradeController.js +1 -0
- package/dist/typechain/out/Trade.sol/index.d.ts +1 -0
- package/dist/typechain/out/Trade.sol/index.js +1 -0
- package/dist/typechain/out/Transfer.sol/TransferController.d.ts +49 -0
- package/dist/typechain/out/Transfer.sol/TransferController.js +1 -0
- package/dist/typechain/out/Transfer.sol/index.d.ts +1 -0
- package/dist/typechain/out/Transfer.sol/index.js +1 -0
- package/dist/typechain/out/UniswapV2Adapter.d.ts +87 -0
- package/dist/typechain/out/UniswapV2Adapter.js +1 -0
- package/dist/typechain/out/UniswapV3Adapter.d.ts +87 -0
- package/dist/typechain/out/UniswapV3Adapter.js +1 -0
- package/dist/typechain/out/UniswapV3SwapRouter02Adapter.d.ts +87 -0
- package/dist/typechain/out/UniswapV3SwapRouter02Adapter.js +1 -0
- package/dist/typechain/out/UniswapV4Adapter.d.ts +110 -0
- package/dist/typechain/out/UniswapV4Adapter.js +1 -0
- package/dist/typechain/out/V0ToLotusMigrator.d.ts +94 -0
- package/dist/typechain/out/V0ToLotusMigrator.js +1 -0
- package/dist/typechain/out/Vault.sol/FundVault.d.ts +197 -0
- package/dist/typechain/out/Vault.sol/FundVault.js +1 -0
- package/dist/typechain/out/Vault.sol/index.d.ts +1 -0
- package/dist/typechain/out/Vault.sol/index.js +1 -0
- package/dist/typechain/out/Withdrawal.sol/WithdrawalController.d.ts +152 -0
- package/dist/typechain/out/Withdrawal.sol/WithdrawalController.js +1 -0
- package/dist/typechain/out/Withdrawal.sol/index.d.ts +1 -0
- package/dist/typechain/out/Withdrawal.sol/index.js +1 -0
- package/dist/typechain/out/index.d.ts +65 -0
- package/dist/typechain/out/index.js +1 -0
- package/dist/types/Fund.d.ts +27 -0
- package/dist/types/Fund.js +1 -0
- package/package.json +67 -0
|
@@ -0,0 +1,807 @@
|
|
|
1
|
+
import { Contract, ZeroAddress, solidityPacked, } from 'ethers';
|
|
2
|
+
import { getGuruProtocolAddresses, isSupportedChainId, } from '../addresses.js';
|
|
3
|
+
import compareAddresses from '../helpers/compareAddresses.js';
|
|
4
|
+
import { Token } from '../helpers/Token.js';
|
|
5
|
+
import { AerodromeV2Adapter__factory, IPancakeQuoterV2__factory, UniswapV2Adapter__factory, UniswapV3Adapter__factory, } from '../typechain/index.js';
|
|
6
|
+
import { finalizeRouteQuote } from './finalizeRoute.js';
|
|
7
|
+
export const SwapType = Object.freeze({
|
|
8
|
+
EXACT_INPUT: 0n,
|
|
9
|
+
EXACT_OUTPUT: 1n,
|
|
10
|
+
});
|
|
11
|
+
const V2_ROUTER_ABI = [
|
|
12
|
+
'function getAmountsOut(uint256 amountIn, address[] path) view returns (uint256[] amounts)',
|
|
13
|
+
'function getAmountsIn(uint256 amountOut, address[] path) view returns (uint256[] amounts)',
|
|
14
|
+
];
|
|
15
|
+
const V2_FACTORY_ABI = [
|
|
16
|
+
'function getPair(address tokenA, address tokenB) view returns (address pair)',
|
|
17
|
+
];
|
|
18
|
+
const V2_PAIR_ABI = [
|
|
19
|
+
'function getReserves() view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast)',
|
|
20
|
+
];
|
|
21
|
+
const V3_FACTORY_ABI = [
|
|
22
|
+
'function getPool(address tokenA, address tokenB, uint24 fee) view returns (address pool)',
|
|
23
|
+
];
|
|
24
|
+
const V3_POOL_ABI = ['function fee() view returns (uint24)'];
|
|
25
|
+
const AERO_FACTORY_ABI = [
|
|
26
|
+
'function getPool(address tokenA, address tokenB, bool stable) view returns (address)',
|
|
27
|
+
];
|
|
28
|
+
const AERO_V2_ROUTER_ABI = [
|
|
29
|
+
'function getAmountsOut(uint256 amountIn, (address from, address to, bool stable, address factory)[] routes) view returns (uint256[] amounts)',
|
|
30
|
+
];
|
|
31
|
+
const AERO_V3_QUOTER_ABI = [
|
|
32
|
+
'function quoteExactInputSingle((address tokenIn, address tokenOut, uint256 amountIn, int24 tickSpacing, uint160 sqrtPriceLimitX96) params) returns (uint256 amountOut, uint160 sqrtPriceX96After, uint32 initializedTicksCrossed, uint256 gasEstimate)',
|
|
33
|
+
'function quoteExactOutputSingle((address tokenIn, address tokenOut, uint256 amount, int24 tickSpacing, uint160 sqrtPriceLimitX96) params) returns (uint256 amountIn, uint160 sqrtPriceX96After, uint32 initializedTicksCrossed, uint256 gasEstimate)',
|
|
34
|
+
];
|
|
35
|
+
const V3_POOL_TICKSPACING_ABI = [
|
|
36
|
+
'function tickSpacing() view returns (int24)',
|
|
37
|
+
];
|
|
38
|
+
function connect(address, abi, runner) {
|
|
39
|
+
return new Contract(address, abi, runner);
|
|
40
|
+
}
|
|
41
|
+
var PoolHelperError;
|
|
42
|
+
(function (PoolHelperError) {
|
|
43
|
+
PoolHelperError["NO_ROUTE_FOUND"] = "NO_ROUTE_FOUND";
|
|
44
|
+
PoolHelperError["NETWORK_NOT_SUPPORTED"] = "NETWORK_NOT_SUPPORTED";
|
|
45
|
+
PoolHelperError["EXCHANGE_INCOMPATIBLE"] = "EXCHANGE_INCOMPATIBLE";
|
|
46
|
+
PoolHelperError["NO_POOL_ADDRESS"] = "NO_POOL_ADDRESS";
|
|
47
|
+
PoolHelperError["LOTUS_ADAPTER_MISSING"] = "LOTUS_ADAPTER_MISSING";
|
|
48
|
+
})(PoolHelperError || (PoolHelperError = {}));
|
|
49
|
+
const UNIV3_FEE_TIERS = [100, 500, 3000, 10000];
|
|
50
|
+
const PANCAKEV3_FEE_TIERS = [100, 500, 2500, 10000];
|
|
51
|
+
const AEROV3_FEE_TIERS = [1, 50, 100, 200];
|
|
52
|
+
function buildDexTable(addresses) {
|
|
53
|
+
const table = {};
|
|
54
|
+
if (addresses.factories.uniswapV2 && addresses.routers.uniswapV2) {
|
|
55
|
+
table[addresses.factories.uniswapV2.toLowerCase()] = {
|
|
56
|
+
type: 'v2',
|
|
57
|
+
router: addresses.routers.uniswapV2,
|
|
58
|
+
kind: 'uniswap',
|
|
59
|
+
swappable: Boolean(addresses.adapters.uniswapV2),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
if (addresses.factories.uniswapV3 && addresses.quoters.uniswapV3) {
|
|
63
|
+
table[addresses.factories.uniswapV3.toLowerCase()] = {
|
|
64
|
+
type: 'v3',
|
|
65
|
+
router: addresses.routers.uniswapV3,
|
|
66
|
+
quoter: addresses.quoters.uniswapV3,
|
|
67
|
+
feeTiers: UNIV3_FEE_TIERS,
|
|
68
|
+
kind: 'uniswap',
|
|
69
|
+
swappable: Boolean(addresses.adapters.uniswapV3),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
if (addresses.factories.pancakeV2 && addresses.routers.pancakeV2) {
|
|
73
|
+
table[addresses.factories.pancakeV2.toLowerCase()] = {
|
|
74
|
+
type: 'v2',
|
|
75
|
+
router: addresses.routers.pancakeV2,
|
|
76
|
+
kind: 'uniswap',
|
|
77
|
+
swappable: Boolean(addresses.adapters.pancakeV2),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
if (addresses.factories.pancakeV3 && addresses.quoters.pancakeV3) {
|
|
81
|
+
table[addresses.factories.pancakeV3.toLowerCase()] = {
|
|
82
|
+
type: 'v3',
|
|
83
|
+
router: addresses.routers.pancakeV3,
|
|
84
|
+
quoter: addresses.quoters.pancakeV3,
|
|
85
|
+
feeTiers: PANCAKEV3_FEE_TIERS,
|
|
86
|
+
kind: 'uniswap',
|
|
87
|
+
swappable: Boolean(addresses.adapters.pancakeV3),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
if (addresses.factories.aerodromeV2 && addresses.routers.aerodromeV2) {
|
|
91
|
+
table[addresses.factories.aerodromeV2.toLowerCase()] = {
|
|
92
|
+
type: 'v2',
|
|
93
|
+
router: addresses.routers.aerodromeV2,
|
|
94
|
+
kind: 'aerodrome',
|
|
95
|
+
swappable: Boolean(addresses.adapters.aerodromeV2),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
if (addresses.factories.aerodromeV3 && addresses.quoters.aerodromeV3) {
|
|
99
|
+
const v3 = {
|
|
100
|
+
type: 'v3',
|
|
101
|
+
router: addresses.routers.aerodromeV3,
|
|
102
|
+
quoter: addresses.quoters.aerodromeV3,
|
|
103
|
+
feeTiers: AEROV3_FEE_TIERS,
|
|
104
|
+
kind: 'aerodrome',
|
|
105
|
+
swappable: Boolean(addresses.adapters.aerodromeV3),
|
|
106
|
+
};
|
|
107
|
+
table[addresses.factories.aerodromeV3.toLowerCase()] = v3;
|
|
108
|
+
if (addresses.factories.aerodromeV3Bis) {
|
|
109
|
+
table[addresses.factories.aerodromeV3Bis.toLowerCase()] = v3;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return table;
|
|
113
|
+
}
|
|
114
|
+
const PERCENT_DENOMINATOR = 100000n;
|
|
115
|
+
const CACHE_DURATION = 60 * 60 * 1000;
|
|
116
|
+
function withSlippageTolerance(amount, slippage) {
|
|
117
|
+
const tolerance = (amount * slippage) / PERCENT_DENOMINATOR;
|
|
118
|
+
return amount - tolerance;
|
|
119
|
+
}
|
|
120
|
+
function calcSlippageTolerance(amount, slippage) {
|
|
121
|
+
return (amount * slippage) / PERCENT_DENOMINATOR;
|
|
122
|
+
}
|
|
123
|
+
function lastAmount(amounts, context) {
|
|
124
|
+
const amount = amounts.at(-1);
|
|
125
|
+
if (amount == null) {
|
|
126
|
+
throw new Error(`${PoolHelperError.NO_ROUTE_FOUND}: empty quote (${context})`);
|
|
127
|
+
}
|
|
128
|
+
return amount;
|
|
129
|
+
}
|
|
130
|
+
function buildAerodromeV2Routes(path, factory) {
|
|
131
|
+
return path.slice(0, -1).map((token, i) => ({
|
|
132
|
+
from: token,
|
|
133
|
+
to: path[i + 1],
|
|
134
|
+
stable: false,
|
|
135
|
+
factory,
|
|
136
|
+
}));
|
|
137
|
+
}
|
|
138
|
+
export default class PoolHelper {
|
|
139
|
+
chainId;
|
|
140
|
+
addresses;
|
|
141
|
+
provider;
|
|
142
|
+
getSwapFeePercentage;
|
|
143
|
+
dexTable;
|
|
144
|
+
poolsCache = {};
|
|
145
|
+
constructor(config) {
|
|
146
|
+
if (!isSupportedChainId(config.chainId)) {
|
|
147
|
+
throw new Error(`${PoolHelperError.NETWORK_NOT_SUPPORTED}: chainId=${config.chainId}`);
|
|
148
|
+
}
|
|
149
|
+
this.chainId = config.chainId;
|
|
150
|
+
this.addresses = getGuruProtocolAddresses(config.chainId);
|
|
151
|
+
this.provider = config.provider;
|
|
152
|
+
this.getSwapFeePercentage = config.getSwapFeePercentage;
|
|
153
|
+
this.dexTable = buildDexTable(this.addresses);
|
|
154
|
+
}
|
|
155
|
+
async _finalizeExecutableQuote({ finalization, adapter, path, amountToSend, amountQuoted, initialTollAmount, outputTollE3, slippage, buildSwap, }) {
|
|
156
|
+
if (!finalization) {
|
|
157
|
+
const amountToReceive = withSlippageTolerance(amountQuoted, slippage);
|
|
158
|
+
const tollAmount = initialTollAmount === 0n
|
|
159
|
+
? (amountToReceive * (outputTollE3 ?? 200n)) /
|
|
160
|
+
PERCENT_DENOMINATOR
|
|
161
|
+
: initialTollAmount;
|
|
162
|
+
const swap = buildSwap(amountToReceive);
|
|
163
|
+
return { ...swap, tollAmount };
|
|
164
|
+
}
|
|
165
|
+
const context = {
|
|
166
|
+
chainId: this.chainId,
|
|
167
|
+
blockNumber: finalization.blockNumber,
|
|
168
|
+
controller: finalization.controller,
|
|
169
|
+
vault: finalization.vault,
|
|
170
|
+
adapter,
|
|
171
|
+
account: finalization.account,
|
|
172
|
+
path,
|
|
173
|
+
buildCallDataForAmount: (amountToReceive) => String(buildSwap(amountToReceive).callData),
|
|
174
|
+
simulator: finalization.simulator,
|
|
175
|
+
prefixTxs: finalization.prefixTxs,
|
|
176
|
+
};
|
|
177
|
+
const result = await finalizeRouteQuote({
|
|
178
|
+
context,
|
|
179
|
+
amountToSend,
|
|
180
|
+
amountQuoted,
|
|
181
|
+
initialTollAmount,
|
|
182
|
+
outputTollE3,
|
|
183
|
+
maxSlippageE3: finalization.maxSlippageE3 ?? slippage,
|
|
184
|
+
});
|
|
185
|
+
const { data } = buildSwap(result.finalAmountToReceive);
|
|
186
|
+
return {
|
|
187
|
+
data,
|
|
188
|
+
callData: result.callData,
|
|
189
|
+
tollAmount: result.finalTollAmount,
|
|
190
|
+
effectiveSlippageBps: result.effectiveSlippageBps,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
getDexData(exchangeFactory) {
|
|
194
|
+
const dex = this.dexTable[exchangeFactory.toLowerCase()];
|
|
195
|
+
if (!dex) {
|
|
196
|
+
throw new Error(`${PoolHelperError.EXCHANGE_INCOMPATIBLE}: factory=${exchangeFactory} chainId=${this.chainId}`);
|
|
197
|
+
}
|
|
198
|
+
return dex;
|
|
199
|
+
}
|
|
200
|
+
isAerodrome(exchangeFactory) {
|
|
201
|
+
const dex = this.dexTable[exchangeFactory.toLowerCase()];
|
|
202
|
+
return dex?.kind === 'aerodrome';
|
|
203
|
+
}
|
|
204
|
+
async getPoolFeeTier(poolAddress) {
|
|
205
|
+
try {
|
|
206
|
+
const pool = connect(poolAddress, V3_POOL_ABI, this.provider);
|
|
207
|
+
const feeTier = await pool.fee();
|
|
208
|
+
return Number(feeTier);
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
try {
|
|
212
|
+
const pair = connect(poolAddress, V2_PAIR_ABI, this.provider);
|
|
213
|
+
await pair.getReserves();
|
|
214
|
+
return 0;
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
throw new Error(`${PoolHelperError.EXCHANGE_INCOMPATIBLE}: poolAddress=${poolAddress}`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
async _getPoolsFromOnChainQuery(token, options = {}) {
|
|
222
|
+
const { exchangeFactory, swappableOnly } = options;
|
|
223
|
+
const weth = this.addresses.tokens.WETH;
|
|
224
|
+
const wethToken = new Token(weth, this.provider);
|
|
225
|
+
const entries = Object.entries(this.dexTable).filter(([factory, dex]) => {
|
|
226
|
+
if (exchangeFactory &&
|
|
227
|
+
!compareAddresses(exchangeFactory, factory)) {
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
if (swappableOnly && !dex.swappable)
|
|
231
|
+
return false;
|
|
232
|
+
return true;
|
|
233
|
+
});
|
|
234
|
+
const pools = [];
|
|
235
|
+
await Promise.all(entries.map(async ([factoryAddress, dex]) => {
|
|
236
|
+
if (dex.type === 'v2') {
|
|
237
|
+
try {
|
|
238
|
+
let pairAddress;
|
|
239
|
+
if (dex.kind === 'aerodrome') {
|
|
240
|
+
const aeroFactory = connect(factoryAddress, AERO_FACTORY_ABI, this.provider);
|
|
241
|
+
pairAddress = await aeroFactory.getPool(token, weth, false);
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
const factory = connect(factoryAddress, V2_FACTORY_ABI, this.provider);
|
|
245
|
+
pairAddress = await factory.getPair(token, weth);
|
|
246
|
+
}
|
|
247
|
+
if (pairAddress === ZeroAddress)
|
|
248
|
+
return;
|
|
249
|
+
const wethBalance = await wethToken.balanceOf(pairAddress);
|
|
250
|
+
if (wethBalance === 0n)
|
|
251
|
+
return;
|
|
252
|
+
pools.push({
|
|
253
|
+
address: pairAddress,
|
|
254
|
+
feeTier: 0,
|
|
255
|
+
exchangeFactory: factoryAddress,
|
|
256
|
+
wethBalance,
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
catch {
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
const factory = connect(factoryAddress, V3_FACTORY_ABI, this.provider);
|
|
264
|
+
await Promise.all(dex.feeTiers.map(async (feeTier) => {
|
|
265
|
+
try {
|
|
266
|
+
const poolAddress = await factory.getPool(token, weth, feeTier);
|
|
267
|
+
if (poolAddress === ZeroAddress)
|
|
268
|
+
return;
|
|
269
|
+
const wethBalance = await wethToken.balanceOf(poolAddress);
|
|
270
|
+
if (wethBalance === 0n)
|
|
271
|
+
return;
|
|
272
|
+
pools.push({
|
|
273
|
+
address: poolAddress,
|
|
274
|
+
feeTier: feeTier,
|
|
275
|
+
exchangeFactory: factoryAddress,
|
|
276
|
+
wethBalance,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
catch {
|
|
280
|
+
}
|
|
281
|
+
}));
|
|
282
|
+
}
|
|
283
|
+
}));
|
|
284
|
+
pools.sort((a, b) => {
|
|
285
|
+
if (a.wethBalance > b.wethBalance)
|
|
286
|
+
return -1;
|
|
287
|
+
if (a.wethBalance < b.wethBalance)
|
|
288
|
+
return 1;
|
|
289
|
+
return 0;
|
|
290
|
+
});
|
|
291
|
+
return pools;
|
|
292
|
+
}
|
|
293
|
+
async getUniswapCompatibleTokenPools(token, options) {
|
|
294
|
+
const swappableOnly = options?.swappableOnly ?? true;
|
|
295
|
+
const cacheKey = `onchain-${this.chainId}-${token.toLowerCase()}-${options?.exchangeFactory?.toLowerCase()}-${swappableOnly}`;
|
|
296
|
+
const hit = this.poolsCache[cacheKey];
|
|
297
|
+
if (!hit || Date.now() - hit.timestamp > CACHE_DURATION) {
|
|
298
|
+
this.poolsCache[cacheKey] = {
|
|
299
|
+
timestamp: Date.now(),
|
|
300
|
+
pools: await this._getPoolsFromOnChainQuery(token, {
|
|
301
|
+
exchangeFactory: options?.exchangeFactory,
|
|
302
|
+
swappableOnly,
|
|
303
|
+
}),
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
return this.poolsCache[cacheKey].pools;
|
|
307
|
+
}
|
|
308
|
+
async getUniswapCompatibleTokenPool(token, options) {
|
|
309
|
+
const pools = await this.getUniswapCompatibleTokenPools(token, options);
|
|
310
|
+
if (pools.length === 0) {
|
|
311
|
+
throw new Error(`${PoolHelperError.NO_ROUTE_FOUND}: token=${token}`);
|
|
312
|
+
}
|
|
313
|
+
return pools[0];
|
|
314
|
+
}
|
|
315
|
+
async getBestQuote({ swapType = SwapType.EXACT_INPUT, tokenAmount, slippage = 500n, path, requireSwappable = true, }) {
|
|
316
|
+
const weth = this.addresses.tokens.WETH;
|
|
317
|
+
if (!path.some((t) => compareAddresses(t, weth))) {
|
|
318
|
+
throw new Error(`${PoolHelperError.NO_ROUTE_FOUND}: WETH not in path`);
|
|
319
|
+
}
|
|
320
|
+
const token = path.find((t) => !compareAddresses(t, weth));
|
|
321
|
+
const pools = await this.getUniswapCompatibleTokenPools(token, {
|
|
322
|
+
swappableOnly: requireSwappable,
|
|
323
|
+
});
|
|
324
|
+
if (pools.length === 0) {
|
|
325
|
+
throw new Error(`${PoolHelperError.NO_ROUTE_FOUND}: token=${token}`);
|
|
326
|
+
}
|
|
327
|
+
let lastError;
|
|
328
|
+
for (const pool of pools) {
|
|
329
|
+
const isV2Pool = pool.feeTier === 0;
|
|
330
|
+
const getQuote = isV2Pool ? this.getV2Quote : this.getV3Quote;
|
|
331
|
+
try {
|
|
332
|
+
return await getQuote.call(this, {
|
|
333
|
+
swapType,
|
|
334
|
+
tokenAmount,
|
|
335
|
+
path,
|
|
336
|
+
slippage,
|
|
337
|
+
exchangeFactory: pool.exchangeFactory,
|
|
338
|
+
feeTier: pool.feeTier,
|
|
339
|
+
poolAddress: pool.address,
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
catch (err) {
|
|
343
|
+
lastError = err;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
throw lastError instanceof Error
|
|
347
|
+
? lastError
|
|
348
|
+
: new Error(`${PoolHelperError.NO_ROUTE_FOUND}: token=${token}`);
|
|
349
|
+
}
|
|
350
|
+
async getV2Quote({ swapType = SwapType.EXACT_INPUT, tokenAmount, exchangeFactory, path, feeTier, slippage, }) {
|
|
351
|
+
const dex = this.getDexData(exchangeFactory);
|
|
352
|
+
if (dex.type !== 'v2') {
|
|
353
|
+
throw new Error(`${PoolHelperError.EXCHANGE_INCOMPATIBLE}: factory=${exchangeFactory}`);
|
|
354
|
+
}
|
|
355
|
+
const swapFeePercentage = await this.getSwapFeePercentage();
|
|
356
|
+
const { adjustedTokenAmount, swapFee: swapFeeFromInputAdjustment } = this._adjustTokenAmountForSwapFee(tokenAmount, swapFeePercentage, swapType, path);
|
|
357
|
+
const getAmounts = () => {
|
|
358
|
+
if (dex.kind === 'aerodrome') {
|
|
359
|
+
const v2Router = connect(dex.router, AERO_V2_ROUTER_ABI, this.provider);
|
|
360
|
+
const _path = swapType === SwapType.EXACT_INPUT
|
|
361
|
+
? path
|
|
362
|
+
: [...path].reverse();
|
|
363
|
+
const routes = [
|
|
364
|
+
{
|
|
365
|
+
from: _path[0],
|
|
366
|
+
to: _path[1],
|
|
367
|
+
stable: false,
|
|
368
|
+
factory: exchangeFactory,
|
|
369
|
+
},
|
|
370
|
+
];
|
|
371
|
+
return v2Router.getAmountsOut(adjustedTokenAmount, routes);
|
|
372
|
+
}
|
|
373
|
+
const v2Router = connect(dex.router, V2_ROUTER_ABI, this.provider);
|
|
374
|
+
return swapType === SwapType.EXACT_INPUT
|
|
375
|
+
? v2Router.getAmountsOut(adjustedTokenAmount, path)
|
|
376
|
+
: v2Router.getAmountsIn(adjustedTokenAmount, path);
|
|
377
|
+
};
|
|
378
|
+
const amounts = await getAmounts();
|
|
379
|
+
const quotedInput = amounts[0];
|
|
380
|
+
if (quotedInput == null) {
|
|
381
|
+
throw new Error(`${PoolHelperError.NO_ROUTE_FOUND}: empty quote (${exchangeFactory})`);
|
|
382
|
+
}
|
|
383
|
+
const quotedOutput = lastAmount(amounts, exchangeFactory);
|
|
384
|
+
const { adjustedQuote, swapFee: swapFeeFromQuoteAdjustment } = this._adjustQuoteForSwapFeeAndSlippage(swapType === SwapType.EXACT_INPUT ? quotedOutput : quotedInput, swapFeePercentage, slippage, swapType, path);
|
|
385
|
+
return {
|
|
386
|
+
amount: adjustedQuote,
|
|
387
|
+
feeTier,
|
|
388
|
+
swapFee: swapFeeFromInputAdjustment + swapFeeFromQuoteAdjustment,
|
|
389
|
+
router: dex.router,
|
|
390
|
+
exchangeFactory,
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
async getV3Quote({ swapType = SwapType.EXACT_INPUT, tokenAmount, exchangeFactory, path, feeTier, slippage, poolAddress, }) {
|
|
394
|
+
const [$INPUT, $OUTPUT] = path;
|
|
395
|
+
const dex = this.getDexData(exchangeFactory);
|
|
396
|
+
if (dex.type !== 'v3') {
|
|
397
|
+
throw new Error(`${PoolHelperError.EXCHANGE_INCOMPATIBLE}: factory=${exchangeFactory}`);
|
|
398
|
+
}
|
|
399
|
+
const swapFeePercentage = await this.getSwapFeePercentage();
|
|
400
|
+
const { adjustedTokenAmount, swapFee: swapFeeFromInputAdjustment } = this._adjustTokenAmountForSwapFee(tokenAmount, swapFeePercentage, swapType, path);
|
|
401
|
+
let tickSpacing;
|
|
402
|
+
let getQuote;
|
|
403
|
+
if (dex.kind === 'aerodrome') {
|
|
404
|
+
if (!poolAddress) {
|
|
405
|
+
throw new Error(`${PoolHelperError.NO_POOL_ADDRESS}: factory=${exchangeFactory}`);
|
|
406
|
+
}
|
|
407
|
+
const poolContract = connect(poolAddress, V3_POOL_TICKSPACING_ABI, this.provider);
|
|
408
|
+
const _tickSpacing = await poolContract.tickSpacing();
|
|
409
|
+
tickSpacing = _tickSpacing;
|
|
410
|
+
const aerodromeQuoter = connect(dex.quoter, AERO_V3_QUOTER_ABI, this.provider);
|
|
411
|
+
if (swapType === SwapType.EXACT_INPUT) {
|
|
412
|
+
getQuote = async () => {
|
|
413
|
+
const result = await aerodromeQuoter.quoteExactInputSingle.staticCall({
|
|
414
|
+
tokenIn: $INPUT,
|
|
415
|
+
tokenOut: $OUTPUT,
|
|
416
|
+
amountIn: adjustedTokenAmount,
|
|
417
|
+
sqrtPriceLimitX96: 0n,
|
|
418
|
+
tickSpacing: _tickSpacing,
|
|
419
|
+
});
|
|
420
|
+
return result.amountOut;
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
getQuote = async () => {
|
|
425
|
+
const result = await aerodromeQuoter.quoteExactOutputSingle.staticCall({
|
|
426
|
+
tokenIn: $INPUT,
|
|
427
|
+
tokenOut: $OUTPUT,
|
|
428
|
+
amount: adjustedTokenAmount,
|
|
429
|
+
sqrtPriceLimitX96: 0n,
|
|
430
|
+
tickSpacing: _tickSpacing,
|
|
431
|
+
});
|
|
432
|
+
return result.amountIn;
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
else {
|
|
437
|
+
const v3Quoter = IPancakeQuoterV2__factory.connect(dex.quoter, this.provider);
|
|
438
|
+
const quoteRequest = {
|
|
439
|
+
tokenIn: $INPUT,
|
|
440
|
+
tokenOut: $OUTPUT,
|
|
441
|
+
fee: BigInt(feeTier),
|
|
442
|
+
sqrtPriceLimitX96: 0n,
|
|
443
|
+
};
|
|
444
|
+
if (swapType === SwapType.EXACT_INPUT) {
|
|
445
|
+
getQuote = async () => {
|
|
446
|
+
const [amountOut] = await v3Quoter.quoteExactInputSingle.staticCall({
|
|
447
|
+
...quoteRequest,
|
|
448
|
+
amountIn: adjustedTokenAmount,
|
|
449
|
+
});
|
|
450
|
+
return amountOut;
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
else {
|
|
454
|
+
getQuote = async () => {
|
|
455
|
+
const [amountIn] = await v3Quoter.quoteExactOutputSingle.staticCall({
|
|
456
|
+
...quoteRequest,
|
|
457
|
+
amount: adjustedTokenAmount,
|
|
458
|
+
});
|
|
459
|
+
return amountIn;
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
const quote = await getQuote();
|
|
464
|
+
const { adjustedQuote, swapFee: swapFeeFromQuoteAdjustment } = this._adjustQuoteForSwapFeeAndSlippage(quote, swapFeePercentage, slippage, swapType, path);
|
|
465
|
+
return {
|
|
466
|
+
amount: adjustedQuote,
|
|
467
|
+
feeTier,
|
|
468
|
+
swapFee: swapFeeFromInputAdjustment + swapFeeFromQuoteAdjustment,
|
|
469
|
+
router: dex.router ?? dex.quoter,
|
|
470
|
+
exchangeFactory,
|
|
471
|
+
poolAddress,
|
|
472
|
+
tickSpacing,
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
async getStableForTokenQuote({ path, inputAmount, slippage = 500n, exchangeFactory, finalization, }) {
|
|
476
|
+
const adapter = this.getLotusAdapter(exchangeFactory);
|
|
477
|
+
const dexData = this.getDexData(exchangeFactory);
|
|
478
|
+
const swapFeePercentage = await this.getSwapFeePercentage();
|
|
479
|
+
const swapFee = (inputAmount * swapFeePercentage) / PERCENT_DENOMINATOR;
|
|
480
|
+
const adjustedInputAmount = inputAmount - swapFee;
|
|
481
|
+
if (dexData.type === 'v2') {
|
|
482
|
+
const deadline = Math.floor((Date.now() + 1000 * 60 * 5) / 1000);
|
|
483
|
+
if (dexData.kind === 'aerodrome') {
|
|
484
|
+
const v2Router = connect(dexData.router, AERO_V2_ROUTER_ABI, this.provider);
|
|
485
|
+
const routes = buildAerodromeV2Routes(path, exchangeFactory);
|
|
486
|
+
const amounts = await v2Router.getAmountsOut(adjustedInputAmount, routes);
|
|
487
|
+
const finalized = await this._finalizeExecutableQuote({
|
|
488
|
+
finalization,
|
|
489
|
+
adapter,
|
|
490
|
+
path,
|
|
491
|
+
amountToSend: inputAmount,
|
|
492
|
+
amountQuoted: lastAmount(amounts, exchangeFactory),
|
|
493
|
+
initialTollAmount: swapFee,
|
|
494
|
+
slippage,
|
|
495
|
+
buildSwap: (amountToReceive) => {
|
|
496
|
+
const data = {
|
|
497
|
+
amountToSend: inputAmount,
|
|
498
|
+
amountToReceive,
|
|
499
|
+
routes,
|
|
500
|
+
deadline,
|
|
501
|
+
};
|
|
502
|
+
return {
|
|
503
|
+
data,
|
|
504
|
+
callData: AerodromeV2Adapter__factory.createInterface().encodeFunctionData('executeSwap', [data]),
|
|
505
|
+
};
|
|
506
|
+
},
|
|
507
|
+
});
|
|
508
|
+
return {
|
|
509
|
+
adapter,
|
|
510
|
+
data: finalized.data,
|
|
511
|
+
callData: finalized.callData,
|
|
512
|
+
toll: { currency: path[0], amount: finalized.tollAmount },
|
|
513
|
+
effectiveSlippageBps: finalized.effectiveSlippageBps,
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
const v2Router = connect(dexData.router, V2_ROUTER_ABI, this.provider);
|
|
517
|
+
const amounts = await v2Router.getAmountsOut(adjustedInputAmount, path);
|
|
518
|
+
const finalized = await this._finalizeExecutableQuote({
|
|
519
|
+
finalization,
|
|
520
|
+
adapter,
|
|
521
|
+
path,
|
|
522
|
+
amountToSend: inputAmount,
|
|
523
|
+
amountQuoted: lastAmount(amounts, exchangeFactory),
|
|
524
|
+
initialTollAmount: swapFee,
|
|
525
|
+
slippage,
|
|
526
|
+
buildSwap: (amountToReceive) => {
|
|
527
|
+
const data = {
|
|
528
|
+
amountToSend: inputAmount,
|
|
529
|
+
amountToReceive,
|
|
530
|
+
path,
|
|
531
|
+
deadline,
|
|
532
|
+
};
|
|
533
|
+
return {
|
|
534
|
+
data,
|
|
535
|
+
callData: UniswapV2Adapter__factory.createInterface().encodeFunctionData('executeSwap', [data]),
|
|
536
|
+
};
|
|
537
|
+
},
|
|
538
|
+
});
|
|
539
|
+
return {
|
|
540
|
+
adapter,
|
|
541
|
+
data: finalized.data,
|
|
542
|
+
callData: finalized.callData,
|
|
543
|
+
toll: { currency: path[0], amount: finalized.tollAmount },
|
|
544
|
+
effectiveSlippageBps: finalized.effectiveSlippageBps,
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
const quoter = dexData.kind === 'aerodrome'
|
|
548
|
+
? connect(dexData.quoter, AERO_V3_QUOTER_ABI, this.provider)
|
|
549
|
+
: IPancakeQuoterV2__factory.connect(dexData.quoter, this.provider);
|
|
550
|
+
const { feeTier: stableFeeTier } = await this.getUniswapCompatibleTokenPool(path[0], {
|
|
551
|
+
exchangeFactory,
|
|
552
|
+
});
|
|
553
|
+
const { feeTier: tokenFeeTier } = await this.getUniswapCompatibleTokenPool(path[2], {
|
|
554
|
+
exchangeFactory,
|
|
555
|
+
});
|
|
556
|
+
const pathBytes = solidityPacked(['address', 'uint24', 'address', 'uint24', 'address'], [path[0], stableFeeTier, path[1], tokenFeeTier, path[2]]);
|
|
557
|
+
if (dexData.kind === 'aerodrome') {
|
|
558
|
+
throw new Error(`${PoolHelperError.EXCHANGE_INCOMPATIBLE}: Aerodrome V3 multi-hop not supported (factory=${exchangeFactory})`);
|
|
559
|
+
}
|
|
560
|
+
const univ3Quoter = quoter;
|
|
561
|
+
const amountOut = await this._quoteV3MultiHop(univ3Quoter, adjustedInputAmount, pathBytes, [
|
|
562
|
+
{ tokenIn: path[0], tokenOut: path[1], fee: stableFeeTier },
|
|
563
|
+
{ tokenIn: path[1], tokenOut: path[2], fee: tokenFeeTier },
|
|
564
|
+
]);
|
|
565
|
+
const deadline = Math.floor((Date.now() + 1000 * 60 * 5) / 1000);
|
|
566
|
+
const finalized = await this._finalizeExecutableQuote({
|
|
567
|
+
finalization,
|
|
568
|
+
adapter,
|
|
569
|
+
path: [
|
|
570
|
+
{
|
|
571
|
+
tokenIn: path[0],
|
|
572
|
+
tokenOut: path[1],
|
|
573
|
+
fee: String(stableFeeTier),
|
|
574
|
+
},
|
|
575
|
+
{
|
|
576
|
+
tokenIn: path[1],
|
|
577
|
+
tokenOut: path[2],
|
|
578
|
+
fee: String(tokenFeeTier),
|
|
579
|
+
},
|
|
580
|
+
],
|
|
581
|
+
amountToSend: inputAmount,
|
|
582
|
+
amountQuoted: amountOut,
|
|
583
|
+
initialTollAmount: swapFee,
|
|
584
|
+
slippage,
|
|
585
|
+
buildSwap: (amountToReceive) => {
|
|
586
|
+
const data = {
|
|
587
|
+
amountToSend: inputAmount,
|
|
588
|
+
amountToReceive,
|
|
589
|
+
path: pathBytes,
|
|
590
|
+
deadline,
|
|
591
|
+
};
|
|
592
|
+
return {
|
|
593
|
+
data,
|
|
594
|
+
callData: UniswapV3Adapter__factory.createInterface().encodeFunctionData('executeSwap', [data]),
|
|
595
|
+
};
|
|
596
|
+
},
|
|
597
|
+
});
|
|
598
|
+
return {
|
|
599
|
+
adapter,
|
|
600
|
+
data: finalized.data,
|
|
601
|
+
callData: finalized.callData,
|
|
602
|
+
toll: { currency: path[0], amount: finalized.tollAmount },
|
|
603
|
+
effectiveSlippageBps: finalized.effectiveSlippageBps,
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
async _quoteV3MultiHop(quoter, amountIn, pathBytes, legs) {
|
|
607
|
+
try {
|
|
608
|
+
const r = await quoter.quoteExactInput.staticCall(pathBytes, amountIn);
|
|
609
|
+
return r.amountOut;
|
|
610
|
+
}
|
|
611
|
+
catch {
|
|
612
|
+
let amount = amountIn;
|
|
613
|
+
for (const leg of legs) {
|
|
614
|
+
const [out] = await quoter.quoteExactInputSingle.staticCall({
|
|
615
|
+
tokenIn: leg.tokenIn,
|
|
616
|
+
tokenOut: leg.tokenOut,
|
|
617
|
+
fee: BigInt(leg.fee),
|
|
618
|
+
amountIn: amount,
|
|
619
|
+
sqrtPriceLimitX96: 0n,
|
|
620
|
+
});
|
|
621
|
+
amount = out;
|
|
622
|
+
}
|
|
623
|
+
return amount;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
async getTokensForStableQuote({ path, inputAmount, slippage = 500n, exchangeFactory, finalization, }) {
|
|
627
|
+
const adapter = this.getLotusAdapter(exchangeFactory);
|
|
628
|
+
const dexData = this.getDexData(exchangeFactory);
|
|
629
|
+
const swapFeePercentage = await this.getSwapFeePercentage();
|
|
630
|
+
if (dexData.type === 'v2') {
|
|
631
|
+
const deadline = Math.floor((Date.now() + 1000 * 60 * 5) / 1000);
|
|
632
|
+
if (dexData.kind === 'aerodrome') {
|
|
633
|
+
const v2Router = connect(dexData.router, AERO_V2_ROUTER_ABI, this.provider);
|
|
634
|
+
const routes = buildAerodromeV2Routes(path, exchangeFactory);
|
|
635
|
+
const amounts = await v2Router.getAmountsOut(inputAmount, routes);
|
|
636
|
+
const grossAmountOut = lastAmount(amounts, exchangeFactory);
|
|
637
|
+
const netAmountOut = grossAmountOut -
|
|
638
|
+
(grossAmountOut * swapFeePercentage) / PERCENT_DENOMINATOR;
|
|
639
|
+
const finalized = await this._finalizeExecutableQuote({
|
|
640
|
+
finalization,
|
|
641
|
+
adapter,
|
|
642
|
+
path,
|
|
643
|
+
amountToSend: inputAmount,
|
|
644
|
+
amountQuoted: netAmountOut,
|
|
645
|
+
initialTollAmount: 0n,
|
|
646
|
+
outputTollE3: swapFeePercentage,
|
|
647
|
+
slippage,
|
|
648
|
+
buildSwap: (amountToReceive) => {
|
|
649
|
+
const data = {
|
|
650
|
+
amountToSend: inputAmount,
|
|
651
|
+
amountToReceive,
|
|
652
|
+
routes,
|
|
653
|
+
deadline,
|
|
654
|
+
};
|
|
655
|
+
return {
|
|
656
|
+
data,
|
|
657
|
+
callData: AerodromeV2Adapter__factory.createInterface().encodeFunctionData('executeSwap', [data]),
|
|
658
|
+
};
|
|
659
|
+
},
|
|
660
|
+
});
|
|
661
|
+
return {
|
|
662
|
+
adapter,
|
|
663
|
+
data: finalized.data,
|
|
664
|
+
callData: finalized.callData,
|
|
665
|
+
toll: { currency: path[2], amount: finalized.tollAmount },
|
|
666
|
+
effectiveSlippageBps: finalized.effectiveSlippageBps,
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
const v2Router = connect(dexData.router, V2_ROUTER_ABI, this.provider);
|
|
670
|
+
const amounts = await v2Router.getAmountsOut(inputAmount, path);
|
|
671
|
+
const grossAmountOut = lastAmount(amounts, exchangeFactory);
|
|
672
|
+
const netAmountOut = grossAmountOut -
|
|
673
|
+
(grossAmountOut * swapFeePercentage) / PERCENT_DENOMINATOR;
|
|
674
|
+
const finalized = await this._finalizeExecutableQuote({
|
|
675
|
+
finalization,
|
|
676
|
+
adapter,
|
|
677
|
+
path,
|
|
678
|
+
amountToSend: inputAmount,
|
|
679
|
+
amountQuoted: netAmountOut,
|
|
680
|
+
initialTollAmount: 0n,
|
|
681
|
+
outputTollE3: swapFeePercentage,
|
|
682
|
+
slippage,
|
|
683
|
+
buildSwap: (amountToReceive) => {
|
|
684
|
+
const data = {
|
|
685
|
+
amountToSend: inputAmount,
|
|
686
|
+
amountToReceive,
|
|
687
|
+
path,
|
|
688
|
+
deadline,
|
|
689
|
+
};
|
|
690
|
+
return {
|
|
691
|
+
data,
|
|
692
|
+
callData: UniswapV2Adapter__factory.createInterface().encodeFunctionData('executeSwap', [data]),
|
|
693
|
+
};
|
|
694
|
+
},
|
|
695
|
+
});
|
|
696
|
+
return {
|
|
697
|
+
adapter,
|
|
698
|
+
data: finalized.data,
|
|
699
|
+
callData: finalized.callData,
|
|
700
|
+
toll: { currency: path[2], amount: finalized.tollAmount },
|
|
701
|
+
effectiveSlippageBps: finalized.effectiveSlippageBps,
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
if (dexData.kind === 'aerodrome') {
|
|
705
|
+
throw new Error(`${PoolHelperError.EXCHANGE_INCOMPATIBLE}: Aerodrome V3 multi-hop not supported (factory=${exchangeFactory})`);
|
|
706
|
+
}
|
|
707
|
+
const v3Quoter = IPancakeQuoterV2__factory.connect(dexData.quoter, this.provider);
|
|
708
|
+
const { feeTier: tokenFeeTier } = await this.getUniswapCompatibleTokenPool(path[0], {
|
|
709
|
+
exchangeFactory,
|
|
710
|
+
});
|
|
711
|
+
const { feeTier: stableFeeTier } = await this.getUniswapCompatibleTokenPool(path[2], {
|
|
712
|
+
exchangeFactory,
|
|
713
|
+
});
|
|
714
|
+
const pathBytes = solidityPacked(['address', 'uint24', 'address', 'uint24', 'address'], [path[0], tokenFeeTier, path[1], stableFeeTier, path[2]]);
|
|
715
|
+
const amountOut = await this._quoteV3MultiHop(v3Quoter, inputAmount, pathBytes, [
|
|
716
|
+
{ tokenIn: path[0], tokenOut: path[1], fee: tokenFeeTier },
|
|
717
|
+
{ tokenIn: path[1], tokenOut: path[2], fee: stableFeeTier },
|
|
718
|
+
]);
|
|
719
|
+
const deadline = Math.floor((Date.now() + 1000 * 60 * 5) / 1000);
|
|
720
|
+
const netAmountOut = amountOut - (amountOut * swapFeePercentage) / PERCENT_DENOMINATOR;
|
|
721
|
+
const finalized = await this._finalizeExecutableQuote({
|
|
722
|
+
finalization,
|
|
723
|
+
adapter,
|
|
724
|
+
path: [
|
|
725
|
+
{
|
|
726
|
+
tokenIn: path[0],
|
|
727
|
+
tokenOut: path[1],
|
|
728
|
+
fee: String(tokenFeeTier),
|
|
729
|
+
},
|
|
730
|
+
{
|
|
731
|
+
tokenIn: path[1],
|
|
732
|
+
tokenOut: path[2],
|
|
733
|
+
fee: String(stableFeeTier),
|
|
734
|
+
},
|
|
735
|
+
],
|
|
736
|
+
amountToSend: inputAmount,
|
|
737
|
+
amountQuoted: netAmountOut,
|
|
738
|
+
initialTollAmount: 0n,
|
|
739
|
+
outputTollE3: swapFeePercentage,
|
|
740
|
+
slippage,
|
|
741
|
+
buildSwap: (amountToReceive) => {
|
|
742
|
+
const data = {
|
|
743
|
+
amountToSend: inputAmount,
|
|
744
|
+
amountToReceive,
|
|
745
|
+
path: pathBytes,
|
|
746
|
+
deadline,
|
|
747
|
+
};
|
|
748
|
+
return {
|
|
749
|
+
data,
|
|
750
|
+
callData: UniswapV3Adapter__factory.createInterface().encodeFunctionData('executeSwap', [data]),
|
|
751
|
+
};
|
|
752
|
+
},
|
|
753
|
+
});
|
|
754
|
+
return {
|
|
755
|
+
adapter,
|
|
756
|
+
data: finalized.data,
|
|
757
|
+
callData: finalized.callData,
|
|
758
|
+
toll: { currency: path[2], amount: finalized.tollAmount },
|
|
759
|
+
effectiveSlippageBps: finalized.effectiveSlippageBps,
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
getLotusAdapter(exchangeFactory) {
|
|
763
|
+
const factoryLower = exchangeFactory.toLowerCase();
|
|
764
|
+
const f = this.addresses.factories;
|
|
765
|
+
const a = this.addresses.adapters;
|
|
766
|
+
const adapter = [
|
|
767
|
+
[f.uniswapV2, a.uniswapV2],
|
|
768
|
+
[f.uniswapV3, a.uniswapV3],
|
|
769
|
+
[f.pancakeV2, a.pancakeV2],
|
|
770
|
+
[f.pancakeV3, a.pancakeV3],
|
|
771
|
+
[f.aerodromeV2, a.aerodromeV2],
|
|
772
|
+
[f.aerodromeV3, a.aerodromeV3],
|
|
773
|
+
[f.aerodromeV3Bis, a.aerodromeV3],
|
|
774
|
+
].find(([factory]) => factory && factory.toLowerCase() === factoryLower)?.[1];
|
|
775
|
+
if (!adapter) {
|
|
776
|
+
throw new Error(`${PoolHelperError.LOTUS_ADAPTER_MISSING}: factory=${exchangeFactory} chainId=${this.chainId}`);
|
|
777
|
+
}
|
|
778
|
+
return adapter;
|
|
779
|
+
}
|
|
780
|
+
_adjustTokenAmountForSwapFee(tokenAmount, swapFeePercentage, swapType, path) {
|
|
781
|
+
const weth = this.addresses.tokens.WETH;
|
|
782
|
+
const shouldAdjust = compareAddresses(path[Number(swapType)], weth);
|
|
783
|
+
if (!shouldAdjust) {
|
|
784
|
+
return { adjustedTokenAmount: tokenAmount, swapFee: 0n };
|
|
785
|
+
}
|
|
786
|
+
const swapFee = (tokenAmount * swapFeePercentage) / PERCENT_DENOMINATOR;
|
|
787
|
+
const adjustedTokenAmount = swapType === SwapType.EXACT_INPUT
|
|
788
|
+
? tokenAmount - swapFee
|
|
789
|
+
: tokenAmount + swapFee;
|
|
790
|
+
return { adjustedTokenAmount, swapFee };
|
|
791
|
+
}
|
|
792
|
+
_adjustQuoteForSwapFeeAndSlippage(quote, swapFeePercentage, slippage, swapType, path) {
|
|
793
|
+
const weth = this.addresses.tokens.WETH;
|
|
794
|
+
const sign = swapType === SwapType.EXACT_INPUT ? -1n : 1n;
|
|
795
|
+
const slippageTolerance = calcSlippageTolerance(quote, slippage) * sign;
|
|
796
|
+
const quoteWithSlippage = quote + slippageTolerance;
|
|
797
|
+
const shouldAdjustQuote = !compareAddresses(path[Number(swapType)], weth);
|
|
798
|
+
if (!shouldAdjustQuote) {
|
|
799
|
+
return { adjustedQuote: quoteWithSlippage, swapFee: 0n };
|
|
800
|
+
}
|
|
801
|
+
const swapFee = (quote * swapFeePercentage) / PERCENT_DENOMINATOR;
|
|
802
|
+
const adjustedQuote = swapType === SwapType.EXACT_INPUT
|
|
803
|
+
? quoteWithSlippage - swapFee
|
|
804
|
+
: quoteWithSlippage + swapFee;
|
|
805
|
+
return { adjustedQuote, swapFee };
|
|
806
|
+
}
|
|
807
|
+
}
|