@paraswap/dex-lib 3.4.0 → 3.4.1
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/README.md +3 -3
- package/abis.txt +92 -0
- package/build/abi/uniswap-v3/AlienBaseV3Router.abi.json +52 -0
- package/build/abi/{integral/relayer.json → wombat/pool.json} +587 -446
- package/build/config.js +0 -35
- package/build/config.js.map +1 -1
- package/build/constants.d.ts +0 -1
- package/build/constants.js +0 -1
- package/build/constants.js.map +1 -1
- package/build/dex/augustus-rfq.js +0 -1
- package/build/dex/augustus-rfq.js.map +1 -1
- package/build/dex/curve-v1/config.js +1 -57
- package/build/dex/curve-v1/config.js.map +1 -1
- package/build/dex/curve-v1-factory/config.js +1 -1
- package/build/dex/curve-v1-factory/config.js.map +1 -1
- package/build/dex/etherfi.js +1 -1
- package/build/dex/gmx/gmx.js.map +1 -1
- package/build/dex/index.js +0 -14
- package/build/dex/index.js.map +1 -1
- package/build/dex/nerve/config.js +0 -399
- package/build/dex/nerve/config.js.map +1 -1
- package/build/dex/paraswap-limit-orders/config.js +0 -7
- package/build/dex/paraswap-limit-orders/config.js.map +1 -1
- package/build/dex/quick-perps/quick-perps.js.map +1 -1
- package/build/dex/quickswap/config.js +0 -8
- package/build/dex/quickswap/config.js.map +1 -1
- package/build/dex/solidly/config.js +1 -27
- package/build/dex/solidly/config.js.map +1 -1
- package/build/dex/solidly/solidly.js +0 -2
- package/build/dex/solidly/solidly.js.map +1 -1
- 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/stable-pool.js +1 -8
- package/build/dex/stable-pool/stable-pool.js.map +1 -1
- package/build/dex/stable-pool.js +1 -8
- package/build/dex/stable-pool.js.map +1 -1
- package/build/dex/uniswap-v2/config.js +0 -152
- package/build/dex/uniswap-v2/config.js.map +1 -1
- package/build/dex/uniswap-v2/constants.js +0 -1
- package/build/dex/uniswap-v2/constants.js.map +1 -1
- 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/weth/config.js +0 -3
- package/build/dex/weth/config.js.map +1 -1
- package/build/dex/wsteth/wsteth.js.map +1 -1
- package/build/types.d.ts +0 -1
- package/package.json +1 -1
- package/src/config.ts +0 -39
- package/src/constants.ts +0 -1
- package/src/dex/augustus-rfq.ts +0 -1
- package/src/dex/curve-v1/config.ts +1 -57
- package/src/dex/curve-v1/curve-v1-e2e.test.ts +0 -51
- package/src/dex/curve-v1-factory/config.ts +1 -1
- package/src/dex/etherfi.ts +1 -1
- package/src/dex/gmx/gmx.ts +1 -4
- package/src/dex/index.ts +0 -14
- package/src/dex/nerve/config.ts +0 -400
- package/src/dex/nerve/nerve-e2e.test.ts +0 -507
- package/src/dex/nerve/nerve-events.test.ts +0 -268
- package/src/dex/paraswap-limit-orders/config.ts +0 -7
- package/src/dex/paraswap-limit-orders/paraswap-limit-orders-e2e.test.ts +2 -2
- package/src/dex/paraswap-limit-orders/paraswap-limit-orders-integration.test.ts +1 -1
- package/src/dex/quick-perps/quick-perps.ts +1 -4
- package/src/dex/quickswap/config.ts +0 -9
- package/src/dex/quickswap/quickswap-e2e.test.ts +0 -97
- package/src/dex/solidly/config.ts +1 -30
- package/src/dex/solidly/solidly-e2e.test.ts +0 -280
- package/src/dex/solidly/solidly-integration.test.ts +0 -135
- package/src/dex/solidly/solidly.ts +0 -2
- package/src/dex/stable-pool/stable-pool.ts +1 -8
- package/src/dex/uniswap-v2/config.ts +0 -185
- package/src/dex/uniswap-v2/constants.ts +0 -1
- package/src/dex/uniswap-v2/uniswap-v2-e2e-avalanche.test.ts +0 -272
- package/src/dex/uniswap-v2/uniswap-v2-e2e-base.test.ts +0 -6
- package/src/dex/uniswap-v2/uniswap-v2-e2e-bsc.test.ts +0 -325
- package/src/dex/uniswap-v2/uniswap-v2-e2e-mainnet.test.ts +0 -175
- package/src/dex/uniswap-v2/uniswap-v2-e2e-polygon.test.ts +0 -413
- package/src/dex/weth/config.ts +0 -3
- package/src/dex/wsteth/wsteth.ts +1 -4
- package/src/types.ts +0 -1
- package/tests/constants-e2e.ts +0 -21
- package/tests/generate-new-limit-order.ts +1 -1
- package/.idea/aws.xml +0 -17
- package/.idea/codeStyles/Project.xml +0 -19
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/paraswap-dex-lib.iml +0 -9
- package/.idea/prettier.xml +0 -7
- package/.idea/vcs.xml +0 -6
- package/build/abi/AugustusV6.abi.json +0 -395
- package/build/abi/algebra/AlgebraPool.abi.json +0 -727
- package/build/abi/curve-v1/StableSwapFRXETH.json +0 -889
- package/build/abi/curve-v1/StableSwapWBETH.json +0 -1223
- package/build/abi/sushiswap-v3/QuoterV2.json +0 -267
- package/build/abi/sushiswap-v3/RouterProcessor3.json +0 -289
- package/build/abi/uniswap-v3/UniswapV3Quoter.abi.json +0 -193
- package/build/abi/uniswap-v3/pancakeswap-v3/PancakeswapV3QuoterV2.abi.json +0 -285
- package/build/abi/uniswap-v3/pancakeswap-v3/PancakeswapV3Router.abi.json +0 -583
- package/build/abi/wBETH.json +0 -1527
- package/build/abi/woo-fi/WooFeeManager.abi.json +0 -318
- package/build/abi/woo-fi/WooPP.abi.json +0 -548
- package/build/abi/woo-fi/Wooracle.abi.json +0 -260
- package/build/dex/aave-v3/tokens/arbitrum.json +0 -50
- package/build/dex/aave-v3/tokens/avalanche.json +0 -74
- package/build/dex/aave-v3/tokens/fantom.json +0 -62
- package/build/dex/aave-v3/tokens/optimism.json +0 -50
- package/build/dex/aave-v3/tokens/polygon.json +0 -116
- package/build/dex/algebra/algebra-pool.d.ts +0 -114
- package/build/dex/algebra/algebra-pool.js +0 -448
- package/build/dex/algebra/algebra-pool.js.map +0 -1
- package/build/dex/balancer-v2/Gyro3Pool.d.ts +0 -43
- package/build/dex/balancer-v2/Gyro3Pool.js +0 -160
- package/build/dex/balancer-v2/Gyro3Pool.js.map +0 -1
- package/build/dex/balancer-v2/LinearMath.d.ts +0 -88
- package/build/dex/balancer-v2/LinearMath.js +0 -285
- package/build/dex/balancer-v2/LinearMath.js.map +0 -1
- package/build/dex/balancer-v2/LinearPool.d.ts +0 -81
- package/build/dex/balancer-v2/LinearPool.js +0 -444
- package/build/dex/balancer-v2/LinearPool.js.map +0 -1
- package/build/dex/balancer-v2/PhantomStablePool.d.ts +0 -90
- package/build/dex/balancer-v2/PhantomStablePool.js +0 -418
- package/build/dex/balancer-v2/PhantomStablePool.js.map +0 -1
- package/build/dex/balancer-v2/StableMath.d.ts +0 -9
- package/build/dex/balancer-v2/StableMath.js +0 -272
- package/build/dex/balancer-v2/StableMath.js.map +0 -1
- package/build/dex/balancer-v2/balancer-v2-pool.d.ts +0 -140
- package/build/dex/balancer-v2/balancer-v2-pool.js +0 -647
- package/build/dex/balancer-v2/balancer-v2-pool.js.map +0 -1
- package/build/dex/curve-v1/pools/frxETHpool.d.ts +0 -7
- package/build/dex/curve-v1/pools/frxETHpool.js +0 -27
- package/build/dex/curve-v1/pools/frxETHpool.js.map +0 -1
- package/build/dex/curve-v1/pools/wbETHpool.d.ts +0 -29
- package/build/dex/curve-v1/pools/wbETHpool.js +0 -408
- package/build/dex/curve-v1/pools/wbETHpool.js.map +0 -1
- package/build/dex/integral/config.d.ts +0 -4
- package/build/dex/integral/config.js +0 -18
- package/build/dex/integral/config.js.map +0 -1
- package/build/dex/integral/integral-pool.d.ts +0 -39
- package/build/dex/integral/integral-pool.js +0 -67
- package/build/dex/integral/integral-pool.js.map +0 -1
- package/build/dex/integral/integral.d.ts +0 -48
- package/build/dex/integral/integral.js +0 -402
- package/build/dex/integral/integral.js.map +0 -1
- package/build/dex/integral/types.d.ts +0 -25
- package/build/dex/integral/types.js +0 -9
- package/build/dex/integral/types.js.map +0 -1
- package/build/dex/lighter-v1/abi/erc20.json +0 -222
- package/build/dex/lighter-v1/abi/factory.json +0 -291
- package/build/dex/lighter-v1/abi/order_book.json +0 -594
- package/build/dex/lighter-v1/abi/order_book_helper.json +0 -176
- package/build/dex/lighter-v1/abi/router.json +0 -488
- package/build/dex/lighter-v1/config.d.ts +0 -4
- package/build/dex/lighter-v1/config.js +0 -17
- package/build/dex/lighter-v1/config.js.map +0 -1
- package/build/dex/lighter-v1/constants.d.ts +0 -2
- package/build/dex/lighter-v1/constants.js +0 -13
- package/build/dex/lighter-v1/constants.js.map +0 -1
- package/build/dex/lighter-v1/lighter-v1-pool.d.ts +0 -57
- package/build/dex/lighter-v1/lighter-v1-pool.js +0 -310
- package/build/dex/lighter-v1/lighter-v1-pool.js.map +0 -1
- package/build/dex/lighter-v1/lighter-v1.d.ts +0 -45
- package/build/dex/lighter-v1/lighter-v1.js +0 -331
- package/build/dex/lighter-v1/lighter-v1.js.map +0 -1
- package/build/dex/lighter-v1/types.d.ts +0 -40
- package/build/dex/lighter-v1/types.js +0 -3
- package/build/dex/lighter-v1/types.js.map +0 -1
- package/build/dex/pancakeswap-v3/types.d.ts +0 -14
- package/build/dex/pancakeswap-v3/types.js +0 -3
- package/build/dex/pancakeswap-v3/types.js.map +0 -1
- package/build/dex/quickswap/camelot-v3.d.ts +0 -6
- package/build/dex/quickswap/camelot-v3.js +0 -19
- package/build/dex/quickswap/camelot-v3.js.map +0 -1
- package/build/dex/quickswap/zyberswap-v3.d.ts +0 -6
- package/build/dex/quickswap/zyberswap-v3.js +0 -19
- package/build/dex/quickswap/zyberswap-v3.js.map +0 -1
- package/build/dex/quickswap-v3.d.ts +0 -21
- package/build/dex/quickswap-v3.js +0 -40
- package/build/dex/quickswap-v3.js.map +0 -1
- package/build/dex/ramses-v2/config.d.ts +0 -4
- package/build/dex/ramses-v2/config.js +0 -28
- package/build/dex/ramses-v2/config.js.map +0 -1
- package/build/dex/ramses-v2/constants.d.ts +0 -28
- package/build/dex/ramses-v2/constants.js +0 -35
- package/build/dex/ramses-v2/constants.js.map +0 -1
- package/build/dex/ramses-v2/contract-math/BitMath.d.ts +0 -4
- package/build/dex/ramses-v2/contract-math/BitMath.js +0 -93
- package/build/dex/ramses-v2/contract-math/BitMath.js.map +0 -1
- package/build/dex/ramses-v2/contract-math/FixedPoint128.d.ts +0 -3
- package/build/dex/ramses-v2/contract-math/FixedPoint128.js +0 -8
- package/build/dex/ramses-v2/contract-math/FixedPoint128.js.map +0 -1
- package/build/dex/ramses-v2/contract-math/FixedPoint96.d.ts +0 -4
- package/build/dex/ramses-v2/contract-math/FixedPoint96.js +0 -9
- package/build/dex/ramses-v2/contract-math/FixedPoint96.js.map +0 -1
- package/build/dex/ramses-v2/contract-math/FullMath.d.ts +0 -4
- package/build/dex/ramses-v2/contract-math/FullMath.js +0 -19
- package/build/dex/ramses-v2/contract-math/FullMath.js.map +0 -1
- package/build/dex/ramses-v2/contract-math/LiquidityMath.d.ts +0 -3
- package/build/dex/ramses-v2/contract-math/LiquidityMath.js +0 -22
- package/build/dex/ramses-v2/contract-math/LiquidityMath.js.map +0 -1
- package/build/dex/ramses-v2/contract-math/Oracle.d.ts +0 -10
- package/build/dex/ramses-v2/contract-math/Oracle.js +0 -133
- package/build/dex/ramses-v2/contract-math/Oracle.js.map +0 -1
- package/build/dex/ramses-v2/contract-math/SqrtPriceMath.d.ts +0 -10
- package/build/dex/ramses-v2/contract-math/SqrtPriceMath.js +0 -91
- package/build/dex/ramses-v2/contract-math/SqrtPriceMath.js.map +0 -1
- package/build/dex/ramses-v2/contract-math/SwapMath.d.ts +0 -8
- package/build/dex/ramses-v2/contract-math/SwapMath.js +0 -70
- package/build/dex/ramses-v2/contract-math/SwapMath.js.map +0 -1
- package/build/dex/ramses-v2/contract-math/Tick.d.ts +0 -7
- package/build/dex/ramses-v2/contract-math/Tick.js +0 -45
- package/build/dex/ramses-v2/contract-math/Tick.js.map +0 -1
- package/build/dex/ramses-v2/contract-math/TickBitMap.d.ts +0 -8
- package/build/dex/ramses-v2/contract-math/TickBitMap.js +0 -87
- package/build/dex/ramses-v2/contract-math/TickBitMap.js.map +0 -1
- package/build/dex/ramses-v2/contract-math/TickMath.d.ts +0 -8
- package/build/dex/ramses-v2/contract-math/TickMath.js +0 -162
- package/build/dex/ramses-v2/contract-math/TickMath.js.map +0 -1
- package/build/dex/ramses-v2/contract-math/UnsafeMath.d.ts +0 -3
- package/build/dex/ramses-v2/contract-math/UnsafeMath.js +0 -10
- package/build/dex/ramses-v2/contract-math/UnsafeMath.js.map +0 -1
- package/build/dex/ramses-v2/contract-math/uniswap-v3-math.d.ts +0 -18
- package/build/dex/ramses-v2/contract-math/uniswap-v3-math.js +0 -392
- package/build/dex/ramses-v2/contract-math/uniswap-v3-math.js.map +0 -1
- package/build/dex/ramses-v2/contract-math/utils.d.ts +0 -3
- package/build/dex/ramses-v2/contract-math/utils.js +0 -16
- package/build/dex/ramses-v2/contract-math/utils.js.map +0 -1
- package/build/dex/ramses-v2/ramses-v2-pool.d.ts +0 -42
- package/build/dex/ramses-v2/ramses-v2-pool.js +0 -307
- package/build/dex/ramses-v2/ramses-v2-pool.js.map +0 -1
- package/build/dex/ramses-v2/ramses-v2.d.ts +0 -61
- package/build/dex/ramses-v2/ramses-v2.js +0 -690
- package/build/dex/ramses-v2/ramses-v2.js.map +0 -1
- package/build/dex/ramses-v2/types.d.ts +0 -154
- package/build/dex/ramses-v2/types.js +0 -9
- package/build/dex/ramses-v2/types.js.map +0 -1
- package/build/dex/ramses-v2/utils.d.ts +0 -6
- package/build/dex/ramses-v2/utils.js +0 -71
- package/build/dex/ramses-v2/utils.js.map +0 -1
- package/build/dex/solidly/forks-override/fvm.d.ts +0 -20
- package/build/dex/solidly/forks-override/fvm.js +0 -42
- package/build/dex/solidly/forks-override/fvm.js.map +0 -1
- package/build/dex/sushiswap-v3/config.d.ts +0 -4
- package/build/dex/sushiswap-v3/config.js +0 -132
- package/build/dex/sushiswap-v3/config.js.map +0 -1
- package/build/dex/sushiswap-v3/constants.d.ts +0 -4
- package/build/dex/sushiswap-v3/constants.js +0 -32
- package/build/dex/sushiswap-v3/constants.js.map +0 -1
- package/build/dex/sushiswap-v3/sushiswap-v3-original.d.ts +0 -56
- package/build/dex/sushiswap-v3/sushiswap-v3-original.js +0 -598
- package/build/dex/sushiswap-v3/sushiswap-v3-original.js.map +0 -1
- package/build/dex/sushiswap-v3/sushiswap-v3-pool.d.ts +0 -1
- package/build/dex/sushiswap-v3/sushiswap-v3-pool.js +0 -6
- package/build/dex/sushiswap-v3/sushiswap-v3-pool.js.map +0 -1
- package/build/dex/sushiswap-v3/sushiswap-v3.d.ts +0 -21
- package/build/dex/sushiswap-v3/sushiswap-v3.js +0 -58
- package/build/dex/sushiswap-v3/sushiswap-v3.js.map +0 -1
- package/build/dex/sushiswap-v3/token.d.ts +0 -6
- package/build/dex/sushiswap-v3/token.js +0 -23
- package/build/dex/sushiswap-v3/token.js.map +0 -1
- package/build/dex/sushiswap-v3/types.d.ts +0 -15
- package/build/dex/sushiswap-v3/types.js +0 -18
- package/build/dex/sushiswap-v3/types.js.map +0 -1
- package/build/dex/uniswap-v3/forks/pancakeswap-v3/pancakeswap-v3.d.ts +0 -12
- package/build/dex/uniswap-v3/forks/pancakeswap-v3/pancakeswap-v3.js +0 -23
- package/build/dex/uniswap-v3/forks/pancakeswap-v3/pancakeswap-v3.js.map +0 -1
- package/build/dex/woo-fi/config.d.ts +0 -4
- package/build/dex/woo-fi/config.js +0 -141
- package/build/dex/woo-fi/config.js.map +0 -1
- package/build/dex/woo-fi/constants.d.ts +0 -5
- package/build/dex/woo-fi/constants.js +0 -20
- package/build/dex/woo-fi/constants.js.map +0 -1
- package/build/dex/woo-fi/types.d.ts +0 -48
- package/build/dex/woo-fi/types.js +0 -3
- package/build/dex/woo-fi/types.js.map +0 -1
- package/build/dex/woo-fi/woo-fi-decimal-math.d.ts +0 -14
- package/build/dex/woo-fi/woo-fi-decimal-math.js +0 -42
- package/build/dex/woo-fi/woo-fi-decimal-math.js.map +0 -1
- package/build/dex/woo-fi/woo-fi-math.d.ts +0 -31
- package/build/dex/woo-fi/woo-fi-math.js +0 -300
- package/build/dex/woo-fi/woo-fi-math.js.map +0 -1
- package/build/dex/woo-fi/woo-fi.d.ts +0 -65
- package/build/dex/woo-fi/woo-fi.js +0 -480
- package/build/dex/woo-fi/woo-fi.js.map +0 -1
- package/build/executor/IExecutorBytecodeBuilder.d.ts +0 -6
- package/build/executor/IExecutorBytecodeBuilder.js +0 -3
- package/build/executor/IExecutorBytecodeBuilder.js.map +0 -1
- package/src/abi/AaveV1_lending_pool.json +0 -27
- package/src/abi/platypus/asset.json +0 -490
- package/src/abi/platypus/avax-pool.json +0 -820
- package/src/abi/platypus/oracle.json +0 -119
- package/src/abi/platypus/pool.json +0 -677
- package/src/abi/smardex/all/smardex-router.json +0 -648
- package/src/abi/smardex/layer-1/smardex-factory.json +0 -242
- package/src/abi/smardex/layer-1/smardex-pool.json +0 -506
- package/src/abi/smardex/layer-2/smardex-factory.json +0 -185
- package/src/abi/smardex/layer-2/smardex-pool.json +0 -578
- package/src/abi/swaap-v1/asset.json +0 -490
- package/src/abi/swaap-v1/oracle.json +0 -119
- package/src/abi/swaap-v1/pool.json +0 -1346
- package/src/dex/aave-v1/aave-v1-e2e.test.ts +0 -105
- package/src/dex/aave-v1/aave-v1-integration.test.ts +0 -100
- package/src/dex/aave-v1/aave-v1.ts +0 -261
- package/src/dex/aave-v1/config.ts +0 -22
- package/src/dex/aave-v1/tokens-mainnet.json +0 -134
- package/src/dex/aave-v1/tokens.ts +0 -58
- package/src/dex/aave-v1/types.ts +0 -24
- package/src/dex/platypus/asset.ts +0 -139
- package/src/dex/platypus/config.ts +0 -72
- package/src/dex/platypus/platypus-e2e.test.ts +0 -168
- package/src/dex/platypus/platypus-events.test.ts +0 -167
- package/src/dex/platypus/platypus-integration.test.ts +0 -256
- package/src/dex/platypus/platypus.ts +0 -639
- package/src/dex/platypus/pool-avax.ts +0 -92
- package/src/dex/platypus/pool-base.ts +0 -155
- package/src/dex/platypus/pool-pure.ts +0 -71
- package/src/dex/platypus/pool-subscriber.ts +0 -239
- package/src/dex/platypus/pool.ts +0 -99
- package/src/dex/platypus/types.ts +0 -104
- package/src/dex/quickswap/spiritswap-v3.ts +0 -19
- package/src/dex/smardex/config.ts +0 -67
- package/src/dex/smardex/constants.ts +0 -35
- package/src/dex/smardex/sdk/constants.ts +0 -10
- package/src/dex/smardex/sdk/core.ts +0 -832
- package/src/dex/smardex/sdk/errors.ts +0 -18
- package/src/dex/smardex/sdk/types.ts +0 -68
- package/src/dex/smardex/sdk/utils.ts +0 -156
- package/src/dex/smardex/smardex-e2e.test.ts +0 -275
- package/src/dex/smardex/smardex-event-pool.ts +0 -164
- package/src/dex/smardex/smardex-events.test.ts +0 -297
- package/src/dex/smardex/smardex-integration.test.ts +0 -258
- package/src/dex/smardex/smardex.ts +0 -770
- package/src/dex/smardex/types.ts +0 -69
- package/src/dex/solidly/forks-override/cone.ts +0 -67
- package/src/dex/solidly/forks-override/solisnek.ts +0 -10
- package/src/dex/swaap-v1/config.ts +0 -62
- package/src/dex/swaap-v1/constants.ts +0 -22
- package/src/dex/swaap-v1/libraries/ChainlinkUtils.ts +0 -152
- package/src/dex/swaap-v1/libraries/Const.ts +0 -29
- package/src/dex/swaap-v1/libraries/GeometricBrownianMotion.ts +0 -433
- package/src/dex/swaap-v1/libraries/LogExpMath.ts +0 -511
- package/src/dex/swaap-v1/libraries/MathMMM.ts +0 -717
- package/src/dex/swaap-v1/libraries/Num.ts +0 -184
- package/src/dex/swaap-v1/libraries/PoolQuotations.ts +0 -322
- package/src/dex/swaap-v1/libraries/Struct.ts +0 -66
- package/src/dex/swaap-v1/swaap-v1-e2e.test.ts +0 -75
- package/src/dex/swaap-v1/swaap-v1-events.test.ts +0 -138
- package/src/dex/swaap-v1/swaap-v1-integration.test.ts +0 -232
- package/src/dex/swaap-v1/swaap-v1-pool.ts +0 -919
- package/src/dex/swaap-v1/swaap-v1.ts +0 -668
- package/src/dex/swaap-v1/types.ts +0 -124
- package/src/dex/uniswap-v2/uniswap-v2-e2e-ropsten.test.ts +0 -481
|
@@ -1,511 +0,0 @@
|
|
|
1
|
-
import { BI_MAX_INT, BI_POWS } from '../../../bigint-constants';
|
|
2
|
-
|
|
3
|
-
const _require = (b: boolean, message: string) => {
|
|
4
|
-
if (!b) throw new Error(message);
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
/* solhint-disable */
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @dev Exponentiation and logarithm functions for 18 decimal fixed point numbers (both base and exponent/argument).
|
|
11
|
-
*
|
|
12
|
-
* Exponentiation and logarithm with arbitrary bases (x^y and log_x(y)) are implemented by conversion to natural
|
|
13
|
-
* exponentiation and logarithm (where the base is Euler's number).
|
|
14
|
-
*
|
|
15
|
-
* @author Fernando Martinelli - @fernandomartinelli
|
|
16
|
-
* @author Sergio Yuhjtman - @sergioyuhjtman
|
|
17
|
-
* @author Daniel Fernandez - @dmf7z
|
|
18
|
-
*/
|
|
19
|
-
export class LogExpMath {
|
|
20
|
-
// All fixed point multiplications and divisions are inlined. This means we need to divide by ONE when multiplying
|
|
21
|
-
// two numbers, and multiply by ONE when dividing them.
|
|
22
|
-
|
|
23
|
-
// All arguments and return values are 18 decimal fixed point numbers.
|
|
24
|
-
static ONE_18 = BI_POWS[18];
|
|
25
|
-
|
|
26
|
-
// Internally, intermediate values are computed with higher precision as 20 decimal fixed point numbers, and in the
|
|
27
|
-
// case of ln36, 36 decimals.
|
|
28
|
-
static ONE_20 = BI_POWS[20];
|
|
29
|
-
static ONE_36 = BI_POWS[36];
|
|
30
|
-
|
|
31
|
-
// The domain of natural exponentiation is bound by the word size and number of decimals used.
|
|
32
|
-
//
|
|
33
|
-
// Because internally the result will be stored using 20 decimals, the largest possible result is
|
|
34
|
-
// (2^255 - 1) / 10^20, which makes the largest exponent ln((2^255 - 1) / 10^20) = 130.700829182905140221.
|
|
35
|
-
// The smallest possible result is 10^(-18), which makes largest negative argument
|
|
36
|
-
// ln(10^(-18)) = -41.446531673892822312.
|
|
37
|
-
// We use 130.0 and -41.0 to have some safety margin.
|
|
38
|
-
static MAX_NATURAL_EXPONENT = 130n * BI_POWS[18];
|
|
39
|
-
static MIN_NATURAL_EXPONENT = -41n * BI_POWS[18];
|
|
40
|
-
|
|
41
|
-
// Bounds for ln_36's argument. Both ln(0.9) and ln(1.1) can be represented with 36 decimal places in a fixed point
|
|
42
|
-
// 256 bit integer.
|
|
43
|
-
static LN_36_LOWER_BOUND = this.ONE_18 - BI_POWS[17];
|
|
44
|
-
static LN_36_UPPER_BOUND = this.ONE_18 + BI_POWS[17];
|
|
45
|
-
|
|
46
|
-
static MILD_EXPONENT_BOUND = 2n ** 254n / this.ONE_20;
|
|
47
|
-
|
|
48
|
-
// 18 decimalants
|
|
49
|
-
static x0 = 128000000000000000000n; // 2ˆ7
|
|
50
|
-
static a0 = 38877084059945950922200000000000000000000000000000000000n; // eˆ(x0) (no decimals)
|
|
51
|
-
static x1 = 64000000000000000000n; // 2ˆ6
|
|
52
|
-
static a1 = 6235149080811616882910000000n; // eˆ(x1) (no decimals)
|
|
53
|
-
|
|
54
|
-
// 20 decimalants
|
|
55
|
-
static x2 = 3200000000000000000000n; // 2ˆ5
|
|
56
|
-
static a2 = 7896296018268069516100000000000000n; // eˆ(x2)
|
|
57
|
-
static x3 = 1600000000000000000000n; // 2ˆ4
|
|
58
|
-
static a3 = 888611052050787263676000000n; // eˆ(x3)
|
|
59
|
-
static x4 = 800000000000000000000n; // 2ˆ3
|
|
60
|
-
static a4 = 298095798704172827474000n; // eˆ(x4)
|
|
61
|
-
static x5 = 400000000000000000000n; // 2ˆ2
|
|
62
|
-
static a5 = 5459815003314423907810n; // eˆ(x5)
|
|
63
|
-
static x6 = 200000000000000000000n; // 2ˆ1
|
|
64
|
-
static a6 = 738905609893065022723n; // eˆ(x6)
|
|
65
|
-
static x7 = 100000000000000000000n; // 2ˆ0
|
|
66
|
-
static a7 = 271828182845904523536n; // eˆ(x7)
|
|
67
|
-
static x8 = 50000000000000000000n; // 2ˆ-1
|
|
68
|
-
static a8 = 164872127070012814685n; // eˆ(x8)
|
|
69
|
-
static x9 = 25000000000000000000n; // 2ˆ-2
|
|
70
|
-
static a9 = 128402541668774148407n; // eˆ(x9)
|
|
71
|
-
static x10 = 12500000000000000000n; // 2ˆ-3
|
|
72
|
-
static a10 = 113314845306682631683n; // eˆ(x10)
|
|
73
|
-
static x11 = 6250000000000000000n; // 2ˆ-4
|
|
74
|
-
static a11 = 106449445891785942956n; // eˆ(x11)
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* @dev Exponentiation (x^y) with unsigned 18 decimal fixed point base and exponent.
|
|
78
|
-
*
|
|
79
|
-
* Reverts if ln(x) * y is smaller than `MIN_NATURAL_EXPONENT`, or larger than `MAX_NATURAL_EXPONENT`.
|
|
80
|
-
*/
|
|
81
|
-
static pow(x: bigint, y: bigint): bigint {
|
|
82
|
-
if (y == 0n) {
|
|
83
|
-
// We solve the 0^0 indetermination by making it equal one.
|
|
84
|
-
return this.ONE_18;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (x == 0n) {
|
|
88
|
-
return 0n;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// Instead of computing x^y directly, we instead rely on the properties of logarithms and exponentiation to
|
|
92
|
-
// arrive at that result. In particular, exp(ln(x)) = x, and ln(x^y) = y * ln(x). This means
|
|
93
|
-
// x^y = exp(y * ln(x)).
|
|
94
|
-
|
|
95
|
-
// The ln function takes a signed value, so we need to make sure x fits in the signed 256 bit range.
|
|
96
|
-
_require(x < 2n ** 255n, 'Err.X_OUT_OF_BOUNDS');
|
|
97
|
-
const x_int256 = x;
|
|
98
|
-
|
|
99
|
-
// We will compute y * ln(x) in a single step. Depending on the value of x, we can either use ln or ln_36. In
|
|
100
|
-
// both cases, we leave the division by ONE_18 (due to fixed point multiplication) to the end.
|
|
101
|
-
|
|
102
|
-
// This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 256 bit range.
|
|
103
|
-
_require(y < this.MILD_EXPONENT_BOUND, 'Err.Y_OUT_OF_BOUNDS');
|
|
104
|
-
const y_int256 = y;
|
|
105
|
-
|
|
106
|
-
let logx_times_y;
|
|
107
|
-
if (
|
|
108
|
-
this.LN_36_LOWER_BOUND < x_int256 &&
|
|
109
|
-
x_int256 < this.LN_36_UPPER_BOUND
|
|
110
|
-
) {
|
|
111
|
-
const ln_36_x = this._ln_36(x_int256);
|
|
112
|
-
|
|
113
|
-
// ln_36_x has 36 decimal places, so multiplying by y_int256 isn't as straightforward, since we can't just
|
|
114
|
-
// bring y_int256 to 36 decimal places, as it might overflow. Instead, we perform two 18 decimal
|
|
115
|
-
// multiplications and add the results: one with the first 18 decimals of ln_36_x, and one with the
|
|
116
|
-
// (downscaled) last 18 decimals.
|
|
117
|
-
logx_times_y =
|
|
118
|
-
(ln_36_x / this.ONE_18) * y_int256 +
|
|
119
|
-
((ln_36_x % this.ONE_18) * y_int256) / this.ONE_18;
|
|
120
|
-
} else {
|
|
121
|
-
logx_times_y = this._ln(x_int256) * y_int256;
|
|
122
|
-
}
|
|
123
|
-
logx_times_y /= this.ONE_18;
|
|
124
|
-
|
|
125
|
-
// Finally, we compute exp(y * ln(x)) to arrive at x^y
|
|
126
|
-
_require(
|
|
127
|
-
this.MIN_NATURAL_EXPONENT <= logx_times_y &&
|
|
128
|
-
logx_times_y <= this.MAX_NATURAL_EXPONENT,
|
|
129
|
-
'Err.PRODUCT_OUT_OF_BOUNDS',
|
|
130
|
-
);
|
|
131
|
-
|
|
132
|
-
return this.exp(logx_times_y);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* @dev Natural exponentiation (e^x) with signed 18 decimal fixed point exponent.
|
|
137
|
-
*
|
|
138
|
-
* Reverts if `x` is smaller than MIN_NATURAL_EXPONENT, or larger than `MAX_NATURAL_EXPONENT`.
|
|
139
|
-
*/
|
|
140
|
-
static exp(x: bigint): bigint {
|
|
141
|
-
_require(
|
|
142
|
-
x >= this.MIN_NATURAL_EXPONENT && x <= this.MAX_NATURAL_EXPONENT,
|
|
143
|
-
'Err.INVALID_EXPONENT',
|
|
144
|
-
);
|
|
145
|
-
|
|
146
|
-
if (x < 0) {
|
|
147
|
-
// We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it
|
|
148
|
-
// fits in the signed 256 bit range (as it is larger than MIN_NATURAL_EXPONENT).
|
|
149
|
-
// Fixed point division requires multiplying by ONE_18.
|
|
150
|
-
return (this.ONE_18 * this.ONE_18) / this.exp(-x);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// First, we use the fact that e^(x+y) = e^x * e^y to decompose x into a sum of powers of two, which we call x_n,
|
|
154
|
-
// where x_n == 2^(7 - n), and e^x_n = a_n has been precomputed. We choose the first x_n, x0, to equal 2^7
|
|
155
|
-
// because all larger powers are larger than MAX_NATURAL_EXPONENT, and therefore not present in the
|
|
156
|
-
// decomposition.
|
|
157
|
-
// At the end of this process we will have the product of all e^x_n = a_n that apply, and the remainder of this
|
|
158
|
-
// decomposition, which will be lower than the smallest x_n.
|
|
159
|
-
// exp(x) = k_0 * a_0 * k_1 * a_1 * ... + k_n * a_n * exp(remainder), where each k_n equals either 0 or 1.
|
|
160
|
-
// We mutate x by subtracting x_n, making it the remainder of the decomposition.
|
|
161
|
-
|
|
162
|
-
// The first two a_n (e^(2^7) and e^(2^6)) are too large if stored as 18 decimal numbers, and could cause
|
|
163
|
-
// intermediate overflows. Instead we store them as plain integers, with 0 decimals.
|
|
164
|
-
// Additionally, x0 + x1 is larger than MAX_NATURAL_EXPONENT, which means they will not both be present in the
|
|
165
|
-
// decomposition.
|
|
166
|
-
|
|
167
|
-
// For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct
|
|
168
|
-
// it and compute the accumulated product.
|
|
169
|
-
|
|
170
|
-
let firstAN;
|
|
171
|
-
if (x >= this.x0) {
|
|
172
|
-
x -= this.x0;
|
|
173
|
-
firstAN = this.a0;
|
|
174
|
-
} else if (x >= this.x1) {
|
|
175
|
-
x -= this.x1;
|
|
176
|
-
firstAN = this.a1;
|
|
177
|
-
} else {
|
|
178
|
-
firstAN = 1n; // One with no decimal places
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// We now transform x into a 20 decimal fixed point number, to have enhanced precision when computing the
|
|
182
|
-
// smaller terms.
|
|
183
|
-
x *= 100n;
|
|
184
|
-
|
|
185
|
-
// `product` is the accumulated product of all a_n (except a0 and a1), which starts at 20 decimal fixed point
|
|
186
|
-
// one. Recall that fixed point multiplication requires dividing by ONE_20.
|
|
187
|
-
let product = this.ONE_20;
|
|
188
|
-
|
|
189
|
-
if (x >= this.x2) {
|
|
190
|
-
x -= this.x2;
|
|
191
|
-
product = (product * this.a2) / this.ONE_20;
|
|
192
|
-
}
|
|
193
|
-
if (x >= this.x3) {
|
|
194
|
-
x -= this.x3;
|
|
195
|
-
product = (product * this.a3) / this.ONE_20;
|
|
196
|
-
}
|
|
197
|
-
if (x >= this.x4) {
|
|
198
|
-
x -= this.x4;
|
|
199
|
-
product = (product * this.a4) / this.ONE_20;
|
|
200
|
-
}
|
|
201
|
-
if (x >= this.x5) {
|
|
202
|
-
x -= this.x5;
|
|
203
|
-
product = (product * this.a5) / this.ONE_20;
|
|
204
|
-
}
|
|
205
|
-
if (x >= this.x6) {
|
|
206
|
-
x -= this.x6;
|
|
207
|
-
product = (product * this.a6) / this.ONE_20;
|
|
208
|
-
}
|
|
209
|
-
if (x >= this.x7) {
|
|
210
|
-
x -= this.x7;
|
|
211
|
-
product = (product * this.a7) / this.ONE_20;
|
|
212
|
-
}
|
|
213
|
-
if (x >= this.x8) {
|
|
214
|
-
x -= this.x8;
|
|
215
|
-
product = (product * this.a8) / this.ONE_20;
|
|
216
|
-
}
|
|
217
|
-
if (x >= this.x9) {
|
|
218
|
-
x -= this.x9;
|
|
219
|
-
product = (product * this.a9) / this.ONE_20;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// x10 and x11 are unnecessary here since we have high enough precision already.
|
|
223
|
-
|
|
224
|
-
// Now we need to compute e^x, where x is small (in particular, it is smaller than x9). We use the Taylor series
|
|
225
|
-
// expansion for e^x: 1 + x + (x^2 / 2!) + (x^3 / 3!) + ... + (x^n / n!).
|
|
226
|
-
|
|
227
|
-
let seriesSum = this.ONE_20; // The initial one in the sum, with 20 decimal places.
|
|
228
|
-
let term; // Each term in the sum, where the nth term is (x^n / n!).
|
|
229
|
-
|
|
230
|
-
// The first term is simply x.
|
|
231
|
-
term = x;
|
|
232
|
-
seriesSum += term;
|
|
233
|
-
|
|
234
|
-
// Each term (x^n / n!) equals the previous one times x, divided by n. Since x is a fixed point number,
|
|
235
|
-
// multiplying by it requires dividing by ONE_20, but dividing by the non-fixed point n values does not.
|
|
236
|
-
|
|
237
|
-
term = (term * x) / this.ONE_20 / 2n;
|
|
238
|
-
seriesSum += term;
|
|
239
|
-
|
|
240
|
-
term = (term * x) / this.ONE_20 / 3n;
|
|
241
|
-
seriesSum += term;
|
|
242
|
-
|
|
243
|
-
term = (term * x) / this.ONE_20 / 4n;
|
|
244
|
-
seriesSum += term;
|
|
245
|
-
|
|
246
|
-
term = (term * x) / this.ONE_20 / 5n;
|
|
247
|
-
seriesSum += term;
|
|
248
|
-
|
|
249
|
-
term = (term * x) / this.ONE_20 / 6n;
|
|
250
|
-
seriesSum += term;
|
|
251
|
-
|
|
252
|
-
term = (term * x) / this.ONE_20 / 7n;
|
|
253
|
-
seriesSum += term;
|
|
254
|
-
|
|
255
|
-
term = (term * x) / this.ONE_20 / 8n;
|
|
256
|
-
seriesSum += term;
|
|
257
|
-
|
|
258
|
-
term = (term * x) / this.ONE_20 / 9n;
|
|
259
|
-
seriesSum += term;
|
|
260
|
-
|
|
261
|
-
term = (term * x) / this.ONE_20 / 10n;
|
|
262
|
-
seriesSum += term;
|
|
263
|
-
|
|
264
|
-
term = (term * x) / this.ONE_20 / 11n;
|
|
265
|
-
seriesSum += term;
|
|
266
|
-
|
|
267
|
-
term = (term * x) / this.ONE_20 / 12n;
|
|
268
|
-
seriesSum += term;
|
|
269
|
-
|
|
270
|
-
// 12 Taylor terms are sufficient for 18 decimal precision.
|
|
271
|
-
|
|
272
|
-
// We now have the first a_n (with no decimals), and the product of all other a_n present, and the Taylor
|
|
273
|
-
// approximation of the exponentiation of the remainder (both with 20 decimals). All that remains is to multiply
|
|
274
|
-
// all three (one 20 decimal fixed point multiplication, dividing by ONE_20, and one integer multiplication),
|
|
275
|
-
// and then drop two digits to return an 18 decimal value.
|
|
276
|
-
|
|
277
|
-
return (((product * seriesSum) / this.ONE_20) * firstAN) / 100n;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* @dev Logarithm (log(arg, base), with signed 18 decimal fixed point base and argument.
|
|
282
|
-
*/
|
|
283
|
-
static log(arg: bigint, base: bigint): bigint {
|
|
284
|
-
// This performs a simple base change: log(arg, base) = ln(arg) / ln(base).
|
|
285
|
-
|
|
286
|
-
// Both logBase and logArg are computed as 36 decimal fixed point numbers, either by using ln_36, or by
|
|
287
|
-
// upscaling.
|
|
288
|
-
|
|
289
|
-
let logBase;
|
|
290
|
-
if (this.LN_36_LOWER_BOUND < base && base < this.LN_36_UPPER_BOUND) {
|
|
291
|
-
logBase = this._ln_36(base);
|
|
292
|
-
} else {
|
|
293
|
-
logBase = this._ln(base) * this.ONE_18;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
let logArg;
|
|
297
|
-
if (this.LN_36_LOWER_BOUND < arg && arg < this.LN_36_UPPER_BOUND) {
|
|
298
|
-
logArg = this._ln_36(arg);
|
|
299
|
-
} else {
|
|
300
|
-
logArg = this._ln(arg) * this.ONE_18;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// When dividing, we multiply by ONE_18 to arrive at a result with 18 decimal places
|
|
304
|
-
return (logArg * this.ONE_18) / logBase;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* @dev Natural logarithm (ln(a)) with signed 18 decimal fixed point argument.
|
|
309
|
-
*/
|
|
310
|
-
static ln(a: bigint): bigint {
|
|
311
|
-
// The real natural logarithm is not defined for negative numbers or zero.
|
|
312
|
-
_require(a > 0, 'Err.OUT_OF_BOUNDS');
|
|
313
|
-
if (this.LN_36_LOWER_BOUND < a && a < this.LN_36_UPPER_BOUND) {
|
|
314
|
-
return this._ln_36(a) / this.ONE_18;
|
|
315
|
-
} else {
|
|
316
|
-
return this._ln(a);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* @dev Internal natural logarithm (ln(a)) with signed 18 decimal fixed point argument.
|
|
322
|
-
*/
|
|
323
|
-
static _ln(a: bigint): bigint {
|
|
324
|
-
if (a < this.ONE_18) {
|
|
325
|
-
// Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a)). If a is less
|
|
326
|
-
// than one, 1/a will be greater than one, and this if statement will not be entered in the recursive call.
|
|
327
|
-
// Fixed point division requires multiplying by ONE_18.
|
|
328
|
-
return -this._ln((this.ONE_18 * this.ONE_18) / a);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
// First, we use the fact that ln^(a * b) = ln(a) + ln(b) to decompose ln(a) into a sum of powers of two, which
|
|
332
|
-
// we call x_n, where x_n == 2^(7 - n), which are the natural logarithm of precomputed quantities a_n (that is,
|
|
333
|
-
// ln(a_n) = x_n). We choose the first x_n, x0, to equal 2^7 because the exponential of all larger powers cannot
|
|
334
|
-
// be represented as 18 fixed point decimal numbers in 256 bits, and are therefore larger than a.
|
|
335
|
-
// At the end of this process we will have the sum of all x_n = ln(a_n) that apply, and the remainder of this
|
|
336
|
-
// decomposition, which will be lower than the smallest a_n.
|
|
337
|
-
// ln(a) = k_0 * x_0 + k_1 * x_1 + ... + k_n * x_n + ln(remainder), where each k_n equals either 0 or 1.
|
|
338
|
-
// We mutate a by subtracting a_n, making it the remainder of the decomposition.
|
|
339
|
-
|
|
340
|
-
// For reasons related to how `exp` works, the first two a_n (e^(2^7) and e^(2^6)) are not stored as fixed point
|
|
341
|
-
// numbers with 18 decimals, but instead as plain integers with 0 decimals, so we need to multiply them by
|
|
342
|
-
// ONE_18 to convert them to fixed point.
|
|
343
|
-
// For each a_n, we test if that term is present in the decomposition (if a is larger than it), and if so divide
|
|
344
|
-
// by it and compute the accumulated sum.
|
|
345
|
-
|
|
346
|
-
let sum = 0n;
|
|
347
|
-
if (a >= this.a0 * this.ONE_18) {
|
|
348
|
-
a /= this.a0; // Integer, not fixed point division
|
|
349
|
-
sum += this.x0;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
if (a >= this.a1 * this.ONE_18) {
|
|
353
|
-
a /= this.a1; // Integer, not fixed point division
|
|
354
|
-
sum += this.x1;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
// All other a_n and x_n are stored as 20 digit fixed point numbers, so we convert the sum and a to this format.
|
|
358
|
-
sum *= 100n;
|
|
359
|
-
a *= 100n;
|
|
360
|
-
|
|
361
|
-
// Because further a_n are 20 digit fixed point numbers, we multiply by ONE_20 when dividing by them.
|
|
362
|
-
|
|
363
|
-
if (a >= this.a2) {
|
|
364
|
-
a = (a * this.ONE_20) / this.a2;
|
|
365
|
-
sum += this.x2;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
if (a >= this.a3) {
|
|
369
|
-
a = (a * this.ONE_20) / this.a3;
|
|
370
|
-
sum += this.x3;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
if (a >= this.a4) {
|
|
374
|
-
a = (a * this.ONE_20) / this.a4;
|
|
375
|
-
sum += this.x4;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
if (a >= this.a5) {
|
|
379
|
-
a = (a * this.ONE_20) / this.a5;
|
|
380
|
-
sum += this.x5;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
if (a >= this.a6) {
|
|
384
|
-
a = (a * this.ONE_20) / this.a6;
|
|
385
|
-
sum += this.x6;
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
if (a >= this.a7) {
|
|
389
|
-
a = (a * this.ONE_20) / this.a7;
|
|
390
|
-
sum += this.x7;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
if (a >= this.a8) {
|
|
394
|
-
a = (a * this.ONE_20) / this.a8;
|
|
395
|
-
sum += this.x8;
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
if (a >= this.a9) {
|
|
399
|
-
a = (a * this.ONE_20) / this.a9;
|
|
400
|
-
sum += this.x9;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
if (a >= this.a10) {
|
|
404
|
-
a = (a * this.ONE_20) / this.a10;
|
|
405
|
-
sum += this.x10;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
if (a >= this.a11) {
|
|
409
|
-
a = (a * this.ONE_20) / this.a11;
|
|
410
|
-
sum += this.x11;
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
// a is now a small number (smaller than a_11, which roughly equals 1.06). This means we can use a Taylor series
|
|
414
|
-
// that converges rapidly for values of `a` close to one - the same one used in ln_36.
|
|
415
|
-
// Let z = (a - 1) / (a + 1).
|
|
416
|
-
// ln(a) = 2 * (z + z^3 / 3 + z^5 / 5 + z^7 / 7 + ... + z^(2 * n + 1) / (2 * n + 1))
|
|
417
|
-
|
|
418
|
-
// Recall that 20 digit fixed point division requires multiplying by ONE_20, and multiplication requires
|
|
419
|
-
// division by ONE_20.
|
|
420
|
-
const z = ((a - this.ONE_20) * this.ONE_20) / (a + this.ONE_20);
|
|
421
|
-
const z_squared = (z * z) / this.ONE_20;
|
|
422
|
-
|
|
423
|
-
// num is the numerator of the series: the z^(2 * n + 1) term
|
|
424
|
-
let num = z;
|
|
425
|
-
|
|
426
|
-
// seriesSum holds the accumulated sum of each term in the series, starting with the initial z
|
|
427
|
-
let seriesSum = num;
|
|
428
|
-
|
|
429
|
-
// In each step, the numerator is multiplied by z^2
|
|
430
|
-
num = (num * z_squared) / this.ONE_20;
|
|
431
|
-
seriesSum += num / 3n;
|
|
432
|
-
|
|
433
|
-
num = (num * z_squared) / this.ONE_20;
|
|
434
|
-
seriesSum += num / 5n;
|
|
435
|
-
|
|
436
|
-
num = (num * z_squared) / this.ONE_20;
|
|
437
|
-
seriesSum += num / 7n;
|
|
438
|
-
|
|
439
|
-
num = (num * z_squared) / this.ONE_20;
|
|
440
|
-
seriesSum += num / 9n;
|
|
441
|
-
|
|
442
|
-
num = (num * z_squared) / this.ONE_20;
|
|
443
|
-
seriesSum += num / 11n;
|
|
444
|
-
|
|
445
|
-
// 6 Taylor terms are sufficient for 36 decimal precision.
|
|
446
|
-
|
|
447
|
-
// Finally, we multiply by 2 (non fixed point) to compute ln(remainder)
|
|
448
|
-
seriesSum *= 2n;
|
|
449
|
-
|
|
450
|
-
// We now have the sum of all x_n present, and the Taylor approximation of the logarithm of the remainder (both
|
|
451
|
-
// with 20 decimals). All that remains is to sum these two, and then drop two digits to return a 18 decimal
|
|
452
|
-
// value.
|
|
453
|
-
|
|
454
|
-
return (sum + seriesSum) / 100n;
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
/**
|
|
458
|
-
* @dev Intrnal high precision (36 decimal places) natural logarithm (ln(x)) with signed 18 decimal fixed point argument,
|
|
459
|
-
* for x close to one.
|
|
460
|
-
*
|
|
461
|
-
* Should only be used if x is between LN_36_LOWER_BOUND and LN_36_UPPER_BOUND.
|
|
462
|
-
*/
|
|
463
|
-
static _ln_36(x: bigint): bigint {
|
|
464
|
-
// Since ln(1) = 0, a value of x close to one will yield a very small result, which makes using 36 digits
|
|
465
|
-
// worthwhile.
|
|
466
|
-
|
|
467
|
-
// First, we transform x to a 36 digit fixed point value.
|
|
468
|
-
x *= this.ONE_18;
|
|
469
|
-
|
|
470
|
-
// We will use the following Taylor expansion, which converges very rapidly. Let z = (x - 1) / (x + 1).
|
|
471
|
-
// ln(x) = 2 * (z + z^3 / 3 + z^5 / 5 + z^7 / 7 + ... + z^(2 * n + 1) / (2 * n + 1))
|
|
472
|
-
|
|
473
|
-
// Recall that 36 digit fixed point division requires multiplying by ONE_36, and multiplication requires
|
|
474
|
-
// division by ONE_36.
|
|
475
|
-
const z = ((x - this.ONE_36) * this.ONE_36) / (x + this.ONE_36);
|
|
476
|
-
const z_squared = (z * z) / this.ONE_36;
|
|
477
|
-
|
|
478
|
-
// num is the numerator of the series: the z^(2 * n + 1) term
|
|
479
|
-
let num = z;
|
|
480
|
-
|
|
481
|
-
// seriesSum holds the accumulated sum of each term in the series, starting with the initial z
|
|
482
|
-
let seriesSum = num;
|
|
483
|
-
|
|
484
|
-
// In each step, the numerator is multiplied by z^2
|
|
485
|
-
num = (num * z_squared) / this.ONE_36;
|
|
486
|
-
seriesSum += num / 3n;
|
|
487
|
-
|
|
488
|
-
num = (num * z_squared) / this.ONE_36;
|
|
489
|
-
seriesSum += num / 5n;
|
|
490
|
-
|
|
491
|
-
num = (num * z_squared) / this.ONE_36;
|
|
492
|
-
seriesSum += num / 7n;
|
|
493
|
-
|
|
494
|
-
num = (num * z_squared) / this.ONE_36;
|
|
495
|
-
seriesSum += num / 9n;
|
|
496
|
-
|
|
497
|
-
num = (num * z_squared) / this.ONE_36;
|
|
498
|
-
seriesSum += num / 11n;
|
|
499
|
-
|
|
500
|
-
num = (num * z_squared) / this.ONE_36;
|
|
501
|
-
seriesSum += num / 13n;
|
|
502
|
-
|
|
503
|
-
num = (num * z_squared) / this.ONE_36;
|
|
504
|
-
seriesSum += num / 15n;
|
|
505
|
-
|
|
506
|
-
// 8 Taylor terms are sufficient for 36 decimal precision.
|
|
507
|
-
|
|
508
|
-
// All that remains is multiplying by 2 (non fixed point).
|
|
509
|
-
return seriesSum * 2n;
|
|
510
|
-
}
|
|
511
|
-
}
|