@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.
Files changed (359) hide show
  1. package/.vscode/launch.json +53 -0
  2. package/.vscode/settings.json +2 -0
  3. package/.vscode/tasks.json +15 -0
  4. package/build/abi/balancer-v3/batch-router.json +989 -0
  5. package/build/abi/balancer-v3/router.json +1792 -0
  6. package/build/abi/balancer-v3/vault-extension.json +2834 -0
  7. package/build/abi/permit2.json +581 -0
  8. package/build/abi/uniswap-v3/AlienBaseV3Router.abi.json +52 -0
  9. package/build/abi/usual-m-smart-m/usualM.abi.json +483 -0
  10. package/build/abi/usual-m-usd0/usualCollateralDao.abi.json +681 -0
  11. package/build/abi/{angle-transmuter/ChainlinkAccessControlledOffchainAggregator.json → wombat/pool.json} +637 -594
  12. package/build/bigint-constants.d.ts +1 -0
  13. package/build/bigint-constants.js +2 -1
  14. package/build/bigint-constants.js.map +1 -1
  15. package/build/config.js +28 -0
  16. package/build/config.js.map +1 -1
  17. package/build/constants.d.ts +2 -0
  18. package/build/constants.js +4 -1
  19. package/build/constants.js.map +1 -1
  20. package/build/dex/aave-v1/aave-v1.js +1 -0
  21. package/build/dex/aave-v1/aave-v1.js.map +1 -1
  22. package/build/dex/aave-v2/tokens-avalanche.json +44 -0
  23. package/build/dex/aave-v2/tokens-mainnet.json +188 -0
  24. package/build/dex/aave-v2/tokens-polygon.json +44 -0
  25. package/build/dex/augustus-approvals.d.ts +3 -2
  26. package/build/dex/augustus-approvals.js +35 -16
  27. package/build/dex/augustus-approvals.js.map +1 -1
  28. package/build/dex/bProtocol.d.ts +18 -0
  29. package/build/dex/bProtocol.js +39 -0
  30. package/build/dex/bProtocol.js.map +1 -0
  31. package/build/dex/balancer-v3/abi/balancerBatchRouter.d.ts +729 -0
  32. package/build/dex/balancer-v3/abi/balancerBatchRouter.js +732 -0
  33. package/build/dex/balancer-v3/abi/balancerBatchRouter.js.map +1 -0
  34. package/build/dex/balancer-v3/abi/balancerRouter.d.ts +1406 -0
  35. package/build/dex/balancer-v3/abi/balancerRouter.js +1175 -0
  36. package/build/dex/balancer-v3/abi/balancerRouter.js.map +1 -0
  37. package/build/dex/balancer-v3/abi/vaultExtension.V3.d.ts +2081 -0
  38. package/build/dex/balancer-v3/abi/vaultExtension.V3.js +1851 -0
  39. package/build/dex/balancer-v3/abi/vaultExtension.V3.js.map +1 -0
  40. package/build/dex/balancer-v3/balancer-v3-pool.d.ts +80 -0
  41. package/build/dex/balancer-v3/balancer-v3-pool.js +469 -0
  42. package/build/dex/balancer-v3/balancer-v3-pool.js.map +1 -0
  43. package/build/dex/balancer-v3/balancer-v3-sepolia-pools.d.ts +34 -0
  44. package/build/dex/balancer-v3/balancer-v3-sepolia-pools.js +406 -0
  45. package/build/dex/balancer-v3/balancer-v3-sepolia-pools.js.map +1 -0
  46. package/build/dex/balancer-v3/balancer-v3.d.ts +56 -0
  47. package/build/dex/balancer-v3/balancer-v3.js +412 -0
  48. package/build/dex/balancer-v3/balancer-v3.js.map +1 -0
  49. package/build/dex/balancer-v3/config.d.ts +9 -0
  50. package/build/dex/balancer-v3/config.js +45 -0
  51. package/build/dex/balancer-v3/config.js.map +1 -0
  52. package/build/dex/balancer-v3/getGasCost.d.ts +2 -0
  53. package/build/dex/balancer-v3/getGasCost.js +40 -0
  54. package/build/dex/balancer-v3/getGasCost.js.map +1 -0
  55. package/build/dex/balancer-v3/getOnChainState.d.ts +10 -0
  56. package/build/dex/balancer-v3/getOnChainState.js +163 -0
  57. package/build/dex/balancer-v3/getOnChainState.js.map +1 -0
  58. package/build/dex/balancer-v3/getPoolsApi.d.ts +2 -0
  59. package/build/dex/balancer-v3/getPoolsApi.js +84 -0
  60. package/build/dex/balancer-v3/getPoolsApi.js.map +1 -0
  61. package/build/dex/balancer-v3/getTopPoolsApi.d.ts +17 -0
  62. package/build/dex/balancer-v3/getTopPoolsApi.js +65 -0
  63. package/build/dex/balancer-v3/getTopPoolsApi.js.map +1 -0
  64. package/build/dex/balancer-v3/optimizer.d.ts +2 -0
  65. package/build/dex/balancer-v3/optimizer.js +47 -0
  66. package/build/dex/balancer-v3/optimizer.js.map +1 -0
  67. package/build/dex/balancer-v3/stablePool.d.ts +7 -0
  68. package/build/dex/balancer-v3/stablePool.js +73 -0
  69. package/build/dex/balancer-v3/stablePool.js.map +1 -0
  70. package/build/dex/balancer-v3/types.d.ts +62 -0
  71. package/build/dex/balancer-v3/types.js +3 -0
  72. package/build/dex/balancer-v3/types.js.map +1 -0
  73. package/build/dex/balancer-v3/weightedPool.d.ts +1 -0
  74. package/build/dex/balancer-v3/weightedPool.js +6 -0
  75. package/build/dex/balancer-v3/weightedPool.js.map +1 -0
  76. package/build/dex/bancor.d.ts +26 -0
  77. package/build/dex/bancor.js +58 -0
  78. package/build/dex/bancor.js.map +1 -0
  79. package/build/dex/cables/cables.d.ts +7 -5
  80. package/build/dex/cables/cables.js +229 -86
  81. package/build/dex/cables/cables.js.map +1 -1
  82. package/build/dex/cables/constants.d.ts +13 -12
  83. package/build/dex/cables/constants.js +14 -16
  84. package/build/dex/cables/constants.js.map +1 -1
  85. package/build/dex/cables/types.d.ts +7 -0
  86. package/build/dex/cables/types.js +8 -1
  87. package/build/dex/cables/types.js.map +1 -1
  88. package/build/dex/compound.d.ts +18 -0
  89. package/build/dex/compound.js +46 -0
  90. package/build/dex/compound.js.map +1 -0
  91. package/build/dex/curve-v2.d.ts +58 -0
  92. package/build/dex/curve-v2.js +180 -0
  93. package/build/dex/curve-v2.js.map +1 -0
  94. package/build/dex/dodo-v1.d.ts +33 -0
  95. package/build/dex/dodo-v1.js +63 -0
  96. package/build/dex/dodo-v1.js.map +1 -0
  97. package/build/dex/fluid-dex/fluid-dex-pool.d.ts +47 -0
  98. package/build/dex/fluid-dex/fluid-dex-pool.js +119 -0
  99. package/build/dex/fluid-dex/fluid-dex-pool.js.map +1 -0
  100. package/build/dex/inception/inception-event-pool.d.ts +4 -4
  101. package/build/dex/inception/inception-event-pool.js +7 -9
  102. package/build/dex/inception/inception-event-pool.js.map +1 -1
  103. package/build/dex/inception/inception.d.ts +6 -6
  104. package/build/dex/inception/inception.js +16 -40
  105. package/build/dex/inception/inception.js.map +1 -1
  106. package/build/dex/inception/utils.d.ts +1 -0
  107. package/build/dex/inception/utils.js +22 -1
  108. package/build/dex/inception/utils.js.map +1 -1
  109. package/build/dex/index.js +4 -0
  110. package/build/dex/index.js.map +1 -1
  111. package/build/dex/lido.d.ts +19 -0
  112. package/build/dex/lido.js +42 -0
  113. package/build/dex/lido.js.map +1 -0
  114. package/build/dex/maker-psm/scripts/gem.abi.json +136 -0
  115. package/build/dex/maker-psm/scripts/validate-state.d.ts +0 -0
  116. package/build/dex/maker-psm/scripts/validate-state.js +185 -0
  117. package/build/dex/maker-psm/scripts/validate-state.js.map +1 -0
  118. package/build/dex/maker-psm/scripts/vat.abi.json +243 -0
  119. package/build/dex/onebit.d.ts +25 -0
  120. package/build/dex/onebit.js +42 -0
  121. package/build/dex/onebit.js.map +1 -0
  122. package/build/dex/platypus/platypus.js +17 -3
  123. package/build/dex/platypus/platypus.js.map +1 -1
  124. package/build/dex/smoothy.d.ts +26 -0
  125. package/build/dex/smoothy.js +48 -0
  126. package/build/dex/smoothy.js.map +1 -0
  127. package/build/dex/solidly-v3/scripts/check-event-pool-event.d.ts +1 -0
  128. package/build/dex/solidly-v3/scripts/check-event-pool-event.js +53 -0
  129. package/build/dex/solidly-v3/scripts/check-event-pool-event.js.map +1 -0
  130. package/build/dex/stable-pool.d.ts +28 -0
  131. package/build/dex/stable-pool.js +55 -0
  132. package/build/dex/stable-pool.js.map +1 -0
  133. package/build/dex/swaap-v1/swaap-v1.js +4 -0
  134. package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
  135. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.d.ts +7 -0
  136. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.js +20 -0
  137. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.js.map +1 -0
  138. package/build/dex/trader-joe-v2.1.d.ts +43 -0
  139. package/build/dex/trader-joe-v2.1.js +79 -0
  140. package/build/dex/trader-joe-v2.1.js.map +1 -0
  141. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.d.ts +17 -0
  142. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js +64 -0
  143. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js.map +1 -0
  144. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.d.ts +17 -0
  145. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js +79 -0
  146. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js.map +1 -0
  147. package/build/dex/uniswap-v3/uniswap-v3-pricing.d.ts +4 -0
  148. package/build/dex/uniswap-v3/uniswap-v3-pricing.js +43 -0
  149. package/build/dex/uniswap-v3/uniswap-v3-pricing.js.map +1 -0
  150. package/build/dex/uniswap-v3/uniswap-v3-worker.d.ts +1 -0
  151. package/build/dex/uniswap-v3/uniswap-v3-worker.js +10 -0
  152. package/build/dex/uniswap-v3/uniswap-v3-worker.js.map +1 -0
  153. package/build/dex/usual/types.d.ts +13 -0
  154. package/build/dex/usual/types.js +3 -0
  155. package/build/dex/usual/types.js.map +1 -0
  156. package/build/dex/usual/usual-bond.d.ts +18 -0
  157. package/build/dex/usual/usual-bond.js +52 -0
  158. package/build/dex/usual/usual-bond.js.map +1 -0
  159. package/build/dex/usual/usual-m-smart-m.d.ts +17 -0
  160. package/build/dex/usual/usual-m-smart-m.js +50 -0
  161. package/build/dex/usual/usual-m-smart-m.js.map +1 -0
  162. package/build/dex/usual/usual-m-usd0.d.ts +17 -0
  163. package/build/dex/usual/usual-m-usd0.js +51 -0
  164. package/build/dex/usual/usual-m-usd0.js.map +1 -0
  165. package/build/dex/usual/usual.d.ts +27 -0
  166. package/build/dex/usual/usual.js +125 -0
  167. package/build/dex/usual/usual.js.map +1 -0
  168. package/build/dex/usual-m-smart-m/config.d.ts +3 -0
  169. package/build/dex/usual-m-smart-m/config.js +13 -0
  170. package/build/dex/usual-m-smart-m/config.js.map +1 -0
  171. package/build/dex/usual-m-smart-m/types.d.ts +7 -0
  172. package/build/dex/usual-m-smart-m/types.js +3 -0
  173. package/build/dex/usual-m-smart-m/types.js.map +1 -0
  174. package/build/dex/usual-m-smart-m/usual-m-smart-m.d.ts +35 -0
  175. package/build/dex/usual-m-smart-m/usual-m-smart-m.js +161 -0
  176. package/build/dex/usual-m-smart-m/usual-m-smart-m.js.map +1 -0
  177. package/build/dex/usual-m-usd0/config.d.ts +3 -0
  178. package/build/dex/usual-m-usd0/config.js +14 -0
  179. package/build/dex/usual-m-usd0/config.js.map +1 -0
  180. package/build/dex/usual-m-usd0/types.d.ts +8 -0
  181. package/build/dex/usual-m-usd0/types.js +3 -0
  182. package/build/dex/usual-m-usd0/types.js.map +1 -0
  183. package/build/dex/usual-m-usd0/usual-m-usd0.d.ts +35 -0
  184. package/build/dex/usual-m-usd0/usual-m-usd0.js +159 -0
  185. package/build/dex/usual-m-usd0/usual-m-usd0.js.map +1 -0
  186. package/build/dex/weth/config.js +3 -0
  187. package/build/dex/weth/config.js.map +1 -1
  188. package/build/executor/Executor01BytecodeBuilder.js +2 -2
  189. package/build/executor/Executor01BytecodeBuilder.js.map +1 -1
  190. package/build/executor/Executor02BytecodeBuilder.js +2 -2
  191. package/build/executor/Executor02BytecodeBuilder.js.map +1 -1
  192. package/build/executor/Executor03BytecodeBuilder.js +2 -2
  193. package/build/executor/Executor03BytecodeBuilder.js.map +1 -1
  194. package/build/executor/ExecutorBytecodeBuilder.d.ts +3 -1
  195. package/build/executor/ExecutorBytecodeBuilder.js +35 -2
  196. package/build/executor/ExecutorBytecodeBuilder.js.map +1 -1
  197. package/build/generic-swap-transaction-builder.js +5 -1
  198. package/build/generic-swap-transaction-builder.js.map +1 -1
  199. package/build/types.d.ts +1 -0
  200. package/package.json +2 -1
  201. package/scripts/run-dex-integration-v3.ts +78 -0
  202. package/src/abi/balancer-v3/batch-router.json +989 -0
  203. package/src/abi/balancer-v3/router.json +1792 -0
  204. package/src/abi/balancer-v3/vault-extension.json +2834 -0
  205. package/src/abi/permit2.json +581 -0
  206. package/src/bigint-constants.ts +1 -0
  207. package/src/config.ts +29 -0
  208. package/src/constants.ts +4 -0
  209. package/src/dex/augustus-approvals.ts +52 -19
  210. package/src/dex/balancer-v3/balancer-v3-e2e.test.ts +275 -0
  211. package/src/dex/balancer-v3/balancer-v3-events.test.ts +223 -0
  212. package/src/dex/balancer-v3/balancer-v3-integration.test.ts +879 -0
  213. package/src/dex/balancer-v3/balancer-v3-pool.ts +656 -0
  214. package/src/dex/balancer-v3/balancer-v3.ts +538 -0
  215. package/src/dex/balancer-v3/config.ts +46 -0
  216. package/src/dex/balancer-v3/getGasCost.ts +37 -0
  217. package/src/dex/balancer-v3/getOnChainState.ts +326 -0
  218. package/src/dex/balancer-v3/getPoolsApi.ts +129 -0
  219. package/src/dex/balancer-v3/getTopPoolsApi.ts +96 -0
  220. package/src/dex/balancer-v3/optimizer.ts +66 -0
  221. package/src/dex/balancer-v3/stablePool.ts +78 -0
  222. package/src/dex/balancer-v3/types.ts +86 -0
  223. package/src/dex/balancer-v3/weightedPool.ts +2 -0
  224. package/src/dex/index.ts +4 -0
  225. package/src/dex/weth/config.ts +3 -0
  226. package/src/executor/Executor01BytecodeBuilder.ts +2 -0
  227. package/src/executor/Executor02BytecodeBuilder.ts +2 -0
  228. package/src/executor/Executor03BytecodeBuilder.ts +2 -0
  229. package/src/executor/ExecutorBytecodeBuilder.ts +63 -2
  230. package/src/generic-swap-transaction-builder.ts +6 -2
  231. package/src/types.ts +1 -0
  232. package/tests/constants-e2e.ts +66 -0
  233. package/build/abi/Aave_GSM.json +0 -751
  234. package/build/abi/Balancer.json +0 -257
  235. package/build/abi/BalancerV2.json +0 -1179
  236. package/build/abi/RamsesV2Pool.abi.json +0 -1554
  237. package/build/abi/RamsesV2Quoter.abi.json +0 -274
  238. package/build/abi/USDM.json +0 -807
  239. package/build/abi/UniswapV2Router.json +0 -397
  240. package/build/abi/angle-transmuter/ChainlinkEACAggregatorProxy.json +0 -509
  241. package/build/abi/curve-v1/CurveBUSDv2.abi.json +0 -1356
  242. package/build/abi/curve-v1/CurveTokenV1.json +0 -305
  243. package/build/abi/curve-v1/CurveTokenV2.json +0 -318
  244. package/build/abi/curve-v1/CurveV1StableNg.json +0 -1452
  245. package/build/abi/curve-v1/CurveV2Swap.json +0 -700
  246. package/build/abi/curve-v1/FactoryCryptoRegistry.json +0 -316
  247. package/build/abi/curve-v1/FactoryRegistry.json +0 -508
  248. package/build/abi/curve-v1/StableSwapAave.json +0 -1152
  249. package/build/abi/curve-v1/StableSwapBUSD.json +0 -814
  250. package/build/abi/curve-v1/StableSwapCompound.json +0 -814
  251. package/build/abi/curve-v1/StableSwapIDLE.json +0 -1253
  252. package/build/abi/curve-v1/StableSwapPax.json +0 -865
  253. package/build/abi/curve-v1/StableSwapSnow.json +0 -900
  254. package/build/abi/curve-v1/StableSwapUSDN.json +0 -1004
  255. package/build/abi/curve-v1/StableSwapUSDT.json +0 -814
  256. package/build/abi/curve-v1/StableSwapY.json +0 -814
  257. package/build/abi/inception/inception-ineth.json +0 -609
  258. package/build/abi/kyberdmm/kyber-storage.abi.json +0 -824
  259. package/build/abi/kyberdmm.abi.json +0 -1148
  260. package/build/abi/maverick-v2/MaverickV2Quoter.json +0 -278
  261. package/build/abi/nerve/nerve-metapool.json +0 -790
  262. package/build/abi/uniswap-v3/UniswapV3Quoter.abi.json +0 -193
  263. package/build/abi/velodrome-slipstream/VelodromeSlipstreamQuoterV2.abi.json +0 -267
  264. package/build/dex/balancer-v1/balancer-pools.json +0 -33333
  265. package/build/dex/bebop/websocket-fetcher.d.ts +0 -35
  266. package/build/dex/bebop/websocket-fetcher.js +0 -87
  267. package/build/dex/bebop/websocket-fetcher.js.map +0 -1
  268. package/build/dex/idle-dao/idle-dao-pool.d.ts +0 -56
  269. package/build/dex/idle-dao/idle-dao-pool.js +0 -176
  270. package/build/dex/idle-dao/idle-dao-pool.js.map +0 -1
  271. package/build/dex/idle-dao/idle-dao-pooling-pool.d.ts +0 -16
  272. package/build/dex/idle-dao/idle-dao-pooling-pool.js +0 -57
  273. package/build/dex/idle-dao/idle-dao-pooling-pool.js.map +0 -1
  274. package/build/dex/idle-dao/scripts.d.ts +0 -9
  275. package/build/dex/idle-dao/scripts.js +0 -552
  276. package/build/dex/idle-dao/scripts.js.map +0 -1
  277. package/build/dex/inception/inception-native.d.ts +0 -43
  278. package/build/dex/inception/inception-native.js +0 -131
  279. package/build/dex/inception/inception-native.js.map +0 -1
  280. package/build/dex/inception/inception-pool.d.ts +0 -18
  281. package/build/dex/inception/inception-pool.js +0 -32
  282. package/build/dex/inception/inception-pool.js.map +0 -1
  283. package/build/dex/inception/inception-price-feed.d.ts +0 -19
  284. package/build/dex/inception/inception-price-feed.js +0 -51
  285. package/build/dex/inception/inception-price-feed.js.map +0 -1
  286. package/build/executor/fixtures/executor01/exchange-params/price-route-multiswap-balancerv1-curvev1-eth-usdc-gusd.json +0 -16
  287. package/build/executor/fixtures/executor01/exchange-params/price-route-multiswap-balancerv1-sushiv3-wbtc-eth-sushi.json +0 -16
  288. package/build/executor/fixtures/executor01/exchange-params/price-route-multiswap-sushiv3-curvev1-eth-usdc-gusd.json +0 -16
  289. package/build/executor/fixtures/executor01/exchange-params/price-route-multiswap-sushiv3-curvev1-gusd-usdc-eth.json +0 -16
  290. package/build/executor/fixtures/executor01/exchange-params/price-route-multiswap-sushiv3-dai-usdc-eth.json +0 -16
  291. package/build/executor/fixtures/executor01/exchange-params/price-route-multiswap-sushiv3-eth-usdc-dai.json +0 -16
  292. package/build/executor/fixtures/executor01/exchange-params/price-route-multiswap-sushiv3-usdc-eth-wbtc.json +0 -16
  293. package/build/executor/fixtures/executor01/exchange-params/price-route-multiswap-sushiv3-usdt-usdc-dai.json +0 -16
  294. package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-balancerv1-eth-usdc.json +0 -9
  295. package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-balancerv1-usdc-eth.json +0 -9
  296. package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-balancerv1-usdc-wbtc.json +0 -9
  297. package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-curvev1-eth-steth.json +0 -9
  298. package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-curvev1-steth-eth.json +0 -9
  299. package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-curvev1-usdc-usdt.json +0 -9
  300. package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-univ3-eth-usdc.json +0 -9
  301. package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-univ3-usdc-eth.json +0 -9
  302. package/build/executor/fixtures/executor01/exchange-params/price-route-simpleSwap-univ3-usdc-usdt.json +0 -9
  303. package/build/executor/fixtures/executor01/maybe-weth-calldata/price-route-multiswap-balancerv1-sushiv3-wbtc-eth-sushi.json +0 -12
  304. package/build/executor/fixtures/executor01/maybe-weth-calldata/price-route-multiswap-sushiv3-curvev1-eth-usdc-gusd.json +0 -7
  305. package/build/executor/fixtures/executor01/maybe-weth-calldata/price-route-multiswap-sushiv3-curvev1-gusd-usdc-eth.json +0 -7
  306. package/build/executor/fixtures/executor01/maybe-weth-calldata/price-route-multiswap-sushiv3-dai-usdc-eth.json +0 -7
  307. package/build/executor/fixtures/executor01/maybe-weth-calldata/price-route-multiswap-sushiv3-eth-usdc-dai.json +0 -7
  308. package/build/executor/fixtures/executor01/maybe-weth-calldata/price-route-multiswap-sushiv3-usdc-eth-wbtc.json +0 -12
  309. package/build/executor/fixtures/executor01/maybe-weth-calldata/price-route-simpleSwap-univ3-eth-usdc.json +0 -7
  310. package/build/executor/fixtures/executor01/maybe-weth-calldata/price-route-simpleSwap-univ3-usdc-eth.json +0 -7
  311. package/build/executor/fixtures/executor01/routes/price-route-multiswap-balancerv1-curvev1-eth-usdc-gusd.json +0 -78
  312. package/build/executor/fixtures/executor01/routes/price-route-multiswap-balancerv1-sushiv3-wbtc-eth-sushi.json +0 -81
  313. package/build/executor/fixtures/executor01/routes/price-route-multiswap-sushiv3-curvev1-eth-usdc-gusd.json +0 -78
  314. package/build/executor/fixtures/executor01/routes/price-route-multiswap-sushiv3-curvev1-gusd-usdc-eth.json +0 -78
  315. package/build/executor/fixtures/executor01/routes/price-route-multiswap-sushiv3-dai-usdc-eth.json +0 -81
  316. package/build/executor/fixtures/executor01/routes/price-route-multiswap-sushiv3-eth-usdc-dai.json +0 -81
  317. package/build/executor/fixtures/executor01/routes/price-route-multiswap-sushiv3-usdc-eth-wbtc.json +0 -81
  318. package/build/executor/fixtures/executor01/routes/price-route-multiswap-sushiv3-usdt-usdc-dai.json +0 -81
  319. package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-balancerv1-eth-usdc.json +0 -64
  320. package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-balancerv1-usdc-eth.json +0 -85
  321. package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-balancerv1-usdc-wbtc.json +0 -64
  322. package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-curvev1-eth-steth.json +0 -52
  323. package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-curvev1-steth-eth.json +0 -52
  324. package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-curvev1-usdc-usdt.json +0 -52
  325. package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-univ3-eth-usdc.json +0 -55
  326. package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-univ3-usdc-eth.json +0 -55
  327. package/build/executor/fixtures/executor01/routes/price-route-simpleSwap-univ3-usdc-usdt.json +0 -55
  328. package/build/executor/fixtures/executor02/exchange-params/price-route-multiSwap-balancerv1-sushiv3-univ3-bal-eth-sushi.json +0 -23
  329. package/build/executor/fixtures/executor02/exchange-params/price-route-multiSwap-maverickv1-sushiv3-univ3-eth-usdc-mav.json +0 -37
  330. package/build/executor/fixtures/executor02/exchange-params/price-route-multiSwap-univ3-sushiv3-sushi-eth-wbtc.json +0 -23
  331. package/build/executor/fixtures/executor02/exchange-params/price-route-multiSwap-univ3-sushiv3-wbtc-eth-sushi.json +0 -23
  332. package/build/executor/fixtures/executor02/exchange-params/price-route-multiswap-curvev1-univ3-dai-usdc-eth.json +0 -51
  333. package/build/executor/fixtures/executor02/exchange-params/price-route-multiswap-univ3-usdt-dai-eth.json +0 -58
  334. package/build/executor/fixtures/executor02/exchange-params/price-route-simpleSwap-sushiv3-balancerv1-eth-usdc.json +0 -23
  335. package/build/executor/fixtures/executor02/exchange-params/price-route-simpleSwap-sushiv3-balancerv1-usdc-eth.json +0 -23
  336. package/build/executor/fixtures/executor02/exchange-params/price-route-simpleSwap-sushiv3-univ3-eth-sushi.json +0 -9
  337. package/build/executor/fixtures/executor02/exchange-params/price-route-simpleSwap-sushiv3-univ3-sushi-eth.json +0 -16
  338. package/build/executor/fixtures/executor02/exchange-params/price-route-simpleSwap-univ3-curvev1-usdt-dai.json +0 -23
  339. package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-multiSwap-balancerv1-sushiv3-univ3-bal-eth-sushi.json +0 -12
  340. package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-multiSwap-maverickv1-sushiv3-univ3-eth-usdc-mav.json +0 -7
  341. package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-multiSwap-univ3-sushiv3-sushi-eth-wbtc.json +0 -12
  342. package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-multiSwap-univ3-sushiv3-wbtc-eth-sushi.json +0 -12
  343. package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-multiswap-curvev1-univ3-dai-usdc-eth.json +0 -7
  344. package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-multiswap-univ3-usdt-dai-eth.json +0 -7
  345. package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-simpleSwap-sushiv3-balancerv1-eth-usdc.json +0 -7
  346. package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-simpleSwap-sushiv3-balancerv1-usdc-eth.json +0 -7
  347. package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-simpleSwap-sushiv3-univ3-eth-sushi.json +0 -7
  348. package/build/executor/fixtures/executor02/maybe-weth-calldata/price-route-simpleSwap-sushiv3-univ3-sushi-eth.json +0 -7
  349. package/build/executor/fixtures/executor02/routes/price-route-multiSwap-balancerv1-sushiv3-univ3-bal-eth-sushi.json +0 -130
  350. package/build/executor/fixtures/executor02/routes/price-route-multiSwap-maverickv1-sushiv3-univ3-eth-usdc-mav.json +0 -136
  351. package/build/executor/fixtures/executor02/routes/price-route-multiSwap-univ3-sushiv3-sushi-eth-wbtc.json +0 -100
  352. package/build/executor/fixtures/executor02/routes/price-route-multiSwap-univ3-sushiv3-wbtc-eth-sushi.json +0 -100
  353. package/build/executor/fixtures/executor02/routes/price-route-multiswap-curvev1-univ3-dai-usdc-eth.json +0 -166
  354. package/build/executor/fixtures/executor02/routes/price-route-multiswap-univ3-usdt-dai-eth.json +0 -190
  355. package/build/executor/fixtures/executor02/routes/price-route-simpleSwap-sushiv3-balancerv1-eth-usdc.json +0 -114
  356. package/build/executor/fixtures/executor02/routes/price-route-simpleSwap-sushiv3-balancerv1-usdc-eth.json +0 -114
  357. package/build/executor/fixtures/executor02/routes/price-route-simpleSwap-sushiv3-univ3-eth-sushi.json +0 -55
  358. package/build/executor/fixtures/executor02/routes/price-route-simpleSwap-sushiv3-univ3-sushi-eth.json +0 -73
  359. 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
+ }