@paraswap/dex-lib 2.16.8-pancakeswapv3.0 → 2.17.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 (198) hide show
  1. package/.idea/aws.xml +17 -0
  2. package/.idea/codeStyles/Project.xml +19 -0
  3. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  4. package/.idea/misc.xml +6 -0
  5. package/.idea/modules.xml +8 -0
  6. package/.idea/paraswap-dex-lib.iml +9 -0
  7. package/.idea/vcs.xml +6 -0
  8. package/build/abi/woo-fi/WooFeeManager.abi.json +318 -0
  9. package/build/abi/woo-fi/WooPP.abi.json +548 -0
  10. package/build/abi/woo-fi/Wooracle.abi.json +260 -0
  11. package/build/config.js +4 -4
  12. package/build/dex/aave-v3/tokens/arbitrum.json +50 -0
  13. package/build/dex/aave-v3/tokens/avalanche.json +74 -0
  14. package/build/dex/aave-v3/tokens/fantom.json +62 -0
  15. package/build/dex/aave-v3/tokens/optimism.json +50 -0
  16. package/build/dex/aave-v3/tokens/polygon.json +116 -0
  17. package/build/dex/balancer-v2/balancer-v2.d.ts +14 -19
  18. package/build/dex/balancer-v2/balancer-v2.js +27 -22
  19. package/build/dex/balancer-v2/balancer-v2.js.map +1 -1
  20. package/build/dex/balancer-v2/config.d.ts +2 -7
  21. package/build/dex/balancer-v2/config.js +20 -30
  22. package/build/dex/balancer-v2/config.js.map +1 -1
  23. package/build/dex/balancer-v2/pools/balancer-v2-pool.d.ts +25 -0
  24. package/build/dex/balancer-v2/pools/balancer-v2-pool.js +107 -0
  25. package/build/dex/balancer-v2/pools/balancer-v2-pool.js.map +1 -0
  26. package/build/dex/balancer-v2/pools/linear/LinearMath.d.ts +20 -0
  27. package/build/dex/balancer-v2/pools/linear/LinearMath.js +179 -0
  28. package/build/dex/balancer-v2/pools/linear/LinearMath.js.map +1 -0
  29. package/build/dex/balancer-v2/pools/linear/LinearPool.d.ts +50 -0
  30. package/build/dex/balancer-v2/pools/linear/LinearPool.js +458 -0
  31. package/build/dex/balancer-v2/pools/linear/LinearPool.js.map +1 -0
  32. package/build/dex/balancer-v2/pools/phantom-stable/PhantomStableMath.d.ts +10 -0
  33. package/build/dex/balancer-v2/pools/phantom-stable/PhantomStableMath.js +314 -0
  34. package/build/dex/balancer-v2/pools/phantom-stable/PhantomStableMath.js.map +1 -0
  35. package/build/dex/balancer-v2/pools/phantom-stable/PhantomStablePool.d.ts +50 -0
  36. package/build/dex/balancer-v2/pools/phantom-stable/PhantomStablePool.js +367 -0
  37. package/build/dex/balancer-v2/pools/phantom-stable/PhantomStablePool.js.map +1 -0
  38. package/build/dex/balancer-v2/pools/stable/StableMath.d.ts +7 -0
  39. package/build/dex/balancer-v2/pools/stable/StableMath.js +111 -0
  40. package/build/dex/balancer-v2/pools/stable/StableMath.js.map +1 -0
  41. package/build/dex/balancer-v2/pools/stable/StablePool.d.ts +30 -0
  42. package/build/dex/balancer-v2/pools/stable/StablePool.js +129 -0
  43. package/build/dex/balancer-v2/pools/stable/StablePool.js.map +1 -0
  44. package/build/dex/balancer-v2/pools/weighted/WeightedMath.d.ts +6 -0
  45. package/build/dex/balancer-v2/pools/weighted/WeightedMath.js +61 -0
  46. package/build/dex/balancer-v2/pools/weighted/WeightedMath.js.map +1 -0
  47. package/build/dex/balancer-v2/pools/weighted/WeightedPool.d.ts +30 -0
  48. package/build/dex/balancer-v2/pools/weighted/WeightedPool.js +112 -0
  49. package/build/dex/balancer-v2/pools/weighted/WeightedPool.js.map +1 -0
  50. package/build/dex/balancer-v2/types.d.ts +1 -0
  51. package/build/dex/balancer-v2/types.js.map +1 -1
  52. package/build/dex/balancer-v2/utils.d.ts +3 -2
  53. package/build/dex/balancer-v2/utils.js +6 -4
  54. package/build/dex/balancer-v2/utils.js.map +1 -1
  55. package/build/dex/index.js +1 -1
  56. package/build/dex/index.js.map +1 -1
  57. package/build/dex/quickswap-v3.d.ts +21 -0
  58. package/build/dex/quickswap-v3.js +40 -0
  59. package/build/dex/quickswap-v3.js.map +1 -0
  60. package/build/dex/uniswap-v3/config.js +28 -0
  61. package/build/dex/uniswap-v3/config.js.map +1 -1
  62. package/build/dex/uniswap-v3/uniswap-v3-pool.d.ts +2 -1
  63. package/build/dex/uniswap-v3/uniswap-v3-pool.js +3 -2
  64. package/build/dex/uniswap-v3/uniswap-v3-pool.js.map +1 -1
  65. package/build/dex/uniswap-v3/uniswap-v3.js +1 -1
  66. package/build/dex/uniswap-v3/uniswap-v3.js.map +1 -1
  67. package/build/dex/{pancakeswap-v3 → woo-fi}/config.d.ts +2 -2
  68. package/build/dex/woo-fi/config.js +141 -0
  69. package/build/dex/woo-fi/config.js.map +1 -0
  70. package/build/dex/woo-fi/constants.d.ts +5 -0
  71. package/build/dex/woo-fi/constants.js +20 -0
  72. package/build/dex/woo-fi/constants.js.map +1 -0
  73. package/build/dex/woo-fi/types.d.ts +48 -0
  74. package/build/dex/woo-fi/types.js +3 -0
  75. package/build/dex/woo-fi/types.js.map +1 -0
  76. package/build/dex/woo-fi/woo-fi-decimal-math.d.ts +14 -0
  77. package/build/dex/woo-fi/woo-fi-decimal-math.js +42 -0
  78. package/build/dex/woo-fi/woo-fi-decimal-math.js.map +1 -0
  79. package/build/dex/woo-fi/woo-fi-math.d.ts +31 -0
  80. package/build/dex/woo-fi/woo-fi-math.js +300 -0
  81. package/build/dex/woo-fi/woo-fi-math.js.map +1 -0
  82. package/build/dex/woo-fi/woo-fi.d.ts +65 -0
  83. package/build/dex/woo-fi/woo-fi.js +480 -0
  84. package/build/dex/woo-fi/woo-fi.js.map +1 -0
  85. package/package.json +1 -1
  86. package/src/config.ts +4 -4
  87. package/src/dex/balancer-v2/balancer-v2-e2e.test.ts +538 -225
  88. package/src/dex/balancer-v2/balancer-v2-integration.test.ts +305 -122
  89. package/src/dex/balancer-v2/balancer-v2.ts +60 -56
  90. package/src/dex/balancer-v2/config.ts +23 -35
  91. package/src/dex/balancer-v2/pools/balancer-v2-pool.ts +274 -0
  92. package/src/dex/balancer-v2/{LinearMath.ts → pools/linear/LinearMath.ts} +11 -10
  93. package/src/dex/balancer-v2/{LinearPool.ts → pools/linear/LinearPool.ts} +210 -6
  94. package/src/dex/balancer-v2/{StableMath.ts → pools/phantom-stable/PhantomStableMath.ts} +101 -21
  95. package/src/dex/balancer-v2/{PhantomStablePool.ts → pools/phantom-stable/PhantomStablePool.ts} +167 -12
  96. package/src/dex/balancer-v2/pools/stable/StableMath.ts +204 -0
  97. package/src/dex/balancer-v2/pools/stable/StablePool.ts +221 -0
  98. package/src/dex/balancer-v2/pools/weighted/WeightedMath.ts +84 -0
  99. package/src/dex/balancer-v2/pools/weighted/WeightedPool.ts +189 -0
  100. package/src/dex/balancer-v2/types.ts +1 -0
  101. package/src/dex/balancer-v2/utils.ts +7 -1
  102. package/src/dex/index.ts +1 -1
  103. package/src/dex/uniswap-v3/config.ts +32 -0
  104. package/src/dex/uniswap-v3/forks/pancakeswap-v3/pancakeswap-v3.ts +28 -0
  105. package/src/dex/uniswap-v3/uniswap-v3-e2e.test.ts +156 -0
  106. package/src/dex/uniswap-v3/uniswap-v3-events.test.ts +0 -4
  107. package/src/dex/uniswap-v3/uniswap-v3-pool.ts +2 -1
  108. package/src/dex/uniswap-v3/uniswap-v3.ts +1 -0
  109. package/tests/constants-e2e.ts +33 -8
  110. package/build/abi/pancakeswap-v3/PancakeV3StateMulticall.abi.json +0 -781
  111. package/build/abi/pancakeswap-v3/PancakeswapV3Pool.abi.json +0 -1039
  112. package/build/abi/pancakeswap-v3/PancakeswapV3Quoter.abi.json +0 -285
  113. package/build/abi/pancakeswap-v3/PancakeswapV3Router.abi.json +0 -583
  114. package/build/dex/pancakeswap-v3/config.js +0 -62
  115. package/build/dex/pancakeswap-v3/config.js.map +0 -1
  116. package/build/dex/pancakeswap-v3/constants.d.ts +0 -23
  117. package/build/dex/pancakeswap-v3/constants.js +0 -29
  118. package/build/dex/pancakeswap-v3/constants.js.map +0 -1
  119. package/build/dex/pancakeswap-v3/contract-math/BitMath.d.ts +0 -4
  120. package/build/dex/pancakeswap-v3/contract-math/BitMath.js +0 -93
  121. package/build/dex/pancakeswap-v3/contract-math/BitMath.js.map +0 -1
  122. package/build/dex/pancakeswap-v3/contract-math/FixedPoint128.d.ts +0 -3
  123. package/build/dex/pancakeswap-v3/contract-math/FixedPoint128.js +0 -8
  124. package/build/dex/pancakeswap-v3/contract-math/FixedPoint128.js.map +0 -1
  125. package/build/dex/pancakeswap-v3/contract-math/FixedPoint96.d.ts +0 -4
  126. package/build/dex/pancakeswap-v3/contract-math/FixedPoint96.js +0 -9
  127. package/build/dex/pancakeswap-v3/contract-math/FixedPoint96.js.map +0 -1
  128. package/build/dex/pancakeswap-v3/contract-math/FullMath.d.ts +0 -4
  129. package/build/dex/pancakeswap-v3/contract-math/FullMath.js +0 -19
  130. package/build/dex/pancakeswap-v3/contract-math/FullMath.js.map +0 -1
  131. package/build/dex/pancakeswap-v3/contract-math/LiquidityMath.d.ts +0 -3
  132. package/build/dex/pancakeswap-v3/contract-math/LiquidityMath.js +0 -22
  133. package/build/dex/pancakeswap-v3/contract-math/LiquidityMath.js.map +0 -1
  134. package/build/dex/pancakeswap-v3/contract-math/Oracle.d.ts +0 -10
  135. package/build/dex/pancakeswap-v3/contract-math/Oracle.js +0 -133
  136. package/build/dex/pancakeswap-v3/contract-math/Oracle.js.map +0 -1
  137. package/build/dex/pancakeswap-v3/contract-math/SqrtPriceMath.d.ts +0 -10
  138. package/build/dex/pancakeswap-v3/contract-math/SqrtPriceMath.js +0 -91
  139. package/build/dex/pancakeswap-v3/contract-math/SqrtPriceMath.js.map +0 -1
  140. package/build/dex/pancakeswap-v3/contract-math/SwapMath.d.ts +0 -8
  141. package/build/dex/pancakeswap-v3/contract-math/SwapMath.js +0 -70
  142. package/build/dex/pancakeswap-v3/contract-math/SwapMath.js.map +0 -1
  143. package/build/dex/pancakeswap-v3/contract-math/Tick.d.ts +0 -7
  144. package/build/dex/pancakeswap-v3/contract-math/Tick.js +0 -45
  145. package/build/dex/pancakeswap-v3/contract-math/Tick.js.map +0 -1
  146. package/build/dex/pancakeswap-v3/contract-math/TickBitMap.d.ts +0 -8
  147. package/build/dex/pancakeswap-v3/contract-math/TickBitMap.js +0 -87
  148. package/build/dex/pancakeswap-v3/contract-math/TickBitMap.js.map +0 -1
  149. package/build/dex/pancakeswap-v3/contract-math/TickMath.d.ts +0 -8
  150. package/build/dex/pancakeswap-v3/contract-math/TickMath.js +0 -162
  151. package/build/dex/pancakeswap-v3/contract-math/TickMath.js.map +0 -1
  152. package/build/dex/pancakeswap-v3/contract-math/UnsafeMath.d.ts +0 -3
  153. package/build/dex/pancakeswap-v3/contract-math/UnsafeMath.js +0 -10
  154. package/build/dex/pancakeswap-v3/contract-math/UnsafeMath.js.map +0 -1
  155. package/build/dex/pancakeswap-v3/contract-math/pancakeswap-v3-math.d.ts +0 -18
  156. package/build/dex/pancakeswap-v3/contract-math/pancakeswap-v3-math.js +0 -407
  157. package/build/dex/pancakeswap-v3/contract-math/pancakeswap-v3-math.js.map +0 -1
  158. package/build/dex/pancakeswap-v3/contract-math/utils.d.ts +0 -3
  159. package/build/dex/pancakeswap-v3/contract-math/utils.js +0 -16
  160. package/build/dex/pancakeswap-v3/contract-math/utils.js.map +0 -1
  161. package/build/dex/pancakeswap-v3/pancakeswap-v3-pool.d.ts +0 -45
  162. package/build/dex/pancakeswap-v3/pancakeswap-v3-pool.js +0 -340
  163. package/build/dex/pancakeswap-v3/pancakeswap-v3-pool.js.map +0 -1
  164. package/build/dex/pancakeswap-v3/pancakeswap-v3.d.ts +0 -55
  165. package/build/dex/pancakeswap-v3/pancakeswap-v3.js +0 -605
  166. package/build/dex/pancakeswap-v3/pancakeswap-v3.js.map +0 -1
  167. package/build/dex/pancakeswap-v3/types.d.ts +0 -137
  168. package/build/dex/pancakeswap-v3/types.js +0 -9
  169. package/build/dex/pancakeswap-v3/types.js.map +0 -1
  170. package/build/dex/pancakeswap-v3/utils.d.ts +0 -6
  171. package/build/dex/pancakeswap-v3/utils.js +0 -65
  172. package/build/dex/pancakeswap-v3/utils.js.map +0 -1
  173. package/src/abi/pancakeswap-v3/PancakeV3StateMulticall.abi.json +0 -781
  174. package/src/abi/pancakeswap-v3/PancakeswapV3Pool.abi.json +0 -1039
  175. package/src/dex/balancer-v2/balancer-v2-pool.ts +0 -744
  176. package/src/dex/pancakeswap-v3/config.ts +0 -68
  177. package/src/dex/pancakeswap-v3/constants.ts +0 -35
  178. package/src/dex/pancakeswap-v3/contract-math/BitMath.ts +0 -90
  179. package/src/dex/pancakeswap-v3/contract-math/FixedPoint128.ts +0 -3
  180. package/src/dex/pancakeswap-v3/contract-math/FixedPoint96.ts +0 -4
  181. package/src/dex/pancakeswap-v3/contract-math/FullMath.ts +0 -30
  182. package/src/dex/pancakeswap-v3/contract-math/LiquidityMath.ts +0 -17
  183. package/src/dex/pancakeswap-v3/contract-math/Oracle.ts +0 -226
  184. package/src/dex/pancakeswap-v3/contract-math/SqrtPriceMath.ts +0 -226
  185. package/src/dex/pancakeswap-v3/contract-math/SwapMath.ts +0 -139
  186. package/src/dex/pancakeswap-v3/contract-math/Tick.ts +0 -82
  187. package/src/dex/pancakeswap-v3/contract-math/TickBitMap.ts +0 -128
  188. package/src/dex/pancakeswap-v3/contract-math/TickMath.ts +0 -211
  189. package/src/dex/pancakeswap-v3/contract-math/UnsafeMath.ts +0 -5
  190. package/src/dex/pancakeswap-v3/contract-math/pancakeswap-v3-math.ts +0 -691
  191. package/src/dex/pancakeswap-v3/contract-math/utils.ts +0 -11
  192. package/src/dex/pancakeswap-v3/pancakeswap-v3-e2e.test.ts +0 -168
  193. package/src/dex/pancakeswap-v3/pancakeswap-v3-events.test.ts +0 -124
  194. package/src/dex/pancakeswap-v3/pancakeswap-v3-integration.test.ts +0 -338
  195. package/src/dex/pancakeswap-v3/pancakeswap-v3-pool.ts +0 -554
  196. package/src/dex/pancakeswap-v3/pancakeswap-v3.ts +0 -1000
  197. /package/src/abi/{pancakeswap-v3/PancakeswapV3Quoter.abi.json → uniswap-v3/pancakeswap-v3/PancakeswapV3QuoterV2.abi.json} +0 -0
  198. /package/src/abi/{pancakeswap-v3 → uniswap-v3/pancakeswap-v3}/PancakeswapV3Router.abi.json +0 -0
