@paraswap/dex-lib 2.42.4-baseswap → 2.42.4

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 (216) hide show
  1. package/.vscode/launch.json +16 -0
  2. package/build/dex/index.js +0 -2
  3. package/build/dex/index.js.map +1 -1
  4. package/build/test.js +26 -0
  5. package/package.json +1 -1
  6. package/src/dex/index.ts +0 -2
  7. package/build/abi/kyberswap-elastic/IAntiSnipAttackPositionManager.json +0 -1276
  8. package/build/abi/kyberswap-elastic/IFactory.json +0 -550
  9. package/build/abi/kyberswap-elastic/IPool.json +0 -1042
  10. package/build/abi/kyberswap-elastic/IQuoterV2.json +0 -226
  11. package/build/abi/kyberswap-elastic/IRouter.json +0 -239
  12. package/build/abi/kyberswap-elastic/TicksFeesReader.json +0 -142
  13. package/build/abi/wombat/asset.json +0 -769
  14. package/build/abi/wombat/bmw.json +0 -1247
  15. package/build/abi/wombat/pool.json +0 -1364
  16. package/build/dex/baseswap-v3/baseswap-v3-factory.d.ts +0 -26
  17. package/build/dex/baseswap-v3/baseswap-v3-factory.js +0 -44
  18. package/build/dex/baseswap-v3/baseswap-v3-factory.js.map +0 -1
  19. package/build/dex/baseswap-v3/baseswap-v3-pool.d.ts +0 -46
  20. package/build/dex/baseswap-v3/baseswap-v3-pool.js +0 -312
  21. package/build/dex/baseswap-v3/baseswap-v3-pool.js.map +0 -1
  22. package/build/dex/baseswap-v3/baseswap-v3.d.ts +0 -71
  23. package/build/dex/baseswap-v3/baseswap-v3.js +0 -752
  24. package/build/dex/baseswap-v3/baseswap-v3.js.map +0 -1
  25. package/build/dex/baseswap-v3/config.d.ts +0 -9
  26. package/build/dex/baseswap-v3/config.js +0 -48
  27. package/build/dex/baseswap-v3/config.js.map +0 -1
  28. package/build/dex/baseswap-v3/constants.d.ts +0 -28
  29. package/build/dex/baseswap-v3/constants.js +0 -35
  30. package/build/dex/baseswap-v3/constants.js.map +0 -1
  31. package/build/dex/baseswap-v3/contract-math/BitMath.d.ts +0 -4
  32. package/build/dex/baseswap-v3/contract-math/BitMath.js +0 -93
  33. package/build/dex/baseswap-v3/contract-math/BitMath.js.map +0 -1
  34. package/build/dex/baseswap-v3/contract-math/FixedPoint128.d.ts +0 -3
  35. package/build/dex/baseswap-v3/contract-math/FixedPoint128.js +0 -8
  36. package/build/dex/baseswap-v3/contract-math/FixedPoint128.js.map +0 -1
  37. package/build/dex/baseswap-v3/contract-math/FixedPoint96.d.ts +0 -4
  38. package/build/dex/baseswap-v3/contract-math/FixedPoint96.js +0 -9
  39. package/build/dex/baseswap-v3/contract-math/FixedPoint96.js.map +0 -1
  40. package/build/dex/baseswap-v3/contract-math/FullMath.d.ts +0 -4
  41. package/build/dex/baseswap-v3/contract-math/FullMath.js +0 -19
  42. package/build/dex/baseswap-v3/contract-math/FullMath.js.map +0 -1
  43. package/build/dex/baseswap-v3/contract-math/LiquidityMath.d.ts +0 -3
  44. package/build/dex/baseswap-v3/contract-math/LiquidityMath.js +0 -22
  45. package/build/dex/baseswap-v3/contract-math/LiquidityMath.js.map +0 -1
  46. package/build/dex/baseswap-v3/contract-math/Oracle.d.ts +0 -10
  47. package/build/dex/baseswap-v3/contract-math/Oracle.js +0 -133
  48. package/build/dex/baseswap-v3/contract-math/Oracle.js.map +0 -1
  49. package/build/dex/baseswap-v3/contract-math/SqrtPriceMath.d.ts +0 -10
  50. package/build/dex/baseswap-v3/contract-math/SqrtPriceMath.js +0 -91
  51. package/build/dex/baseswap-v3/contract-math/SqrtPriceMath.js.map +0 -1
  52. package/build/dex/baseswap-v3/contract-math/SwapMath.d.ts +0 -8
  53. package/build/dex/baseswap-v3/contract-math/SwapMath.js +0 -70
  54. package/build/dex/baseswap-v3/contract-math/SwapMath.js.map +0 -1
  55. package/build/dex/baseswap-v3/contract-math/Tick.d.ts +0 -7
  56. package/build/dex/baseswap-v3/contract-math/Tick.js +0 -45
  57. package/build/dex/baseswap-v3/contract-math/Tick.js.map +0 -1
  58. package/build/dex/baseswap-v3/contract-math/TickBitMap.d.ts +0 -7
  59. package/build/dex/baseswap-v3/contract-math/TickBitMap.js +0 -84
  60. package/build/dex/baseswap-v3/contract-math/TickBitMap.js.map +0 -1
  61. package/build/dex/baseswap-v3/contract-math/TickMath.d.ts +0 -8
  62. package/build/dex/baseswap-v3/contract-math/TickMath.js +0 -162
  63. package/build/dex/baseswap-v3/contract-math/TickMath.js.map +0 -1
  64. package/build/dex/baseswap-v3/contract-math/UnsafeMath.d.ts +0 -3
  65. package/build/dex/baseswap-v3/contract-math/UnsafeMath.js +0 -10
  66. package/build/dex/baseswap-v3/contract-math/UnsafeMath.js.map +0 -1
  67. package/build/dex/baseswap-v3/contract-math/uniswap-v3-math.d.ts +0 -37
  68. package/build/dex/baseswap-v3/contract-math/uniswap-v3-math.js +0 -393
  69. package/build/dex/baseswap-v3/contract-math/uniswap-v3-math.js.map +0 -1
  70. package/build/dex/baseswap-v3/contract-math/utils.d.ts +0 -7
  71. package/build/dex/baseswap-v3/contract-math/utils.js +0 -42
  72. package/build/dex/baseswap-v3/contract-math/utils.js.map +0 -1
  73. package/build/dex/baseswap-v3/types.d.ts +0 -164
  74. package/build/dex/baseswap-v3/types.js +0 -9
  75. package/build/dex/baseswap-v3/types.js.map +0 -1
  76. package/build/dex/baseswap-v3/utils.d.ts +0 -6
  77. package/build/dex/baseswap-v3/utils.js +0 -65
  78. package/build/dex/baseswap-v3/utils.js.map +0 -1
  79. package/build/dex/hello/config.d.ts +0 -4
  80. package/build/dex/hello/config.js +0 -15
  81. package/build/dex/hello/config.js.map +0 -1
  82. package/build/dex/hello/hello-pool.d.ts +0 -39
  83. package/build/dex/hello/hello-pool.js +0 -64
  84. package/build/dex/hello/hello-pool.js.map +0 -1
  85. package/build/dex/hello/hello.d.ts +0 -37
  86. package/build/dex/hello/hello.js +0 -126
  87. package/build/dex/hello/hello.js.map +0 -1
  88. package/build/dex/hello/types.d.ts +0 -6
  89. package/build/dex/hello/types.js +0 -3
  90. package/build/dex/hello/types.js.map +0 -1
  91. package/build/dex/kyberswap-elastic/config.d.ts +0 -4
  92. package/build/dex/kyberswap-elastic/config.js +0 -80
  93. package/build/dex/kyberswap-elastic/config.js.map +0 -1
  94. package/build/dex/kyberswap-elastic/constants.d.ts +0 -17
  95. package/build/dex/kyberswap-elastic/constants.js +0 -42
  96. package/build/dex/kyberswap-elastic/constants.js.map +0 -1
  97. package/build/dex/kyberswap-elastic/contract-math/FixedPoint96.d.ts +0 -4
  98. package/build/dex/kyberswap-elastic/contract-math/FixedPoint96.js +0 -9
  99. package/build/dex/kyberswap-elastic/contract-math/FixedPoint96.js.map +0 -1
  100. package/build/dex/kyberswap-elastic/contract-math/FullMath.d.ts +0 -5
  101. package/build/dex/kyberswap-elastic/contract-math/FullMath.js +0 -22
  102. package/build/dex/kyberswap-elastic/contract-math/FullMath.js.map +0 -1
  103. package/build/dex/kyberswap-elastic/contract-math/LiqDeltaMath.d.ts +0 -3
  104. package/build/dex/kyberswap-elastic/contract-math/LiqDeltaMath.js +0 -12
  105. package/build/dex/kyberswap-elastic/contract-math/LiqDeltaMath.js.map +0 -1
  106. package/build/dex/kyberswap-elastic/contract-math/LiquidityMath.d.ts +0 -3
  107. package/build/dex/kyberswap-elastic/contract-math/LiquidityMath.js +0 -22
  108. package/build/dex/kyberswap-elastic/contract-math/LiquidityMath.js.map +0 -1
  109. package/build/dex/kyberswap-elastic/contract-math/QtyDeltaMath.d.ts +0 -5
  110. package/build/dex/kyberswap-elastic/contract-math/QtyDeltaMath.js +0 -25
  111. package/build/dex/kyberswap-elastic/contract-math/QtyDeltaMath.js.map +0 -1
  112. package/build/dex/kyberswap-elastic/contract-math/QuadMath.d.ts +0 -4
  113. package/build/dex/kyberswap-elastic/contract-math/QuadMath.js +0 -27
  114. package/build/dex/kyberswap-elastic/contract-math/QuadMath.js.map +0 -1
  115. package/build/dex/kyberswap-elastic/contract-math/ReinvestmentMath.d.ts +0 -3
  116. package/build/dex/kyberswap-elastic/contract-math/ReinvestmentMath.js +0 -14
  117. package/build/dex/kyberswap-elastic/contract-math/ReinvestmentMath.js.map +0 -1
  118. package/build/dex/kyberswap-elastic/contract-math/SafeCast.d.ts +0 -8
  119. package/build/dex/kyberswap-elastic/contract-math/SafeCast.js +0 -30
  120. package/build/dex/kyberswap-elastic/contract-math/SafeCast.js.map +0 -1
  121. package/build/dex/kyberswap-elastic/contract-math/SqrtPriceMath.d.ts +0 -10
  122. package/build/dex/kyberswap-elastic/contract-math/SqrtPriceMath.js +0 -91
  123. package/build/dex/kyberswap-elastic/contract-math/SqrtPriceMath.js.map +0 -1
  124. package/build/dex/kyberswap-elastic/contract-math/SwapMath.d.ts +0 -13
  125. package/build/dex/kyberswap-elastic/contract-math/SwapMath.js +0 -188
  126. package/build/dex/kyberswap-elastic/contract-math/SwapMath.js.map +0 -1
  127. package/build/dex/kyberswap-elastic/contract-math/TickLinkedList.d.ts +0 -6
  128. package/build/dex/kyberswap-elastic/contract-math/TickLinkedList.js +0 -41
  129. package/build/dex/kyberswap-elastic/contract-math/TickLinkedList.js.map +0 -1
  130. package/build/dex/kyberswap-elastic/contract-math/TickMath.d.ts +0 -9
  131. package/build/dex/kyberswap-elastic/contract-math/TickMath.js +0 -165
  132. package/build/dex/kyberswap-elastic/contract-math/TickMath.js.map +0 -1
  133. package/build/dex/kyberswap-elastic/contract-math/UnsafeMath.d.ts +0 -3
  134. package/build/dex/kyberswap-elastic/contract-math/UnsafeMath.js +0 -10
  135. package/build/dex/kyberswap-elastic/contract-math/UnsafeMath.js.map +0 -1
  136. package/build/dex/kyberswap-elastic/contract-math/kyberswap-elastic-math.d.ts +0 -28
  137. package/build/dex/kyberswap-elastic/contract-math/kyberswap-elastic-math.js +0 -502
  138. package/build/dex/kyberswap-elastic/contract-math/kyberswap-elastic-math.js.map +0 -1
  139. package/build/dex/kyberswap-elastic/contract-math/utils.d.ts +0 -3
  140. package/build/dex/kyberswap-elastic/contract-math/utils.js +0 -16
  141. package/build/dex/kyberswap-elastic/contract-math/utils.js.map +0 -1
  142. package/build/dex/kyberswap-elastic/errors.d.ts +0 -2
  143. package/build/dex/kyberswap-elastic/errors.js +0 -6
  144. package/build/dex/kyberswap-elastic/errors.js.map +0 -1
  145. package/build/dex/kyberswap-elastic/kyberswap-elastic-pool.d.ts +0 -80
  146. package/build/dex/kyberswap-elastic/kyberswap-elastic-pool.js +0 -458
  147. package/build/dex/kyberswap-elastic/kyberswap-elastic-pool.js.map +0 -1
  148. package/build/dex/kyberswap-elastic/kyberswap-elastic.d.ts +0 -53
  149. package/build/dex/kyberswap-elastic/kyberswap-elastic.js +0 -610
  150. package/build/dex/kyberswap-elastic/kyberswap-elastic.js.map +0 -1
  151. package/build/dex/kyberswap-elastic/types.d.ts +0 -134
  152. package/build/dex/kyberswap-elastic/types.js +0 -9
  153. package/build/dex/kyberswap-elastic/types.js.map +0 -1
  154. package/build/dex/kyberswap-elastic/utils/decoders.d.ts +0 -9
  155. package/build/dex/kyberswap-elastic/utils/decoders.js +0 -78
  156. package/build/dex/kyberswap-elastic/utils/decoders.js.map +0 -1
  157. package/build/dex/my-test-dex/config.d.ts +0 -4
  158. package/build/dex/my-test-dex/config.js +0 -15
  159. package/build/dex/my-test-dex/config.js.map +0 -1
  160. package/build/dex/my-test-dex/my-test-dex-pool.d.ts +0 -39
  161. package/build/dex/my-test-dex/my-test-dex-pool.js +0 -64
  162. package/build/dex/my-test-dex/my-test-dex-pool.js.map +0 -1
  163. package/build/dex/my-test-dex/my-test-dex.d.ts +0 -37
  164. package/build/dex/my-test-dex/my-test-dex.js +0 -126
  165. package/build/dex/my-test-dex/my-test-dex.js.map +0 -1
  166. package/build/dex/my-test-dex/types.d.ts +0 -6
  167. package/build/dex/my-test-dex/types.js +0 -3
  168. package/build/dex/my-test-dex/types.js.map +0 -1
  169. package/build/dex/solidly/forks-override/fvm.d.ts +0 -20
  170. package/build/dex/solidly/forks-override/fvm.js +0 -42
  171. package/build/dex/solidly/forks-override/fvm.js.map +0 -1
  172. package/build/dex/wombat/config.d.ts +0 -5
  173. package/build/dex/wombat/config.js +0 -36
  174. package/build/dex/wombat/config.js.map +0 -1
  175. package/build/dex/wombat/types.d.ts +0 -33
  176. package/build/dex/wombat/types.js +0 -3
  177. package/build/dex/wombat/types.js.map +0 -1
  178. package/build/dex/wombat/utils.d.ts +0 -9
  179. package/build/dex/wombat/utils.js +0 -66
  180. package/build/dex/wombat/utils.js.map +0 -1
  181. package/build/dex/wombat/wombat-bmw.d.ts +0 -40
  182. package/build/dex/wombat/wombat-bmw.js +0 -145
  183. package/build/dex/wombat/wombat-bmw.js.map +0 -1
  184. package/build/dex/wombat/wombat-pool.d.ts +0 -22
  185. package/build/dex/wombat/wombat-pool.js +0 -161
  186. package/build/dex/wombat/wombat-pool.js.map +0 -1
  187. package/build/dex/wombat/wombat-quoter.d.ts +0 -16
  188. package/build/dex/wombat/wombat-quoter.js +0 -176
  189. package/build/dex/wombat/wombat-quoter.js.map +0 -1
  190. package/build/dex/wombat/wombat.d.ts +0 -55
  191. package/build/dex/wombat/wombat.js +0 -292
  192. package/build/dex/wombat/wombat.js.map +0 -1
  193. package/src/dex/baseswap-v3/baseswap-v3-e2e.test.ts +0 -129
  194. package/src/dex/baseswap-v3/baseswap-v3-events.test.ts +0 -141
  195. package/src/dex/baseswap-v3/baseswap-v3-factory.ts +0 -73
  196. package/src/dex/baseswap-v3/baseswap-v3-integration.test.ts +0 -472
  197. package/src/dex/baseswap-v3/baseswap-v3-pool.ts +0 -519
  198. package/src/dex/baseswap-v3/baseswap-v3.ts +0 -1243
  199. package/src/dex/baseswap-v3/config.ts +0 -53
  200. package/src/dex/baseswap-v3/constants.ts +0 -41
  201. package/src/dex/baseswap-v3/contract-math/BitMath.ts +0 -90
  202. package/src/dex/baseswap-v3/contract-math/FixedPoint128.ts +0 -3
  203. package/src/dex/baseswap-v3/contract-math/FixedPoint96.ts +0 -4
  204. package/src/dex/baseswap-v3/contract-math/FullMath.ts +0 -30
  205. package/src/dex/baseswap-v3/contract-math/LiquidityMath.ts +0 -17
  206. package/src/dex/baseswap-v3/contract-math/Oracle.ts +0 -226
  207. package/src/dex/baseswap-v3/contract-math/SqrtPriceMath.ts +0 -226
  208. package/src/dex/baseswap-v3/contract-math/SwapMath.ts +0 -139
  209. package/src/dex/baseswap-v3/contract-math/Tick.ts +0 -82
  210. package/src/dex/baseswap-v3/contract-math/TickBitMap.ts +0 -123
  211. package/src/dex/baseswap-v3/contract-math/TickMath.ts +0 -211
  212. package/src/dex/baseswap-v3/contract-math/UnsafeMath.ts +0 -5
  213. package/src/dex/baseswap-v3/contract-math/uniswap-v3-math.ts +0 -666
  214. package/src/dex/baseswap-v3/contract-math/utils.ts +0 -55
  215. package/src/dex/baseswap-v3/types.ts +0 -191
  216. package/src/dex/baseswap-v3/utils.ts +0 -83
