@paraswap/dex-lib 2.42.3 → 2.42.4-baseswap

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 (99) hide show
  1. package/build/dex/balancer-v1/balancer-v1.js +21 -10
  2. package/build/dex/balancer-v1/balancer-v1.js.map +1 -1
  3. package/build/dex/balancer-v1/config.d.ts +2 -1
  4. package/build/dex/balancer-v1/config.js +3 -2
  5. package/build/dex/balancer-v1/config.js.map +1 -1
  6. package/build/dex/baseswap-v3/baseswap-v3-factory.d.ts +26 -0
  7. package/build/dex/baseswap-v3/baseswap-v3-factory.js +44 -0
  8. package/build/dex/baseswap-v3/baseswap-v3-factory.js.map +1 -0
  9. package/build/dex/baseswap-v3/baseswap-v3-pool.d.ts +46 -0
  10. package/build/dex/baseswap-v3/baseswap-v3-pool.js +312 -0
  11. package/build/dex/baseswap-v3/baseswap-v3-pool.js.map +1 -0
  12. package/build/dex/baseswap-v3/baseswap-v3.d.ts +71 -0
  13. package/build/dex/baseswap-v3/baseswap-v3.js +752 -0
  14. package/build/dex/baseswap-v3/baseswap-v3.js.map +1 -0
  15. package/build/dex/baseswap-v3/config.d.ts +9 -0
  16. package/build/dex/baseswap-v3/config.js +48 -0
  17. package/build/dex/baseswap-v3/config.js.map +1 -0
  18. package/build/dex/baseswap-v3/constants.d.ts +28 -0
  19. package/build/dex/baseswap-v3/constants.js +35 -0
  20. package/build/dex/baseswap-v3/constants.js.map +1 -0
  21. package/build/dex/baseswap-v3/contract-math/BitMath.d.ts +4 -0
  22. package/build/dex/baseswap-v3/contract-math/BitMath.js +93 -0
  23. package/build/dex/baseswap-v3/contract-math/BitMath.js.map +1 -0
  24. package/build/dex/baseswap-v3/contract-math/FixedPoint128.d.ts +3 -0
  25. package/build/dex/baseswap-v3/contract-math/FixedPoint128.js +8 -0
  26. package/build/dex/baseswap-v3/contract-math/FixedPoint128.js.map +1 -0
  27. package/build/dex/baseswap-v3/contract-math/FixedPoint96.d.ts +4 -0
  28. package/build/dex/baseswap-v3/contract-math/FixedPoint96.js +9 -0
  29. package/build/dex/baseswap-v3/contract-math/FixedPoint96.js.map +1 -0
  30. package/build/dex/baseswap-v3/contract-math/FullMath.d.ts +4 -0
  31. package/build/dex/baseswap-v3/contract-math/FullMath.js +19 -0
  32. package/build/dex/baseswap-v3/contract-math/FullMath.js.map +1 -0
  33. package/build/dex/baseswap-v3/contract-math/LiquidityMath.d.ts +3 -0
  34. package/build/dex/baseswap-v3/contract-math/LiquidityMath.js +22 -0
  35. package/build/dex/baseswap-v3/contract-math/LiquidityMath.js.map +1 -0
  36. package/build/dex/baseswap-v3/contract-math/Oracle.d.ts +10 -0
  37. package/build/dex/baseswap-v3/contract-math/Oracle.js +133 -0
  38. package/build/dex/baseswap-v3/contract-math/Oracle.js.map +1 -0
  39. package/build/dex/baseswap-v3/contract-math/SqrtPriceMath.d.ts +10 -0
  40. package/build/dex/baseswap-v3/contract-math/SqrtPriceMath.js +91 -0
  41. package/build/dex/baseswap-v3/contract-math/SqrtPriceMath.js.map +1 -0
  42. package/build/dex/baseswap-v3/contract-math/SwapMath.d.ts +8 -0
  43. package/build/dex/baseswap-v3/contract-math/SwapMath.js +70 -0
  44. package/build/dex/baseswap-v3/contract-math/SwapMath.js.map +1 -0
  45. package/build/dex/baseswap-v3/contract-math/Tick.d.ts +7 -0
  46. package/build/dex/baseswap-v3/contract-math/Tick.js +45 -0
  47. package/build/dex/baseswap-v3/contract-math/Tick.js.map +1 -0
  48. package/build/dex/baseswap-v3/contract-math/TickBitMap.d.ts +7 -0
  49. package/build/dex/baseswap-v3/contract-math/TickBitMap.js +84 -0
  50. package/build/dex/baseswap-v3/contract-math/TickBitMap.js.map +1 -0
  51. package/build/dex/baseswap-v3/contract-math/TickMath.d.ts +8 -0
  52. package/build/dex/baseswap-v3/contract-math/TickMath.js +162 -0
  53. package/build/dex/baseswap-v3/contract-math/TickMath.js.map +1 -0
  54. package/build/dex/baseswap-v3/contract-math/UnsafeMath.d.ts +3 -0
  55. package/build/dex/baseswap-v3/contract-math/UnsafeMath.js +10 -0
  56. package/build/dex/baseswap-v3/contract-math/UnsafeMath.js.map +1 -0
  57. package/build/dex/baseswap-v3/contract-math/uniswap-v3-math.d.ts +37 -0
  58. package/build/dex/baseswap-v3/contract-math/uniswap-v3-math.js +393 -0
  59. package/build/dex/baseswap-v3/contract-math/uniswap-v3-math.js.map +1 -0
  60. package/build/dex/baseswap-v3/contract-math/utils.d.ts +7 -0
  61. package/build/dex/baseswap-v3/contract-math/utils.js +42 -0
  62. package/build/dex/baseswap-v3/contract-math/utils.js.map +1 -0
  63. package/build/dex/baseswap-v3/types.d.ts +164 -0
  64. package/build/dex/baseswap-v3/types.js +9 -0
  65. package/build/dex/baseswap-v3/types.js.map +1 -0
  66. package/build/dex/baseswap-v3/utils.d.ts +6 -0
  67. package/build/dex/baseswap-v3/utils.js +65 -0
  68. package/build/dex/baseswap-v3/utils.js.map +1 -0
  69. package/build/dex/index.js +2 -0
  70. package/build/dex/index.js.map +1 -1
  71. package/package.json +1 -1
  72. package/src/dex/balancer-v1/balancer-v1.ts +32 -13
  73. package/src/dex/balancer-v1/config.ts +2 -1
  74. package/src/dex/baseswap-v3/baseswap-v3-e2e.test.ts +129 -0
  75. package/src/dex/baseswap-v3/baseswap-v3-events.test.ts +141 -0
  76. package/src/dex/baseswap-v3/baseswap-v3-factory.ts +73 -0
  77. package/src/dex/baseswap-v3/baseswap-v3-integration.test.ts +472 -0
  78. package/src/dex/baseswap-v3/baseswap-v3-pool.ts +519 -0
  79. package/src/dex/baseswap-v3/baseswap-v3.ts +1243 -0
  80. package/src/dex/baseswap-v3/config.ts +53 -0
  81. package/src/dex/baseswap-v3/constants.ts +41 -0
  82. package/src/dex/baseswap-v3/contract-math/BitMath.ts +90 -0
  83. package/src/dex/baseswap-v3/contract-math/FixedPoint128.ts +3 -0
  84. package/src/dex/baseswap-v3/contract-math/FixedPoint96.ts +4 -0
  85. package/src/dex/baseswap-v3/contract-math/FullMath.ts +30 -0
  86. package/src/dex/baseswap-v3/contract-math/LiquidityMath.ts +17 -0
  87. package/src/dex/baseswap-v3/contract-math/Oracle.ts +226 -0
  88. package/src/dex/baseswap-v3/contract-math/SqrtPriceMath.ts +226 -0
  89. package/src/dex/baseswap-v3/contract-math/SwapMath.ts +139 -0
  90. package/src/dex/baseswap-v3/contract-math/Tick.ts +82 -0
  91. package/src/dex/baseswap-v3/contract-math/TickBitMap.ts +123 -0
  92. package/src/dex/baseswap-v3/contract-math/TickMath.ts +211 -0
  93. package/src/dex/baseswap-v3/contract-math/UnsafeMath.ts +5 -0
  94. package/src/dex/baseswap-v3/contract-math/uniswap-v3-math.ts +666 -0
  95. package/src/dex/baseswap-v3/contract-math/utils.ts +55 -0
  96. package/src/dex/baseswap-v3/types.ts +191 -0
  97. package/src/dex/baseswap-v3/utils.ts +83 -0
  98. package/src/dex/index.ts +2 -0
  99. package/tests/constants-e2e.ts +1 -1
