@paraswap/dex-lib 2.28.9-ramses-v2 → 2.28.9

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 (142) hide show
  1. package/build/dex/algebra/algebra.js +5 -0
  2. package/build/dex/algebra/algebra.js.map +1 -1
  3. package/build/dex/algebra/config.js +1 -0
  4. package/build/dex/algebra/config.js.map +1 -1
  5. package/build/dex/algebra/types.d.ts +1 -0
  6. package/build/dex/index.js +0 -2
  7. package/build/dex/index.js.map +1 -1
  8. package/build/dex/{ramses-v2 → test-dex}/config.d.ts +1 -1
  9. package/build/dex/test-dex/config.js +15 -0
  10. package/build/dex/test-dex/config.js.map +1 -0
  11. package/build/dex/test-dex/test-dex-pool.d.ts +39 -0
  12. package/build/dex/test-dex/test-dex-pool.js +63 -0
  13. package/build/dex/test-dex/test-dex-pool.js.map +1 -0
  14. package/build/dex/test-dex/test-dex.d.ts +37 -0
  15. package/build/dex/test-dex/test-dex.js +126 -0
  16. package/build/dex/test-dex/test-dex.js.map +1 -0
  17. package/build/dex/test-dex/types.d.ts +6 -0
  18. package/build/dex/{pancakeswap-v3 → test-dex}/types.js.map +1 -1
  19. package/build/dex/woo-fi-v2/woo-fi-math.d.ts +16 -0
  20. package/build/dex/woo-fi-v2/woo-fi-math.js +137 -0
  21. package/build/dex/woo-fi-v2/woo-fi-math.js.map +1 -0
  22. package/build/lib/multi-wrapper.js +3 -5
  23. package/build/lib/multi-wrapper.js.map +1 -1
  24. package/package.json +1 -1
  25. package/src/dex/algebra/algebra.ts +13 -0
  26. package/src/dex/algebra/config.ts +1 -0
  27. package/src/dex/algebra/types.ts +1 -0
  28. package/src/dex/index.ts +0 -2
  29. package/src/dex/uniswap-v3/uniswap-v3-integration.test.ts +3 -26
  30. package/src/lib/multi-wrapper.ts +4 -4
  31. package/tests/constants-e2e.ts +5 -1
  32. package/tests/utils-e2e.ts +0 -1
  33. package/.idea/aws.xml +0 -17
  34. package/.idea/codeStyles/Project.xml +0 -19
  35. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  36. package/.idea/misc.xml +0 -6
  37. package/.idea/modules.xml +0 -8
  38. package/.idea/paraswap-dex-lib.iml +0 -9
  39. package/.idea/vcs.xml +0 -6
  40. package/build/abi/RamsesV2StateMulticall.abi.json +0 -891
  41. package/build/abi/algebra/AlgebraPool.abi.json +0 -727
  42. package/build/abi/curve-v1/StableSwapFRXETH.json +0 -889
  43. package/build/abi/curve-v1/StableSwapWBETH.json +0 -1223
  44. package/build/abi/wBETH.json +0 -1527
  45. package/build/dex/algebra/algebra-pool.d.ts +0 -46
  46. package/build/dex/algebra/algebra-pool.js +0 -336
  47. package/build/dex/algebra/algebra-pool.js.map +0 -1
  48. package/build/dex/curve-v1/pools/frxETHpool.d.ts +0 -7
  49. package/build/dex/curve-v1/pools/frxETHpool.js +0 -27
  50. package/build/dex/curve-v1/pools/frxETHpool.js.map +0 -1
  51. package/build/dex/curve-v1/pools/wbETHpool.d.ts +0 -29
  52. package/build/dex/curve-v1/pools/wbETHpool.js +0 -408
  53. package/build/dex/curve-v1/pools/wbETHpool.js.map +0 -1
  54. package/build/dex/pancakeswap-v3/types.d.ts +0 -14
  55. package/build/dex/ramses-v2/config.js +0 -28
  56. package/build/dex/ramses-v2/config.js.map +0 -1
  57. package/build/dex/ramses-v2/constants.d.ts +0 -28
  58. package/build/dex/ramses-v2/constants.js +0 -35
  59. package/build/dex/ramses-v2/constants.js.map +0 -1
  60. package/build/dex/ramses-v2/contract-math/BitMath.d.ts +0 -4
  61. package/build/dex/ramses-v2/contract-math/BitMath.js +0 -93
  62. package/build/dex/ramses-v2/contract-math/BitMath.js.map +0 -1
  63. package/build/dex/ramses-v2/contract-math/FixedPoint128.d.ts +0 -3
  64. package/build/dex/ramses-v2/contract-math/FixedPoint128.js +0 -8
  65. package/build/dex/ramses-v2/contract-math/FixedPoint128.js.map +0 -1
  66. package/build/dex/ramses-v2/contract-math/FixedPoint96.d.ts +0 -4
  67. package/build/dex/ramses-v2/contract-math/FixedPoint96.js +0 -9
  68. package/build/dex/ramses-v2/contract-math/FixedPoint96.js.map +0 -1
  69. package/build/dex/ramses-v2/contract-math/FullMath.d.ts +0 -4
  70. package/build/dex/ramses-v2/contract-math/FullMath.js +0 -19
  71. package/build/dex/ramses-v2/contract-math/FullMath.js.map +0 -1
  72. package/build/dex/ramses-v2/contract-math/LiquidityMath.d.ts +0 -3
  73. package/build/dex/ramses-v2/contract-math/LiquidityMath.js +0 -22
  74. package/build/dex/ramses-v2/contract-math/LiquidityMath.js.map +0 -1
  75. package/build/dex/ramses-v2/contract-math/Oracle.d.ts +0 -10
  76. package/build/dex/ramses-v2/contract-math/Oracle.js +0 -133
  77. package/build/dex/ramses-v2/contract-math/Oracle.js.map +0 -1
  78. package/build/dex/ramses-v2/contract-math/SqrtPriceMath.d.ts +0 -10
  79. package/build/dex/ramses-v2/contract-math/SqrtPriceMath.js +0 -91
  80. package/build/dex/ramses-v2/contract-math/SqrtPriceMath.js.map +0 -1
  81. package/build/dex/ramses-v2/contract-math/SwapMath.d.ts +0 -8
  82. package/build/dex/ramses-v2/contract-math/SwapMath.js +0 -70
  83. package/build/dex/ramses-v2/contract-math/SwapMath.js.map +0 -1
  84. package/build/dex/ramses-v2/contract-math/Tick.d.ts +0 -7
  85. package/build/dex/ramses-v2/contract-math/Tick.js +0 -45
  86. package/build/dex/ramses-v2/contract-math/Tick.js.map +0 -1
  87. package/build/dex/ramses-v2/contract-math/TickBitMap.d.ts +0 -8
  88. package/build/dex/ramses-v2/contract-math/TickBitMap.js +0 -87
  89. package/build/dex/ramses-v2/contract-math/TickBitMap.js.map +0 -1
  90. package/build/dex/ramses-v2/contract-math/TickMath.d.ts +0 -8
  91. package/build/dex/ramses-v2/contract-math/TickMath.js +0 -162
  92. package/build/dex/ramses-v2/contract-math/TickMath.js.map +0 -1
  93. package/build/dex/ramses-v2/contract-math/UnsafeMath.d.ts +0 -3
  94. package/build/dex/ramses-v2/contract-math/UnsafeMath.js +0 -10
  95. package/build/dex/ramses-v2/contract-math/UnsafeMath.js.map +0 -1
  96. package/build/dex/ramses-v2/contract-math/uniswap-v3-math.d.ts +0 -18
  97. package/build/dex/ramses-v2/contract-math/uniswap-v3-math.js +0 -392
  98. package/build/dex/ramses-v2/contract-math/uniswap-v3-math.js.map +0 -1
  99. package/build/dex/ramses-v2/contract-math/utils.d.ts +0 -3
  100. package/build/dex/ramses-v2/contract-math/utils.js +0 -16
  101. package/build/dex/ramses-v2/contract-math/utils.js.map +0 -1
  102. package/build/dex/ramses-v2/ramses-v2-pool.d.ts +0 -44
  103. package/build/dex/ramses-v2/ramses-v2-pool.js +0 -327
  104. package/build/dex/ramses-v2/ramses-v2-pool.js.map +0 -1
  105. package/build/dex/ramses-v2/ramses-v2.d.ts +0 -61
  106. package/build/dex/ramses-v2/ramses-v2.js +0 -690
  107. package/build/dex/ramses-v2/ramses-v2.js.map +0 -1
  108. package/build/dex/ramses-v2/types.d.ts +0 -154
  109. package/build/dex/ramses-v2/types.js +0 -9
  110. package/build/dex/ramses-v2/types.js.map +0 -1
  111. package/build/dex/ramses-v2/utils.d.ts +0 -6
  112. package/build/dex/ramses-v2/utils.js +0 -71
  113. package/build/dex/ramses-v2/utils.js.map +0 -1
  114. package/build/dex/solidly/forks-override/ramses.d.ts +0 -8
  115. package/build/dex/solidly/forks-override/ramses.js +0 -15
  116. package/build/dex/solidly/forks-override/ramses.js.map +0 -1
  117. package/src/abi/RamsesV2Pool.abi.json +0 -1554
  118. package/src/abi/RamsesV2StateMulticall.abi.json +0 -891
  119. package/src/dex/ramses-v2/config.ts +0 -31
  120. package/src/dex/ramses-v2/constants.ts +0 -41
  121. package/src/dex/ramses-v2/contract-math/BitMath.ts +0 -90
  122. package/src/dex/ramses-v2/contract-math/FixedPoint128.ts +0 -3
  123. package/src/dex/ramses-v2/contract-math/FixedPoint96.ts +0 -4
  124. package/src/dex/ramses-v2/contract-math/FullMath.ts +0 -30
  125. package/src/dex/ramses-v2/contract-math/LiquidityMath.ts +0 -17
  126. package/src/dex/ramses-v2/contract-math/Oracle.ts +0 -226
  127. package/src/dex/ramses-v2/contract-math/SqrtPriceMath.ts +0 -226
  128. package/src/dex/ramses-v2/contract-math/SwapMath.ts +0 -139
  129. package/src/dex/ramses-v2/contract-math/Tick.ts +0 -82
  130. package/src/dex/ramses-v2/contract-math/TickBitMap.ts +0 -128
  131. package/src/dex/ramses-v2/contract-math/TickMath.ts +0 -211
  132. package/src/dex/ramses-v2/contract-math/UnsafeMath.ts +0 -5
  133. package/src/dex/ramses-v2/contract-math/uniswap-v3-math.ts +0 -666
  134. package/src/dex/ramses-v2/contract-math/utils.ts +0 -11
  135. package/src/dex/ramses-v2/ramses-v2-e2e.test.ts +0 -100
  136. package/src/dex/ramses-v2/ramses-v2-events.test.ts +0 -182
  137. package/src/dex/ramses-v2/ramses-v2-integration.test.ts +0 -462
  138. package/src/dex/ramses-v2/ramses-v2-pool.ts +0 -546
  139. package/src/dex/ramses-v2/ramses-v2.ts +0 -1136
  140. package/src/dex/ramses-v2/types.ts +0 -177
  141. package/src/dex/ramses-v2/utils.ts +0 -90
  142. /package/build/dex/{pancakeswap-v3 → test-dex}/types.js +0 -0