@@ -1,53 +0,0 @@
1
- import { DexParams } from './types';
2
- import { DexConfigMap, AdapterMappings } from '../../types';
3
- import { Network, SwapSide } from '../../constants';
4
- import { Address } from '../../types';
5
- // import RamsesV2StateMulticallABI from '../../abi/RamsesV2StateMulticall.abi.json';
6
- // import { AbiItem } from 'web3-utils';
7
- // import { decodeStateMultiCallResultWithRelativeBitmaps } from './forks/ramses-v2/utils';
8
- // import { RamsesV2EventPool } from './forks/ramses-v2/ramses-v2-pool';
9
-
10
- const SUPPORTED_FEES = [10000n, 2500n, 450n, 80n];
11
-
12
- // Pools that will be initialized on app startup
13
- // They are added for testing
14
- export const PoolsToPreload: DexConfigMap<
15
- { token0: Address; token1: Address }[]
16
- > = {
17
- BaseswapV3: {
18
- [Network.BASE]: [
19
- {
20
- token0: '0x4200000000000000000000000000000000000006'.toLowerCase(),
21
- token1: '0xd9aAEc86B65D86f6A7B5B1b0c42FFA531710b6CA'.toLowerCase(), // USDbC
22
- },
23
- {
24
- token0: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913'.toLowerCase(), // USDC
25
- token1: '0xd9aAEc86B65D86f6A7B5B1b0c42FFA531710b6CA'.toLowerCase(), // USDbC
26
- },
27
- ],
28
- },
29
- };
30
-
31
- export const BaseswapV3Config: DexConfigMap<DexParams> = {
32
- BaseswapV3: {
33
- [Network.BASE]: {
34
- factory: '0x38015D05f4fEC8AFe15D7cc0386a126574e8077B',
35
- quoter: '0x4fDBD73aD4B1DDde594BF05497C15f76308eFfb9',
36
- router: '0x1B8eea9315bE495187D873DA7773a874545D9D48',
37
- supportedFees: SUPPORTED_FEES,
38
- stateMulticall: '0x7160f736c52e1e78e92FD4eE4D73e21A7Cf4F950',
39
- uniswapMulticall: '0x091e99cb1C49331a94dD62755D168E941AbD0693',
40
- chunksCount: 10,
41
- initRetryFrequency: 10,
42
- initHash: `0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54`,
43
- subgraphURL: 'https://api.thegraph.com/subgraphs/name/baseswapfi/v3-base',
44
- },
45
- },
46
- };
47
-
48
- export const Adapters: Record<number, AdapterMappings> = {
49
- [Network.BASE]: {
50
- [SwapSide.SELL]: [{ name: 'BaseAdapter01', index: 1 }],
51
- [SwapSide.BUY]: [{ name: 'BaseBuyAdapter', index: 1 }],
52
- },
53
- };
@@ -1,41 +0,0 @@
1
- export const UNISWAPV3_TICK_GAS_COST = 24_000; // Ceiled
2
- export const UNISWAPV3_TICK_BASE_OVERHEAD = 75_000;
3
- export const UNISWAPV3_POOL_SEARCH_OVERHEAD = 10_000;
4
-
5
- // This is used for price calculation. If out of scope, return 0n
6
- export const TICK_BITMAP_TO_USE = 4n;
7
-
8
- // This is used to check if the state is still valid.
9
- export const TICK_BITMAP_BUFFER = 8n;
10
-
11
- export const MAX_PRICING_COMPUTATION_STEPS_ALLOWED = 128;
12
-
13
- export const UNISWAPV3_SUBGRAPH_URL =
14
- 'https://api.thegraph.com/subgraphs/name/baseswapfi/v3-base';
15
-
16
- export const UNISWAPV3_EFFICIENCY_FACTOR = 3;
17
-
18
- export const ZERO_TICK_INFO = {
19
- liquidityGross: 0n,
20
- liquidityNet: 0n,
21
- tickCumulativeOutside: 0n,
22
- secondsPerLiquidityOutsideX128: 0n,
23
- secondsOutside: 0n,
24
- initialized: false,
25
- };
26
-
27
- export const ZERO_ORACLE_OBSERVATION = {
28
- blockTimestamp: 0n,
29
- tickCumulative: 0n,
30
- secondsPerLiquidityCumulativeX128: 0n,
31
- initialized: false,
32
- };
33
-
34
- export const OUT_OF_RANGE_ERROR_POSTFIX = `INVALID_TICK_BIT_MAP_RANGES`;
35
-
36
- export const DEFAULT_POOL_INIT_CODE_HASH = `0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54`;
37
-
38
- export enum DirectMethods {
39
- directSell = 'directUniV3Swap',
40
- directBuy = 'directUniV3Buy',
41
- }
@@ -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 '../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
- }