@paraswap/dex-lib 4.0.0 → 4.0.1-balancer-v3-cables.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/.vscode/launch.json +53 -0
- package/.vscode/settings.json +2 -0
- package/.vscode/tasks.json +15 -0
- package/build/abi/balancer-v3/batch-router.json +989 -0
- package/build/abi/balancer-v3/router.json +1792 -0
- package/build/abi/balancer-v3/vault-extension.json +2834 -0
- package/build/abi/permit2.json +581 -0
- package/build/abi/uniswap-v3/AlienBaseV3Router.abi.json +52 -0
- package/build/abi/usual-m-smart-m/usualM.abi.json +483 -0
- package/build/abi/usual-m-usd0/usualCollateralDao.abi.json +681 -0
- package/build/abi/{angle-transmuter/ChainlinkAccessControlledOffchainAggregator.json → wombat/pool.json} +637 -594
- package/build/bigint-constants.d.ts +1 -0
- package/build/bigint-constants.js +2 -1
- package/build/bigint-constants.js.map +1 -1
- package/build/config.js +28 -0
- package/build/config.js.map +1 -1
- package/build/constants.d.ts +2 -0
- package/build/constants.js +4 -1
- package/build/constants.js.map +1 -1
- package/build/dex/aave-v1/aave-v1.js +1 -0
- package/build/dex/aave-v1/aave-v1.js.map +1 -1
- package/build/dex/aave-v2/tokens-avalanche.json +44 -0
- package/build/dex/aave-v2/tokens-mainnet.json +188 -0
- package/build/dex/aave-v2/tokens-polygon.json +44 -0
- package/build/dex/augustus-approvals.d.ts +3 -2
- package/build/dex/augustus-approvals.js +35 -16
- package/build/dex/augustus-approvals.js.map +1 -1
- package/build/dex/bProtocol.d.ts +18 -0
- package/build/dex/bProtocol.js +39 -0
- package/build/dex/bProtocol.js.map +1 -0
- package/build/dex/balancer-v3/abi/balancerBatchRouter.d.ts +729 -0
- package/build/dex/balancer-v3/abi/balancerBatchRouter.js +732 -0
- package/build/dex/balancer-v3/abi/balancerBatchRouter.js.map +1 -0
- package/build/dex/balancer-v3/abi/balancerRouter.d.ts +1406 -0
- package/build/dex/balancer-v3/abi/balancerRouter.js +1175 -0
- package/build/dex/balancer-v3/abi/balancerRouter.js.map +1 -0
- package/build/dex/balancer-v3/abi/vaultExtension.V3.d.ts +2081 -0
- package/build/dex/balancer-v3/abi/vaultExtension.V3.js +1851 -0
- package/build/dex/balancer-v3/abi/vaultExtension.V3.js.map +1 -0
- package/build/dex/balancer-v3/balancer-v3-pool.d.ts +80 -0
- package/build/dex/balancer-v3/balancer-v3-pool.js +469 -0
- package/build/dex/balancer-v3/balancer-v3-pool.js.map +1 -0
- package/build/dex/balancer-v3/balancer-v3-sepolia-pools.d.ts +34 -0
- package/build/dex/balancer-v3/balancer-v3-sepolia-pools.js +406 -0
- package/build/dex/balancer-v3/balancer-v3-sepolia-pools.js.map +1 -0
- package/build/dex/balancer-v3/balancer-v3.d.ts +56 -0
- package/build/dex/balancer-v3/balancer-v3.js +412 -0
- package/build/dex/balancer-v3/balancer-v3.js.map +1 -0
- package/build/dex/balancer-v3/config.d.ts +9 -0
- package/build/dex/balancer-v3/config.js +45 -0
- package/build/dex/balancer-v3/config.js.map +1 -0
- package/build/dex/balancer-v3/getGasCost.d.ts +2 -0
- package/build/dex/balancer-v3/getGasCost.js +40 -0
- package/build/dex/balancer-v3/getGasCost.js.map +1 -0
- package/build/dex/balancer-v3/getOnChainState.d.ts +10 -0
- package/build/dex/balancer-v3/getOnChainState.js +163 -0
- package/build/dex/balancer-v3/getOnChainState.js.map +1 -0
- package/build/dex/balancer-v3/getPoolsApi.d.ts +2 -0
- package/build/dex/balancer-v3/getPoolsApi.js +84 -0
- package/build/dex/balancer-v3/getPoolsApi.js.map +1 -0
- package/build/dex/balancer-v3/getTopPoolsApi.d.ts +17 -0
- package/build/dex/balancer-v3/getTopPoolsApi.js +65 -0
- package/build/dex/balancer-v3/getTopPoolsApi.js.map +1 -0
- package/build/dex/balancer-v3/optimizer.d.ts +2 -0
- package/build/dex/balancer-v3/optimizer.js +47 -0
- package/build/dex/balancer-v3/optimizer.js.map +1 -0
- package/build/dex/balancer-v3/stablePool.d.ts +7 -0
- package/build/dex/balancer-v3/stablePool.js +73 -0
- package/build/dex/balancer-v3/stablePool.js.map +1 -0
- package/build/dex/balancer-v3/types.d.ts +62 -0
- package/build/dex/balancer-v3/types.js +3 -0
- package/build/dex/balancer-v3/types.js.map +1 -0
- package/build/dex/balancer-v3/weightedPool.d.ts +1 -0
- package/build/dex/balancer-v3/weightedPool.js +6 -0
- package/build/dex/balancer-v3/weightedPool.js.map +1 -0
- package/build/dex/bancor.d.ts +26 -0
- package/build/dex/bancor.js +58 -0
- package/build/dex/bancor.js.map +1 -0
- package/build/dex/cables/cables.d.ts +7 -5
- package/build/dex/cables/cables.js +229 -86
- package/build/dex/cables/cables.js.map +1 -1
- package/build/dex/cables/constants.d.ts +13 -12
- package/build/dex/cables/constants.js +14 -16
- package/build/dex/cables/constants.js.map +1 -1
- package/build/dex/cables/types.d.ts +7 -0
- package/build/dex/cables/types.js +8 -1
- package/build/dex/cables/types.js.map +1 -1
- package/build/dex/compound.d.ts +18 -0
- package/build/dex/compound.js +46 -0
- package/build/dex/compound.js.map +1 -0
- package/build/dex/curve-v2.d.ts +58 -0
- package/build/dex/curve-v2.js +180 -0
- package/build/dex/curve-v2.js.map +1 -0
- package/build/dex/dodo-v1.d.ts +33 -0
- package/build/dex/dodo-v1.js +63 -0
- package/build/dex/dodo-v1.js.map +1 -0
- package/build/dex/fluid-dex/fluid-dex-pool.d.ts +47 -0
- package/build/dex/fluid-dex/fluid-dex-pool.js +119 -0
- package/build/dex/fluid-dex/fluid-dex-pool.js.map +1 -0
- package/build/dex/inception/inception-event-pool.d.ts +4 -4
- package/build/dex/inception/inception-event-pool.js +7 -9
- package/build/dex/inception/inception-event-pool.js.map +1 -1
- package/build/dex/inception/inception.d.ts +6 -6
- package/build/dex/inception/inception.js +16 -40
- package/build/dex/inception/inception.js.map +1 -1
- package/build/dex/inception/utils.d.ts +1 -0
- package/build/dex/inception/utils.js +22 -1
- package/build/dex/inception/utils.js.map +1 -1
- package/build/dex/index.js +4 -0
- package/build/dex/index.js.map +1 -1
- package/build/dex/lido.d.ts +19 -0
- package/build/dex/lido.js +42 -0
- package/build/dex/lido.js.map +1 -0
- package/build/dex/maker-psm/scripts/gem.abi.json +136 -0
- package/build/dex/maker-psm/scripts/validate-state.d.ts +0 -0
- package/build/dex/maker-psm/scripts/validate-state.js +185 -0
- package/build/dex/maker-psm/scripts/validate-state.js.map +1 -0
- package/build/dex/maker-psm/scripts/vat.abi.json +243 -0
- package/build/dex/onebit.d.ts +25 -0
- package/build/dex/onebit.js +42 -0
- package/build/dex/onebit.js.map +1 -0
- package/build/dex/platypus/platypus.js +17 -3
- package/build/dex/platypus/platypus.js.map +1 -1
- package/build/dex/smoothy.d.ts +26 -0
- package/build/dex/smoothy.js +48 -0
- package/build/dex/smoothy.js.map +1 -0
- package/build/dex/solidly-v3/scripts/check-event-pool-event.d.ts +1 -0
- package/build/dex/solidly-v3/scripts/check-event-pool-event.js +53 -0
- package/build/dex/solidly-v3/scripts/check-event-pool-event.js.map +1 -0
- package/build/dex/stable-pool.d.ts +28 -0
- package/build/dex/stable-pool.js +55 -0
- package/build/dex/stable-pool.js.map +1 -0
- package/build/dex/swaap-v1/swaap-v1.js +4 -0
- package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
- package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.d.ts +7 -0
- package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.js +20 -0
- package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.js.map +1 -0
- package/build/dex/trader-joe-v2.1.d.ts +43 -0
- package/build/dex/trader-joe-v2.1.js +79 -0
- package/build/dex/trader-joe-v2.1.js.map +1 -0
- package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.d.ts +17 -0
- package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js +64 -0
- package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js.map +1 -0
- package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.d.ts +17 -0
- package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js +79 -0
- package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js.map +1 -0
- package/build/dex/uniswap-v3/uniswap-v3-pricing.d.ts +4 -0
- package/build/dex/uniswap-v3/uniswap-v3-pricing.js +43 -0
- package/build/dex/uniswap-v3/uniswap-v3-pricing.js.map +1 -0
- package/build/dex/uniswap-v3/uniswap-v3-worker.d.ts +1 -0
- package/build/dex/uniswap-v3/uniswap-v3-worker.js +10 -0
- package/build/dex/uniswap-v3/uniswap-v3-worker.js.map +1 -0
- package/build/dex/usual/types.d.ts +13 -0
- package/build/dex/usual/types.js +3 -0
- package/build/dex/usual/types.js.map +1 -0
- package/build/dex/usual/usual-bond.d.ts +18 -0
- package/build/dex/usual/usual-bond.js +52 -0
- package/build/dex/usual/usual-bond.js.map +1 -0
- package/build/dex/usual/usual-m-smart-m.d.ts +17 -0
- package/build/dex/usual/usual-m-smart-m.js +50 -0
- package/build/dex/usual/usual-m-smart-m.js.map +1 -0
- package/build/dex/usual/usual-m-usd0.d.ts +17 -0
- package/build/dex/usual/usual-m-usd0.js +51 -0
- package/build/dex/usual/usual-m-usd0.js.map +1 -0
- package/build/dex/usual/usual.d.ts +27 -0
- package/build/dex/usual/usual.js +125 -0
- package/build/dex/usual/usual.js.map +1 -0
- package/build/dex/usual-m-smart-m/config.d.ts +3 -0
- package/build/dex/usual-m-smart-m/config.js +13 -0
- package/build/dex/usual-m-smart-m/config.js.map +1 -0
- package/build/dex/usual-m-smart-m/types.d.ts +7 -0
- package/build/dex/usual-m-smart-m/types.js +3 -0
- package/build/dex/usual-m-smart-m/types.js.map +1 -0
- package/build/dex/usual-m-smart-m/usual-m-smart-m.d.ts +35 -0
- package/build/dex/usual-m-smart-m/usual-m-smart-m.js +161 -0
- package/build/dex/usual-m-smart-m/usual-m-smart-m.js.map +1 -0
- package/build/dex/usual-m-usd0/config.d.ts +3 -0
- package/build/dex/usual-m-usd0/config.js +14 -0
- package/build/dex/usual-m-usd0/config.js.map +1 -0
- package/build/dex/usual-m-usd0/types.d.ts +8 -0
- package/build/dex/usual-m-usd0/types.js +3 -0
- package/build/dex/usual-m-usd0/types.js.map +1 -0
- package/build/dex/usual-m-usd0/usual-m-usd0.d.ts +35 -0
- package/build/dex/usual-m-usd0/usual-m-usd0.js +159 -0
- package/build/dex/usual-m-usd0/usual-m-usd0.js.map +1 -0
- package/build/dex/weth/config.js +3 -0
- package/build/dex/weth/config.js.map +1 -1
- package/build/executor/Executor01BytecodeBuilder.js +2 -2
- package/build/executor/Executor01BytecodeBuilder.js.map +1 -1
- package/build/executor/Executor02BytecodeBuilder.js +2 -2
- package/build/executor/Executor02BytecodeBuilder.js.map +1 -1
- package/build/executor/Executor03BytecodeBuilder.js +2 -2
- package/build/executor/Executor03BytecodeBuilder.js.map +1 -1
- package/build/executor/ExecutorBytecodeBuilder.d.ts +3 -1
- package/build/executor/ExecutorBytecodeBuilder.js +35 -2
- package/build/executor/ExecutorBytecodeBuilder.js.map +1 -1
- package/build/generic-swap-transaction-builder.js +5 -1
- package/build/generic-swap-transaction-builder.js.map +1 -1
- package/build/types.d.ts +1 -0
- package/package.json +2 -1
- package/scripts/run-dex-integration-v3.ts +78 -0
- package/src/abi/balancer-v3/batch-router.json +989 -0
- package/src/abi/balancer-v3/router.json +1792 -0
- package/src/abi/balancer-v3/vault-extension.json +2834 -0
- package/src/abi/permit2.json +581 -0
- package/src/bigint-constants.ts +1 -0
- package/src/config.ts +29 -0
- package/src/constants.ts +4 -0
- package/src/dex/augustus-approvals.ts +52 -19
- package/src/dex/balancer-v3/balancer-v3-e2e.test.ts +275 -0
- package/src/dex/balancer-v3/balancer-v3-events.test.ts +223 -0
- package/src/dex/balancer-v3/balancer-v3-integration.test.ts +879 -0
- package/src/dex/balancer-v3/balancer-v3-pool.ts +656 -0
- package/src/dex/balancer-v3/balancer-v3.ts +538 -0
- package/src/dex/balancer-v3/config.ts +46 -0
- package/src/dex/balancer-v3/getGasCost.ts +37 -0
- package/src/dex/balancer-v3/getOnChainState.ts +326 -0
- package/src/dex/balancer-v3/getPoolsApi.ts +129 -0
- package/src/dex/balancer-v3/getTopPoolsApi.ts +96 -0
- package/src/dex/balancer-v3/optimizer.ts +66 -0
- package/src/dex/balancer-v3/stablePool.ts +78 -0
- package/src/dex/balancer-v3/types.ts +86 -0
- package/src/dex/balancer-v3/weightedPool.ts +2 -0
- package/src/dex/index.ts +4 -0
- package/src/dex/weth/config.ts +3 -0
- package/src/executor/Executor01BytecodeBuilder.ts +2 -0
- package/src/executor/Executor02BytecodeBuilder.ts +2 -0
- package/src/executor/Executor03BytecodeBuilder.ts +2 -0
- package/src/executor/ExecutorBytecodeBuilder.ts +63 -2
- package/src/generic-swap-transaction-builder.ts +6 -2
- package/src/types.ts +1 -0
- package/tests/constants-e2e.ts +66 -0
- package/build/abi/Aave_GSM.json +0 -751
- package/build/abi/Balancer.json +0 -257
- package/build/abi/BalancerV2.json +0 -1179
- package/build/abi/RamsesV2Pool.abi.json +0 -1554
- package/build/abi/RamsesV2Quoter.abi.json +0 -274
- package/build/abi/USDM.json +0 -807
- package/build/abi/UniswapV2Router.json +0 -397
- package/build/abi/angle-transmuter/ChainlinkEACAggregatorProxy.json +0 -509
- package/build/abi/curve-v1/CurveBUSDv2.abi.json +0 -1356
- package/build/abi/curve-v1/CurveTokenV1.json +0 -305
- package/build/abi/curve-v1/CurveTokenV2.json +0 -318
- package/build/abi/curve-v1/CurveV1StableNg.json +0 -1452
- package/build/abi/curve-v1/CurveV2Swap.json +0 -700
- package/build/abi/curve-v1/FactoryCryptoRegistry.json +0 -316
- package/build/abi/curve-v1/FactoryRegistry.json +0 -508
- package/build/abi/curve-v1/StableSwapAave.json +0 -1152
- package/build/abi/curve-v1/StableSwapBUSD.json +0 -814
- package/build/abi/curve-v1/StableSwapCompound.json +0 -814
- package/build/abi/curve-v1/StableSwapIDLE.json +0 -1253
- package/build/abi/curve-v1/StableSwapPax.json +0 -865
- package/build/abi/curve-v1/StableSwapSnow.json +0 -900
- package/build/abi/curve-v1/StableSwapUSDN.json +0 -1004
- package/build/abi/curve-v1/StableSwapUSDT.json +0 -814
- package/build/abi/curve-v1/StableSwapY.json +0 -814
- package/build/abi/inception/inception-ineth.json +0 -609
- package/build/abi/kyberdmm/kyber-storage.abi.json +0 -824
- package/build/abi/kyberdmm.abi.json +0 -1148
- package/build/abi/maverick-v2/MaverickV2Quoter.json +0 -278
- package/build/abi/nerve/nerve-metapool.json +0 -790
- package/build/abi/uniswap-v3/UniswapV3Quoter.abi.json +0 -193
- package/build/abi/velodrome-slipstream/VelodromeSlipstreamQuoterV2.abi.json +0 -267
- package/build/dex/balancer-v1/balancer-pools.json +0 -33333
- package/build/dex/bebop/websocket-fetcher.d.ts +0 -35
- package/build/dex/bebop/websocket-fetcher.js +0 -87
- package/build/dex/bebop/websocket-fetcher.js.map +0 -1
- package/build/dex/idle-dao/idle-dao-pool.d.ts +0 -56
- package/build/dex/idle-dao/idle-dao-pool.js +0 -176
- package/build/dex/idle-dao/idle-dao-pool.js.map +0 -1
- package/build/dex/idle-dao/idle-dao-pooling-pool.d.ts +0 -16
- package/build/dex/idle-dao/idle-dao-pooling-pool.js +0 -57
- package/build/dex/idle-dao/idle-dao-pooling-pool.js.map +0 -1
- package/build/dex/idle-dao/scripts.d.ts +0 -9
- package/build/dex/idle-dao/scripts.js +0 -552
- package/build/dex/idle-dao/scripts.js.map +0 -1
- package/build/dex/inception/inception-native.d.ts +0 -43
- package/build/dex/inception/inception-native.js +0 -131
- package/build/dex/inception/inception-native.js.map +0 -1
- package/build/dex/inception/inception-pool.d.ts +0 -18
- package/build/dex/inception/inception-pool.js +0 -32
- package/build/dex/inception/inception-pool.js.map +0 -1
- package/build/dex/inception/inception-price-feed.d.ts +0 -19
- package/build/dex/inception/inception-price-feed.js +0 -51
- package/build/dex/inception/inception-price-feed.js.map +0 -1
- package/build/executor/fixtures/executor01/exchange-params/price-route-multiswap-balancerv1-curvev1-eth-usdc-gusd.json +0 -16
- package/build/executor/fixtures/executor01/exchange-params/price-route-multiswap-balancerv1-sushiv3-wbtc-eth-sushi.json +0 -16
- package/build/executor/fixtures/executor01/exchange-params/price-route-multiswap-sushiv3-curvev1-eth-usdc-gusd.json +0 -16
- package/build/executor/fixtures/executor01/exchange-params/price-route-multiswap-sushiv3-curvev1-gusd-usdc-eth.json +0 -16
- package/build/executor/fixtures/executor01/exchange-params/price-route-multiswap-sushiv3-dai-usdc-eth.json +0 -16
- package/build/executor/fixtures/executor01/exchange-params/price-route-multiswap-sushiv3-eth-usdc-dai.json +0 -16
- package/build/executor/fixtures/executor01/exchange-params/price-route-multiswap-sushiv3-usdc-eth-wbtc.json +0 -16
- package/build/executor/fixtures/executor01/exchange-params/price-route-multiswap-sushiv3-usdt-usdc-dai.json +0 -16
- package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-balancerv1-eth-usdc.json +0 -9
- package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-balancerv1-usdc-eth.json +0 -9
- package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-balancerv1-usdc-wbtc.json +0 -9
- package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-curvev1-eth-steth.json +0 -9
- package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-curvev1-steth-eth.json +0 -9
- package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-curvev1-usdc-usdt.json +0 -9
- package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-univ3-eth-usdc.json +0 -9
- package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-univ3-usdc-eth.json +0 -9
- package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-univ3-usdc-usdt.json +0 -9
- package/build/executor/fixtures/executor01/maybe-weth-calldata/price-route-multiswap-balancerv1-sushiv3-wbtc-eth-sushi.json +0 -12
- package/build/executor/fixtures/executor01/maybe-weth-calldata/price-route-multiswap-sushiv3-curvev1-eth-usdc-gusd.json +0 -7
- package/build/executor/fixtures/executor01/maybe-weth-calldata/price-route-multiswap-sushiv3-curvev1-gusd-usdc-eth.json +0 -7
- package/build/executor/fixtures/executor01/maybe-weth-calldata/price-route-multiswap-sushiv3-dai-usdc-eth.json +0 -7
- package/build/executor/fixtures/executor01/maybe-weth-calldata/price-route-multiswap-sushiv3-eth-usdc-dai.json +0 -7
- package/build/executor/fixtures/executor01/maybe-weth-calldata/price-route-multiswap-sushiv3-usdc-eth-wbtc.json +0 -12
- package/build/executor/fixtures/executor01/maybe-weth-calldata/price-route-simpleSwap-univ3-eth-usdc.json +0 -7
- package/build/executor/fixtures/executor01/maybe-weth-calldata/price-route-simpleSwap-univ3-usdc-eth.json +0 -7
- package/build/executor/fixtures/executor01/routes/price-route-multiswap-balancerv1-curvev1-eth-usdc-gusd.json +0 -78
- package/build/executor/fixtures/executor01/routes/price-route-multiswap-balancerv1-sushiv3-wbtc-eth-sushi.json +0 -81
- package/build/executor/fixtures/executor01/routes/price-route-multiswap-sushiv3-curvev1-eth-usdc-gusd.json +0 -78
- package/build/executor/fixtures/executor01/routes/price-route-multiswap-sushiv3-curvev1-gusd-usdc-eth.json +0 -78
- package/build/executor/fixtures/executor01/routes/price-route-multiswap-sushiv3-dai-usdc-eth.json +0 -81
- package/build/executor/fixtures/executor01/routes/price-route-multiswap-sushiv3-eth-usdc-dai.json +0 -81
- package/build/executor/fixtures/executor01/routes/price-route-multiswap-sushiv3-usdc-eth-wbtc.json +0 -81
- package/build/executor/fixtures/executor01/routes/price-route-multiswap-sushiv3-usdt-usdc-dai.json +0 -81
- package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-balancerv1-eth-usdc.json +0 -64
- package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-balancerv1-usdc-eth.json +0 -85
- package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-balancerv1-usdc-wbtc.json +0 -64
- package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-curvev1-eth-steth.json +0 -52
- package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-curvev1-steth-eth.json +0 -52
- package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-curvev1-usdc-usdt.json +0 -52
- package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-univ3-eth-usdc.json +0 -55
- package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-univ3-usdc-eth.json +0 -55
- package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-univ3-usdc-usdt.json +0 -55
- package/build/executor/fixtures/executor02/exchange-params/price-route-multiSwap-balancerv1-sushiv3-univ3-bal-eth-sushi.json +0 -23
- package/build/executor/fixtures/executor02/exchange-params/price-route-multiSwap-maverickv1-sushiv3-univ3-eth-usdc-mav.json +0 -37
- package/build/executor/fixtures/executor02/exchange-params/price-route-multiSwap-univ3-sushiv3-sushi-eth-wbtc.json +0 -23
- package/build/executor/fixtures/executor02/exchange-params/price-route-multiSwap-univ3-sushiv3-wbtc-eth-sushi.json +0 -23
- package/build/executor/fixtures/executor02/exchange-params/price-route-multiswap-curvev1-univ3-dai-usdc-eth.json +0 -51
- package/build/executor/fixtures/executor02/exchange-params/price-route-multiswap-univ3-usdt-dai-eth.json +0 -58
- package/build/executor/fixtures/executor02/exchange-params/price-route-simpleSwap-sushiv3-balancerv1-eth-usdc.json +0 -23
- package/build/executor/fixtures/executor02/exchange-params/price-route-simpleSwap-sushiv3-balancerv1-usdc-eth.json +0 -23
- package/build/executor/fixtures/executor02/exchange-params/price-route-simpleSwap-sushiv3-univ3-eth-sushi.json +0 -9
- package/build/executor/fixtures/executor02/exchange-params/price-route-simpleSwap-sushiv3-univ3-sushi-eth.json +0 -16
- package/build/executor/fixtures/executor02/exchange-params/price-route-simpleSwap-univ3-curvev1-usdt-dai.json +0 -23
- package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-multiSwap-balancerv1-sushiv3-univ3-bal-eth-sushi.json +0 -12
- package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-multiSwap-maverickv1-sushiv3-univ3-eth-usdc-mav.json +0 -7
- package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-multiSwap-univ3-sushiv3-sushi-eth-wbtc.json +0 -12
- package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-multiSwap-univ3-sushiv3-wbtc-eth-sushi.json +0 -12
- package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-multiswap-curvev1-univ3-dai-usdc-eth.json +0 -7
- package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-multiswap-univ3-usdt-dai-eth.json +0 -7
- package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-simpleSwap-sushiv3-balancerv1-eth-usdc.json +0 -7
- package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-simpleSwap-sushiv3-balancerv1-usdc-eth.json +0 -7
- package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-simpleSwap-sushiv3-univ3-eth-sushi.json +0 -7
- package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-simpleSwap-sushiv3-univ3-sushi-eth.json +0 -7
- package/build/executor/fixtures/executor02/routes/price-route-multiSwap-balancerv1-sushiv3-univ3-bal-eth-sushi.json +0 -130
- package/build/executor/fixtures/executor02/routes/price-route-multiSwap-maverickv1-sushiv3-univ3-eth-usdc-mav.json +0 -136
- package/build/executor/fixtures/executor02/routes/price-route-multiSwap-univ3-sushiv3-sushi-eth-wbtc.json +0 -100
- package/build/executor/fixtures/executor02/routes/price-route-multiSwap-univ3-sushiv3-wbtc-eth-sushi.json +0 -100
- package/build/executor/fixtures/executor02/routes/price-route-multiswap-curvev1-univ3-dai-usdc-eth.json +0 -166
- package/build/executor/fixtures/executor02/routes/price-route-multiswap-univ3-usdt-dai-eth.json +0 -190
- package/build/executor/fixtures/executor02/routes/price-route-simpleSwap-sushiv3-balancerv1-eth-usdc.json +0 -114
- package/build/executor/fixtures/executor02/routes/price-route-simpleSwap-sushiv3-balancerv1-usdc-eth.json +0 -114
- package/build/executor/fixtures/executor02/routes/price-route-simpleSwap-sushiv3-univ3-eth-sushi.json +0 -55
- package/build/executor/fixtures/executor02/routes/price-route-simpleSwap-sushiv3-univ3-sushi-eth.json +0 -73
- package/build/executor/fixtures/executor02/routes/price-route-simpleSwap-univ3-curvev1-usdt-dai.json +0 -82
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import { Interface, defaultAbiCoder } from '@ethersproject/abi';
|
|
3
|
+
import { DeepReadonly } from 'ts-essentials';
|
|
4
|
+
import { Log, Logger } from '../../types';
|
|
5
|
+
import { catchParseLogError } from '../../utils';
|
|
6
|
+
import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
|
|
7
|
+
import { IDexHelper } from '../../dex-helper/idex-helper';
|
|
8
|
+
import {
|
|
9
|
+
PoolState,
|
|
10
|
+
PoolStateMap,
|
|
11
|
+
StableMutableState,
|
|
12
|
+
Step,
|
|
13
|
+
TokenInfo,
|
|
14
|
+
} from './types';
|
|
15
|
+
import { getPoolsApi } from './getPoolsApi';
|
|
16
|
+
import vaultExtensionAbi_V3 from '../../abi/balancer-v3/vault-extension.json';
|
|
17
|
+
import { decodeThrowError, getOnChainState } from './getOnChainState';
|
|
18
|
+
import { BalancerV3Config } from './config';
|
|
19
|
+
import { SwapKind, Vault } from '@balancer-labs/balancer-maths';
|
|
20
|
+
import {
|
|
21
|
+
ampUpdateStartedEvent,
|
|
22
|
+
ampUpdateStoppedEvent,
|
|
23
|
+
getAmplificationParameter,
|
|
24
|
+
isStableMutableState,
|
|
25
|
+
} from './stablePool';
|
|
26
|
+
import { BI_POWS } from '../../bigint-constants';
|
|
27
|
+
|
|
28
|
+
const WAD = BI_POWS[18];
|
|
29
|
+
|
|
30
|
+
export class BalancerV3EventPool extends StatefulEventSubscriber<PoolStateMap> {
|
|
31
|
+
handlers: {
|
|
32
|
+
[event: string]: (
|
|
33
|
+
event: any,
|
|
34
|
+
state: DeepReadonly<PoolStateMap>,
|
|
35
|
+
log: Readonly<Log>,
|
|
36
|
+
) => DeepReadonly<PoolStateMap> | null;
|
|
37
|
+
} = {};
|
|
38
|
+
|
|
39
|
+
logDecoder: (log: Log) => any;
|
|
40
|
+
|
|
41
|
+
addressesSubscribed: string[];
|
|
42
|
+
|
|
43
|
+
interfaces: {
|
|
44
|
+
[name: string]: Interface;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
vault: Vault;
|
|
48
|
+
|
|
49
|
+
constructor(
|
|
50
|
+
readonly parentName: string,
|
|
51
|
+
protected network: number,
|
|
52
|
+
protected dexHelper: IDexHelper,
|
|
53
|
+
logger: Logger,
|
|
54
|
+
) {
|
|
55
|
+
super(
|
|
56
|
+
parentName,
|
|
57
|
+
BalancerV3Config.BalancerV3[network].vaultAddress,
|
|
58
|
+
dexHelper,
|
|
59
|
+
logger,
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
this.interfaces = {
|
|
63
|
+
['VAULT']: new Interface(vaultExtensionAbi_V3),
|
|
64
|
+
['STABLE']: new Interface([
|
|
65
|
+
'function getAmplificationParameter() external view returns (uint256 value, bool isUpdating, uint256 precision)',
|
|
66
|
+
'function getAmplificationState() external view returns (tuple(uint64 startValue, uint64 endValue, uint32 startTime, uint32 endTime) amplificationState, uint256 precision)',
|
|
67
|
+
]),
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
this.logDecoder = (log: Log) => this.interfaces['VAULT'].parseLog(log);
|
|
71
|
+
this.addressesSubscribed = [
|
|
72
|
+
BalancerV3Config.BalancerV3[network].vaultAddress,
|
|
73
|
+
];
|
|
74
|
+
|
|
75
|
+
// Add handlers
|
|
76
|
+
this.handlers['LiquidityAdded'] = this.liquidityAddedEvent.bind(this);
|
|
77
|
+
this.handlers['LiquidityRemoved'] = this.liquidityRemovedEvent.bind(this);
|
|
78
|
+
this.handlers['Swap'] = this.swapEvent.bind(this);
|
|
79
|
+
this.handlers['VaultAuxiliary'] = this.vaultAuxiliaryEvent.bind(this);
|
|
80
|
+
this.handlers['AggregateSwapFeePercentageChanged'] =
|
|
81
|
+
this.poolAggregateSwapFeePercentageEvent.bind(this);
|
|
82
|
+
this.handlers['SwapFeePercentageChanged'] =
|
|
83
|
+
this.poolSwapFeePercentageChangedEvent.bind(this);
|
|
84
|
+
this.handlers['PoolPausedStateChanged'] =
|
|
85
|
+
this.poolPausedStateChanged.bind(this);
|
|
86
|
+
|
|
87
|
+
// replicates V3 maths with fees, pool and hook logic
|
|
88
|
+
this.vault = new Vault();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The function is called every time any of the subscribed
|
|
93
|
+
* addresses release log. The function accepts the current
|
|
94
|
+
* state, updates the state according to the log, and returns
|
|
95
|
+
* the updated state.
|
|
96
|
+
* @param state - Current state of event subscriber
|
|
97
|
+
* @param log - Log released by one of the subscribed addresses
|
|
98
|
+
* @returns Updates state of the event subscriber after the log
|
|
99
|
+
*/
|
|
100
|
+
protected processLog(
|
|
101
|
+
state: DeepReadonly<PoolStateMap>,
|
|
102
|
+
log: Readonly<Log>,
|
|
103
|
+
): DeepReadonly<PoolStateMap> | null {
|
|
104
|
+
try {
|
|
105
|
+
const event = this.logDecoder(log);
|
|
106
|
+
if (event.name in this.handlers) {
|
|
107
|
+
return this.handlers[event.name](event, state, log);
|
|
108
|
+
}
|
|
109
|
+
} catch (e) {
|
|
110
|
+
catchParseLogError(e, this.logger);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The function generates state using on-chain calls. This
|
|
118
|
+
* function is called to regenerate state if the event based
|
|
119
|
+
* system fails to fetch events and the local state is no
|
|
120
|
+
* more correct.
|
|
121
|
+
* @param blockNumber - Blocknumber for which the state should
|
|
122
|
+
* should be generated
|
|
123
|
+
* @returns state of the event subscriber at blocknumber
|
|
124
|
+
*/
|
|
125
|
+
async generateState(
|
|
126
|
+
blockNumber: number,
|
|
127
|
+
): Promise<DeepReadonly<PoolStateMap>> {
|
|
128
|
+
const block = await this.dexHelper.provider.getBlock(blockNumber);
|
|
129
|
+
const apiPoolStateMap = await getPoolsApi(this.network, block.timestamp);
|
|
130
|
+
const allOnChainPools = await getOnChainState(
|
|
131
|
+
this.network,
|
|
132
|
+
apiPoolStateMap,
|
|
133
|
+
this.dexHelper,
|
|
134
|
+
this.interfaces,
|
|
135
|
+
blockNumber,
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
// Filter out all paused pools
|
|
139
|
+
const filteredPools = Object.entries(allOnChainPools)
|
|
140
|
+
.filter(([address, pool]) => {
|
|
141
|
+
return !pool.isPoolPaused;
|
|
142
|
+
})
|
|
143
|
+
.reduce((acc, [address, pool]) => {
|
|
144
|
+
acc[address] = pool;
|
|
145
|
+
return acc;
|
|
146
|
+
}, {} as PoolStateMap);
|
|
147
|
+
|
|
148
|
+
return filteredPools;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async getUpdatedPoolState(
|
|
152
|
+
existingPoolState: DeepReadonly<PoolStateMap>,
|
|
153
|
+
): Promise<DeepReadonly<PoolStateMap> | null> {
|
|
154
|
+
// Get all latest pools from API
|
|
155
|
+
const apiPoolStateMap = await getPoolsApi(this.network);
|
|
156
|
+
|
|
157
|
+
// Filter out pools that already exist in existing state
|
|
158
|
+
const newApiPools = Object.entries(apiPoolStateMap).reduce(
|
|
159
|
+
(acc, [address, pool]) => {
|
|
160
|
+
if (!existingPoolState[address]) {
|
|
161
|
+
acc[address] = pool;
|
|
162
|
+
}
|
|
163
|
+
return acc;
|
|
164
|
+
},
|
|
165
|
+
{} as typeof apiPoolStateMap,
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
// If no new pools return
|
|
169
|
+
if (Object.keys(newApiPools).length === 0) {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Only get on-chain state for new pools
|
|
174
|
+
const newOnChainPools = await getOnChainState(
|
|
175
|
+
this.network,
|
|
176
|
+
newApiPools,
|
|
177
|
+
this.dexHelper,
|
|
178
|
+
this.interfaces,
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
// Filter out pools with hooks and paused pools from new state
|
|
182
|
+
// TODO this won't be necessary once API has this filter option
|
|
183
|
+
const filteredNewPools = Object.entries(newOnChainPools)
|
|
184
|
+
.filter(([_, pool]) => !(pool.hasHook || pool.isPoolPaused))
|
|
185
|
+
.reduce((acc, [address, pool]) => {
|
|
186
|
+
acc[address] = pool;
|
|
187
|
+
return acc;
|
|
188
|
+
}, {} as PoolStateMap);
|
|
189
|
+
|
|
190
|
+
// Merge existing pools with new pools
|
|
191
|
+
return {
|
|
192
|
+
...existingPoolState,
|
|
193
|
+
...filteredNewPools,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
liquidityAddedEvent(
|
|
198
|
+
event: any,
|
|
199
|
+
state: DeepReadonly<PoolStateMap>,
|
|
200
|
+
log: Readonly<Log>,
|
|
201
|
+
): DeepReadonly<PoolStateMap> | null {
|
|
202
|
+
const poolAddress = event.args.pool.toLowerCase();
|
|
203
|
+
// Vault will send events from all pools, some of which are not officially supported by Balancer
|
|
204
|
+
if (!state[poolAddress]) {
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
const newState = _.cloneDeep(state) as PoolStateMap;
|
|
208
|
+
for (
|
|
209
|
+
let i = 0;
|
|
210
|
+
i < newState[poolAddress].balancesLiveScaled18.length;
|
|
211
|
+
i++
|
|
212
|
+
) {
|
|
213
|
+
newState[poolAddress].balancesLiveScaled18[i] += this.toScaled18(
|
|
214
|
+
BigInt(event.args.amountsAddedRaw[i]),
|
|
215
|
+
newState[poolAddress].scalingFactors[i],
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
newState[poolAddress].totalSupply = BigInt(event.args.totalSupply);
|
|
219
|
+
|
|
220
|
+
return newState;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
liquidityRemovedEvent(
|
|
224
|
+
event: any,
|
|
225
|
+
state: DeepReadonly<PoolStateMap>,
|
|
226
|
+
log: Readonly<Log>,
|
|
227
|
+
): DeepReadonly<PoolStateMap> | null {
|
|
228
|
+
const poolAddress = event.args.pool.toLowerCase();
|
|
229
|
+
// Vault will send events from all pools, some of which are not officially supported by Balancer
|
|
230
|
+
if (!state[poolAddress]) {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
const newState = _.cloneDeep(state) as PoolStateMap;
|
|
234
|
+
for (
|
|
235
|
+
let i = 0;
|
|
236
|
+
i < newState[poolAddress].balancesLiveScaled18.length;
|
|
237
|
+
i++
|
|
238
|
+
) {
|
|
239
|
+
newState[poolAddress].balancesLiveScaled18[i] -= this.toScaled18(
|
|
240
|
+
BigInt(event.args.amountsRemovedRaw[i]),
|
|
241
|
+
newState[poolAddress].scalingFactors[i],
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
newState[poolAddress].totalSupply = BigInt(event.args.totalSupply);
|
|
245
|
+
|
|
246
|
+
return newState;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
swapEvent(
|
|
250
|
+
event: any,
|
|
251
|
+
state: DeepReadonly<PoolStateMap>,
|
|
252
|
+
log: Readonly<Log>,
|
|
253
|
+
): DeepReadonly<PoolStateMap> | null {
|
|
254
|
+
const poolAddress = event.args.pool.toLowerCase();
|
|
255
|
+
// Vault will send events from all pools, some of which are not officially supported by Balancer
|
|
256
|
+
if (!state[poolAddress]) {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
const newState = _.cloneDeep(state) as PoolStateMap;
|
|
260
|
+
const tokenInIndex = newState[poolAddress].tokens.findIndex(
|
|
261
|
+
address => address.toLowerCase() === event.args.tokenIn.toLowerCase(),
|
|
262
|
+
);
|
|
263
|
+
const tokenOutIndex = newState[poolAddress].tokens.findIndex(
|
|
264
|
+
address => address.toLowerCase() === event.args.tokenOut.toLowerCase(),
|
|
265
|
+
);
|
|
266
|
+
if (tokenInIndex === -1 || tokenOutIndex === -1) {
|
|
267
|
+
this.logger.error(`swapEvent - token index not found in pool state`);
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
newState[poolAddress].balancesLiveScaled18[tokenInIndex] += this.toScaled18(
|
|
271
|
+
BigInt(event.args.amountIn),
|
|
272
|
+
newState[poolAddress].scalingFactors[tokenInIndex],
|
|
273
|
+
);
|
|
274
|
+
newState[poolAddress].balancesLiveScaled18[tokenOutIndex] -=
|
|
275
|
+
this.toScaled18(
|
|
276
|
+
BigInt(event.args.amountOut),
|
|
277
|
+
newState[poolAddress].scalingFactors[tokenOutIndex],
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
return newState;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
vaultAuxiliaryEvent(
|
|
284
|
+
event: any,
|
|
285
|
+
state: DeepReadonly<PoolStateMap>,
|
|
286
|
+
log: Readonly<Log>,
|
|
287
|
+
): DeepReadonly<PoolStateMap> | null {
|
|
288
|
+
// In SC Pools can use this event to emit event data from the Vault.
|
|
289
|
+
// Allows us to track pool specific events using only the Vault subscription.
|
|
290
|
+
// https://github.com/balancer/balancer-v3-monorepo/blob/main/pkg/interfaces/contracts/vault/IVaultExtension.sol
|
|
291
|
+
const poolAddress = event.args.pool.toLowerCase();
|
|
292
|
+
// Vault will send events from all pools, some of which are not officially supported by Balancer
|
|
293
|
+
if (!state[poolAddress]) {
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const newState = _.cloneDeep(state) as PoolStateMap;
|
|
298
|
+
switch (event.args.eventKey) {
|
|
299
|
+
case 'AmpUpdateStarted':
|
|
300
|
+
ampUpdateStartedEvent(newState[poolAddress], event.args.eventData);
|
|
301
|
+
return newState;
|
|
302
|
+
case 'AmpUpdateStopped':
|
|
303
|
+
ampUpdateStoppedEvent(newState[poolAddress], event.args.eventData);
|
|
304
|
+
return newState;
|
|
305
|
+
default:
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
poolAggregateSwapFeePercentageEvent(
|
|
311
|
+
event: any,
|
|
312
|
+
state: DeepReadonly<PoolStateMap>,
|
|
313
|
+
log: Readonly<Log>,
|
|
314
|
+
): DeepReadonly<PoolStateMap> | null {
|
|
315
|
+
const poolAddress = event.args.pool.toLowerCase();
|
|
316
|
+
// Vault will send events from all pools, some of which are not officially supported by Balancer
|
|
317
|
+
if (!state[poolAddress]) {
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
320
|
+
const newState = _.cloneDeep(state) as PoolStateMap;
|
|
321
|
+
newState[poolAddress].aggregateSwapFee = BigInt(
|
|
322
|
+
event.args.aggregateSwapFeePercentage,
|
|
323
|
+
);
|
|
324
|
+
return newState;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
poolSwapFeePercentageChangedEvent(
|
|
328
|
+
event: any,
|
|
329
|
+
state: DeepReadonly<PoolStateMap>,
|
|
330
|
+
log: Readonly<Log>,
|
|
331
|
+
): DeepReadonly<PoolStateMap> | null {
|
|
332
|
+
const poolAddress = event.args.pool.toLowerCase();
|
|
333
|
+
// Vault will send events from all pools, some of which are not officially supported by Balancer
|
|
334
|
+
if (!state[poolAddress]) {
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
337
|
+
const newState = _.cloneDeep(state) as PoolStateMap;
|
|
338
|
+
newState[poolAddress].swapFee = BigInt(event.args.swapFeePercentage);
|
|
339
|
+
return newState;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
poolPausedStateChanged(
|
|
343
|
+
event: any,
|
|
344
|
+
state: DeepReadonly<PoolStateMap>,
|
|
345
|
+
log: Readonly<Log>,
|
|
346
|
+
): DeepReadonly<PoolStateMap> | null {
|
|
347
|
+
// Unpaused pools will be added with correct state during updateStatePools
|
|
348
|
+
if (event.args.paused === false) return null;
|
|
349
|
+
const poolAddress = event.args.pool.toLowerCase();
|
|
350
|
+
// Vault will send events from all pools, some of which are not officially supported by Balancer
|
|
351
|
+
if (!state[poolAddress]) {
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
// Remove paused pool from state as it can't be swapped against
|
|
355
|
+
const newState = _.cloneDeep(state) as PoolStateMap;
|
|
356
|
+
delete newState[poolAddress];
|
|
357
|
+
return newState;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
getMaxSwapAmount(
|
|
361
|
+
pool: PoolState,
|
|
362
|
+
tokenIn: TokenInfo,
|
|
363
|
+
tokenOut: TokenInfo,
|
|
364
|
+
swapKind: SwapKind,
|
|
365
|
+
): bigint {
|
|
366
|
+
// Find the maximum swap amount the pool will support
|
|
367
|
+
const maxSwapAmount = this.vault.getMaxSwapAmount(
|
|
368
|
+
{
|
|
369
|
+
swapKind,
|
|
370
|
+
balancesLiveScaled18: pool.balancesLiveScaled18,
|
|
371
|
+
tokenRates: pool.tokenRates,
|
|
372
|
+
scalingFactors: pool.scalingFactors,
|
|
373
|
+
indexIn: tokenIn.index,
|
|
374
|
+
indexOut: tokenOut.index,
|
|
375
|
+
},
|
|
376
|
+
pool,
|
|
377
|
+
);
|
|
378
|
+
return maxSwapAmount;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
getSwapResult(
|
|
382
|
+
steps: Step[],
|
|
383
|
+
amountRaw: bigint,
|
|
384
|
+
swapKind: SwapKind,
|
|
385
|
+
timestamp: number,
|
|
386
|
+
): bigint {
|
|
387
|
+
if (amountRaw === 0n) return 0n;
|
|
388
|
+
|
|
389
|
+
// A GivenOut needs to use steps in reverse during calculation
|
|
390
|
+
const indices =
|
|
391
|
+
swapKind === SwapKind.GivenIn
|
|
392
|
+
? steps.keys()
|
|
393
|
+
: Array.from(steps.keys()).reverse();
|
|
394
|
+
|
|
395
|
+
let amount = amountRaw;
|
|
396
|
+
let outputAmountRaw = 0n;
|
|
397
|
+
for (const i of indices) {
|
|
398
|
+
const step = steps[i];
|
|
399
|
+
// If its a Stable Pool with an updating Amp factor calculate current Amp value
|
|
400
|
+
if (
|
|
401
|
+
step.poolState.poolType === 'STABLE' &&
|
|
402
|
+
isStableMutableState(step.poolState)
|
|
403
|
+
) {
|
|
404
|
+
if (step.poolState.ampIsUpdating) {
|
|
405
|
+
step.poolState.amp = getAmplificationParameter(
|
|
406
|
+
step.poolState.ampStartValue,
|
|
407
|
+
step.poolState.ampEndValue,
|
|
408
|
+
step.poolState.ampStartTime,
|
|
409
|
+
step.poolState.ampStopTime,
|
|
410
|
+
BigInt(timestamp),
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
outputAmountRaw = this.vault.swap(
|
|
415
|
+
{
|
|
416
|
+
...step.swapInput,
|
|
417
|
+
amountRaw: amount,
|
|
418
|
+
swapKind,
|
|
419
|
+
},
|
|
420
|
+
step.poolState,
|
|
421
|
+
);
|
|
422
|
+
// Next step uses output from previous step as input
|
|
423
|
+
amount = outputAmountRaw;
|
|
424
|
+
}
|
|
425
|
+
return outputAmountRaw;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Retrieves any new pools via API/multicall and adds to state
|
|
430
|
+
*/
|
|
431
|
+
async updateStatePools(): Promise<void> {
|
|
432
|
+
const blockNumber = await this.dexHelper.provider.getBlockNumber();
|
|
433
|
+
// We just want the current saved state
|
|
434
|
+
const currentState = this.getStaleState() || {};
|
|
435
|
+
const updatedPoolState = await this.getUpdatedPoolState(currentState);
|
|
436
|
+
if (updatedPoolState) this.setState(updatedPoolState, blockNumber);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Uses multicall to get onchain token rate for each pool then updates pool state
|
|
441
|
+
*/
|
|
442
|
+
async updateStatePoolRates(): Promise<void> {
|
|
443
|
+
// Get existing state
|
|
444
|
+
const poolState = _.cloneDeep(this.getStaleState()) as PoolStateMap;
|
|
445
|
+
if (!poolState) return;
|
|
446
|
+
|
|
447
|
+
// Fetch onchain pool rates
|
|
448
|
+
const poolRates = await this.getPoolRates(Object.keys(poolState));
|
|
449
|
+
|
|
450
|
+
// Update each pools rate
|
|
451
|
+
poolRates.forEach(({ poolAddress, tokenRates }, i) => {
|
|
452
|
+
poolState[poolAddress].tokenRates = tokenRates;
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
// Update state
|
|
456
|
+
const blockNumber = await this.dexHelper.provider.getBlockNumber();
|
|
457
|
+
this.setState(poolState, blockNumber);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
private async getPoolRates(poolAddresses: string[]) {
|
|
461
|
+
// For each pool make the getPoolTokenRates call
|
|
462
|
+
const multiCallData = poolAddresses.map(address => {
|
|
463
|
+
return {
|
|
464
|
+
target: BalancerV3Config.BalancerV3[this.network].vaultAddress,
|
|
465
|
+
callData: this.interfaces['VAULT'].encodeFunctionData(
|
|
466
|
+
'getPoolTokenRates',
|
|
467
|
+
[address],
|
|
468
|
+
),
|
|
469
|
+
};
|
|
470
|
+
});
|
|
471
|
+
// 500 is an arbitrary number chosen based on the blockGasLimit
|
|
472
|
+
const slicedMultiCallData = _.chunk(multiCallData, 500);
|
|
473
|
+
|
|
474
|
+
// Make the multicall
|
|
475
|
+
const multicallData = (
|
|
476
|
+
await Promise.all(
|
|
477
|
+
slicedMultiCallData.map(async _multiCallData =>
|
|
478
|
+
this.dexHelper.multiContract.methods
|
|
479
|
+
.tryAggregate(false, _multiCallData)
|
|
480
|
+
.call({}),
|
|
481
|
+
),
|
|
482
|
+
)
|
|
483
|
+
).flat();
|
|
484
|
+
|
|
485
|
+
return poolAddresses.map((address, i) => {
|
|
486
|
+
const tokenRateResult = decodeThrowError(
|
|
487
|
+
this.interfaces['VAULT'],
|
|
488
|
+
'getPoolTokenRates',
|
|
489
|
+
multicallData[i],
|
|
490
|
+
address,
|
|
491
|
+
);
|
|
492
|
+
return {
|
|
493
|
+
poolAddress: address,
|
|
494
|
+
tokenRates: tokenRateResult.tokenRates.map((r: string) => BigInt(r)),
|
|
495
|
+
};
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// If a token is "boosted" it can be auto wrapped/unwrapped by Vault, e.g. aDAI<>DAI
|
|
500
|
+
// mainToken is the actual token the pool would contain, e.g. in a bbausd type setup it would be aDAI/aUSDC/aUSDT
|
|
501
|
+
// underlyingToken would be the unwrapped, e.g. DAI/USDC/USDT
|
|
502
|
+
// need rate info to calculate wrap/unwrap
|
|
503
|
+
getTokenInfo(poolState: PoolState, tokenAddress: string): TokenInfo | null {
|
|
504
|
+
// Check in main tokens
|
|
505
|
+
let tokenIndex = poolState.tokens.findIndex(
|
|
506
|
+
address => address.toLowerCase() === tokenAddress.toLowerCase(),
|
|
507
|
+
);
|
|
508
|
+
if (tokenIndex !== -1) {
|
|
509
|
+
return {
|
|
510
|
+
isBoosted: false,
|
|
511
|
+
mainToken: tokenAddress,
|
|
512
|
+
underlyingToken: null,
|
|
513
|
+
index: tokenIndex,
|
|
514
|
+
rate: poolState.tokenRates[tokenIndex],
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// Check in underlying tokens if available
|
|
519
|
+
if (poolState.tokensUnderlying) {
|
|
520
|
+
tokenIndex = poolState.tokensUnderlying.findIndex(
|
|
521
|
+
address =>
|
|
522
|
+
address && address.toLowerCase() === tokenAddress.toLowerCase(),
|
|
523
|
+
);
|
|
524
|
+
if (tokenIndex !== -1) {
|
|
525
|
+
return {
|
|
526
|
+
isBoosted: true,
|
|
527
|
+
mainToken: poolState.tokens[tokenIndex],
|
|
528
|
+
underlyingToken: tokenAddress,
|
|
529
|
+
index: tokenIndex,
|
|
530
|
+
rate: poolState.tokenRates[tokenIndex],
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// Token not found
|
|
536
|
+
this.logger.error(`getTokenInfo token not found`);
|
|
537
|
+
return null;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Prepares all the step data required to simulate maths and construct swap transaction.
|
|
542
|
+
* Balancer V3 has the concepts of Boosted Pools and ERC4626 Liquidity Buffers.
|
|
543
|
+
* These enable highly capital efficient pools and gas efficient swaps.
|
|
544
|
+
* To swap via a buffer we must provide the correct ""steps" to the router transaction.
|
|
545
|
+
* Wrap: e.g. USDC>aUSDC
|
|
546
|
+
* Unwrap: e.g. aUSDC>USDC
|
|
547
|
+
* A full swap between USDC>DAI for an example bbausd pool consisting of aDAI/aUSDC/aUSDT would look like:
|
|
548
|
+
* USDC[wrap-buffer]aUSDC[swap-pool]aDAI[unwrap-buffer]USDC
|
|
549
|
+
* See docs for further info:
|
|
550
|
+
* https://docs-v3.balancer.fi/concepts/explore-available-balancer-pools/boosted-pool.html
|
|
551
|
+
* https://docs-v3.balancer.fi/concepts/vault/buffer.html
|
|
552
|
+
*/
|
|
553
|
+
getSteps(pool: PoolState, tokenIn: TokenInfo, tokenOut: TokenInfo): Step[] {
|
|
554
|
+
if (tokenIn.isBoosted && tokenOut.isBoosted) {
|
|
555
|
+
return [
|
|
556
|
+
// Wrap tokenIn underlying to main token
|
|
557
|
+
this.getWrapStep(tokenIn),
|
|
558
|
+
// Swap main > main
|
|
559
|
+
this.getSwapStep(pool, tokenIn, tokenOut),
|
|
560
|
+
// Unwrap tokenOut main to underlying token
|
|
561
|
+
this.getUnwrapStep(tokenOut),
|
|
562
|
+
];
|
|
563
|
+
} else if (tokenIn.isBoosted) {
|
|
564
|
+
if (tokenIn.mainToken === tokenOut.mainToken) {
|
|
565
|
+
// wrap, token > erc4626
|
|
566
|
+
// tokenIn is boosted, e.g. isn't pool token and must be wrapped
|
|
567
|
+
return [this.getWrapStep(tokenIn)];
|
|
568
|
+
}
|
|
569
|
+
return [
|
|
570
|
+
// Wrap tokenIn underlying to main token
|
|
571
|
+
this.getWrapStep(tokenIn),
|
|
572
|
+
// Swap main > main
|
|
573
|
+
this.getSwapStep(pool, tokenIn, tokenOut),
|
|
574
|
+
];
|
|
575
|
+
} else if (tokenOut.isBoosted) {
|
|
576
|
+
if (tokenIn.mainToken === tokenOut.mainToken) {
|
|
577
|
+
// unwrap, stata > token
|
|
578
|
+
// token out is boosted, e.g. isn't pool token
|
|
579
|
+
return [this.getUnwrapStep(tokenOut)];
|
|
580
|
+
}
|
|
581
|
+
return [
|
|
582
|
+
// Swap main > main
|
|
583
|
+
this.getSwapStep(pool, tokenIn, tokenOut),
|
|
584
|
+
// Unwrap tokenOut main to underlying token
|
|
585
|
+
this.getUnwrapStep(tokenOut),
|
|
586
|
+
];
|
|
587
|
+
} else {
|
|
588
|
+
return [
|
|
589
|
+
// Swap main > main
|
|
590
|
+
this.getSwapStep(pool, tokenIn, tokenOut),
|
|
591
|
+
];
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
getWrapStep(token: TokenInfo): Step {
|
|
596
|
+
if (!token.underlyingToken)
|
|
597
|
+
throw new Error(
|
|
598
|
+
`Buffer wrap: token has no underlying. ${token.mainToken}`,
|
|
599
|
+
);
|
|
600
|
+
// Vault expects pool to be the ERC4626 wrapped token, e.g. aUSDC
|
|
601
|
+
return {
|
|
602
|
+
pool: token.mainToken,
|
|
603
|
+
isBuffer: true,
|
|
604
|
+
swapInput: {
|
|
605
|
+
tokenIn: token.underlyingToken,
|
|
606
|
+
tokenOut: token.mainToken,
|
|
607
|
+
},
|
|
608
|
+
poolState: {
|
|
609
|
+
poolType: 'Buffer',
|
|
610
|
+
rate: token.rate,
|
|
611
|
+
poolAddress: token.mainToken,
|
|
612
|
+
tokens: [token.mainToken, token.underlyingToken], // staticToken & underlying
|
|
613
|
+
},
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
getUnwrapStep(token: TokenInfo): Step {
|
|
618
|
+
if (!token.underlyingToken)
|
|
619
|
+
throw new Error(
|
|
620
|
+
`Buffer unwrap: token has no underlying. ${token.mainToken}`,
|
|
621
|
+
);
|
|
622
|
+
// Vault expects pool to be the ERC4626 wrapped token, e.g. aUSDC
|
|
623
|
+
return {
|
|
624
|
+
pool: token.mainToken,
|
|
625
|
+
isBuffer: true,
|
|
626
|
+
swapInput: {
|
|
627
|
+
tokenIn: token.mainToken,
|
|
628
|
+
tokenOut: token.underlyingToken,
|
|
629
|
+
},
|
|
630
|
+
poolState: {
|
|
631
|
+
poolType: 'Buffer',
|
|
632
|
+
// TODO: for ERC4626 fetch the wrap/unwrap rate
|
|
633
|
+
rate: token.rate,
|
|
634
|
+
poolAddress: token.mainToken,
|
|
635
|
+
tokens: [token.mainToken, token.underlyingToken], // staticToken & underlying
|
|
636
|
+
},
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
getSwapStep(pool: PoolState, tokenIn: TokenInfo, tokenOut: TokenInfo): Step {
|
|
641
|
+
// A normal swap between two tokens in a pool
|
|
642
|
+
return {
|
|
643
|
+
pool: pool.poolAddress,
|
|
644
|
+
isBuffer: false,
|
|
645
|
+
swapInput: {
|
|
646
|
+
tokenIn: tokenIn.mainToken,
|
|
647
|
+
tokenOut: tokenOut.mainToken,
|
|
648
|
+
},
|
|
649
|
+
poolState: pool,
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
toScaled18(amount: bigint, scalingFactor: bigint): bigint {
|
|
654
|
+
return (amount * scalingFactor * WAD) / WAD;
|
|
655
|
+
}
|
|
656
|
+
}
|