@@ -1,139 +0,0 @@
1
- import { BI_POWS } from '../../../bigint-constants';
2
- import { FullMath } from './FullMath';
3
- import { SqrtPriceMath } from './SqrtPriceMath';
4
-
5
- export class SwapMath {
6
- static computeSwapStep(
7
- sqrtRatioCurrentX96: bigint,
8
- sqrtRatioTargetX96: bigint,
9
- liquidity: bigint,
10
- amountRemaining: bigint,
11
- feePips: bigint,
12
- ): {
13
- sqrtRatioNextX96: bigint;
14
- amountIn: bigint;
15
- amountOut: bigint;
16
- feeAmount: bigint;
17
- } {
18
- const zeroForOne = sqrtRatioCurrentX96 >= sqrtRatioTargetX96;
19
- const exactIn = amountRemaining >= 0n;
20
-
21
- let sqrtRatioNextX96 = 0n;
22
- let amountIn = 0n;
23
- let amountOut = 0n;
24
- let feeAmount = 0n;
25
-
26
- if (exactIn) {
27
- const amountRemainingLessFee = FullMath.mulDiv(
28
- BigInt.asUintN(256, amountRemaining),
29
- BI_POWS[6] - feePips,
30
- BI_POWS[6],
31
- );
32
- amountIn = zeroForOne
33
- ? SqrtPriceMath.getAmount0Delta(
34
- sqrtRatioTargetX96,
35
- sqrtRatioCurrentX96,
36
- liquidity,
37
- true,
38
- )
39
- : SqrtPriceMath.getAmount1Delta(
40
- sqrtRatioCurrentX96,
41
- sqrtRatioTargetX96,
42
- liquidity,
43
- true,
44
- );
45
- if (amountRemainingLessFee >= amountIn)
46
- sqrtRatioNextX96 = sqrtRatioTargetX96;
47
- else
48
- sqrtRatioNextX96 = SqrtPriceMath.getNextSqrtPriceFromInput(
49
- sqrtRatioCurrentX96,
50
- liquidity,
51
- amountRemainingLessFee,
52
- zeroForOne,
53
- );
54
- } else {
55
- amountOut = zeroForOne
56
- ? SqrtPriceMath.getAmount1Delta(
57
- sqrtRatioTargetX96,
58
- sqrtRatioCurrentX96,
59
- liquidity,
60
- false,
61
- )
62
- : SqrtPriceMath.getAmount0Delta(
63
- sqrtRatioCurrentX96,
64
- sqrtRatioTargetX96,
65
- liquidity,
66
- false,
67
- );
68
- if (BigInt.asUintN(256, -amountRemaining) >= amountOut)
69
- sqrtRatioNextX96 = sqrtRatioTargetX96;
70
- else
71
- sqrtRatioNextX96 = SqrtPriceMath.getNextSqrtPriceFromOutput(
72
- sqrtRatioCurrentX96,
73
- liquidity,
74
- BigInt.asUintN(256, -amountRemaining),
75
- zeroForOne,
76
- );
77
- }
78
-
79
- const max = sqrtRatioTargetX96 == sqrtRatioNextX96;
80
-
81
- if (zeroForOne) {
82
- amountIn =
83
- max && exactIn
84
- ? amountIn
85
- : SqrtPriceMath.getAmount0Delta(
86
- sqrtRatioNextX96,
87
- sqrtRatioCurrentX96,
88
- liquidity,
89
- true,
90
- );
91
- amountOut =
92
- max && !exactIn
93
- ? amountOut
94
- : SqrtPriceMath.getAmount1Delta(
95
- sqrtRatioNextX96,
96
- sqrtRatioCurrentX96,
97
- liquidity,
98
- false,
99
- );
100
- } else {
101
- amountIn =
102
- max && exactIn
103
- ? amountIn
104
- : SqrtPriceMath.getAmount1Delta(
105
- sqrtRatioCurrentX96,
106
- sqrtRatioNextX96,
107
- liquidity,
108
- true,
109
- );
110
- amountOut =
111
- max && !exactIn
112
- ? amountOut
113
- : SqrtPriceMath.getAmount0Delta(
114
- sqrtRatioCurrentX96,
115
- sqrtRatioNextX96,
116
- liquidity,
117
- false,
118
- );
119
- }
120
-
121
- // cap the output amount to not exceed the remaining output amount
122
- if (!exactIn && amountOut > BigInt.asUintN(256, -amountRemaining)) {
123
- amountOut = BigInt.asUintN(256, -amountRemaining);
124
- }
125
-
126
- if (exactIn && sqrtRatioNextX96 != sqrtRatioTargetX96) {
127
- // we didn't reach the target, so take the remainder of the maximum input as fee
128
- feeAmount = BigInt.asUintN(256, amountRemaining) - amountIn;
129
- } else {
130
- feeAmount = FullMath.mulDivRoundingUp(
131
- amountIn,
132
- feePips,
133
- BI_POWS[6] - feePips,
134
- );
135
- }
136
-
137
- return { sqrtRatioNextX96, amountIn, amountOut, feeAmount };
138
- }
139
- }
@@ -1,82 +0,0 @@
1
- import { PoolState, TickInfo } from '../types';
2
- import { LiquidityMath } from './LiquidityMath';
3
- import { _require } from '../../../utils';
4
- import { NumberAsString } from '@paraswap/core';
5
- import { ZERO_TICK_INFO } from '../constants';
6
-
7
- export class Tick {
8
- static update(
9
- state: PoolState,
10
- tick: bigint,
11
- tickCurrent: bigint,
12
- liquidityDelta: bigint,
13
- secondsPerLiquidityCumulativeX128: bigint,
14
- tickCumulative: bigint,
15
- time: bigint,
16
- upper: boolean,
17
- maxLiquidity: bigint,
18
- ): boolean {
19
- let info = state.ticks[Number(tick)];
20
-
21
- if (info === undefined) {
22
- info = { ...ZERO_TICK_INFO };
23
- state.ticks[Number(tick)] = info;
24
- }
25
-
26
- const liquidityGrossBefore = info.liquidityGross;
27
- const liquidityGrossAfter = LiquidityMath.addDelta(
28
- liquidityGrossBefore,
29
- liquidityDelta,
30
- );
31
-
32
- _require(
33
- liquidityGrossAfter <= maxLiquidity,
34
- 'LO',
35
- { liquidityGrossAfter, maxLiquidity },
36
- 'liquidityGrossAfter <= maxLiquidity',
37
- );
38
-
39
- const flipped = (liquidityGrossAfter == 0n) != (liquidityGrossBefore == 0n);
40
-
41
- if (liquidityGrossBefore == 0n) {
42
- if (tick <= tickCurrent) {
43
- info.secondsPerLiquidityOutsideX128 = secondsPerLiquidityCumulativeX128;
44
- info.tickCumulativeOutside = tickCumulative;
45
- info.secondsOutside = time;
46
- }
47
- info.initialized = true;
48
- }
49
-
50
- info.liquidityGross = liquidityGrossAfter;
51
-
52
- info.liquidityNet = upper
53
- ? BigInt.asIntN(
54
- 128,
55
- BigInt.asIntN(256, info.liquidityNet) - liquidityDelta,
56
- )
57
- : BigInt.asIntN(
58
- 128,
59
- BigInt.asIntN(256, info.liquidityNet) + liquidityDelta,
60
- );
61
- return flipped;
62
- }
63
-
64
- static clear(state: PoolState, tick: bigint) {
65
- delete state.ticks[Number(tick)];
66
- }
67
-
68
- static cross(
69
- ticks: Record<NumberAsString, TickInfo>,
70
- tick: bigint,
71
- secondsPerLiquidityCumulativeX128: bigint,
72
- tickCumulative: bigint,
73
- time: bigint,
74
- ): bigint {
75
- const info = ticks[Number(tick)];
76
- info.secondsPerLiquidityOutsideX128 =
77
- secondsPerLiquidityCumulativeX128 - info.secondsPerLiquidityOutsideX128;
78
- info.tickCumulativeOutside = tickCumulative - info.tickCumulativeOutside;
79
- info.secondsOutside = time - info.secondsOutside;
80
- return info.liquidityNet;
81
- }
82
- }
@@ -1,128 +0,0 @@
1
- import { BI_MAX_UINT8 } from '../../../bigint-constants';
2
- import { PoolState } from '../types';
3
- import { BitMath } from './BitMath';
4
- import { _require } from '../../../utils';
5
- import { DeepReadonly } from 'ts-essentials';
6
- import {
7
- OUT_OF_RANGE_ERROR_POSTFIX,
8
- TICK_BITMAP_BUFFER,
9
- TICK_BITMAP_TO_USE,
10
- } from '../constants';
11
-
12
- function isWordPosOut(
13
- wordPos: bigint,
14
- startTickBitmap: bigint,
15
- // For pricing we use wider range to check price impact. If function called from event
16
- // it must always be within buffer
17
- isPriceQuery: boolean,
18
- ) {
19
- let lowerTickBitmapLimit;
20
- let upperTickBitmapLimit;
21
-
22
- if (isPriceQuery) {
23
- lowerTickBitmapLimit =
24
- startTickBitmap - (TICK_BITMAP_BUFFER + TICK_BITMAP_TO_USE);
25
- upperTickBitmapLimit =
26
- startTickBitmap + (TICK_BITMAP_BUFFER + TICK_BITMAP_TO_USE);
27
- } else {
28
- lowerTickBitmapLimit = startTickBitmap - TICK_BITMAP_BUFFER;
29
- upperTickBitmapLimit = startTickBitmap + TICK_BITMAP_BUFFER;
30
- }
31
-
32
- _require(
33
- wordPos >= lowerTickBitmapLimit && wordPos <= upperTickBitmapLimit,
34
- `wordPos is out of safe state tickBitmap request range: ${OUT_OF_RANGE_ERROR_POSTFIX}`,
35
- { wordPos },
36
- `wordPos >= LOWER_TICK_REQUEST_LIMIT && wordPos <= UPPER_TICK_REQUEST_LIMIT`,
37
- );
38
- }
39
-
40
- export class TickBitMap {
41
- static position(tick: bigint): [bigint, bigint] {
42
- return [BigInt.asIntN(16, tick >> 8n), BigInt.asUintN(8, tick % 256n)];
43
- }
44
-
45
- static flipTick(state: PoolState, tick: bigint, tickSpacing: bigint) {
46
- _require(
47
- tick % tickSpacing === 0n,
48
- '',
49
- { tick, tickSpacing },
50
- 'tick % tickSpacing == 0n,',
51
- );
52
- const [wordPos, bitPos] = TickBitMap.position(tick / tickSpacing);
53
- const mask = 1n << bitPos;
54
-
55
- // flipTick is used only in _updatePosition which is always state changing event
56
- // Therefore it is never used in price query
57
- isWordPosOut(wordPos, state.startTickBitmap, false);
58
-
59
- const stringWordPos = wordPos.toString();
60
- if (state.tickBitmap[stringWordPos] === undefined) {
61
- state.tickBitmap[stringWordPos] = 0n;
62
- }
63
-
64
- state.tickBitmap[stringWordPos] ^= mask;
65
- }
66
-
67
- static nextInitializedTickWithinOneWord(
68
- state: DeepReadonly<PoolState>,
69
- tick: bigint,
70
- tickSpacing: bigint,
71
- lte: boolean,
72
- isPriceQuery: boolean,
73
- ): [bigint, boolean] {
74
- let compressed = tick / tickSpacing;
75
- if (tick < 0n && tick % tickSpacing != 0n) compressed--;
76
-
77
- let next = 0n;
78
- let initialized = false;
79
-
80
- if (lte) {
81
- const [wordPos, bitPos] = TickBitMap.position(compressed);
82
- const mask = (1n << bitPos) - 1n + (1n << bitPos);
83
-
84
- isWordPosOut(wordPos, state.startTickBitmap, isPriceQuery);
85
- let tickBitmapValue = state.tickBitmap[wordPos.toString()];
86
- tickBitmapValue = tickBitmapValue === undefined ? 0n : tickBitmapValue;
87
-
88
- const masked = tickBitmapValue & mask;
89
-
90
- initialized = masked != 0n;
91
- next = initialized
92
- ? (compressed -
93
- BigInt.asIntN(24, bitPos - BitMath.mostSignificantBit(masked))) *
94
- tickSpacing
95
- : (compressed - BigInt.asIntN(24, bitPos)) * tickSpacing;
96
- } else {
97
- // start from the word of the next tick, since the current tick state doesn't matter
98
- const [wordPos, bitPos] = TickBitMap.position(compressed + 1n);
99
- const mask = ~((1n << bitPos) - 1n);
100
-
101
- isWordPosOut(wordPos, state.startTickBitmap, isPriceQuery);
102
- let tickBitmapValue = state.tickBitmap[wordPos.toString()];
103
- tickBitmapValue = tickBitmapValue === undefined ? 0n : tickBitmapValue;
104
-
105
- const masked = tickBitmapValue & mask;
106
-
107
- initialized = masked != 0n;
108
- next = initialized
109
- ? (compressed +
110
- 1n +
111
- BigInt.asIntN(24, BitMath.leastSignificantBit(masked) - bitPos)) *
112
- tickSpacing
113
- : (compressed + 1n + BigInt.asIntN(24, BI_MAX_UINT8 - bitPos)) *
114
- tickSpacing;
115
- }
116
-
117
- return [next, initialized];
118
- }
119
-
120
- static _putZeroIfUndefined(
121
- state: PoolState,
122
- tickBitmapValue: bigint | undefined,
123
- wordPos: bigint,
124
- isPriceQuery: boolean = false,
125
- ): bigint {
126
- return tickBitmapValue === undefined ? 0n : tickBitmapValue;
127
- }
128
- }
@@ -1,211 +0,0 @@
1
- import { gt } from 'lodash';
2
- import { BI_MAX_UINT256 } from '../../../bigint-constants';
3
- import { _gt } from './utils';
4
- import { _require } from '../../../utils';
5
-
6
- export class TickMath {
7
- static readonly MIN_TICK = -887272n;
8
- static readonly MAX_TICK = -TickMath.MIN_TICK;
9
- static readonly MIN_SQRT_RATIO = 4295128739n;
10
- static readonly MAX_SQRT_RATIO =
11
- 1461446703485210103287273052203988822378723970342n;
12
-
13
- static getSqrtRatioAtTick(tick: bigint): bigint {
14
- const absTick =
15
- tick < 0n
16
- ? BigInt.asUintN(256, -BigInt.asIntN(256, tick))
17
- : BigInt.asUintN(256, BigInt.asIntN(256, tick));
18
- _require(
19
- absTick <= BigInt.asUintN(256, TickMath.MAX_TICK),
20
- 'T',
21
- { absTick },
22
- 'absTick <= BigInt.asUintN(256, TickMath.MAX_TICK)',
23
- );
24
-
25
- let ratio =
26
- (absTick & 0x1n) !== 0n
27
- ? 0xfffcb933bd6fad37aa2d162d1a594001n
28
- : 0x100000000000000000000000000000000n;
29
- if ((absTick & 0x2n) !== 0n)
30
- ratio = (ratio * 0xfff97272373d413259a46990580e213an) >> 128n;
31
- if ((absTick & 0x4n) !== 0n)
32
- ratio = (ratio * 0xfff2e50f5f656932ef12357cf3c7fdccn) >> 128n;
33
- if ((absTick & 0x8n) !== 0n)
34
- ratio = (ratio * 0xffe5caca7e10e4e61c3624eaa0941cd0n) >> 128n;
35
- if ((absTick & 0x10n) !== 0n)
36
- ratio = (ratio * 0xffcb9843d60f6159c9db58835c926644n) >> 128n;
37
- if ((absTick & 0x20n) !== 0n)
38
- ratio = (ratio * 0xff973b41fa98c081472e6896dfb254c0n) >> 128n;
39
- if ((absTick & 0x40n) !== 0n)
40
- ratio = (ratio * 0xff2ea16466c96a3843ec78b326b52861n) >> 128n;
41
- if ((absTick & 0x80n) !== 0n)
42
- ratio = (ratio * 0xfe5dee046a99a2a811c461f1969c3053n) >> 128n;
43
- if ((absTick & 0x100n) !== 0n)
44
- ratio = (ratio * 0xfcbe86c7900a88aedcffc83b479aa3a4n) >> 128n;
45
- if ((absTick & 0x200n) !== 0n)
46
- ratio = (ratio * 0xf987a7253ac413176f2b074cf7815e54n) >> 128n;
47
- if ((absTick & 0x400n) !== 0n)
48
- ratio = (ratio * 0xf3392b0822b70005940c7a398e4b70f3n) >> 128n;
49
- if ((absTick & 0x800n) !== 0n)
50
- ratio = (ratio * 0xe7159475a2c29b7443b29c7fa6e889d9n) >> 128n;
51
- if ((absTick & 0x1000n) !== 0n)
52
- ratio = (ratio * 0xd097f3bdfd2022b8845ad8f792aa5825n) >> 128n;
53
- if ((absTick & 0x2000n) !== 0n)
54
- ratio = (ratio * 0xa9f746462d870fdf8a65dc1f90e061e5n) >> 128n;
55
- if ((absTick & 0x4000n) !== 0n)
56
- ratio = (ratio * 0x70d869a156d2a1b890bb3df62baf32f7n) >> 128n;
57
- if ((absTick & 0x8000n) !== 0n)
58
- ratio = (ratio * 0x31be135f97d08fd981231505542fcfa6n) >> 128n;
59
- if ((absTick & 0x10000n) !== 0n)
60
- ratio = (ratio * 0x9aa508b5b7a84e1c677de54f3e99bc9n) >> 128n;
61
- if ((absTick & 0x20000n) !== 0n)
62
- ratio = (ratio * 0x5d6af8dedb81196699c329225ee604n) >> 128n;
63
- if ((absTick & 0x40000n) !== 0n)
64
- ratio = (ratio * 0x2216e584f5fa1ea926041bedfe98n) >> 128n;
65
- if ((absTick & 0x80000n) !== 0n)
66
- ratio = (ratio * 0x48a170391f7dc42444e8fa2n) >> 128n;
67
-
68
- if (tick > 0) ratio = BI_MAX_UINT256 / ratio;
69
- return BigInt.asUintN(
70
- 160,
71
- (ratio >> 32n) + (ratio % (1n << 32n) == 0n ? 0n : 1n),
72
- );
73
- }
74
-
75
- static getTickAtSqrtRatio(sqrtPriceX96: bigint): bigint {
76
- _require(
77
- sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO &&
78
- sqrtPriceX96 < TickMath.MAX_SQRT_RATIO,
79
- 'R',
80
- { sqrtPriceX96 },
81
- 'sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO && sqrtPriceX96 < TickMath.MAX_SQRT_RATIO',
82
- );
83
-
84
- let ratio = BigInt.asUintN(256, sqrtPriceX96) << 32n;
85
-
86
- let r = ratio;
87
- let msb = 0n;
88
-
89
- let f = _gt(r, 0xffffffffffffffffffffffffffffffffn) << 7n;
90
- msb = msb | f;
91
- r = r >> f;
92
-
93
- f = _gt(r, 0xffffffffffffffffn) << 6n;
94
- msb = msb | f;
95
- r = r >> f;
96
-
97
- f = _gt(r, 0xffffffffn) << 5n;
98
- msb = msb | f;
99
- r = r >> f;
100
-
101
- f = _gt(r, 0xffffn) << 4n;
102
- msb = msb | f;
103
- r = r >> f;
104
-
105
- f = _gt(r, 0xffn) << 3n;
106
- msb = msb | f;
107
- r = r >> f;
108
-
109
- f = _gt(r, 0xfn) << 2n;
110
- msb = msb | f;
111
- r = r >> f;
112
-
113
- f = _gt(r, 0x3n) << 1n;
114
- msb = msb | f;
115
- r = r >> f;
116
-
117
- f = _gt(r, 0x1n);
118
- msb = msb | f;
119
-
120
- if (msb >= 128n) r = ratio >> (msb - 127n);
121
- else r = ratio << (127n - msb);
122
-
123
- let log_2 = (BigInt.asIntN(256, msb) - 128n) << 64n;
124
-
125
- r = (r * r) >> 127n;
126
- f = r >> 128n;
127
- log_2 = log_2 | (f << 63n);
128
- r = r >> f;
129
-
130
- r = (r * r) >> 127n;
131
- f = r >> 128n;
132
- log_2 = log_2 | (f << 62n);
133
- r = r >> f;
134
-
135
- r = (r * r) >> 127n;
136
- f = r >> 128n;
137
- log_2 = log_2 | (f << 61n);
138
- r = r >> f;
139
-
140
- r = (r * r) >> 127n;
141
- f = r >> 128n;
142
- log_2 = log_2 | (f << 60n);
143
- r = r >> f;
144
-
145
- r = (r * r) >> 127n;
146
- f = r >> 128n;
147
- log_2 = log_2 | (f << 59n);
148
- r = r >> f;
149
-
150
- r = (r * r) >> 127n;
151
- f = r >> 128n;
152
- log_2 = log_2 | (f << 58n);
153
- r = r >> f;
154
-
155
- r = (r * r) >> 127n;
156
- f = r >> 128n;
157
- log_2 = log_2 | (f << 57n);
158
- r = r >> f;
159
-
160
- r = (r * r) >> 127n;
161
- f = r >> 128n;
162
- log_2 = log_2 | (f << 56n);
163
- r = r >> f;
164
-
165
- r = (r * r) >> 127n;
166
- f = r >> 128n;
167
- log_2 = log_2 | (f << 55n);
168
- r = r >> f;
169
-
170
- r = (r * r) >> 127n;
171
- f = r >> 128n;
172
- log_2 = log_2 | (f << 54n);
173
- r = r >> f;
174
-
175
- r = (r * r) >> 127n;
176
- f = r >> 128n;
177
- log_2 = log_2 | (f << 53n);
178
- r = r >> f;
179
-
180
- r = (r * r) >> 127n;
181
- f = r >> 128n;
182
- log_2 = log_2 | (f << 52n);
183
- r = r >> f;
184
-
185
- r = (r * r) >> 127n;
186
- f = r >> 128n;
187
- log_2 = log_2 | (f << 51n);
188
- r = r >> f;
189
-
190
- r = (r * r) >> 127n;
191
- f = r >> 128n;
192
- log_2 = log_2 | (f << 50n);
193
-
194
- const log_sqrt10001 = log_2 * 255738958999603826347141n; // 128.128 number
195
-
196
- const tickLow = BigInt.asIntN(
197
- 24,
198
- (log_sqrt10001 - 3402992956809132418596140100660247210n) >> 128n,
199
- );
200
- const tickHi = BigInt.asIntN(
201
- 24,
202
- (log_sqrt10001 + 291339464771989622907027621153398088495n) >> 128n,
203
- );
204
-
205
- return tickLow === tickHi
206
- ? tickLow
207
- : TickMath.getSqrtRatioAtTick(tickHi) <= sqrtPriceX96
208
- ? tickHi
209
- : tickLow;
210
- }
211
- }
@@ -1,5 +0,0 @@
1
- export class UnsafeMath {
2
- static divRoundingUp(x: bigint, y: bigint) {
3
- return (x + y - 1n) / y;
4
- }
5
- }