@paraswap/dex-lib 2.28.3 → 2.28.4-algebra-events-fix.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/build/dex/algebra/algebra-pool-v1_1.js +1 -0
  2. package/build/dex/algebra/algebra-pool-v1_1.js.map +1 -1
  3. package/build/dex/algebra/algebra-pool-v1_9.js +1 -0
  4. package/build/dex/algebra/algebra-pool-v1_9.js.map +1 -1
  5. package/build/dex/algebra/algebra.js.map +1 -1
  6. package/build/dex/algebra/lib/AlgebraMath.d.ts +1 -1
  7. package/build/dex/algebra/lib/AlgebraMath.js +12 -12
  8. package/build/dex/algebra/lib/AlgebraMath.js.map +1 -1
  9. package/build/dex/algebra/lib/TickManager.d.ts +4 -0
  10. package/build/dex/algebra/lib/TickManager.js +60 -0
  11. package/build/dex/algebra/lib/TickManager.js.map +1 -0
  12. package/build/dex/algebra/lib/TickTable.d.ts +10 -0
  13. package/build/dex/algebra/lib/TickTable.js +145 -0
  14. package/build/dex/algebra/lib/TickTable.js.map +1 -0
  15. package/build/dex/algebra/lib/yul-helper.d.ts +9 -0
  16. package/build/dex/algebra/lib/yul-helper.js +28 -0
  17. package/build/dex/algebra/lib/yul-helper.js.map +1 -0
  18. package/build/dex/algebra/types.d.ts +3 -0
  19. package/build/dex/test-dex/config.d.ts +4 -0
  20. package/build/dex/test-dex/config.js +15 -0
  21. package/build/dex/test-dex/config.js.map +1 -0
  22. package/build/dex/test-dex/test-dex-pool.d.ts +39 -0
  23. package/build/dex/test-dex/test-dex-pool.js +63 -0
  24. package/build/dex/test-dex/test-dex-pool.js.map +1 -0
  25. package/build/dex/test-dex/test-dex.d.ts +37 -0
  26. package/build/dex/test-dex/test-dex.js +126 -0
  27. package/build/dex/test-dex/test-dex.js.map +1 -0
  28. package/build/dex/test-dex/types.d.ts +6 -0
  29. package/build/dex/{pancakeswap-v3 → test-dex}/types.js.map +1 -1
  30. package/build/dex/uniswap-v3/contract-math/TickBitMap.d.ts +0 -1
  31. package/build/dex/uniswap-v3/contract-math/TickBitMap.js +0 -3
  32. package/build/dex/uniswap-v3/contract-math/TickBitMap.js.map +1 -1
  33. package/build/dex/uniswap-v3/contract-math/utils.js +10 -8
  34. package/build/dex/uniswap-v3/contract-math/utils.js.map +1 -1
  35. package/build/dex/woo-fi-v2/woo-fi-math.d.ts +16 -0
  36. package/build/dex/woo-fi-v2/woo-fi-math.js +137 -0
  37. package/build/dex/woo-fi-v2/woo-fi-math.js.map +1 -0
  38. package/package.json +1 -1
  39. package/src/dex/algebra/algebra-events.test.ts +73 -7
  40. package/src/dex/algebra/algebra-pool-v1_1.ts +1 -0
  41. package/src/dex/algebra/algebra-pool-v1_9.ts +1 -0
  42. package/src/dex/algebra/algebra.ts +1 -2
  43. package/src/dex/algebra/lib/AlgebraMath.ts +21 -15
  44. package/src/dex/algebra/lib/TickManager.ts +91 -0
  45. package/src/dex/algebra/lib/TickTable.ts +215 -0
  46. package/src/dex/algebra/lib/yul-helper.ts +29 -0
  47. package/src/dex/algebra/types.ts +4 -0
  48. package/src/dex/uniswap-v3/contract-math/TickBitMap.ts +0 -9
  49. package/src/dex/uniswap-v3/contract-math/utils.ts +12 -10
  50. package/tests/constants-e2e.ts +9 -1
  51. package/.idea/aws.xml +0 -17
  52. package/.idea/codeStyles/Project.xml +0 -19
  53. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  54. package/.idea/misc.xml +0 -6
  55. package/.idea/modules.xml +0 -8
  56. package/.idea/paraswap-dex-lib.iml +0 -9
  57. package/.idea/vcs.xml +0 -6
  58. package/build/abi/algebra/AlgebraPool.abi.json +0 -727
  59. package/build/abi/curve-v1/StableSwapFRXETH.json +0 -889
  60. package/build/abi/curve-v1/StableSwapWBETH.json +0 -1223
  61. package/build/abi/wBETH.json +0 -1527
  62. package/build/dex/algebra/algebra-pool.d.ts +0 -46
  63. package/build/dex/algebra/algebra-pool.js +0 -336
  64. package/build/dex/algebra/algebra-pool.js.map +0 -1
  65. package/build/dex/curve-v1/pools/frxETHpool.d.ts +0 -7
  66. package/build/dex/curve-v1/pools/frxETHpool.js +0 -27
  67. package/build/dex/curve-v1/pools/frxETHpool.js.map +0 -1
  68. package/build/dex/curve-v1/pools/wbETHpool.d.ts +0 -29
  69. package/build/dex/curve-v1/pools/wbETHpool.js +0 -408
  70. package/build/dex/curve-v1/pools/wbETHpool.js.map +0 -1
  71. package/build/dex/pancakeswap-v3/types.d.ts +0 -14
  72. package/build/dex/solidly/forks-override/ramses.d.ts +0 -8
  73. package/build/dex/solidly/forks-override/ramses.js +0 -15
  74. package/build/dex/solidly/forks-override/ramses.js.map +0 -1
  75. /package/build/dex/{pancakeswap-v3 → test-dex}/types.js +0 -0
