@paraswap/dex-lib 5.0.1-new-dexs-encoding.0 → 5.0.2-native-dex-math.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/build/abi/BProtocol.json +1155 -0
- package/build/abi/Jarvis.json +1172 -0
- package/build/abi/MStableAsset.json +1545 -0
- package/build/abi/OneInchLp.json +1304 -0
- package/build/abi/Onebit.json +736 -0
- package/build/abi/Shell.json +1294 -0
- package/build/abi/TraderJoeV2Router.json +50 -0
- package/build/abi/idle-dao/idle-cdo-factory.json +38 -0
- package/build/abi/idle-dao/idle-cdo.json +1245 -0
- package/build/abi/infusion/InfusionFactory.json +147 -0
- package/build/abi/infusion/InfusionPair.json +658 -0
- package/build/abi/infusion/InfusionRouter.json +442 -0
- package/build/abi/maker-psm/pot.json +322 -0
- package/build/abi/maker-psm/psm.json +243 -0
- package/build/abi/maker-psm/vat.json +363 -0
- package/build/abi/nomiswap-v2/nomiswap-v2-pool.json +773 -0
- package/build/abi/quick-perps/fast-price-events.json +70 -0
- package/build/abi/quick-perps/fast-price-feed.json +741 -0
- package/build/abi/quick-perps/reader.json +313 -0
- package/build/abi/quick-perps/vault-price-feed.json +323 -0
- package/build/abi/quick-perps/vault.json +1953 -0
- package/build/abi/uniswap-v2/excalibur-pool.json +881 -0
- package/build/abi/uniswap-v2/mdex-factory.json +759 -0
- package/build/abi/wUSDM.json +757 -0
- package/build/abi/zrx.v2.json +1967 -0
- package/build/abi/zrx.v3.json +3454 -0
- package/build/abi/zrx.v4.json +2193 -0
- package/build/dex/OneInchLp.d.ts +24 -0
- package/build/dex/OneInchLp.js +43 -0
- package/build/dex/OneInchLp.js.map +1 -0
- package/build/dex/aave-gsm/config.js +2 -2
- package/build/dex/bProtocol/bProtocol.d.ts +15 -0
- package/build/dex/bProtocol/bProtocol.js +56 -0
- package/build/dex/bProtocol/bProtocol.js.map +1 -0
- package/build/dex/bProtocol/types.d.ts +11 -0
- package/build/dex/bProtocol/types.js +8 -0
- package/build/dex/bProtocol/types.js.map +1 -0
- package/build/dex/ekubo/pools/base-pool.d.ts +47 -0
- package/build/dex/ekubo/pools/base-pool.js +184 -0
- package/build/dex/ekubo/pools/base-pool.js.map +1 -0
- package/build/dex/ekubo/pools/iface.d.ts +46 -0
- package/build/dex/ekubo/pools/iface.js +75 -0
- package/build/dex/ekubo/pools/iface.js.map +1 -0
- package/build/dex/ekubo/pools/math/price.d.ts +7 -0
- package/build/dex/ekubo/pools/math/price.js +112 -0
- package/build/dex/ekubo/pools/math/price.js.map +1 -0
- package/build/dex/ekubo/pools/oracle-pool.d.ts +10 -0
- package/build/dex/ekubo/pools/oracle-pool.js +19 -0
- package/build/dex/ekubo/pools/oracle-pool.js.map +1 -0
- package/build/dex/ekubo/pools/pool-utils.d.ts +44 -0
- package/build/dex/ekubo/pools/pool-utils.js +240 -0
- package/build/dex/ekubo/pools/pool-utils.js.map +1 -0
- package/build/dex/idex.d.ts +3 -3
- package/build/dex/idle-dao/config.d.ts +5 -0
- package/build/dex/idle-dao/config.js +24 -0
- package/build/dex/idle-dao/config.js.map +1 -0
- package/build/dex/idle-dao/idle-dao.d.ts +42 -0
- package/build/dex/idle-dao/idle-dao.js +279 -0
- package/build/dex/idle-dao/idle-dao.js.map +1 -0
- package/build/dex/idle-dao/token_list.d.ts +4 -0
- package/build/dex/idle-dao/token_list.js +289 -0
- package/build/dex/idle-dao/token_list.js.map +1 -0
- package/build/dex/idle-dao/tokens.d.ts +9 -0
- package/build/dex/idle-dao/tokens.js +68 -0
- package/build/dex/idle-dao/tokens.js.map +1 -0
- package/build/dex/idle-dao/types.d.ts +35 -0
- package/build/dex/idle-dao/types.js +11 -0
- package/build/dex/idle-dao/types.js.map +1 -0
- package/build/dex/idle-dao/utils.d.ts +8 -0
- package/build/dex/idle-dao/utils.js +149 -0
- package/build/dex/idle-dao/utils.js.map +1 -0
- package/build/dex/infusion/config.d.ts +3 -0
- package/build/dex/infusion/config.js +20 -0
- package/build/dex/infusion/config.js.map +1 -0
- package/build/dex/infusion/infusion-stable-pool.d.ts +4 -0
- package/build/dex/infusion/infusion-stable-pool.js +74 -0
- package/build/dex/infusion/infusion-stable-pool.js.map +1 -0
- package/build/dex/infusion/infusion.d.ts +51 -0
- package/build/dex/infusion/infusion.js +500 -0
- package/build/dex/infusion/infusion.js.map +1 -0
- package/build/dex/infusion/types.d.ts +45 -0
- package/build/dex/infusion/types.js.map +1 -0
- package/build/dex/infusion/utils/isStablePair.d.ts +2 -0
- package/build/dex/infusion/utils/isStablePair.js +18 -0
- package/build/dex/infusion/utils/isStablePair.js.map +1 -0
- package/build/dex/jarvis.d.ts +56 -0
- package/build/dex/jarvis.js +163 -0
- package/build/dex/jarvis.js.map +1 -0
- package/build/dex/mStable.d.ts +44 -0
- package/build/dex/mStable.js +75 -0
- package/build/dex/mStable.js.map +1 -0
- package/build/dex/maker-psm/config.d.ts +11 -0
- package/build/dex/maker-psm/config.js +53 -0
- package/build/dex/maker-psm/config.js.map +1 -0
- package/build/dex/maker-psm/maker-psm.d.ts +103 -0
- package/build/dex/maker-psm/maker-psm.js +493 -0
- package/build/dex/maker-psm/maker-psm.js.map +1 -0
- package/build/dex/maker-psm/types.d.ts +39 -0
- package/build/{generic-swap-transaction-builder/dex-encoder/ports.js → dex/maker-psm/types.js} +1 -1
- package/build/dex/maker-psm/types.js.map +1 -0
- package/build/dex/metric/metric.d.ts +2 -2
- package/build/dex/metric/metric.js +2 -5
- package/build/dex/metric/metric.js.map +1 -1
- package/build/dex/onebit/onebit.d.ts +16 -0
- package/build/dex/onebit/onebit.js +61 -0
- package/build/dex/onebit/onebit.js.map +1 -0
- package/build/dex/onebit/types.d.ts +14 -0
- package/build/dex/onebit/types.js +8 -0
- package/build/dex/onebit/types.js.map +1 -0
- package/build/dex/pancakeswap-infinity/encoder.js +16 -12
- package/build/dex/pancakeswap-infinity/encoder.js.map +1 -1
- package/build/dex/pancakeswap-v3/pancakeswap-v3-pool.d.ts +2 -0
- package/build/dex/pancakeswap-v3/pancakeswap-v3-pool.js +5 -0
- package/build/dex/pancakeswap-v3/pancakeswap-v3-pool.js.map +1 -1
- package/build/dex/pancakeswap-v3/pancakeswap-v3.d.ts +6 -3
- package/build/dex/pancakeswap-v3/pancakeswap-v3.js +85 -9
- package/build/dex/pancakeswap-v3/pancakeswap-v3.js.map +1 -1
- package/build/dex/quick-perps/config.d.ts +11 -0
- package/build/dex/quick-perps/config.js +27 -0
- package/build/dex/quick-perps/config.js.map +1 -0
- package/build/dex/quick-perps/fast-price-feed.d.ts +26 -0
- package/build/dex/quick-perps/fast-price-feed.js +184 -0
- package/build/dex/quick-perps/fast-price-feed.js.map +1 -0
- package/build/dex/quick-perps/pool.d.ts +21 -0
- package/build/dex/quick-perps/pool.js +229 -0
- package/build/dex/quick-perps/pool.js.map +1 -0
- package/build/dex/quick-perps/quick-perps.d.ts +53 -0
- package/build/dex/quick-perps/quick-perps.js +247 -0
- package/build/dex/quick-perps/quick-perps.js.map +1 -0
- package/build/dex/quick-perps/types.d.ts +98 -0
- package/build/{executor/encoding-types.js → dex/quick-perps/types.js} +1 -1
- package/build/dex/quick-perps/types.js.map +1 -0
- package/build/dex/quick-perps/usdq.d.ts +15 -0
- package/build/dex/quick-perps/usdq.js +62 -0
- package/build/dex/quick-perps/usdq.js.map +1 -0
- package/build/dex/quick-perps/vault-price-feed.d.ts +43 -0
- package/build/dex/quick-perps/vault-price-feed.js +203 -0
- package/build/dex/quick-perps/vault-price-feed.js.map +1 -0
- package/build/dex/quick-perps/vault-utils.d.ts +8 -0
- package/build/dex/quick-perps/vault-utils.js +42 -0
- package/build/dex/quick-perps/vault-utils.js.map +1 -0
- package/build/dex/quick-perps/vault.d.ts +46 -0
- package/build/dex/quick-perps/vault.js +182 -0
- package/build/dex/quick-perps/vault.js.map +1 -0
- package/build/dex/se-vlr/config.d.ts +3 -0
- package/build/dex/se-vlr/config.js +24 -0
- package/build/dex/se-vlr/config.js.map +1 -0
- package/build/dex/se-vlr/se-vlr-pool.d.ts +39 -0
- package/build/dex/se-vlr/se-vlr-pool.js +70 -0
- package/build/dex/se-vlr/se-vlr-pool.js.map +1 -0
- package/build/dex/se-vlr/se-vlr.d.ts +35 -0
- package/build/dex/se-vlr/se-vlr.js +131 -0
- package/build/dex/se-vlr/se-vlr.js.map +1 -0
- package/build/dex/se-vlr/types.d.ts +6 -0
- package/build/dex/se-vlr/types.js +3 -0
- package/build/dex/se-vlr/types.js.map +1 -0
- package/build/dex/shell.d.ts +25 -0
- package/build/dex/shell.js +41 -0
- package/build/dex/shell.js.map +1 -0
- package/build/dex/simple-exchange.d.ts +1 -1
- package/build/dex/simple-exchange.js +1 -1
- package/build/dex/simple-exchange.js.map +1 -1
- package/build/dex/solidly/forks-override/aerodrome.d.ts +14 -0
- package/build/dex/solidly/forks-override/aerodrome.js +46 -0
- package/build/dex/solidly/forks-override/aerodrome.js.map +1 -0
- package/build/dex/solidly/forks-override/chronos.d.ts +23 -0
- package/build/dex/solidly/forks-override/chronos.js +141 -0
- package/build/dex/solidly/forks-override/chronos.js.map +1 -0
- package/build/dex/solidly/forks-override/usdfi.d.ts +8 -0
- package/build/dex/solidly/forks-override/usdfi.js +15 -0
- package/build/dex/solidly/forks-override/usdfi.js.map +1 -0
- package/build/dex/solidly/forks-override/velocimeter.d.ts +23 -0
- package/build/dex/solidly/forks-override/velocimeter.js +77 -0
- package/build/dex/solidly/forks-override/velocimeter.js.map +1 -0
- package/build/dex/solidly-v3/solidly-v3-pool.d.ts +3 -0
- package/build/dex/solidly-v3/solidly-v3-pool.js +8 -0
- package/build/dex/solidly-v3/solidly-v3-pool.js.map +1 -1
- package/build/dex/solidly-v3/solidly-v3.d.ts +5 -2
- package/build/dex/solidly-v3/solidly-v3.js +84 -8
- package/build/dex/solidly-v3/solidly-v3.js.map +1 -1
- package/build/dex/tessera/types.d.ts +3 -0
- package/build/dex/tessera/types.js +3 -0
- package/build/dex/tessera/types.js.map +1 -0
- package/build/dex/trader-joe-v2.d.ts +40 -0
- package/build/dex/trader-joe-v2.js +74 -0
- package/build/dex/trader-joe-v2.js.map +1 -0
- package/build/dex/uniswap-v2/dfyn.d.ts +19 -0
- package/build/dex/uniswap-v2/dfyn.js +61 -0
- package/build/dex/uniswap-v2/dfyn.js.map +1 -0
- package/build/dex/uniswap-v2/excalibur.d.ts +24 -0
- package/build/dex/uniswap-v2/excalibur.js +47 -0
- package/build/dex/uniswap-v2/excalibur.js.map +1 -0
- package/build/dex/uniswap-v2/mdex.d.ts +24 -0
- package/build/dex/uniswap-v2/mdex.js +50 -0
- package/build/dex/uniswap-v2/mdex.js.map +1 -0
- package/build/dex/uniswap-v2/nomiswap-v2.d.ts +24 -0
- package/build/dex/uniswap-v2/nomiswap-v2.js +57 -0
- package/build/dex/uniswap-v2/nomiswap-v2.js.map +1 -0
- package/build/dex/uniswap-v3/scripts/bench-all-dexes.js +160 -0
- package/build/dex/uniswap-v3/scripts/bench-all-dexes.js.map +1 -0
- package/build/dex/uniswap-v3/scripts/measure-calc-time.js +222 -110
- package/build/dex/uniswap-v3/scripts/measure-calc-time.js.map +1 -1
- package/build/dex/uniswap-v3/types.d.ts +1 -0
- package/build/dex/uniswap-v3/types.js.map +1 -1
- package/build/dex/uniswap-v3/uniswap-v3-new.d.ts +94 -0
- package/build/dex/uniswap-v3/uniswap-v3-new.js +923 -0
- package/build/dex/uniswap-v3/uniswap-v3-new.js.map +1 -0
- package/build/dex/uniswap-v3/uniswap-v3-pool.d.ts +2 -0
- package/build/dex/uniswap-v3/uniswap-v3-pool.js +5 -0
- package/build/dex/uniswap-v3/uniswap-v3-pool.js.map +1 -1
- package/build/dex/uniswap-v3/uniswap-v3.d.ts +11 -3
- package/build/dex/uniswap-v3/uniswap-v3.js +86 -9
- package/build/dex/uniswap-v3/uniswap-v3.js.map +1 -1
- package/build/dex/uniswap-v4/contract-math/Position.d.ts +3 -0
- package/build/dex/uniswap-v4/contract-math/Position.js +10 -0
- package/build/dex/uniswap-v4/contract-math/Position.js.map +1 -0
- package/build/dex/uniswap-v4/types.d.ts +1 -0
- package/build/dex/uniswap-v4/uniswap-v4-pool-manager.d.ts +2 -0
- package/build/dex/uniswap-v4/uniswap-v4-pool-manager.js +3 -0
- package/build/dex/uniswap-v4/uniswap-v4-pool-manager.js.map +1 -1
- package/build/dex/uniswap-v4/uniswap-v4-pool.d.ts +3 -0
- package/build/dex/uniswap-v4/uniswap-v4-pool.js +18 -0
- package/build/dex/uniswap-v4/uniswap-v4-pool.js.map +1 -1
- package/build/dex/uniswap-v4/uniswap-v4.d.ts +3 -1
- package/build/dex/uniswap-v4/uniswap-v4.js +74 -14
- package/build/dex/uniswap-v4/uniswap-v4.js.map +1 -1
- package/build/dex/usual-bond/config.d.ts +3 -0
- package/build/dex/usual-bond/config.js +13 -0
- package/build/dex/usual-bond/config.js.map +1 -0
- package/build/dex/usual-bond/types.d.ts +7 -0
- package/build/dex/usual-bond/types.js +3 -0
- package/build/dex/usual-bond/types.js.map +1 -0
- package/build/dex/usual-bond/usual-bond.d.ts +35 -0
- package/build/dex/usual-bond/usual-bond.js +176 -0
- package/build/dex/usual-bond/usual-bond.js.map +1 -0
- package/build/dex/wusdm/config.d.ts +3 -0
- package/build/dex/wusdm/config.js +43 -0
- package/build/dex/wusdm/config.js.map +1 -0
- package/build/dex/wusdm/constants.d.ts +2 -0
- package/build/dex/wusdm/constants.js +6 -0
- package/build/dex/wusdm/constants.js.map +1 -0
- package/build/dex/wusdm/types.d.ts +18 -0
- package/build/dex/wusdm/types.js +11 -0
- package/build/dex/wusdm/types.js.map +1 -0
- package/build/dex/wusdm/wusdm-pool.d.ts +21 -0
- package/build/dex/wusdm/wusdm-pool.js +74 -0
- package/build/dex/wusdm/wusdm-pool.js.map +1 -0
- package/build/dex/wusdm/wusdm.d.ts +46 -0
- package/build/dex/wusdm/wusdm.js +243 -0
- package/build/dex/wusdm/wusdm.js.map +1 -0
- package/build/dex/zerox/config.d.ts +2 -0
- package/build/dex/zerox/config.js +33 -0
- package/build/dex/zerox/config.js.map +1 -0
- package/build/dex/zerox/index.d.ts +22 -0
- package/build/dex/zerox/index.js +225 -0
- package/build/dex/zerox/index.js.map +1 -0
- package/build/dex/zerox/order.d.ts +88 -0
- package/build/dex/zerox/order.js +53 -0
- package/build/dex/zerox/order.js.map +1 -0
- package/build/dex/zerox/types.d.ts +73 -0
- package/build/dex/zerox/types.js +21 -0
- package/build/dex/zerox/types.js.map +1 -0
- package/build/generic-swap-transaction-builder.d.ts +4 -6
- package/build/generic-swap-transaction-builder.js +6 -8
- package/build/generic-swap-transaction-builder.js.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/index.js.map +1 -1
- package/build/pricing-helper.d.ts +1 -1
- package/build/pricing-helper.js +2 -2
- package/build/pricing-helper.js.map +1 -1
- package/build/types.d.ts +0 -3
- package/native/Cargo.lock +331 -0
- package/native/Cargo.toml +22 -0
- package/native/build.rs +5 -0
- package/native/package-lock.json +32 -0
- package/native/package.json +20 -0
- package/native/src/config.rs +73 -0
- package/native/src/lib.rs +528 -0
- package/native/src/math/bit_math.rs +177 -0
- package/native/src/math/full_math.rs +217 -0
- package/native/src/math/liquidity_math.rs +72 -0
- package/native/src/math/mod.rs +10 -0
- package/native/src/math/oracle.rs +493 -0
- package/native/src/math/sqrt_price_math.rs +272 -0
- package/native/src/math/swap_math.rs +306 -0
- package/native/src/math/tick.rs +239 -0
- package/native/src/math/tick_bitmap.rs +312 -0
- package/native/src/math/tick_math.rs +321 -0
- package/native/src/math/unsafe_math.rs +67 -0
- package/native/src/pool_state.rs +41 -0
- package/native/src/query_outputs.rs +379 -0
- package/native/src/v4_query_outputs.rs +255 -0
- package/package.json +2 -1
- package/build/abi/pancakeswap-infinity/cl-pool-manager.json +0 -80
- package/build/dex/fluid-dex/scripts/measure-calc-time.js +0 -178
- package/build/dex/fluid-dex/scripts/measure-calc-time.js.map +0 -1
- package/build/dex/uniswap-v4/api-go/compare-pricing.d.ts +0 -1
- package/build/dex/uniswap-v4/api-go/compare-pricing.js +0 -187
- package/build/dex/uniswap-v4/api-go/compare-pricing.js.map +0 -1
- package/build/dex/uniswap-v4/api-go/compare-states.d.ts +0 -1
- package/build/dex/uniswap-v4/api-go/compare-states.js +0 -149
- package/build/dex/uniswap-v4/api-go/compare-states.js.map +0 -1
- package/build/dex/uniswap-v4/api-go/fetch-pool-key.d.ts +0 -0
- package/build/dex/uniswap-v4/api-go/fetch-pool-key.js +0 -140
- package/build/dex/uniswap-v4/api-go/fetch-pool-key.js.map +0 -1
- package/build/executor/__test-utils__/snapshot-test-helpers.d.ts +0 -10
- package/build/executor/__test-utils__/snapshot-test-helpers.js +0 -72
- package/build/executor/__test-utils__/snapshot-test-helpers.js.map +0 -1
- package/build/executor/address-utils.d.ts +0 -2
- package/build/executor/address-utils.js +0 -7
- package/build/executor/address-utils.js.map +0 -1
- package/build/executor/approval.d.ts +0 -9
- package/build/executor/approval.js +0 -29
- package/build/executor/approval.js.map +0 -1
- package/build/executor/encoding-context.d.ts +0 -4
- package/build/executor/encoding-context.js +0 -54
- package/build/executor/encoding-context.js.map +0 -1
- package/build/executor/encoding-types.d.ts +0 -75
- package/build/executor/encoding-types.js.map +0 -1
- package/build/executor/factory.d.ts +0 -4
- package/build/executor/factory.js +0 -23
- package/build/executor/factory.js.map +0 -1
- package/build/executor/route-plan.d.ts +0 -8
- package/build/executor/route-plan.js +0 -109
- package/build/executor/route-plan.js.map +0 -1
- package/build/generic-swap-transaction-builder/dex-encoder/direct-methods.d.ts +0 -6
- package/build/generic-swap-transaction-builder/dex-encoder/direct-methods.js +0 -39
- package/build/generic-swap-transaction-builder/dex-encoder/direct-methods.js.map +0 -1
- package/build/generic-swap-transaction-builder/dex-encoder/index.d.ts +0 -4
- package/build/generic-swap-transaction-builder/dex-encoder/index.js +0 -21
- package/build/generic-swap-transaction-builder/dex-encoder/index.js.map +0 -1
- package/build/generic-swap-transaction-builder/dex-encoder/ports.d.ts +0 -26
- package/build/generic-swap-transaction-builder/dex-encoder/ports.js.map +0 -1
- package/build/generic-swap-transaction-builder/dex-encoder/ts-adapter.d.ts +0 -22
- package/build/generic-swap-transaction-builder/dex-encoder/ts-adapter.js +0 -299
- package/build/generic-swap-transaction-builder/dex-encoder/ts-adapter.js.map +0 -1
- package/build/generic-swap-transaction-builder/dex-encoder/types.d.ts +0 -207
- package/build/generic-swap-transaction-builder/dex-encoder/types.js +0 -194
- package/build/generic-swap-transaction-builder/dex-encoder/types.js.map +0 -1
- package/build/generic-swap-transaction-builder/orchestration.d.ts +0 -65
- package/build/generic-swap-transaction-builder/orchestration.js +0 -198
- package/build/generic-swap-transaction-builder/orchestration.js.map +0 -1
- package/build/generic-swap-transaction-builder/resolved/build-transaction.d.ts +0 -13
- package/build/generic-swap-transaction-builder/resolved/build-transaction.js +0 -343
- package/build/generic-swap-transaction-builder/resolved/build-transaction.js.map +0 -1
- package/build/generic-swap-transaction-builder/resolved/index.d.ts +0 -4
- package/build/generic-swap-transaction-builder/resolved/index.js +0 -21
- package/build/generic-swap-transaction-builder/resolved/index.js.map +0 -1
- package/build/generic-swap-transaction-builder/resolved/route-plan.d.ts +0 -2
- package/build/generic-swap-transaction-builder/resolved/route-plan.js +0 -11
- package/build/generic-swap-transaction-builder/resolved/route-plan.js.map +0 -1
- package/build/generic-swap-transaction-builder/resolved/types.d.ts +0 -67
- package/build/generic-swap-transaction-builder/resolved/types.js.map +0 -1
- package/build/generic-swap-transaction-builder/resolved/validation.d.ts +0 -10
- package/build/generic-swap-transaction-builder/resolved/validation.js +0 -69
- package/build/generic-swap-transaction-builder/resolved/validation.js.map +0 -1
- /package/build/{generic-swap-transaction-builder/resolved → dex/infusion}/types.js +0 -0
- /package/build/dex/{fluid-dex/scripts/measure-calc-time.d.ts → uniswap-v3/scripts/bench-all-dexes.d.ts} +0 -0
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
pub mod config;
|
|
2
|
+
pub mod math;
|
|
3
|
+
pub mod pool_state;
|
|
4
|
+
pub mod query_outputs;
|
|
5
|
+
pub mod v4_query_outputs;
|
|
6
|
+
|
|
7
|
+
use ethnum::{I256, U256};
|
|
8
|
+
use napi::bindgen_prelude::*;
|
|
9
|
+
use napi_derive::napi;
|
|
10
|
+
use rayon::prelude::*;
|
|
11
|
+
use std::collections::HashMap;
|
|
12
|
+
|
|
13
|
+
/// Set the number of threads rayon uses for parallel queries.
|
|
14
|
+
/// Call once at startup. Defaults to all available cores if not called.
|
|
15
|
+
#[napi]
|
|
16
|
+
pub fn set_thread_count(n: u32) {
|
|
17
|
+
rayon::ThreadPoolBuilder::new()
|
|
18
|
+
.num_threads(n as usize)
|
|
19
|
+
.build_global()
|
|
20
|
+
.ok();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
use config::MathVariant;
|
|
24
|
+
use math::oracle::OracleObservation;
|
|
25
|
+
use math::tick::TickInfo;
|
|
26
|
+
use pool_state::PoolState;
|
|
27
|
+
|
|
28
|
+
// ---- NAPI type definitions for JS interop ----
|
|
29
|
+
|
|
30
|
+
#[napi(object)]
|
|
31
|
+
pub struct JsTickEntry {
|
|
32
|
+
pub key: i32,
|
|
33
|
+
pub liquidity_gross: BigInt,
|
|
34
|
+
pub liquidity_net: BigInt,
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
#[napi(object)]
|
|
38
|
+
pub struct JsBitmapEntry {
|
|
39
|
+
pub key: i32,
|
|
40
|
+
pub value: BigInt,
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
#[napi(object)]
|
|
44
|
+
pub struct JsObservationEntry {
|
|
45
|
+
pub key: i32,
|
|
46
|
+
pub block_timestamp: BigInt,
|
|
47
|
+
pub tick_cumulative: BigInt,
|
|
48
|
+
pub seconds_per_liquidity_cumulative_x128: BigInt,
|
|
49
|
+
pub initialized: bool,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
#[napi(object)]
|
|
53
|
+
pub struct JsPoolStateInit {
|
|
54
|
+
pub variant: String,
|
|
55
|
+
pub bitmap_range: i32,
|
|
56
|
+
pub block_timestamp: BigInt,
|
|
57
|
+
pub tick_spacing: BigInt,
|
|
58
|
+
pub fee: BigInt,
|
|
59
|
+
pub sqrt_price_x96: BigInt,
|
|
60
|
+
pub tick: BigInt,
|
|
61
|
+
pub observation_index: i32,
|
|
62
|
+
pub observation_cardinality: i32,
|
|
63
|
+
pub observation_cardinality_next: i32,
|
|
64
|
+
pub fee_protocol: BigInt,
|
|
65
|
+
pub liquidity: BigInt,
|
|
66
|
+
pub max_liquidity_per_tick: BigInt,
|
|
67
|
+
pub start_tick_bitmap: BigInt,
|
|
68
|
+
pub lowest_known_tick: BigInt,
|
|
69
|
+
pub highest_known_tick: BigInt,
|
|
70
|
+
pub tick_bitmap: Vec<JsBitmapEntry>,
|
|
71
|
+
pub ticks: Vec<JsTickEntry>,
|
|
72
|
+
pub observations: Vec<JsObservationEntry>,
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
#[napi(object)]
|
|
76
|
+
pub struct JsOutputResult {
|
|
77
|
+
pub outputs: Vec<BigInt>,
|
|
78
|
+
pub tick_counts: Vec<i32>,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ---- Conversion helpers ----
|
|
82
|
+
// napi::bindgen_prelude::BigInt stores { sign_bit: bool, words: Vec<u64> }
|
|
83
|
+
// words are little-endian u64 limbs.
|
|
84
|
+
|
|
85
|
+
fn bigint_to_u256(bi: &BigInt) -> U256 {
|
|
86
|
+
let words = &bi.words;
|
|
87
|
+
let low = words.first().copied().unwrap_or(0) as u128
|
|
88
|
+
| (words.get(1).copied().unwrap_or(0) as u128) << 64;
|
|
89
|
+
let high = words.get(2).copied().unwrap_or(0) as u128
|
|
90
|
+
| (words.get(3).copied().unwrap_or(0) as u128) << 64;
|
|
91
|
+
U256::from_words(high, low)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
fn bigint_to_i256(bi: &BigInt) -> I256 {
|
|
95
|
+
let u = bigint_to_u256(bi);
|
|
96
|
+
let val = u.as_i256();
|
|
97
|
+
if bi.sign_bit {
|
|
98
|
+
-val
|
|
99
|
+
} else {
|
|
100
|
+
val
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
fn u256_to_bigint(val: U256) -> BigInt {
|
|
105
|
+
let (high, low) = val.into_words();
|
|
106
|
+
let mut words = vec![
|
|
107
|
+
low as u64,
|
|
108
|
+
(low >> 64) as u64,
|
|
109
|
+
high as u64,
|
|
110
|
+
(high >> 64) as u64,
|
|
111
|
+
];
|
|
112
|
+
// Trim trailing zeros for cleaner representation
|
|
113
|
+
while words.len() > 1 && *words.last().unwrap() == 0 {
|
|
114
|
+
words.pop();
|
|
115
|
+
}
|
|
116
|
+
BigInt {
|
|
117
|
+
sign_bit: false,
|
|
118
|
+
words,
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ---- The main NAPI class ----
|
|
123
|
+
|
|
124
|
+
#[napi]
|
|
125
|
+
pub struct RustPoolHandle {
|
|
126
|
+
state: PoolState,
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
#[napi]
|
|
130
|
+
impl RustPoolHandle {
|
|
131
|
+
/// Create a new Rust-owned pool state from JS data.
|
|
132
|
+
#[napi(factory)]
|
|
133
|
+
pub fn create(init: JsPoolStateInit) -> Result<Self> {
|
|
134
|
+
let variant = MathVariant::from_str(&init.variant);
|
|
135
|
+
|
|
136
|
+
let mut tick_bitmap = HashMap::with_capacity(init.tick_bitmap.len());
|
|
137
|
+
for entry in &init.tick_bitmap {
|
|
138
|
+
tick_bitmap.insert(entry.key as i16, bigint_to_u256(&entry.value));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let mut ticks = HashMap::with_capacity(init.ticks.len());
|
|
142
|
+
for entry in &init.ticks {
|
|
143
|
+
ticks.insert(
|
|
144
|
+
entry.key,
|
|
145
|
+
TickInfo {
|
|
146
|
+
liquidity_gross: bigint_to_u256(&entry.liquidity_gross),
|
|
147
|
+
liquidity_net: bigint_to_i256(&entry.liquidity_net),
|
|
148
|
+
initialized: true,
|
|
149
|
+
},
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
let mut observations = HashMap::with_capacity(init.observations.len());
|
|
154
|
+
for entry in &init.observations {
|
|
155
|
+
observations.insert(
|
|
156
|
+
entry.key as u16,
|
|
157
|
+
OracleObservation {
|
|
158
|
+
block_timestamp: bigint_to_u256(&entry.block_timestamp),
|
|
159
|
+
tick_cumulative: bigint_to_i256(&entry.tick_cumulative),
|
|
160
|
+
seconds_per_liquidity_cumulative_x128: bigint_to_u256(
|
|
161
|
+
&entry.seconds_per_liquidity_cumulative_x128,
|
|
162
|
+
),
|
|
163
|
+
initialized: entry.initialized,
|
|
164
|
+
},
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
let start_tick_bitmap = bigint_to_i256(&init.start_tick_bitmap);
|
|
169
|
+
|
|
170
|
+
// bitmap_range is the total half-width (TICK_BITMAP_BUFFER + TICK_BITMAP_TO_USE)
|
|
171
|
+
// passed from TS since it varies per DEX and per network.
|
|
172
|
+
let start_i16 = start_tick_bitmap.as_i32() as i16;
|
|
173
|
+
let range = init.bitmap_range as i16;
|
|
174
|
+
let bitmap_range_lower = start_i16 - range;
|
|
175
|
+
let bitmap_range_upper = start_i16 + range;
|
|
176
|
+
|
|
177
|
+
Ok(Self {
|
|
178
|
+
state: PoolState {
|
|
179
|
+
block_timestamp: bigint_to_u256(&init.block_timestamp),
|
|
180
|
+
tick_spacing: bigint_to_i256(&init.tick_spacing),
|
|
181
|
+
fee: bigint_to_u256(&init.fee),
|
|
182
|
+
sqrt_price_x96: bigint_to_u256(&init.sqrt_price_x96),
|
|
183
|
+
tick: bigint_to_i256(&init.tick),
|
|
184
|
+
observation_index: init.observation_index as u16,
|
|
185
|
+
observation_cardinality: init.observation_cardinality as u16,
|
|
186
|
+
observation_cardinality_next: init.observation_cardinality_next as u16,
|
|
187
|
+
fee_protocol: bigint_to_u256(&init.fee_protocol),
|
|
188
|
+
liquidity: bigint_to_u256(&init.liquidity),
|
|
189
|
+
max_liquidity_per_tick: bigint_to_u256(&init.max_liquidity_per_tick),
|
|
190
|
+
tick_bitmap,
|
|
191
|
+
ticks,
|
|
192
|
+
observations,
|
|
193
|
+
start_tick_bitmap,
|
|
194
|
+
lowest_known_tick: bigint_to_i256(&init.lowest_known_tick),
|
|
195
|
+
highest_known_tick: bigint_to_i256(&init.highest_known_tick),
|
|
196
|
+
bitmap_range_lower,
|
|
197
|
+
bitmap_range_upper,
|
|
198
|
+
variant,
|
|
199
|
+
},
|
|
200
|
+
})
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/// HOT PATH: Price N amounts in one call (BigInt version).
|
|
204
|
+
/// side: 0 = SELL, 1 = BUY
|
|
205
|
+
#[napi]
|
|
206
|
+
pub fn query_outputs(
|
|
207
|
+
&self,
|
|
208
|
+
amounts: Vec<BigInt>,
|
|
209
|
+
zero_for_one: bool,
|
|
210
|
+
side: u8,
|
|
211
|
+
) -> Result<JsOutputResult> {
|
|
212
|
+
let amounts_u256: Vec<U256> = amounts.iter().map(|a| bigint_to_u256(a)).collect();
|
|
213
|
+
|
|
214
|
+
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
|
215
|
+
query_outputs::query_outputs(&self.state, &amounts_u256, zero_for_one, side)
|
|
216
|
+
}));
|
|
217
|
+
|
|
218
|
+
match result {
|
|
219
|
+
Ok(output) => {
|
|
220
|
+
let outputs: Vec<BigInt> =
|
|
221
|
+
output.outputs.iter().map(|v| u256_to_bigint(*v)).collect();
|
|
222
|
+
|
|
223
|
+
Ok(JsOutputResult {
|
|
224
|
+
outputs,
|
|
225
|
+
tick_counts: output.tick_counts,
|
|
226
|
+
})
|
|
227
|
+
}
|
|
228
|
+
Err(panic_info) => {
|
|
229
|
+
let msg = if let Some(s) = panic_info.downcast_ref::<&str>() {
|
|
230
|
+
s.to_string()
|
|
231
|
+
} else if let Some(s) = panic_info.downcast_ref::<String>() {
|
|
232
|
+
s.clone()
|
|
233
|
+
} else {
|
|
234
|
+
"Unknown panic in query_outputs".to_string()
|
|
235
|
+
};
|
|
236
|
+
Err(Error::new(Status::GenericFailure, msg))
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// ---- Pool Registry: batch parallel queries ----
|
|
244
|
+
|
|
245
|
+
fn build_pool_state(init: &JsPoolStateInit) -> Result<PoolState> {
|
|
246
|
+
let variant = MathVariant::from_str(&init.variant);
|
|
247
|
+
|
|
248
|
+
let mut tick_bitmap = HashMap::with_capacity(init.tick_bitmap.len());
|
|
249
|
+
for entry in &init.tick_bitmap {
|
|
250
|
+
tick_bitmap.insert(entry.key as i16, bigint_to_u256(&entry.value));
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
let mut ticks = HashMap::with_capacity(init.ticks.len());
|
|
254
|
+
for entry in &init.ticks {
|
|
255
|
+
ticks.insert(
|
|
256
|
+
entry.key,
|
|
257
|
+
TickInfo {
|
|
258
|
+
liquidity_gross: bigint_to_u256(&entry.liquidity_gross),
|
|
259
|
+
liquidity_net: bigint_to_i256(&entry.liquidity_net),
|
|
260
|
+
initialized: true,
|
|
261
|
+
},
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
let mut observations = HashMap::with_capacity(init.observations.len());
|
|
266
|
+
for entry in &init.observations {
|
|
267
|
+
observations.insert(
|
|
268
|
+
entry.key as u16,
|
|
269
|
+
OracleObservation {
|
|
270
|
+
block_timestamp: bigint_to_u256(&entry.block_timestamp),
|
|
271
|
+
tick_cumulative: bigint_to_i256(&entry.tick_cumulative),
|
|
272
|
+
seconds_per_liquidity_cumulative_x128: bigint_to_u256(
|
|
273
|
+
&entry.seconds_per_liquidity_cumulative_x128,
|
|
274
|
+
),
|
|
275
|
+
initialized: entry.initialized,
|
|
276
|
+
},
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
let start_tick_bitmap = bigint_to_i256(&init.start_tick_bitmap);
|
|
281
|
+
let start_i16 = start_tick_bitmap.as_i32() as i16;
|
|
282
|
+
let range = init.bitmap_range as i16;
|
|
283
|
+
|
|
284
|
+
Ok(PoolState {
|
|
285
|
+
block_timestamp: bigint_to_u256(&init.block_timestamp),
|
|
286
|
+
tick_spacing: bigint_to_i256(&init.tick_spacing),
|
|
287
|
+
fee: bigint_to_u256(&init.fee),
|
|
288
|
+
sqrt_price_x96: bigint_to_u256(&init.sqrt_price_x96),
|
|
289
|
+
tick: bigint_to_i256(&init.tick),
|
|
290
|
+
observation_index: init.observation_index as u16,
|
|
291
|
+
observation_cardinality: init.observation_cardinality as u16,
|
|
292
|
+
observation_cardinality_next: init.observation_cardinality_next as u16,
|
|
293
|
+
fee_protocol: bigint_to_u256(&init.fee_protocol),
|
|
294
|
+
liquidity: bigint_to_u256(&init.liquidity),
|
|
295
|
+
max_liquidity_per_tick: bigint_to_u256(&init.max_liquidity_per_tick),
|
|
296
|
+
tick_bitmap,
|
|
297
|
+
ticks,
|
|
298
|
+
observations,
|
|
299
|
+
start_tick_bitmap,
|
|
300
|
+
lowest_known_tick: bigint_to_i256(&init.lowest_known_tick),
|
|
301
|
+
highest_known_tick: bigint_to_i256(&init.highest_known_tick),
|
|
302
|
+
bitmap_range_lower: start_i16 - range,
|
|
303
|
+
bitmap_range_upper: start_i16 + range,
|
|
304
|
+
variant,
|
|
305
|
+
})
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
#[napi(object)]
|
|
309
|
+
pub struct JsPoolQueryResult {
|
|
310
|
+
pub key: String,
|
|
311
|
+
pub outputs: Vec<BigInt>,
|
|
312
|
+
pub tick_counts: Vec<i32>,
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
#[napi]
|
|
316
|
+
pub struct RustPoolRegistry {
|
|
317
|
+
pools: HashMap<String, PoolState>,
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
#[napi]
|
|
321
|
+
impl RustPoolRegistry {
|
|
322
|
+
#[napi(constructor)]
|
|
323
|
+
pub fn new() -> Self {
|
|
324
|
+
Self {
|
|
325
|
+
pools: HashMap::new(),
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/// Register or update a pool.
|
|
330
|
+
#[napi]
|
|
331
|
+
pub fn set_pool(&mut self, key: String, init: JsPoolStateInit) -> Result<()> {
|
|
332
|
+
let state = build_pool_state(&init)?;
|
|
333
|
+
self.pools.insert(key, state);
|
|
334
|
+
Ok(())
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/// Remove a pool.
|
|
338
|
+
#[napi]
|
|
339
|
+
pub fn remove_pool(&mut self, key: String) {
|
|
340
|
+
self.pools.remove(&key);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/// Query multiple pools in parallel. Returns results for all registered pools
|
|
344
|
+
/// whose keys are in the provided list.
|
|
345
|
+
/// Each pool is queried with the SAME amounts and direction.
|
|
346
|
+
#[napi]
|
|
347
|
+
pub fn query_many(
|
|
348
|
+
&self,
|
|
349
|
+
keys: Vec<String>,
|
|
350
|
+
amounts: Vec<BigInt>,
|
|
351
|
+
zero_for_one: bool,
|
|
352
|
+
side: u8,
|
|
353
|
+
) -> Result<Vec<JsPoolQueryResult>> {
|
|
354
|
+
let amounts_u256: Vec<U256> = amounts.iter().map(|a| bigint_to_u256(a)).collect();
|
|
355
|
+
|
|
356
|
+
// Collect references to pools that exist
|
|
357
|
+
let pool_refs: Vec<(&str, &PoolState)> = keys
|
|
358
|
+
.iter()
|
|
359
|
+
.filter_map(|k| self.pools.get(k).map(|p| (k.as_str(), p)))
|
|
360
|
+
.collect();
|
|
361
|
+
|
|
362
|
+
// Run all pool queries in parallel using rayon
|
|
363
|
+
let results: Vec<Result<JsPoolQueryResult>> = pool_refs
|
|
364
|
+
.par_iter()
|
|
365
|
+
.map(|(key, pool)| {
|
|
366
|
+
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
|
367
|
+
query_outputs::query_outputs(pool, &amounts_u256, zero_for_one, side)
|
|
368
|
+
}));
|
|
369
|
+
|
|
370
|
+
match result {
|
|
371
|
+
Ok(output) => {
|
|
372
|
+
let outputs: Vec<BigInt> =
|
|
373
|
+
output.outputs.iter().map(|v| u256_to_bigint(*v)).collect();
|
|
374
|
+
Ok(JsPoolQueryResult {
|
|
375
|
+
key: key.to_string(),
|
|
376
|
+
outputs,
|
|
377
|
+
tick_counts: output.tick_counts,
|
|
378
|
+
})
|
|
379
|
+
}
|
|
380
|
+
Err(_) => Ok(JsPoolQueryResult {
|
|
381
|
+
key: key.to_string(),
|
|
382
|
+
outputs: vec![],
|
|
383
|
+
tick_counts: vec![],
|
|
384
|
+
}),
|
|
385
|
+
}
|
|
386
|
+
})
|
|
387
|
+
.collect();
|
|
388
|
+
|
|
389
|
+
results.into_iter().collect()
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
#[napi]
|
|
393
|
+
pub fn pool_count(&self) -> u32 {
|
|
394
|
+
self.pools.len() as u32
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// ---- V4 Pool Registry ----
|
|
399
|
+
|
|
400
|
+
#[napi(object)]
|
|
401
|
+
pub struct JsV4PoolStateInit {
|
|
402
|
+
pub sqrt_price_x96: BigInt,
|
|
403
|
+
pub tick: BigInt,
|
|
404
|
+
pub protocol_fee: BigInt,
|
|
405
|
+
pub lp_fee: BigInt,
|
|
406
|
+
pub liquidity: BigInt,
|
|
407
|
+
pub tick_spacing: BigInt,
|
|
408
|
+
pub fee_growth_global0_x128: BigInt,
|
|
409
|
+
pub fee_growth_global1_x128: BigInt,
|
|
410
|
+
pub bitmap_range: i32,
|
|
411
|
+
pub start_tick_bitmap: BigInt,
|
|
412
|
+
pub tick_bitmap: Vec<JsBitmapEntry>,
|
|
413
|
+
pub ticks: Vec<JsTickEntry>,
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
#[napi(object)]
|
|
417
|
+
pub struct JsV4QueryResult {
|
|
418
|
+
pub key: String,
|
|
419
|
+
pub outputs: Vec<BigInt>,
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
fn build_v4_pool_state(init: &JsV4PoolStateInit) -> v4_query_outputs::V4PoolState {
|
|
423
|
+
let mut tick_bitmap = HashMap::with_capacity(init.tick_bitmap.len());
|
|
424
|
+
for entry in &init.tick_bitmap {
|
|
425
|
+
tick_bitmap.insert(entry.key as i16, bigint_to_u256(&entry.value));
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
let mut ticks = HashMap::with_capacity(init.ticks.len());
|
|
429
|
+
for entry in &init.ticks {
|
|
430
|
+
ticks.insert(
|
|
431
|
+
entry.key,
|
|
432
|
+
TickInfo {
|
|
433
|
+
liquidity_gross: bigint_to_u256(&entry.liquidity_gross),
|
|
434
|
+
liquidity_net: bigint_to_i256(&entry.liquidity_net),
|
|
435
|
+
initialized: true,
|
|
436
|
+
},
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
v4_query_outputs::V4PoolState {
|
|
441
|
+
sqrt_price_x96: bigint_to_u256(&init.sqrt_price_x96),
|
|
442
|
+
tick: bigint_to_i256(&init.tick),
|
|
443
|
+
protocol_fee: bigint_to_u256(&init.protocol_fee),
|
|
444
|
+
lp_fee: bigint_to_u256(&init.lp_fee),
|
|
445
|
+
liquidity: bigint_to_u256(&init.liquidity),
|
|
446
|
+
tick_spacing: bigint_to_i256(&init.tick_spacing),
|
|
447
|
+
fee_growth_global0_x128: bigint_to_u256(&init.fee_growth_global0_x128),
|
|
448
|
+
fee_growth_global1_x128: bigint_to_u256(&init.fee_growth_global1_x128),
|
|
449
|
+
tick_bitmap,
|
|
450
|
+
ticks,
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
#[napi]
|
|
455
|
+
pub struct RustV4PoolRegistry {
|
|
456
|
+
pools: HashMap<String, (v4_query_outputs::V4PoolState, I256)>, // state + tickSpacing
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
#[napi]
|
|
460
|
+
impl RustV4PoolRegistry {
|
|
461
|
+
#[napi(constructor)]
|
|
462
|
+
pub fn new() -> Self {
|
|
463
|
+
Self {
|
|
464
|
+
pools: HashMap::new(),
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
#[napi]
|
|
469
|
+
pub fn set_pool(&mut self, key: String, init: JsV4PoolStateInit) {
|
|
470
|
+
let tick_spacing = bigint_to_i256(&init.tick_spacing);
|
|
471
|
+
let state = build_v4_pool_state(&init);
|
|
472
|
+
self.pools.insert(key, (state, tick_spacing));
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
#[napi]
|
|
476
|
+
pub fn remove_pool(&mut self, key: String) {
|
|
477
|
+
self.pools.remove(&key);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
#[napi]
|
|
481
|
+
pub fn query_many(
|
|
482
|
+
&self,
|
|
483
|
+
keys: Vec<String>,
|
|
484
|
+
amounts: Vec<BigInt>,
|
|
485
|
+
zero_for_one: bool,
|
|
486
|
+
side: u8,
|
|
487
|
+
) -> Vec<JsV4QueryResult> {
|
|
488
|
+
let amounts_u256: Vec<U256> = amounts.iter().map(|a| bigint_to_u256(a)).collect();
|
|
489
|
+
|
|
490
|
+
let pool_refs: Vec<(&str, &v4_query_outputs::V4PoolState, I256)> = keys
|
|
491
|
+
.iter()
|
|
492
|
+
.filter_map(|k| {
|
|
493
|
+
self.pools
|
|
494
|
+
.get(k)
|
|
495
|
+
.map(|(p, ts)| (k.as_str(), p, *ts))
|
|
496
|
+
})
|
|
497
|
+
.collect();
|
|
498
|
+
|
|
499
|
+
pool_refs
|
|
500
|
+
.par_iter()
|
|
501
|
+
.map(|(key, pool, tick_spacing)| {
|
|
502
|
+
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
|
503
|
+
v4_query_outputs::query_outputs(pool, *tick_spacing, &amounts_u256, zero_for_one, side)
|
|
504
|
+
}));
|
|
505
|
+
|
|
506
|
+
match result {
|
|
507
|
+
Ok(outputs) => {
|
|
508
|
+
let bigint_outputs: Vec<BigInt> =
|
|
509
|
+
outputs.iter().map(|v| u256_to_bigint(*v)).collect();
|
|
510
|
+
JsV4QueryResult {
|
|
511
|
+
key: key.to_string(),
|
|
512
|
+
outputs: bigint_outputs,
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
Err(_) => JsV4QueryResult {
|
|
516
|
+
key: key.to_string(),
|
|
517
|
+
outputs: vec![],
|
|
518
|
+
},
|
|
519
|
+
}
|
|
520
|
+
})
|
|
521
|
+
.collect()
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
#[napi]
|
|
525
|
+
pub fn pool_count(&self) -> u32 {
|
|
526
|
+
self.pools.len() as u32
|
|
527
|
+
}
|
|
528
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
use ethnum::U256;
|
|
2
|
+
|
|
3
|
+
/// Returns the index of the most significant bit of the number,
|
|
4
|
+
/// where the least significant bit is at index 0 and the most significant bit is at index 255.
|
|
5
|
+
///
|
|
6
|
+
/// Panics if x is zero.
|
|
7
|
+
pub fn most_significant_bit(x: U256) -> u8 {
|
|
8
|
+
assert!(x > U256::ZERO, "x must be > 0");
|
|
9
|
+
let mut x = x;
|
|
10
|
+
let mut r: u8 = 0;
|
|
11
|
+
|
|
12
|
+
if x >= U256::from_words(1, 0) {
|
|
13
|
+
x >>= 128;
|
|
14
|
+
r += 128;
|
|
15
|
+
}
|
|
16
|
+
if x >= U256::from(0x10000000000000000u128) {
|
|
17
|
+
x >>= 64;
|
|
18
|
+
r += 64;
|
|
19
|
+
}
|
|
20
|
+
if x >= U256::from(0x100000000u128) {
|
|
21
|
+
x >>= 32;
|
|
22
|
+
r += 32;
|
|
23
|
+
}
|
|
24
|
+
if x >= U256::from(0x10000u64) {
|
|
25
|
+
x >>= 16;
|
|
26
|
+
r += 16;
|
|
27
|
+
}
|
|
28
|
+
if x >= U256::from(0x100u64) {
|
|
29
|
+
x >>= 8;
|
|
30
|
+
r += 8;
|
|
31
|
+
}
|
|
32
|
+
if x >= U256::from(0x10u64) {
|
|
33
|
+
x >>= 4;
|
|
34
|
+
r += 4;
|
|
35
|
+
}
|
|
36
|
+
if x >= U256::from(0x4u64) {
|
|
37
|
+
x >>= 2;
|
|
38
|
+
r += 2;
|
|
39
|
+
}
|
|
40
|
+
if x >= U256::from(0x2u64) {
|
|
41
|
+
r += 1;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
r
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/// Returns the index of the least significant bit of the number,
|
|
48
|
+
/// where the least significant bit is at index 0 and the most significant bit is at index 255.
|
|
49
|
+
///
|
|
50
|
+
/// Panics if x is zero.
|
|
51
|
+
pub fn least_significant_bit(x: U256) -> u8 {
|
|
52
|
+
assert!(x > U256::ZERO, "x must be > 0");
|
|
53
|
+
let mut x = x;
|
|
54
|
+
let mut r: u8 = 255;
|
|
55
|
+
|
|
56
|
+
let max_uint128: U256 = U256::new(u128::MAX);
|
|
57
|
+
let max_uint64: U256 = U256::from(u64::MAX);
|
|
58
|
+
let max_uint32: U256 = U256::from(u32::MAX as u64);
|
|
59
|
+
let max_uint16: U256 = U256::from(u16::MAX as u64);
|
|
60
|
+
let max_uint8: U256 = U256::from(u8::MAX as u64);
|
|
61
|
+
|
|
62
|
+
if (x & max_uint128) > U256::ZERO {
|
|
63
|
+
r -= 128;
|
|
64
|
+
} else {
|
|
65
|
+
x >>= 128;
|
|
66
|
+
}
|
|
67
|
+
if (x & max_uint64) > U256::ZERO {
|
|
68
|
+
r -= 64;
|
|
69
|
+
} else {
|
|
70
|
+
x >>= 64;
|
|
71
|
+
}
|
|
72
|
+
if (x & max_uint32) > U256::ZERO {
|
|
73
|
+
r -= 32;
|
|
74
|
+
} else {
|
|
75
|
+
x >>= 32;
|
|
76
|
+
}
|
|
77
|
+
if (x & max_uint16) > U256::ZERO {
|
|
78
|
+
r -= 16;
|
|
79
|
+
} else {
|
|
80
|
+
x >>= 16;
|
|
81
|
+
}
|
|
82
|
+
if (x & max_uint8) > U256::ZERO {
|
|
83
|
+
r -= 8;
|
|
84
|
+
} else {
|
|
85
|
+
x >>= 8;
|
|
86
|
+
}
|
|
87
|
+
if (x & U256::from(0xFu64)) > U256::ZERO {
|
|
88
|
+
r -= 4;
|
|
89
|
+
} else {
|
|
90
|
+
x >>= 4;
|
|
91
|
+
}
|
|
92
|
+
if (x & U256::from(0x3u64)) > U256::ZERO {
|
|
93
|
+
r -= 2;
|
|
94
|
+
} else {
|
|
95
|
+
x >>= 2;
|
|
96
|
+
}
|
|
97
|
+
if (x & U256::ONE) > U256::ZERO {
|
|
98
|
+
r -= 1;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
r
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
#[cfg(test)]
|
|
105
|
+
mod tests {
|
|
106
|
+
use super::*;
|
|
107
|
+
|
|
108
|
+
#[test]
|
|
109
|
+
fn test_msb_one() {
|
|
110
|
+
assert_eq!(most_significant_bit(U256::ONE), 0);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
#[test]
|
|
114
|
+
fn test_msb_two() {
|
|
115
|
+
assert_eq!(most_significant_bit(U256::from(2u64)), 1);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
#[test]
|
|
119
|
+
fn test_msb_powers_of_two() {
|
|
120
|
+
for i in 0..=255u8 {
|
|
121
|
+
let x = U256::ONE << i;
|
|
122
|
+
assert_eq!(most_significant_bit(x), i);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
#[test]
|
|
127
|
+
fn test_msb_max() {
|
|
128
|
+
assert_eq!(most_significant_bit(U256::MAX), 255);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
#[test]
|
|
132
|
+
#[should_panic]
|
|
133
|
+
fn test_msb_zero_panics() {
|
|
134
|
+
most_significant_bit(U256::ZERO);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
#[test]
|
|
138
|
+
fn test_lsb_one() {
|
|
139
|
+
assert_eq!(least_significant_bit(U256::ONE), 0);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
#[test]
|
|
143
|
+
fn test_lsb_two() {
|
|
144
|
+
assert_eq!(least_significant_bit(U256::from(2u64)), 1);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
#[test]
|
|
148
|
+
fn test_lsb_powers_of_two() {
|
|
149
|
+
for i in 0..=255u8 {
|
|
150
|
+
let x = U256::ONE << i;
|
|
151
|
+
assert_eq!(least_significant_bit(x), i);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
#[test]
|
|
156
|
+
fn test_lsb_max() {
|
|
157
|
+
assert_eq!(least_significant_bit(U256::MAX), 0);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
#[test]
|
|
161
|
+
#[should_panic]
|
|
162
|
+
fn test_lsb_zero_panics() {
|
|
163
|
+
least_significant_bit(U256::ZERO);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
#[test]
|
|
167
|
+
fn test_msb_mixed() {
|
|
168
|
+
assert_eq!(most_significant_bit(U256::from(10u64)), 3);
|
|
169
|
+
assert_eq!(most_significant_bit(U256::from(24u64)), 4);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
#[test]
|
|
173
|
+
fn test_lsb_mixed() {
|
|
174
|
+
assert_eq!(least_significant_bit(U256::from(10u64)), 1);
|
|
175
|
+
assert_eq!(least_significant_bit(U256::from(24u64)), 3);
|
|
176
|
+
}
|
|
177
|
+
}
|