@@ -0,0 +1,139 @@
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
+ }
@@ -0,0 +1,82 @@
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: Pick<PoolState, 'ticks'>,
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: Pick<PoolState, 'ticks'>, 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
+ }
@@ -0,0 +1,123 @@
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(
46
+ state: Pick<PoolState, 'startTickBitmap' | 'tickBitmap'>,
47
+ tick: bigint,
48
+ tickSpacing: bigint,
49
+ ) {
50
+ _require(
51
+ tick % tickSpacing === 0n,
52
+ '',
53
+ { tick, tickSpacing },
54
+ 'tick % tickSpacing == 0n,',
55
+ );
56
+ const [wordPos, bitPos] = TickBitMap.position(tick / tickSpacing);
57
+ const mask = 1n << bitPos;
58
+
59
+ // flipTick is used only in _updatePosition which is always state changing event
60
+ // Therefore it is never used in price query
61
+ isWordPosOut(wordPos, state.startTickBitmap, false);
62
+
63
+ const stringWordPos = wordPos.toString();
64
+ if (state.tickBitmap[stringWordPos] === undefined) {
65
+ state.tickBitmap[stringWordPos] = 0n;
66
+ }
67
+
68
+ state.tickBitmap[stringWordPos] ^= mask;
69
+ }
70
+
71
+ static nextInitializedTickWithinOneWord(
72
+ state: DeepReadonly<Pick<PoolState, 'startTickBitmap' | 'tickBitmap'>>,
73
+ tick: bigint,
74
+ tickSpacing: bigint,
75
+ lte: boolean,
76
+ isPriceQuery: boolean,
77
+ ): [bigint, boolean] {
78
+ let compressed = tick / tickSpacing;
79
+ if (tick < 0n && tick % tickSpacing != 0n) compressed--;
80
+
81
+ let next = 0n;
82
+ let initialized = false;
83
+
84
+ if (lte) {
85
+ const [wordPos, bitPos] = TickBitMap.position(compressed);
86
+ const mask = (1n << bitPos) - 1n + (1n << bitPos);
87
+
88
+ isWordPosOut(wordPos, state.startTickBitmap, isPriceQuery);
89
+ let tickBitmapValue = state.tickBitmap[wordPos.toString()];
90
+ tickBitmapValue = tickBitmapValue === undefined ? 0n : tickBitmapValue;
91
+
92
+ const masked = tickBitmapValue & mask;
93
+
94
+ initialized = masked != 0n;
95
+ next = initialized
96
+ ? (compressed -
97
+ BigInt.asIntN(24, bitPos - BitMath.mostSignificantBit(masked))) *
98
+ tickSpacing
99
+ : (compressed - BigInt.asIntN(24, bitPos)) * tickSpacing;
100
+ } else {
101
+ // start from the word of the next tick, since the current tick state doesn't matter
102
+ const [wordPos, bitPos] = TickBitMap.position(compressed + 1n);
103
+ const mask = ~((1n << bitPos) - 1n);
104
+
105
+ isWordPosOut(wordPos, state.startTickBitmap, isPriceQuery);
106
+ let tickBitmapValue = state.tickBitmap[wordPos.toString()];
107
+ tickBitmapValue = tickBitmapValue === undefined ? 0n : tickBitmapValue;
108
+
109
+ const masked = tickBitmapValue & mask;
110
+
111
+ initialized = masked != 0n;
112
+ next = initialized
113
+ ? (compressed +
114
+ 1n +
115
+ BigInt.asIntN(24, BitMath.leastSignificantBit(masked) - bitPos)) *
116
+ tickSpacing
117
+ : (compressed + 1n + BigInt.asIntN(24, BI_MAX_UINT8 - bitPos)) *
118
+ tickSpacing;
119
+ }
120
+
121
+ return [next, initialized];
122
+ }
123
+ }
@@ -0,0 +1,211 @@
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
+ }
@@ -0,0 +1,5 @@
1
+ export class UnsafeMath {
2
+ static divRoundingUp(x: bigint, y: bigint) {
3
+ return (x + y - 1n) / y;
4
+ }
5
+ }