@@ -0,0 +1,215 @@
1
+ import { IAlgebraPoolState } from '../types';
2
+ import { _require } from '../../../utils';
3
+ import { DeepReadonly } from 'ts-essentials';
4
+ import {
5
+ OUT_OF_RANGE_ERROR_POSTFIX,
6
+ TICK_BITMAP_BUFFER,
7
+ TICK_BITMAP_TO_USE,
8
+ } from '../../uniswap-v3/constants';
9
+ import { TickMath } from '../../uniswap-v3/contract-math/TickMath';
10
+ import { Yul } from './yul-helper';
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 TickTable {
41
+ static position(tick: bigint) {
42
+ return [
43
+ // wordPos
44
+ BigInt.asIntN(16, tick >> 8n),
45
+ // bitPos
46
+ BigInt.asUintN(8, tick & BigInt(0xff)),
47
+ ];
48
+ }
49
+
50
+ static toggleTick(
51
+ state: Pick<IAlgebraPoolState, 'startTickBitmap' | 'tickBitmap'>,
52
+ tick: bigint,
53
+ tickSpacing?: bigint,
54
+ ) {
55
+ if (tickSpacing !== undefined) {
56
+ tick /= tickSpacing;
57
+ }
58
+ const [rowNumber, bitNumber] = TickTable.position(tick);
59
+ const mask = 1n << bitNumber;
60
+
61
+ // toggleTick is used only in _updatePosition which is always state changing event
62
+ // Therefore it is never used in price query
63
+ isWordPosOut(rowNumber, state.startTickBitmap, false);
64
+
65
+ const stringWordPos = rowNumber.toString();
66
+ if (state.tickBitmap[stringWordPos] === undefined) {
67
+ state.tickBitmap[stringWordPos] = 0n;
68
+ }
69
+
70
+ state.tickBitmap[stringWordPos] ^= mask;
71
+ if (state.tickBitmap[stringWordPos] === 0n) {
72
+ delete state.tickBitmap[stringWordPos];
73
+ }
74
+ }
75
+
76
+ static nextInitializedTickWithinOneWord(
77
+ state: DeepReadonly<
78
+ Pick<IAlgebraPoolState, 'startTickBitmap' | 'tickBitmap'>
79
+ >,
80
+ tick: bigint,
81
+ lte: boolean,
82
+ isPriceQuery: boolean,
83
+ tickSpacing?: bigint,
84
+ ): [bigint, boolean] {
85
+ if (tickSpacing !== undefined) {
86
+ tick = Yul.sub(
87
+ Yul.sdiv(tick, tickSpacing),
88
+ Yul.and(
89
+ Yul.slt(tick, 0n),
90
+ Yul.not(Yul.iszero(Yul.smod(tick, tickSpacing))),
91
+ ),
92
+ );
93
+ }
94
+ const [rowNumber, bitNumber] = TickTable.position(tick);
95
+ isWordPosOut(rowNumber, state.startTickBitmap, isPriceQuery);
96
+ let tickBitmapValue = state.tickBitmap[rowNumber.toString()];
97
+ tickBitmapValue = tickBitmapValue === undefined ? 0n : tickBitmapValue;
98
+ if (lte) {
99
+ const _row = tickBitmapValue << (255n - bitNumber);
100
+ if (_row != 0n) {
101
+ tick -= BigInt.asIntN(24, 255n - TickTable.getMostSignificantBit(_row));
102
+ return [TickTable.boundTick(tick), true];
103
+ } else {
104
+ tick -= BigInt.asIntN(24, bitNumber);
105
+ return [TickTable.boundTick(tick), false];
106
+ }
107
+ } else {
108
+ tick += 1n;
109
+ const _row = tickBitmapValue >> bitNumber;
110
+ if (_row !== 0n) {
111
+ tick += BigInt.asIntN(
112
+ 24,
113
+ TickTable.getSingleSignificantBit(-_row & _row),
114
+ );
115
+ return [TickTable.boundTick(tick), true];
116
+ } else {
117
+ tick += BigInt.asIntN(24, 255n - bitNumber);
118
+ return [TickTable.boundTick(tick), false];
119
+ }
120
+ }
121
+ }
122
+
123
+ static getSingleSignificantBit(word: bigint): bigint {
124
+ let singleBitPos = 0n;
125
+ singleBitPos = Yul.iszero(
126
+ word &&
127
+ BigInt(
128
+ '0x5555555555555555555555555555555555555555555555555555555555555555',
129
+ ),
130
+ );
131
+ singleBitPos =
132
+ singleBitPos ||
133
+ Yul.iszero(
134
+ word &&
135
+ BigInt(
136
+ '0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff',
137
+ ),
138
+ ) << 7n;
139
+ singleBitPos =
140
+ singleBitPos ||
141
+ Yul.iszero(
142
+ word &&
143
+ BigInt(
144
+ '0x0000000000000000ffffffffffffffff0000000000000000ffffffffffffffff',
145
+ ),
146
+ ) << 6n;
147
+ singleBitPos =
148
+ singleBitPos ||
149
+ Yul.iszero(
150
+ word &&
151
+ BigInt(
152
+ '0x00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff',
153
+ ),
154
+ ) << 5n;
155
+ singleBitPos =
156
+ singleBitPos ||
157
+ Yul.iszero(
158
+ word &&
159
+ BigInt(
160
+ '0x0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff',
161
+ ),
162
+ ) << 4n;
163
+ singleBitPos =
164
+ singleBitPos ||
165
+ Yul.iszero(
166
+ word &&
167
+ BigInt(
168
+ '0x00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff',
169
+ ),
170
+ ) << 3n;
171
+ singleBitPos =
172
+ singleBitPos ||
173
+ Yul.iszero(
174
+ word &&
175
+ BigInt(
176
+ '0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f',
177
+ ),
178
+ ) << 2n;
179
+ singleBitPos =
180
+ singleBitPos ||
181
+ Yul.iszero(
182
+ word &&
183
+ BigInt(
184
+ '0x3333333333333333333333333333333333333333333333333333333333333333',
185
+ ),
186
+ ) << 1n;
187
+
188
+ return BigInt.asUintN(8, singleBitPos);
189
+ }
190
+
191
+ static getMostSignificantBit(word: bigint): bigint {
192
+ word = word || word >> 1n;
193
+ word = word || word >> 2n;
194
+ word = word || word >> 4n;
195
+ word = word || word >> 8n;
196
+ word = word || word >> 16n;
197
+ word = word || word >> 32n;
198
+ word = word || word >> 64n;
199
+ word = word - (word >> 1n);
200
+ return TickTable.getSingleSignificantBit(word);
201
+ }
202
+
203
+ static boundTick(tick: bigint, tickSpacing?: bigint): bigint {
204
+ if (tickSpacing !== undefined) {
205
+ tick *= tickSpacing;
206
+ }
207
+ let boundedTick = tick;
208
+ if (boundedTick < TickMath.MIN_TICK) {
209
+ boundedTick = TickMath.MIN_TICK;
210
+ } else if (boundedTick > TickMath.MAX_TICK) {
211
+ boundedTick = TickMath.MAX_TICK;
212
+ }
213
+ return boundedTick;
214
+ }
215
+ }
@@ -0,0 +1,29 @@
1
+ export class Yul {
2
+ static sub(x: bigint, y: bigint): bigint {
3
+ return x - y;
4
+ }
5
+
6
+ static sdiv(x: bigint, y: bigint): bigint {
7
+ return y === 0n ? 0n : x / y;
8
+ }
9
+
10
+ static and(x: bigint, y: bigint): bigint {
11
+ return x & y;
12
+ }
13
+
14
+ static slt(x: bigint, y: bigint): bigint {
15
+ return x < y ? 1n : 0n;
16
+ }
17
+
18
+ static not(x: bigint): bigint {
19
+ return ~x;
20
+ }
21
+
22
+ static iszero(x: bigint): bigint {
23
+ return x === 0n ? 1n : 0n;
24
+ }
25
+
26
+ static smod(x: bigint, y: bigint): bigint {
27
+ return y === 0n ? 0n : x % y;
28
+ }
29
+ }
@@ -23,6 +23,7 @@ export type PoolStateV1_1 = {
23
23
  startTickBitmap: bigint;
24
24
  balance0: bigint;
25
25
  balance1: bigint;
26
+ areTicksCompressed: boolean;
26
27
  };