@@ -1,68 +0,0 @@
1
- import { DexParams } from '../uniswap-v3/types';
2
- import { DexConfigMap, AdapterMappings } from '../../types';
3
- import { Network, SwapSide } from '../../constants';
4
- import { Address } from '../../types';
5
-
6
- const PANCAKE_SUPPORTED_FEES = [10000n, 2500n, 500n, 100n];
7
-
8
- // Pools that will be initialized on app startup
9
- // They are added for testing
10
- export const PancakeswapV3Config: DexConfigMap<DexParams> = {
11
- PancakeswapV3: {
12
- [Network.MAINNET]: {
13
- factory: '0x0BFbCF9fa4f9C56B0F40a671Ad40E0805A091865',
14
- deployer: '0x41ff9AA7e16B8B1a8a8dc4f0eFacd93D02d071c9',
15
- quoter: '0xB048Bbc1Ee6b733FFfCFb9e9CeF7375518e25997',
16
- router: '0x1b81D678ffb9C0263b24A97847620C99d213eB14',
17
- supportedFees: PANCAKE_SUPPORTED_FEES,
18
- stateMulticall: '0x80898f80cFA3Fa3AbF410d90e69aDc432AE5D4c2',
19
- uniswapMulticall: '0xac1cE734566f390A94b00eb9bf561c2625BF44ea',
20
- chunksCount: 10,
21
- initHash:
22
- '0x6ce8eb472fa82df5469c6ab6d485f17c3ad13c8cd7af59b3d4a8026c5ce0f7e2',
23
- subgraphURL:
24
- 'https://api.thegraph.com/subgraphs/name/pancakeswap/exchange-v3-eth',
25
- },
26
- [Network.BSC]: {
27
- factory: '0x0BFbCF9fa4f9C56B0F40a671Ad40E0805A091865',
28
- deployer: '0x41ff9AA7e16B8B1a8a8dc4f0eFacd93D02d071c9',
29
- quoter: '0xB048Bbc1Ee6b733FFfCFb9e9CeF7375518e25997',
30
- router: '0x1b81D678ffb9C0263b24A97847620C99d213eB14',
31
- supportedFees: PANCAKE_SUPPORTED_FEES,
32
- stateMulticall: '0x9DAd2ED7ADc6eaacf81589Cd043579c9684E5C81',
33
- uniswapMulticall: '0xac1cE734566f390A94b00eb9bf561c2625BF44ea',
34
- chunksCount: 10,
35
- initHash:
36
- '0x6ce8eb472fa82df5469c6ab6d485f17c3ad13c8cd7af59b3d4a8026c5ce0f7e2',
37
- subgraphURL:
38
- 'https://api.thegraph.com/subgraphs/name/pancakeswap/exchange-v3-bsc',
39
- },
40
- },
41
- };
42
-
43
- export const Adapters: Record<number, AdapterMappings> = {
44
- [Network.MAINNET]: {
45
- [SwapSide.SELL]: [{ name: 'Adapter01', index: 6 }],
46
- [SwapSide.BUY]: [{ name: 'BuyAdapter', index: 2 }],
47
- },
48
- [Network.BSC]: {
49
- [SwapSide.SELL]: [{ name: 'BscAdapter02', index: 4 }],
50
- [SwapSide.BUY]: [{ name: 'BscBuyAdapter', index: 5 }],
51
- },
52
- [Network.POLYGON]: {
53
- [SwapSide.SELL]: [{ name: 'PolygonAdapter01', index: 13 }],
54
- [SwapSide.BUY]: [{ name: 'PolygonBuyAdapter', index: 2 }],
55
- },
56
- [Network.ARBITRUM]: {
57
- [SwapSide.SELL]: [{ name: 'ArbitrumAdapter01', index: 3 }],
58
- [SwapSide.BUY]: [{ name: 'ArbitrumBuyAdapter', index: 2 }],
59
- },
60
- [Network.OPTIMISM]: {
61
- [SwapSide.SELL]: [{ name: 'OptimismAdapter01', index: 3 }],
62
- [SwapSide.BUY]: [{ name: 'OptimismBuyAdapter', index: 2 }],
63
- },
64
- [Network.FANTOM]: {
65
- [SwapSide.SELL]: [{ name: 'FantomAdapter01', index: 11 }],
66
- [SwapSide.BUY]: [{ name: 'FantomBuyAdapter', index: 3 }],
67
- },
68
- };
@@ -1,35 +0,0 @@
1
- export const PANCAKESWAP3_FUNCTION_CALL_GAS_COST = 21_000; // Ceiled
2
- export const PANCAKESWAPV3_TICK_GAS_COST = 24_000; // Ceiled
3
-
4
- // This is used for price calculation. If out of scope, return 0n
5
- export const TICK_BITMAP_TO_USE = 4n;
6
-
7
- // This is used to check if the state is still valid.
8
- export const TICK_BITMAP_BUFFER = 8n;
9
-
10
- export const MAX_PRICING_COMPUTATION_STEPS_ALLOWED = 128;
11
-
12
- export const PANCAKESWAPV3_SUBGRAPH_URL =
13
- 'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3';
14
-
15
- export const PANCAKESWAPV3_EFFICIENCY_FACTOR = 3;
16
-
17
- export const ZERO_TICK_INFO = {
18
- liquidityGross: 0n,
19
- liquidityNet: 0n,
20
- tickCumulativeOutside: 0n,
21
- secondsPerLiquidityOutsideX128: 0n,
22
- secondsOutside: 0n,
23
- initialized: false,
24
- };
25
-
26
- export const ZERO_ORACLE_OBSERVATION = {
27
- blockTimestamp: 0n,
28
- tickCumulative: 0n,
29
- secondsPerLiquidityCumulativeX128: 0n,
30
- initialized: false,
31
- };
32
-
33
- export const OUT_OF_RANGE_ERROR_POSTFIX = `INVALID_TICK_BIT_MAP_RANGES`;
34
-
35
- export const DEFAULT_POOL_INIT_CODE_HASH = `0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54`;
@@ -1,90 +0,0 @@
1
- import {
2
- BI_MAX_UINT128,
3
- BI_MAX_UINT16,
4
- BI_MAX_UINT32,
5
- BI_MAX_UINT64,
6
- BI_MAX_UINT8,
7
- } from '../../../bigint-constants';
8
- import { _require } from '../../../utils';
9
-
10
- export class BitMath {
11
- static mostSignificantBit(x: bigint): bigint {
12
- _require(x > 0, '', { x }, 'x > 0');
13
- let r = 0n;
14
-
15
- if (x >= 0x100000000000000000000000000000000n) {
16
- x >>= 128n;
17
- r += 128n;
18
- }
19
- if (x >= 0x10000000000000000n) {
20
- x >>= 64n;
21
- r += 64n;
22
- }
23
- if (x >= 0x100000000n) {
24
- x >>= 32n;
25
- r += 32n;
26
- }
27
- if (x >= 0x10000n) {
28
- x >>= 16n;
29
- r += 16n;
30
- }
31
- if (x >= 0x100n) {
32
- x >>= 8n;
33
- r += 8n;
34
- }
35
- if (x >= 0x10n) {
36
- x >>= 4n;
37
- r += 4n;
38
- }
39
- if (x >= 0x4n) {
40
- x >>= 2n;
41
- r += 2n;
42
- }
43
- if (x >= 0x2n) r += 1n;
44
-
45
- return r;
46
- }
47
-
48
- static leastSignificantBit(x: bigint): bigint {
49
- _require(x > 0, '', { x }, 'x > 0');
50
-
51
- let r = 255n;
52
- if ((x & BI_MAX_UINT128) > 0n) {
53
- r -= 128n;
54
- } else {
55
- x >>= 128n;
56
- }
57
- if ((x & BI_MAX_UINT64) > 0n) {
58
- r -= 64n;
59
- } else {
60
- x >>= 64n;
61
- }
62
- if ((x & BI_MAX_UINT32) > 0n) {
63
- r -= 32n;
64
- } else {
65
- x >>= 32n;
66
- }
67
- if ((x & BI_MAX_UINT16) > 0n) {
68
- r -= 16n;
69
- } else {
70
- x >>= 16n;
71
- }
72
- if ((x & BI_MAX_UINT8) > 0n) {
73
- r -= 8n;
74
- } else {
75
- x >>= 8n;
76
- }
77
- if ((x & 0xfn) > 0n) {
78
- r -= 4n;
79
- } else {
80
- x >>= 4n;
81
- }
82
- if ((x & 0x3n) > 0n) {
83
- r -= 2n;
84
- } else {
85
- x >>= 2n;
86
- }
87
- if ((x & 0x1n) > 0n) r -= 1n;
88
- return r;
89
- }
90
- }
@@ -1,3 +0,0 @@
1
- export class FixedPoint128 {
2
- static readonly Q128 = 0x100000000000000000000000000000000n;
3
- }
@@ -1,4 +0,0 @@
1
- export class FixedPoint96 {
2
- static readonly RESOLUTION = 96n;
3
- static readonly Q96 = 0x1000000000000000000000000n;
4
- }
@@ -1,30 +0,0 @@
1
- import { BI_MAX_UINT256 } from '../../../bigint-constants';
2
- import { _require } from '../../../utils';
3
-
4
- export class FullMath {
5
- static mulDiv(a: bigint, b: bigint, denominator: bigint) {
6
- const result = (a * b) / denominator;
7
-
8
- _require(
9
- result <= BI_MAX_UINT256,
10
- '',
11
- { result, BI_MAX_UINT: BI_MAX_UINT256 },
12
- 'result <= BI_MAX_UINT',
13
- );
14
-
15
- return result;
16
- }
17
-
18
- static mulDivRoundingUp(a: bigint, b: bigint, denominator: bigint) {
19
- const result = (a * b + denominator - 1n) / denominator;
20
-
21
- _require(
22
- result <= BI_MAX_UINT256,
23
- '',
24
- { result, BI_MAX_UINT: BI_MAX_UINT256 },
25
- 'result <= BI_MAX_UINT',
26
- );
27
-
28
- return result;
29
- }
30
- }
@@ -1,17 +0,0 @@
1
- import { _require } from '../../../utils';
2
-
3
- export class LiquidityMath {
4
- static addDelta(x: bigint, y: bigint): bigint {
5
- let z;
6
- if (y < 0) {
7
- const _y = BigInt.asUintN(128, -y);
8
- z = x - _y;
9
- _require(z < x, 'LS', { z, x, y, _y }, 'z < x');
10
- } else {
11
- const _y = BigInt.asUintN(128, y);
12
- z = x + _y;
13
- _require(z >= x, 'LA', { z, x, y, _y }, 'z >= x');
14
- }
15
- return z;
16
- }
17
- }
@@ -1,226 +0,0 @@
1
- import {
2
- OracleObservation,
3
- OracleObservationCandidates,
4
- PoolState,
5
- } from '../../uniswap-v3/types';
6
- import { _require } from '../../../utils';
7
- import { DeepReadonly } from 'ts-essentials';
8
- import { ZERO_ORACLE_OBSERVATION } from '../constants';
9
-
10
- function replaceUndefinedObservationWithZero(state: PoolState, index: number) {
11
- if (state.observations[index] === undefined) {
12
- state.observations[index] = { ...ZERO_ORACLE_OBSERVATION };
13
- }
14
- }
15
-
16
- export class Oracle {
17
- static transform(
18
- state: DeepReadonly<PoolState>,
19
- last: OracleObservation,
20
- blockTimestamp: bigint,
21
- tick: bigint,
22
- liquidity: bigint,
23
- ): OracleObservation {
24
- const delta = blockTimestamp - last.blockTimestamp;
25
- return {
26
- blockTimestamp: state.blockTimestamp,
27
- tickCumulative: last.tickCumulative + BigInt.asIntN(56, tick) * delta,
28
- secondsPerLiquidityCumulativeX128:
29
- last.secondsPerLiquidityCumulativeX128 +
30
- (BigInt.asUintN(160, delta) << 128n) /
31
- (liquidity > 0n ? liquidity : 1n),
32
- initialized: true,
33
- };
34
- }
35
-
36
- static write(
37
- state: PoolState,
38
- index: number,
39
- blockTimestamp: bigint,
40
- tick: bigint,
41
- liquidity: bigint,
42
- cardinality: number,
43
- cardinalityNext: number,
44
- ): [number, number] {
45
- const last = state.observations[index];
46
-
47
- if (last.blockTimestamp == state.blockTimestamp)
48
- return [index, cardinality];
49
-
50
- let indexUpdated = 0;
51
- let cardinalityUpdated = 0;
52
-
53
- if (cardinalityNext > cardinality && index == cardinality - 1) {
54
- cardinalityUpdated = cardinalityNext;
55
- } else {
56
- cardinalityUpdated = cardinality;
57
- }
58
-
59
- indexUpdated = (index + 1) % cardinalityUpdated;
60
-
61
- state.observations[indexUpdated] = Oracle.transform(
62
- state,
63
- last,
64
- blockTimestamp,
65
- tick,
66
- liquidity,
67
- );
68
- if (indexUpdated !== index) {
69
- delete state.observations[index];
70
- }
71
- return [indexUpdated, cardinalityUpdated];
72
- }
73
-
74
- static lte(time: bigint, a: bigint, b: bigint): boolean {
75
- if (a <= time && b <= time) return a <= b;
76
-
77
- const aAdjusted = a > time ? a : a + 2n ** 32n;
78
- const bAdjusted = b > time ? b : b + 2n ** 32n;
79
- return aAdjusted <= bAdjusted;
80
- }
81
-
82
- static binarySearch(
83
- state: DeepReadonly<PoolState>,
84
- time: bigint,
85
- target: bigint,
86
- index: number,
87
- cardinality: number,
88
- ): OracleObservationCandidates {
89
- let l = (index + 1) % cardinality;
90
- let r = l + cardinality - 1;
91
- let i;
92
-
93
- let beforeOrAt;
94
- let atOrAfter;
95
- while (true) {
96
- i = (l + r) / 2;
97
-
98
- beforeOrAt = state.observations[i % cardinality];
99
-
100
- // we've landed on an uninitialized tick, keep searching higher (more recently)
101
- if (!beforeOrAt.initialized) {
102
- l = i + 1;
103
- continue;
104
- }
105
-
106
- atOrAfter = state.observations[(i + 1) % cardinality];
107
-
108
- const targetAtOrAfter = Oracle.lte(
109
- time,
110
- beforeOrAt.blockTimestamp,
111
- target,
112
- );
113
-
114
- // check if we've found the answer!
115
- if (targetAtOrAfter && Oracle.lte(time, target, atOrAfter.blockTimestamp))
116
- break;
117
-
118
- if (!targetAtOrAfter) r = i - 1;
119
- else l = i + 1;
120
- }
121
- return { beforeOrAt, atOrAfter };
122
- }
123
-
124
- static getSurroundingObservations(
125
- state: DeepReadonly<PoolState>,
126
- time: bigint,
127
- target: bigint,
128
- tick: bigint,
129
- index: number,
130
- liquidity: bigint,
131
- cardinality: number,
132
- ): OracleObservationCandidates {
133
- let beforeOrAt = state.observations[index];
134
-
135
- if (Oracle.lte(time, beforeOrAt.blockTimestamp, target)) {
136
- if (beforeOrAt.blockTimestamp === target) {
137
- return { beforeOrAt, atOrAfter: beforeOrAt };
138
- } else {
139
- return {
140
- beforeOrAt,
141
- atOrAfter: Oracle.transform(
142
- state,
143
- beforeOrAt,
144
- target,
145
- tick,
146
- liquidity,
147
- ),
148
- };
149
- }
150
- }
151
-
152
- beforeOrAt = state.observations[(index + 1) % cardinality];
153
- if (!beforeOrAt.initialized) beforeOrAt = state.observations[0];
154
-
155
- _require(
156
- Oracle.lte(time, beforeOrAt.blockTimestamp, target),
157
- 'OLD',
158
- { time, beforeOrAtBlockTimestamp: beforeOrAt.blockTimestamp, target },
159
- 'Oracle.lte(time, beforeOrAt.blockTimestamp, target)',
160
- );
161
-
162
- return Oracle.binarySearch(state, time, target, index, cardinality);
163
- }
164
-
165
- static observeSingle(
166
- state: DeepReadonly<PoolState>,
167
- time: bigint,
168
- secondsAgo: bigint,
169
- tick: bigint,
170
- index: number,
171
- liquidity: bigint,
172
- cardinality: number,
173
- ): [bigint, bigint] {
174
- if (secondsAgo == 0n) {
175
- let last = state.observations[index];
176
- if (last.blockTimestamp != time)
177
- last = Oracle.transform(state, last, time, tick, liquidity);
178
- return [last.tickCumulative, last.secondsPerLiquidityCumulativeX128];
179
- }
180
-
181
- const target = time - secondsAgo;
182
-
183
- const { beforeOrAt, atOrAfter } = Oracle.getSurroundingObservations(
184
- state,
185
- time,
186
- target,
187
- tick,
188
- index,
189
- liquidity,
190
- cardinality,
191
- );
192
-
193
- if (target === beforeOrAt.blockTimestamp) {
194
- return [
195
- beforeOrAt.tickCumulative,
196
- beforeOrAt.secondsPerLiquidityCumulativeX128,
197
- ];
198
- } else if (target === atOrAfter.blockTimestamp) {
199
- return [
200
- atOrAfter.tickCumulative,
201
- atOrAfter.secondsPerLiquidityCumulativeX128,
202
- ];
203
- } else {
204
- const observationTimeDelta =
205
- atOrAfter.blockTimestamp - beforeOrAt.blockTimestamp;
206
- const targetDelta = target - beforeOrAt.blockTimestamp;
207
- return [
208
- beforeOrAt.tickCumulative +
209
- ((atOrAfter.tickCumulative - beforeOrAt.tickCumulative) /
210
- observationTimeDelta) *
211
- targetDelta,
212
- beforeOrAt.secondsPerLiquidityCumulativeX128 +
213
- BigInt.asUintN(
214
- 160,
215
- (BigInt.asUintN(
216
- 256,
217
- atOrAfter.secondsPerLiquidityCumulativeX128 -
218
- beforeOrAt.secondsPerLiquidityCumulativeX128,
219
- ) *
220
- targetDelta) /
221
- observationTimeDelta,
222
- ),
223
- ];
224
- }
225
- }
226
- }
@@ -1,226 +0,0 @@
1
- import { BI_MAX_UINT160 } from '../../../bigint-constants';
2
- import { FixedPoint96 } from './FixedPoint96';
3
- import { FullMath } from './FullMath';
4
- import { UnsafeMath } from './UnsafeMath';
5
- import { _require } from '../../../utils';
6
-
7
- export class SqrtPriceMath {
8
- static getNextSqrtPriceFromAmount0RoundingUp(
9
- sqrtPX96: bigint,
10
- liquidity: bigint,
11
- amount: bigint,
12
- add: boolean,
13
- ): bigint {
14
- if (amount === 0n) return sqrtPX96;
15
- const numerator1 =
16
- BigInt.asUintN(256, liquidity) << FixedPoint96.RESOLUTION;
17
-
18
- const product = amount * sqrtPX96;
19
- if (add) {
20
- if (product / amount === sqrtPX96) {
21
- const denominator = numerator1 + product;
22
- if (denominator >= numerator1) {
23
- return BigInt.asUintN(
24
- 160,
25
- FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator),
26
- );
27
- }
28
- }
29
- return BigInt.asUintN(
30
- 160,
31
- UnsafeMath.divRoundingUp(numerator1, numerator1 / sqrtPX96 + amount),
32
- );
33
- } else {
34
- _require(
35
- product / amount === sqrtPX96 && numerator1 > product,
36
- '',
37
- { product, amount, sqrtPX96, numerator1 },
38
- 'product / amount === sqrtPX96 && numerator1 > product',
39
- );
40
- const denominator = numerator1 - product;
41
- return BigInt.asUintN(
42
- 160,
43
- FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator),
44
- );
45
- }
46
- }
47
-
48
- static getNextSqrtPriceFromAmount1RoundingDown(
49
- sqrtPX96: bigint,
50
- liquidity: bigint,
51
- amount: bigint,
52
- add: boolean,
53
- ): bigint {
54
- if (add) {
55
- const quotient =
56
- amount <= BI_MAX_UINT160
57
- ? (amount << FixedPoint96.RESOLUTION) / liquidity
58
- : FullMath.mulDiv(amount, FixedPoint96.Q96, liquidity);
59
- return BigInt.asUintN(160, BigInt.asUintN(256, sqrtPX96) + quotient);
60
- } else {
61
- const quotient =
62
- amount <= BI_MAX_UINT160
63
- ? UnsafeMath.divRoundingUp(
64
- amount << FixedPoint96.RESOLUTION,
65
- liquidity,
66
- )
67
- : FullMath.mulDivRoundingUp(amount, FixedPoint96.Q96, liquidity);
68
-
69
- _require(
70
- sqrtPX96 > quotient,
71
- '',
72
- { sqrtPX96, quotient },
73
- 'sqrtPX96 > quotient',
74
- );
75
- return BigInt.asUintN(160, sqrtPX96 - quotient);
76
- }
77
- }
78
-
79
- static getNextSqrtPriceFromInput(
80
- sqrtPX96: bigint,
81
- liquidity: bigint,
82
- amountIn: bigint,
83
- zeroForOne: boolean,
84
- ): bigint {
85
- _require(sqrtPX96 > 0n, '', { sqrtPX96 }, 'sqrtPX96 > 0n');
86
- _require(liquidity > 0n, '', { liquidity }, 'liquidity > 0n');
87
-
88
- return zeroForOne
89
- ? SqrtPriceMath.getNextSqrtPriceFromAmount0RoundingUp(
90
- sqrtPX96,
91
- liquidity,
92
- amountIn,
93
- true,
94
- )
95
- : SqrtPriceMath.getNextSqrtPriceFromAmount1RoundingDown(
96
- sqrtPX96,
97
- liquidity,
98
- amountIn,
99
- true,
100
- );
101
- }
102
-
103
- static getNextSqrtPriceFromOutput(
104
- sqrtPX96: bigint,
105
- liquidity: bigint,
106
- amountOut: bigint,
107
- zeroForOne: boolean,
108
- ): bigint {
109
- _require(sqrtPX96 > 0n, '', { sqrtPX96 }, 'sqrtPX96 > 0n');
110
- _require(liquidity > 0n, '', { liquidity }, 'liquidity > 0n');
111
-
112
- return zeroForOne
113
- ? SqrtPriceMath.getNextSqrtPriceFromAmount1RoundingDown(
114
- sqrtPX96,
115
- liquidity,
116
- amountOut,
117
- false,
118
- )
119
- : SqrtPriceMath.getNextSqrtPriceFromAmount0RoundingUp(
120
- sqrtPX96,
121
- liquidity,
122
- amountOut,
123
- false,
124
- );
125
- }
126
-
127
- static getAmount0Delta(
128
- sqrtRatioAX96: bigint,
129
- sqrtRatioBX96: bigint,
130
- liquidity: bigint,
131
- roundUp: boolean,
132
- ) {
133
- if (sqrtRatioAX96 > sqrtRatioBX96) {
134
- [sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
135
- }
136
-
137
- const numerator1 =
138
- BigInt.asUintN(256, liquidity) << FixedPoint96.RESOLUTION;
139
- const numerator2 = sqrtRatioBX96 - sqrtRatioAX96;
140
-
141
- _require(sqrtRatioAX96 > 0, '', { sqrtRatioAX96 }, 'sqrtRatioAX96 > 0');
142
-
143
- return roundUp
144
- ? UnsafeMath.divRoundingUp(
145
- FullMath.mulDivRoundingUp(numerator1, numerator2, sqrtRatioBX96),
146
- sqrtRatioAX96,
147
- )
148
- : FullMath.mulDiv(numerator1, numerator2, sqrtRatioBX96) / sqrtRatioAX96;
149
- }
150
-
151
- static getAmount1Delta(
152
- sqrtRatioAX96: bigint,
153
- sqrtRatioBX96: bigint,
154
- liquidity: bigint,
155
- roundUp: boolean,
156
- ) {
157
- if (sqrtRatioAX96 > sqrtRatioBX96)
158
- [sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
159
-
160
- return roundUp
161
- ? FullMath.mulDivRoundingUp(
162
- liquidity,
163
- sqrtRatioBX96 - sqrtRatioAX96,
164
- FixedPoint96.Q96,
165
- )
166
- : FullMath.mulDiv(
167
- liquidity,
168
- sqrtRatioBX96 - sqrtRatioAX96,
169
- FixedPoint96.Q96,
170
- );
171
- }
172
-
173
- // Overloaded with different argument numbers
174
- static _getAmount0DeltaO(
175
- sqrtRatioAX96: bigint,
176
- sqrtRatioBX96: bigint,
177
- liquidity: bigint,
178
- ) {
179
- return liquidity < 0
180
- ? -BigInt.asIntN(
181
- 256,
182
- SqrtPriceMath.getAmount0Delta(
183
- sqrtRatioAX96,
184
- sqrtRatioBX96,
185
- BigInt.asUintN(128, -liquidity),
186
- false,
187
- ),
188
- )
189
- : BigInt.asIntN(
190
- 256,
191
- SqrtPriceMath.getAmount0Delta(
192
- sqrtRatioAX96,
193
- sqrtRatioBX96,
194
- BigInt.asUintN(128, liquidity),
195
- true,
196
- ),
197
- );
198
- }
199
-
200
- // Overloaded with different argument numbers
201
- static _getAmount1DeltaO(
202
- sqrtRatioAX96: bigint,
203
- sqrtRatioBX96: bigint,
204
- liquidity: bigint,
205
- ) {
206
- return liquidity < 0
207
- ? -BigInt.asIntN(
208
- 256,
209
- SqrtPriceMath.getAmount1Delta(
210
- sqrtRatioAX96,
211
- sqrtRatioBX96,
212
- BigInt.asUintN(128, -liquidity),
213
- false,
214
- ),
215
- )
216
- : BigInt.asIntN(
217
- 256,
218
- SqrtPriceMath.getAmount1Delta(
219
- sqrtRatioAX96,
220
- sqrtRatioBX96,
221
- BigInt.asUintN(128, liquidity),
222
- true,
223
- ),
224
- );
225
- }
226
- }