27
28
 
28
29
  type GlobalState_v1_9 = {
@@ -47,6 +48,7 @@ export type PoolState_v1_9 = {
47
48
  startTickBitmap: bigint;
48
49
  balance0: bigint;
49
50
  balance1: bigint;
51
+ areTicksCompressed: boolean;
50
52
  };
51
53
 
52
54
  export type AlgebraData = {
@@ -71,6 +73,8 @@ export type DexParams = {
71
73
  version: 'v1.1' | 'v1.9';
72
74
  };
73
75
 
76
+ export type IAlgebraPoolState = PoolStateV1_1 | PoolState_v1_9;
77
+
74
78
  export type TickBitMapMappingsWithBigNumber = {
75
79
  index: number;
76
80
  value: BigNumber;
@@ -120,13 +120,4 @@ export class TickBitMap {
120
120
 
121
121
  return [next, initialized];
122
122
  }
123
-
124
- static _putZeroIfUndefined(
125
- state: PoolState,
126
- tickBitmapValue: bigint | undefined,
127
- wordPos: bigint,
128
- isPriceQuery: boolean = false,
129
- ): bigint {
130
- return tickBitmapValue === undefined ? 0n : tickBitmapValue;
131
- }
132
123
  }
@@ -38,16 +38,18 @@ export function _reduceTicks(
38
38
  ) {
39
39
  return ticksToReduce.reduce<Record<string, TickInfo>>((acc, curr) => {
40
40
  const { index, value } = curr;
41
- acc[index] = {
42
- liquidityGross: bigIntify(value.liquidityGross),
43
- liquidityNet: bigIntify(value.liquidityNet),
44
- tickCumulativeOutside: bigIntify(value.tickCumulativeOutside),
45
- secondsPerLiquidityOutsideX128: bigIntify(
46
- value.secondsPerLiquidityOutsideX128,
47
- ),
48
- secondsOutside: bigIntify(value.secondsOutside),
49
- initialized: value.initialized,
50
- };
41
+ if (value.initialized) {
42
+ acc[index] = {
43
+ liquidityGross: bigIntify(value.liquidityGross),
44
+ liquidityNet: bigIntify(value.liquidityNet),
45
+ tickCumulativeOutside: bigIntify(value.tickCumulativeOutside),
46
+ secondsPerLiquidityOutsideX128: bigIntify(
47
+ value.secondsPerLiquidityOutsideX128,
48
+ ),
49
+ secondsOutside: bigIntify(value.secondsOutside),
50
+ initialized: value.initialized,
51
+ };
52
+ }
51
53
  return acc;
52
54
  }, ticks);
53
55
  }
@@ -705,7 +705,7 @@ export const Tokens: {
705
705
  HATCHY: {
706
706
  address: '0x502580fc390606b47fc3b741d6d49909383c28a9',
707
707
  decimals: 18,
708
- }
708
+ },
709
709
  },
710
710
  [Network.ARBITRUM]: {
711
711
  DAI: {
@@ -781,6 +781,14 @@ export const Tokens: {
781
781
  address: '0x2f2a2543b76a4166549f7aab2e75bef0aefc5b0f',
782
782
  decimals: 8,
783
783
  },
784
+ USDCe: {
785
+ address: '0xff970a61a04b1ca14834a43f5de4533ebddb5cc8',
786
+ decimals: 6,
787
+ },
788
+ LEX: {
789
+ address: '0x6bB7A17AcC227fd1F6781D1EEDEAE01B42047eE0',
790
+ decimals: 18,
791
+ },
784
792
  },
785
793
  [Network.OPTIMISM]: {
786
794
  DAI: {
package/.idea/aws.xml DELETED
@@ -1,17 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="accountSettings">
4
- <option name="activeProfile" value="profile:default" />
5
- <option name="activeRegion" value="us-east-1" />
6
- <option name="recentlyUsedProfiles">
7
- <list>
8
- <option value="profile:default" />
9
- </list>
10
- </option>
11
- <option name="recentlyUsedRegions">
12
- <list>
13
- <option value="us-east-1" />
14
- </list>
15
- </option>
16
- </component>
17
- </project>
@@ -1,19 +0,0 @@
1
- <component name="ProjectCodeStyleConfiguration">
2
- <code_scheme name="Project" version="173">
3
- <ScalaCodeStyleSettings>
4
- <option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
5
- </ScalaCodeStyleSettings>
6
- <TypeScriptCodeStyleSettings version="0">
7
- <option name="SPACE_WITHIN_ARRAY_INITIALIZER_BRACKETS" value="true" />
8
- <option name="USE_DOUBLE_QUOTES" value="false" />
9
- <option name="SPACES_WITHIN_IMPORTS" value="true" />
10
- </TypeScriptCodeStyleSettings>
11
- <codeStyleSettings language="TypeScript">
12
- <indentOptions>
13
- <option name="INDENT_SIZE" value="2" />
14
- <option name="CONTINUATION_INDENT_SIZE" value="2" />
15
- <option name="TAB_SIZE" value="2" />
16
- </indentOptions>
17
- </codeStyleSettings>
18
- </code_scheme>
19
- </component>
@@ -1,5 +0,0 @@
1
- <component name="ProjectCodeStyleConfiguration">
2
- <state>
3
- <option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
4
- </state>
5
- </component>
package/.idea/misc.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectRootManager">
4
- <output url="file://$PROJECT_DIR$/out" />
5
- </component>
6
- </project>
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/paraswap-dex-lib.iml" filepath="$PROJECT_DIR$/.idea/paraswap-dex-lib.iml" />
6
- </modules>
7
- </component>
8
- </project>
@@ -1,9 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="JAVA_MODULE" version="4">
3
- <component name="NewModuleRootManager" inherit-compiler-output="true">
4
- <exclude-output />
5
- <content url="file://$MODULE_DIR$" />
6
- <orderEntry type="inheritedJdk" />
7
- <orderEntry type="sourceFolder" forTests="false" />
8
- </component>
9
- </module>
package/.idea/vcs.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="" vcs="Git" />
5
- </component>
6
- </project>