@paraswap/dex-lib 2.31.1-limit-pools.0 → 2.31.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.
- package/build/dex/aave-v3/tokens/arbitrum.json +20 -20
- package/build/dex/aave-v3/tokens/avalanche.json +44 -20
- package/build/dex/aave-v3/tokens/fantom.json +30 -30
- package/build/dex/aave-v3/tokens/optimism.json +18 -18
- package/build/dex/aave-v3/tokens/polygon.json +60 -42
- package/build/dex/balancer-v2/balancer-v2.js +21 -3
- package/build/dex/balancer-v2/balancer-v2.js.map +1 -1
- package/build/dex/generic-rfq/generic-rfq.js +0 -3
- package/build/dex/generic-rfq/generic-rfq.js.map +1 -1
- package/build/dex/{test-aave-v3 → test-dex}/config.d.ts +1 -1
- package/build/dex/{test-aave-v3 → test-dex}/config.js +3 -3
- package/build/dex/test-dex/config.js.map +1 -0
- package/build/dex/{test-aave-v3/test-aave-v3-pool.d.ts → test-dex/test-dex-pool.d.ts} +3 -3
- package/build/dex/{test-aave-v3/test-aave-v3-pool.js → test-dex/test-dex-pool.js} +7 -7
- package/build/dex/test-dex/test-dex-pool.js.map +1 -0
- package/build/dex/{test-aave-v3/test-aave-v3.d.ts → test-dex/test-dex.d.ts} +8 -8
- package/build/dex/{test-aave-v3/test-aave-v3.js → test-dex/test-dex.js} +7 -7
- package/build/dex/test-dex/test-dex.js.map +1 -0
- package/build/dex/{test-aave-v3 → test-dex}/types.d.ts +1 -1
- package/build/dex/{test-aave-v3 → test-dex}/types.js.map +1 -1
- package/build/dex/woo-fi-v2/woo-fi-math.d.ts +16 -0
- package/build/dex/woo-fi-v2/woo-fi-math.js +137 -0
- package/build/dex/woo-fi-v2/woo-fi-math.js.map +1 -0
- package/package.json +1 -1
- package/src/dex/balancer-v2/balancer-v2-e2e.test.ts +14 -4
- package/src/dex/balancer-v2/balancer-v2.ts +22 -3
- package/src/dex/generic-rfq/generic-rfq.ts +0 -4
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/.vscode/launch.json +0 -21
- package/build/abi/AugustusRFQRouter.abi.json +0 -1248
- package/build/abi/algebra/AlgebraPool.abi.json +0 -727
- package/build/dex/aave-v3/tokens/mainnet.json +0 -44
- package/build/dex/algebra/algebra-pool.d.ts +0 -41
- package/build/dex/algebra/algebra-pool.js +0 -277
- package/build/dex/algebra/algebra-pool.js.map +0 -1
- package/build/dex/algebra/lib/AlgebraXUniV3.d.ts +0 -5
- package/build/dex/algebra/lib/AlgebraXUniV3.js +0 -45
- package/build/dex/algebra/lib/AlgebraXUniV3.js.map +0 -1
- package/build/dex/algebra/lib/DataStorage.d.ts +0 -24
- package/build/dex/algebra/lib/DataStorage.js +0 -397
- package/build/dex/algebra/lib/DataStorage.js.map +0 -1
- package/build/dex/algebra/lib/DataStorageOperator.d.ts +0 -9
- package/build/dex/algebra/lib/DataStorageOperator.js +0 -66
- package/build/dex/algebra/lib/DataStorageOperator.js.map +0 -1
- package/build/dex/algebra/lib/Sqrt.d.ts +0 -3
- package/build/dex/algebra/lib/Sqrt.js +0 -60
- package/build/dex/algebra/lib/Sqrt.js.map +0 -1
- package/build/dex/balancer-v2/VirtualBoostedPool.d.ts +0 -79
- package/build/dex/balancer-v2/VirtualBoostedPool.js +0 -382
- package/build/dex/balancer-v2/VirtualBoostedPool.js.map +0 -1
- package/build/dex/generic-rfq/constants.d.ts +0 -1
- package/build/dex/generic-rfq/constants.js +0 -5
- package/build/dex/generic-rfq/constants.js.map +0 -1
- package/build/dex/test-aave-v3/config.js.map +0 -1
- package/build/dex/test-aave-v3/test-aave-v3-pool.js.map +0 -1
- package/build/dex/test-aave-v3/test-aave-v3.js.map +0 -1
- /package/build/dex/{test-aave-v3 → test-dex}/types.js +0 -0
|
@@ -1,397 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DataStorage = exports.TIMEPOINT_ZERO = void 0;
|
|
4
|
-
const ts_essentials_1 = require("ts-essentials");
|
|
5
|
-
const utils_1 = require("../../../utils");
|
|
6
|
-
exports.TIMEPOINT_ZERO = {
|
|
7
|
-
averageTick: 0n,
|
|
8
|
-
blockTimestamp: 0n,
|
|
9
|
-
initialized: false,
|
|
10
|
-
secondsPerLiquidityCumulative: 0n,
|
|
11
|
-
tickCumulative: 0n,
|
|
12
|
-
volatilityCumulative: 0n,
|
|
13
|
-
volumePerLiquidityCumulative: 0n,
|
|
14
|
-
};
|
|
15
|
-
const WINDOW = 24n * 60n;
|
|
16
|
-
const UINT16_MODULO = 65536n;
|
|
17
|
-
class DataStorage {
|
|
18
|
-
/// @notice Calculates volatility between two sequential timepoints with resampling to 1 sec frequency
|
|
19
|
-
/// @param dt Timedelta between timepoints, must be within uint32 range
|
|
20
|
-
/// @param tick0 The tick at the left timepoint, must be within int24 range
|
|
21
|
-
/// @param tick1 The tick at the right timepoint, must be within int24 range
|
|
22
|
-
/// @param avgTick0 The average tick at the left timepoint, must be within int24 range
|
|
23
|
-
/// @param avgTick1 The average tick at the right timepoint, must be within int24 range
|
|
24
|
-
/// @return volatility The volatility between two sequential timepoints
|
|
25
|
-
/// If the requirements for the parameters are met, it always fits 88 bits
|
|
26
|
-
static _volatilityOnRange(dt, tick0, tick1, avgTick0, avgTick1) {
|
|
27
|
-
// On the time interval from the previous timepoint to the current
|
|
28
|
-
// we can represent tick and average tick change as two straight lines:
|
|
29
|
-
// tick = k*t + b, where k and b are some constants
|
|
30
|
-
// avgTick = p*t + q, where p and q are some constants
|
|
31
|
-
// we want to get sum of (tick(t) - avgTick(t))^2 for every t in the interval (0; dt]
|
|
32
|
-
// so: (tick(t) - avgTick(t))^2 = ((k*t + b) - (p*t + q))^2 = (k-p)^2 * t^2 + 2(k-p)(b-q)t + (b-q)^2
|
|
33
|
-
// since everything except t is a constant, we need to use progressions for t and t^2:
|
|
34
|
-
// sum(t) for t from 1 to dt = dt*(dt + 1)/2 = sumOfSequence
|
|
35
|
-
// sum(t^2) for t from 1 to dt = dt*(dt+1)*(2dt + 1)/6 = sumOfSquares
|
|
36
|
-
// so result will be: (k-p)^2 * sumOfSquares + 2(k-p)(b-q)*sumOfSequence + dt*(b-q)^2
|
|
37
|
-
const K = tick1 - tick0 - (avgTick1 - avgTick0); // (k - p)*dt
|
|
38
|
-
const B = (tick0 - avgTick0) * dt; // (b - q)*dt
|
|
39
|
-
const sumOfSquares = dt * (dt + 1n) * (2n * dt + 1n); // sumOfSquares * 6
|
|
40
|
-
const sumOfSequence = dt * (dt + 1n); // sumOfSequence * 2
|
|
41
|
-
const volatility = (0, utils_1.uint256)((K ** 2n * sumOfSquares +
|
|
42
|
-
6n * B * K * sumOfSequence +
|
|
43
|
-
6n * dt * B ** 2n) /
|
|
44
|
-
(6n * dt ** 2n));
|
|
45
|
-
return volatility;
|
|
46
|
-
}
|
|
47
|
-
/// @notice Transforms a previous timepoint into a new timepoint, given the passage of time and the current tick and liquidity values
|
|
48
|
-
/// @dev blockTimestamp _must_ be chronologically equal to or greater than last.blockTimestamp, safe for 0 or 1 overflows
|
|
49
|
-
/// @param last The specified timepoint to be used in creation of new timepoint
|
|
50
|
-
/// @param blockTimestamp The timestamp of the new timepoint
|
|
51
|
-
/// @param tick The active tick at the time of the new timepoint
|
|
52
|
-
/// @param prevTick The active tick at the time of the last timepoint
|
|
53
|
-
/// @param liquidity The total in-range liquidity at the time of the new timepoint
|
|
54
|
-
/// @param averageTick The average tick at the time of the new timepoint
|
|
55
|
-
/// @param volumePerLiquidity The gmean(volumes)/liquidity at the time of the new timepoint
|
|
56
|
-
/// @return Timepoint The newly populated timepoint
|
|
57
|
-
static createNewTimepoint(last, blockTimestamp, tick, prevTick, liquidity, averageTick, volumePerLiquidity) {
|
|
58
|
-
const delta = blockTimestamp - last.blockTimestamp;
|
|
59
|
-
last.initialized = true;
|
|
60
|
-
last.blockTimestamp = blockTimestamp;
|
|
61
|
-
last.tickCumulative += (0, utils_1.int56)(tick) * delta;
|
|
62
|
-
last.secondsPerLiquidityCumulative +=
|
|
63
|
-
((0, utils_1.uint160)(delta) << 128n) / (liquidity > 0n ? liquidity : 1n); // just timedelta if liquidity == 0
|
|
64
|
-
last.volatilityCumulative += (0, utils_1.uint88)(this._volatilityOnRange(delta, prevTick, tick, last.averageTick, averageTick)); // always fits 88 bits
|
|
65
|
-
last.averageTick = averageTick;
|
|
66
|
-
last.volumePerLiquidityCumulative += volumePerLiquidity;
|
|
67
|
-
return last;
|
|
68
|
-
}
|
|
69
|
-
/// @notice comparator for 32-bit timestamps
|
|
70
|
-
/// @dev safe for 0 or 1 overflows, a and b _must_ be chronologically before or equal to currentTime
|
|
71
|
-
/// @param a A comparison timestamp from which to determine the relative position of `currentTime`
|
|
72
|
-
/// @param b From which to determine the relative position of `currentTime`
|
|
73
|
-
/// @param currentTime A timestamp truncated to 32 bits
|
|
74
|
-
/// @return res Whether `a` is chronologically <= `b`
|
|
75
|
-
static lteConsideringOverflow(a, b, currentTime) {
|
|
76
|
-
let res = a > currentTime;
|
|
77
|
-
if (res == b > currentTime)
|
|
78
|
-
res = a <= b; // if both are on the same side
|
|
79
|
-
return res;
|
|
80
|
-
}
|
|
81
|
-
/// @dev guaranteed that the result is within the bounds of int24
|
|
82
|
-
/// returns int256 for fuzzy tests
|
|
83
|
-
static _getAverageTick(self, time, tick, index, oldestIndex, lastTimestamp, lastTickCumulative) {
|
|
84
|
-
const oldestTimestamp = (self[Number(oldestIndex)] || exports.TIMEPOINT_ZERO)
|
|
85
|
-
.blockTimestamp;
|
|
86
|
-
const oldestTickCumulative = self[Number(oldestIndex)]?.tickCumulative || 0n;
|
|
87
|
-
let avgTick;
|
|
88
|
-
if (this.lteConsideringOverflow(oldestTimestamp, time - WINDOW, time)) {
|
|
89
|
-
if (this.lteConsideringOverflow(lastTimestamp, time - WINDOW, time)) {
|
|
90
|
-
index -= 1n; // considering underflow
|
|
91
|
-
const startTimepoint = self[Number(index)] || exports.TIMEPOINT_ZERO;
|
|
92
|
-
avgTick = startTimepoint.initialized
|
|
93
|
-
? (lastTickCumulative - startTimepoint.tickCumulative) /
|
|
94
|
-
(lastTimestamp - startTimepoint.blockTimestamp)
|
|
95
|
-
: tick;
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
const startOfWindow = this.getSingleTimepoint(self, time, WINDOW, tick, index, oldestIndex, 0n);
|
|
99
|
-
// current-WINDOW last current
|
|
100
|
-
// _________*____________*_______*_
|
|
101
|
-
// ||||||||||||
|
|
102
|
-
avgTick =
|
|
103
|
-
(lastTickCumulative - startOfWindow.tickCumulative) /
|
|
104
|
-
(lastTimestamp - time + WINDOW);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
avgTick =
|
|
109
|
-
lastTimestamp == oldestTimestamp
|
|
110
|
-
? tick
|
|
111
|
-
: (lastTickCumulative - oldestTickCumulative) /
|
|
112
|
-
(lastTimestamp - oldestTimestamp);
|
|
113
|
-
}
|
|
114
|
-
return avgTick;
|
|
115
|
-
}
|
|
116
|
-
/// @notice Fetches the timepoints beforeOrAt and atOrAfter a target, i.e. where [beforeOrAt, atOrAfter] is satisfied.
|
|
117
|
-
/// The result may be the same timepoint, or adjacent timepoints.
|
|
118
|
-
/// @dev The answer must be contained in the array, used when the target is located within the stored timepoint
|
|
119
|
-
/// boundaries: older than the most recent timepoint and younger, or the same age as, the oldest timepoint
|
|
120
|
-
/// @param self The stored dataStorage array
|
|
121
|
-
/// @param time The current block.timestamp
|
|
122
|
-
/// @param target The timestamp at which the reserved timepoint should be for
|
|
123
|
-
/// @param lastIndex The index of the timepoint that was most recently written to the timepoints array
|
|
124
|
-
/// @param oldestIndex The index of the oldest timepoint in the timepoints array
|
|
125
|
-
/// @return beforeOrAt The timepoint recorded before, or at, the target
|
|
126
|
-
/// @return atOrAfter The timepoint recorded at, or after, the target
|
|
127
|
-
static binarySearch(self, time, target, lastIndex, oldestIndex) {
|
|
128
|
-
let left = oldestIndex; // oldest timepoint
|
|
129
|
-
let right = lastIndex >= oldestIndex ? lastIndex : lastIndex + UINT16_MODULO; // newest timepoint considering one index overflow
|
|
130
|
-
let current = (left + right) >> 1n; // "middle" point between the boundaries
|
|
131
|
-
let beforeOrAt;
|
|
132
|
-
let atOrAfter;
|
|
133
|
-
let initializedAfter;
|
|
134
|
-
let timestampAfter;
|
|
135
|
-
do {
|
|
136
|
-
beforeOrAt = self[Number((0, utils_1.uint16)(current))] || exports.TIMEPOINT_ZERO; // checking the "middle" point between the boundaries
|
|
137
|
-
let [initializedBefore, timestampBefore] = [
|
|
138
|
-
beforeOrAt.initialized,
|
|
139
|
-
beforeOrAt.blockTimestamp,
|
|
140
|
-
];
|
|
141
|
-
if (initializedBefore) {
|
|
142
|
-
if (this.lteConsideringOverflow(timestampBefore, target, time)) {
|
|
143
|
-
// is current point before or at `target`?
|
|
144
|
-
atOrAfter = self[Number((0, utils_1.uint16)(current + 1n))] || exports.TIMEPOINT_ZERO; // checking the next point after "middle"
|
|
145
|
-
[initializedAfter, timestampAfter] = [
|
|
146
|
-
atOrAfter.initialized,
|
|
147
|
-
atOrAfter.blockTimestamp,
|
|
148
|
-
];
|
|
149
|
-
if (initializedAfter) {
|
|
150
|
-
if (this.lteConsideringOverflow(target, timestampAfter, time)) {
|
|
151
|
-
// is the "next" point after or at `target`?
|
|
152
|
-
return [beforeOrAt, atOrAfter]; // the only fully correct way to finish
|
|
153
|
-
}
|
|
154
|
-
left = current + 1n; // "next" point is before the `target`, so looking in the right half
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
// beforeOrAt is initialized and <= target, and next timepoint is uninitialized
|
|
158
|
-
// should be impossible if initial boundaries and `target` are correct
|
|
159
|
-
return [beforeOrAt, beforeOrAt];
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
right = current - 1n; // current point is after the `target`, so looking in the left half
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
// we've landed on an uninitialized timepoint, keep searching higher
|
|
168
|
-
// should be impossible if initial boundaries and `target` are correct
|
|
169
|
-
left = current + 1n;
|
|
170
|
-
}
|
|
171
|
-
current = (left + right) >> 1n; // calculating the new "middle" point index after updating the bounds
|
|
172
|
-
} while (true); // protect with max nb of iterations?
|
|
173
|
-
// unreachable code here
|
|
174
|
-
// atOrAfter = beforeOrAt; // code is unreachable, to suppress compiler warning
|
|
175
|
-
// assert(false);
|
|
176
|
-
// return [beforeOrAt, atOrAfter]
|
|
177
|
-
}
|
|
178
|
-
/// @dev Reverts if an timepoint at or before the desired timepoint timestamp does not exist.
|
|
179
|
-
/// 0 may be passed as `secondsAgo' to return the current cumulative values.
|
|
180
|
-
/// If called with a timestamp falling between two timepoints, returns the counterfactual accumulator values
|
|
181
|
-
/// at exactly the timestamp between the two timepoints.
|
|
182
|
-
/// @param self The stored dataStorage array
|
|
183
|
-
/// @param time The current block timestamp
|
|
184
|
-
/// @param secondsAgo The amount of time to look back, in seconds, at which point to return an timepoint
|
|
185
|
-
/// @param tick The current tick
|
|
186
|
-
/// @param index The index of the timepoint that was most recently written to the timepoints array
|
|
187
|
-
/// @param oldestIndex The index of the oldest timepoint
|
|
188
|
-
/// @param liquidity The current in-range pool liquidity
|
|
189
|
-
/// @return targetTimepoint desired timepoint or it's approximation
|
|
190
|
-
static getSingleTimepoint(self, time, secondsAgo, tick, index, oldestIndex, liquidity) {
|
|
191
|
-
let target = time - secondsAgo;
|
|
192
|
-
// if target is newer than last timepoint
|
|
193
|
-
if (secondsAgo == 0n ||
|
|
194
|
-
this.lteConsideringOverflow((self[Number(index)] || exports.TIMEPOINT_ZERO).blockTimestamp, target, time)) {
|
|
195
|
-
let last = self[Number(index)] || exports.TIMEPOINT_ZERO;
|
|
196
|
-
if (last.blockTimestamp == target) {
|
|
197
|
-
return last;
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
// otherwise, we need to add new timepoint
|
|
201
|
-
let avgTick = (0, utils_1.int24)(this._getAverageTick(self, time, tick, index, oldestIndex, last.blockTimestamp, last.tickCumulative));
|
|
202
|
-
let prevTick = tick;
|
|
203
|
-
{
|
|
204
|
-
if (index != oldestIndex) {
|
|
205
|
-
// FIXME?
|
|
206
|
-
let prevLast = {
|
|
207
|
-
averageTick: 0n,
|
|
208
|
-
blockTimestamp: 0n,
|
|
209
|
-
initialized: false,
|
|
210
|
-
secondsPerLiquidityCumulative: 0n,
|
|
211
|
-
tickCumulative: 0n,
|
|
212
|
-
volatilityCumulative: 0n,
|
|
213
|
-
volumePerLiquidityCumulative: 0n,
|
|
214
|
-
};
|
|
215
|
-
let _prevLast = self[Number(index - 1n)] || exports.TIMEPOINT_ZERO; // considering index underflow
|
|
216
|
-
prevLast.blockTimestamp = _prevLast.blockTimestamp;
|
|
217
|
-
prevLast.tickCumulative = _prevLast.tickCumulative;
|
|
218
|
-
prevTick = (0, utils_1.int24)((last.tickCumulative - prevLast.tickCumulative) /
|
|
219
|
-
(last.blockTimestamp - prevLast.blockTimestamp));
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
return this.createNewTimepoint(last, target, tick, prevTick, liquidity, avgTick, 0n);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
(0, utils_1._require)(this.lteConsideringOverflow((self[Number(oldestIndex)] || exports.TIMEPOINT_ZERO).blockTimestamp, target, time), 'OLD');
|
|
226
|
-
let [beforeOrAt, atOrAfter] = this.binarySearch(self, time, target, index, oldestIndex);
|
|
227
|
-
if (target == atOrAfter.blockTimestamp) {
|
|
228
|
-
return atOrAfter; // we're at the right boundary
|
|
229
|
-
}
|
|
230
|
-
if (target != beforeOrAt.blockTimestamp) {
|
|
231
|
-
// we're in the middle
|
|
232
|
-
let timepointTimeDelta = atOrAfter.blockTimestamp - beforeOrAt.blockTimestamp;
|
|
233
|
-
let targetDelta = target - beforeOrAt.blockTimestamp;
|
|
234
|
-
// For gas savings the resulting point is written to beforeAt
|
|
235
|
-
beforeOrAt.tickCumulative +=
|
|
236
|
-
((atOrAfter.tickCumulative - beforeOrAt.tickCumulative) /
|
|
237
|
-
timepointTimeDelta) *
|
|
238
|
-
targetDelta;
|
|
239
|
-
beforeOrAt.secondsPerLiquidityCumulative += (0, utils_1.uint160)(((0, utils_1.uint256)(atOrAfter.secondsPerLiquidityCumulative -
|
|
240
|
-
beforeOrAt.secondsPerLiquidityCumulative) *
|
|
241
|
-
targetDelta) /
|
|
242
|
-
timepointTimeDelta);
|
|
243
|
-
beforeOrAt.volatilityCumulative +=
|
|
244
|
-
((atOrAfter.volatilityCumulative - beforeOrAt.volatilityCumulative) /
|
|
245
|
-
timepointTimeDelta) *
|
|
246
|
-
targetDelta;
|
|
247
|
-
beforeOrAt.volumePerLiquidityCumulative +=
|
|
248
|
-
((atOrAfter.volumePerLiquidityCumulative -
|
|
249
|
-
beforeOrAt.volumePerLiquidityCumulative) /
|
|
250
|
-
timepointTimeDelta) *
|
|
251
|
-
targetDelta;
|
|
252
|
-
}
|
|
253
|
-
// we're at the left boundary or at the middle
|
|
254
|
-
return beforeOrAt;
|
|
255
|
-
}
|
|
256
|
-
/// @notice Returns the accumulator values as of each time seconds ago from the given time in the array of `secondsAgos`
|
|
257
|
-
/// @dev Reverts if `secondsAgos` > oldest timepoint
|
|
258
|
-
/// @param self The stored dataStorage array
|
|
259
|
-
/// @param time The current block.timestamp
|
|
260
|
-
/// @param secondsAgos Each amount of time to look back, in seconds, at which point to return an timepoint
|
|
261
|
-
/// @param tick The current tick
|
|
262
|
-
/// @param index The index of the timepoint that was most recently written to the timepoints array
|
|
263
|
-
/// @param liquidity The current in-range pool liquidity
|
|
264
|
-
/// @return tickCumulatives The tick * time elapsed since the pool was first initialized, as of each `secondsAgo`
|
|
265
|
-
/// @return secondsPerLiquidityCumulatives The cumulative seconds / max(1, liquidity) since the pool was first initialized, as of each `secondsAgo`
|
|
266
|
-
/// @return volatilityCumulatives The cumulative volatility values since the pool was first initialized, as of each `secondsAgo`
|
|
267
|
-
/// @return volumePerAvgLiquiditys The cumulative volume per liquidity values since the pool was first initialized, as of each `secondsAgo`
|
|
268
|
-
static getTimepoints(self, time, secondsAgos, tick, index, liquidity) {
|
|
269
|
-
let tickCumulatives = new Array(secondsAgos.length);
|
|
270
|
-
let secondsPerLiquidityCumulatives = new Array(secondsAgos.length);
|
|
271
|
-
let volatilityCumulatives = new Array(secondsAgos.length);
|
|
272
|
-
let volumePerAvgLiquiditys = new Array(secondsAgos.length);
|
|
273
|
-
let oldestIndex = 0n; // solidity default
|
|
274
|
-
// check if we have overflow in the past
|
|
275
|
-
let nextIndex = index + 1n; // considering overflow
|
|
276
|
-
if ((self[Number(nextIndex)] || exports.TIMEPOINT_ZERO).initialized) {
|
|
277
|
-
oldestIndex = nextIndex;
|
|
278
|
-
}
|
|
279
|
-
let current;
|
|
280
|
-
for (let i = 0; i < secondsAgos.length; i++) {
|
|
281
|
-
current = this.getSingleTimepoint(self, time, secondsAgos[i], tick, index, oldestIndex, liquidity);
|
|
282
|
-
[
|
|
283
|
-
tickCumulatives[i],
|
|
284
|
-
secondsPerLiquidityCumulatives[i],
|
|
285
|
-
volatilityCumulatives[i],
|
|
286
|
-
volumePerAvgLiquiditys[i],
|
|
287
|
-
] = [
|
|
288
|
-
current.tickCumulative,
|
|
289
|
-
current.secondsPerLiquidityCumulative,
|
|
290
|
-
current.volatilityCumulative,
|
|
291
|
-
current.volumePerLiquidityCumulative,
|
|
292
|
-
];
|
|
293
|
-
}
|
|
294
|
-
return [
|
|
295
|
-
tickCumulatives,
|
|
296
|
-
secondsPerLiquidityCumulatives,
|
|
297
|
-
volatilityCumulatives,
|
|
298
|
-
volumePerAvgLiquiditys,
|
|
299
|
-
];
|
|
300
|
-
}
|
|
301
|
-
/// @notice Returns average volatility in the range from time-WINDOW to time
|
|
302
|
-
/// @param self The stored dataStorage array
|
|
303
|
-
/// @param time The current block.timestamp
|
|
304
|
-
/// @param tick The current tick
|
|
305
|
-
/// @param index The index of the timepoint that was most recently written to the timepoints array
|
|
306
|
-
/// @param liquidity The current in-range pool liquidity
|
|
307
|
-
/// @return volatilityAverage The average volatility in the recent range
|
|
308
|
-
/// @return volumePerLiqAverage The average volume per liquidity in the recent range
|
|
309
|
-
static getAverages(self, time, tick, index, liquidity) {
|
|
310
|
-
let oldestIndex;
|
|
311
|
-
let oldest = self[0];
|
|
312
|
-
let nextIndex = index + 1n; // considering overflow
|
|
313
|
-
if ((self[Number(nextIndex)] || exports.TIMEPOINT_ZERO).initialized) {
|
|
314
|
-
oldest = self[Number(nextIndex)] || exports.TIMEPOINT_ZERO;
|
|
315
|
-
oldestIndex = nextIndex;
|
|
316
|
-
}
|
|
317
|
-
(0, ts_essentials_1.assert)(oldestIndex); // FIXME ?
|
|
318
|
-
let endOfWindow = this.getSingleTimepoint(self, time, 0n, tick, index, oldestIndex, liquidity);
|
|
319
|
-
let oldestTimestamp = oldest.blockTimestamp;
|
|
320
|
-
if (this.lteConsideringOverflow(oldestTimestamp, time - WINDOW, time)) {
|
|
321
|
-
let startOfWindow = this.getSingleTimepoint(self, time, WINDOW, tick, index, oldestIndex, liquidity);
|
|
322
|
-
return [
|
|
323
|
-
(endOfWindow.volatilityCumulative -
|
|
324
|
-
startOfWindow.volatilityCumulative) /
|
|
325
|
-
WINDOW,
|
|
326
|
-
(0, utils_1.uint256)(endOfWindow.volumePerLiquidityCumulative -
|
|
327
|
-
startOfWindow.volumePerLiquidityCumulative) >> 57n,
|
|
328
|
-
];
|
|
329
|
-
}
|
|
330
|
-
else if (time != oldestTimestamp) {
|
|
331
|
-
let _oldestVolatilityCumulative = oldest.volatilityCumulative;
|
|
332
|
-
let _oldestVolumePerLiquidityCumulative = oldest.volumePerLiquidityCumulative;
|
|
333
|
-
return [
|
|
334
|
-
(endOfWindow.volatilityCumulative - _oldestVolatilityCumulative) /
|
|
335
|
-
(time - oldestTimestamp),
|
|
336
|
-
(0, utils_1.uint256)(endOfWindow.volumePerLiquidityCumulative -
|
|
337
|
-
_oldestVolumePerLiquidityCumulative) >> 57n,
|
|
338
|
-
];
|
|
339
|
-
}
|
|
340
|
-
return [0n, 0n]; // should not enter here ?
|
|
341
|
-
}
|
|
342
|
-
/// @notice Initialize the dataStorage array by writing the first slot. Called once for the lifecycle of the timepoints array
|
|
343
|
-
/// @param self The stored dataStorage array
|
|
344
|
-
/// @param time The time of the dataStorage initialization, via block.timestamp truncated to uint32
|
|
345
|
-
/// @param tick Initial tick
|
|
346
|
-
// static initialize(
|
|
347
|
-
// Timepoint[UINT16_MODULO] storage self,
|
|
348
|
-
// uint32 time,
|
|
349
|
-
// int24 tick
|
|
350
|
-
// ) internal {
|
|
351
|
-
// require(!self[0].initialized);
|
|
352
|
-
// self[0].initialized = true;
|
|
353
|
-
// self[0].blockTimestamp = time;
|
|
354
|
-
// self[0].averageTick = tick;
|
|
355
|
-
// }
|
|
356
|
-
/// @notice Writes an dataStorage timepoint to the array
|
|
357
|
-
/// @dev Writable at most once per block. Index represents the most recently written element. index must be tracked externally.
|
|
358
|
-
/// @param self The stored dataStorage array
|
|
359
|
-
/// @param index The index of the timepoint that was most recently written to the timepoints array
|
|
360
|
-
/// @param blockTimestamp The timestamp of the new timepoint
|
|
361
|
-
/// @param tick The active tick at the time of the new timepoint
|
|
362
|
-
/// @param liquidity The total in-range liquidity at the time of the new timepoint
|
|
363
|
-
/// @param volumePerLiquidity The gmean(volumes)/liquidity at the time of the new timepoint
|
|
364
|
-
/// @return indexUpdated The new index of the most recently written element in the dataStorage array
|
|
365
|
-
static write(self, index, blockTimestamp, tick, liquidity, volumePerLiquidity) {
|
|
366
|
-
let _last = self[Number(index)] || exports.TIMEPOINT_ZERO;
|
|
367
|
-
// early return if we've already written an timepoint this block
|
|
368
|
-
if (_last.blockTimestamp == blockTimestamp) {
|
|
369
|
-
return index;
|
|
370
|
-
}
|
|
371
|
-
let last = _last;
|
|
372
|
-
// get next index considering overflow
|
|
373
|
-
let indexUpdated = index + 1n;
|
|
374
|
-
let oldestIndex = 0n; // default in evm
|
|
375
|
-
// check if we have overflow in the past
|
|
376
|
-
if ((self[Number(indexUpdated)] || exports.TIMEPOINT_ZERO).initialized) {
|
|
377
|
-
oldestIndex = indexUpdated;
|
|
378
|
-
}
|
|
379
|
-
let avgTick = (0, utils_1.int24)(this._getAverageTick(self, blockTimestamp, tick, index, oldestIndex, last.blockTimestamp, last.tickCumulative));
|
|
380
|
-
let prevTick = tick;
|
|
381
|
-
if (index != oldestIndex) {
|
|
382
|
-
let _prevLast = self[Number(index - 1n)] || exports.TIMEPOINT_ZERO; // considering index underflow
|
|
383
|
-
let _prevLastBlockTimestamp = _prevLast.blockTimestamp;
|
|
384
|
-
let _prevLastTickCumulative = _prevLast.tickCumulative;
|
|
385
|
-
prevTick = (0, utils_1.int24)((last.tickCumulative - _prevLastTickCumulative) /
|
|
386
|
-
(last.blockTimestamp - _prevLastBlockTimestamp));
|
|
387
|
-
}
|
|
388
|
-
self[Number(indexUpdated)] = this.createNewTimepoint(last, blockTimestamp, tick, prevTick, liquidity, avgTick, volumePerLiquidity);
|
|
389
|
-
// FIXME should delete ?
|
|
390
|
-
// delete self[Number(index)];
|
|
391
|
-
return indexUpdated;
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
exports.DataStorage = DataStorage;
|
|
395
|
-
DataStorage.WINDOW = WINDOW;
|
|
396
|
-
DataStorage.UINT16_MODULO = UINT16_MODULO;
|
|
397
|
-
//# sourceMappingURL=DataStorage.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"DataStorage.js","sourceRoot":"","sources":["../../../../src/dex/algebra/lib/DataStorage.ts"],"names":[],"mappings":";;;AAAA,iDAAuC;AACvC,0CAQwB;AAoBX,QAAA,cAAc,GAAc;IACvC,WAAW,EAAE,EAAE;IACf,cAAc,EAAE,EAAE;IAClB,WAAW,EAAE,KAAK;IAClB,6BAA6B,EAAE,EAAE;IACjC,cAAc,EAAE,EAAE;IAClB,oBAAoB,EAAE,E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C,YAAY,CAAC,CAAC,GAAG,IAAI,CAAC,kBAAkB,CAClD,IAAI,EACJ,cAAc,EACd,IAAI,EACJ,QAAQ,EACR,SAAS,EACT,OAAO,EACP,kBAAkB,CACnB,CAAC;QAEF,wBAAwB;QACxB,8BAA8B;QAE9B,OAAO,YAAY,CAAC;IACtB,CAAC;;AAzkBH,kCA0kBC;AAzkBQ,kBAAM,GAAG,MAAM,CAAC;AAChB,yBAAa,GAAG,aAAa,CAAC"}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { PoolState } from '../types';
|
|
2
|
-
export declare class DataStorageOperator {
|
|
3
|
-
static getSingleTimepoint(state: PoolState, time: bigint, secondsAgo: bigint, tick: bigint, index: bigint, liquidity: bigint): [bigint, bigint, bigint, bigint];
|
|
4
|
-
static getTimepoints(state: PoolState, time: bigint, secondsAgos: bigint[], tick: bigint, index: bigint, liquidity: bigint): [bigint[], bigint[], bigint[], bigint[]];
|
|
5
|
-
static getAverages(state: PoolState, time: bigint, tick: bigint, index: bigint, liquidity: bigint): [bigint, bigint];
|
|
6
|
-
static write(state: PoolState, index: bigint, blockTimestamp: bigint, tick: bigint, liquidity: bigint, volumePerLiquidity: bigint): bigint;
|
|
7
|
-
static calculateVolumePerLiquidity(liquidity: bigint, amount0: bigint, amount1: bigint): bigint;
|
|
8
|
-
static window(): bigint;
|
|
9
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DataStorageOperator = void 0;
|
|
4
|
-
const ts_essentials_1 = require("ts-essentials");
|
|
5
|
-
const DataStorage_1 = require("./DataStorage");
|
|
6
|
-
const bigint_constants_1 = require("../../../bigint-constants");
|
|
7
|
-
const utils_1 = require("../../../utils");
|
|
8
|
-
const Sqrt_1 = require("./Sqrt");
|
|
9
|
-
// TO COMPARE WITH https://github.com/cryptoalgebra/Algebra/blob/3cd9a65bb5bb39d26fb4346e8f7d6d2a28672cc6/src/core/contracts/DataStorageOperator.sol#L16-L17
|
|
10
|
-
const MAX_VOLUME_PER_LIQUIDITY = 100000 << 64; // maximum meaningful ratio of volume to liquidity
|
|
11
|
-
class DataStorageOperator {
|
|
12
|
-
/// @inheritdoc IDataStorageOperator
|
|
13
|
-
static getSingleTimepoint(state, time, secondsAgo, tick, index, liquidity) {
|
|
14
|
-
let oldestIndex;
|
|
15
|
-
// check if we have overflow in the past
|
|
16
|
-
let nextIndex = index + 1n; // considering overflow
|
|
17
|
-
if ((state.timepoints[Number(nextIndex)] || DataStorage_1.TIMEPOINT_ZERO).initialized) {
|
|
18
|
-
oldestIndex = nextIndex;
|
|
19
|
-
}
|
|
20
|
-
(0, ts_essentials_1.assert)(oldestIndex); //
|
|
21
|
-
let result = DataStorage_1.DataStorage.getSingleTimepoint(state.timepoints, time, secondsAgo, tick, index, oldestIndex, liquidity);
|
|
22
|
-
let [tickCumulative, secondsPerLiquidityCumulative, volatilityCumulative, volumePerAvgLiquidity,] = [
|
|
23
|
-
result.tickCumulative,
|
|
24
|
-
result.secondsPerLiquidityCumulative,
|
|
25
|
-
result.volatilityCumulative,
|
|
26
|
-
result.volumePerLiquidityCumulative,
|
|
27
|
-
];
|
|
28
|
-
return [
|
|
29
|
-
tickCumulative,
|
|
30
|
-
secondsPerLiquidityCumulative,
|
|
31
|
-
volatilityCumulative,
|
|
32
|
-
volumePerAvgLiquidity,
|
|
33
|
-
];
|
|
34
|
-
}
|
|
35
|
-
/// @inheritdoc IDataStorageOperator
|
|
36
|
-
static getTimepoints(state, time, secondsAgos, tick, index, liquidity) {
|
|
37
|
-
return DataStorage_1.DataStorage.getTimepoints(state.timepoints, time, secondsAgos, tick, index, liquidity);
|
|
38
|
-
}
|
|
39
|
-
/// @inheritdoc IDataStorageOperator
|
|
40
|
-
static getAverages(state, time, tick, index, liquidity) {
|
|
41
|
-
return DataStorage_1.DataStorage.getAverages(state.timepoints, time, tick, index, liquidity);
|
|
42
|
-
}
|
|
43
|
-
/// @inheritdoc IDataStorageOperator
|
|
44
|
-
static write(state, index, blockTimestamp, tick, liquidity, volumePerLiquidity) {
|
|
45
|
-
return DataStorage_1.DataStorage.write(state.timepoints, index, blockTimestamp, tick, liquidity, volumePerLiquidity);
|
|
46
|
-
}
|
|
47
|
-
/// @inheritdoc IDataStorageOperator
|
|
48
|
-
static calculateVolumePerLiquidity(liquidity, amount0, amount1) {
|
|
49
|
-
let volume = Sqrt_1.Sqrt.sqrtAbs(amount0) * Sqrt_1.Sqrt.sqrtAbs(amount1);
|
|
50
|
-
let volumeShifted;
|
|
51
|
-
if (volume >= 2n ** 192n)
|
|
52
|
-
volumeShifted = bigint_constants_1.BI_MAX_UINT256 / (liquidity > 0n ? liquidity : 1n);
|
|
53
|
-
else
|
|
54
|
-
volumeShifted = (volume << 64n) / (liquidity > 0n ? liquidity : 1n);
|
|
55
|
-
if (volumeShifted >= MAX_VOLUME_PER_LIQUIDITY)
|
|
56
|
-
return BigInt(MAX_VOLUME_PER_LIQUIDITY);
|
|
57
|
-
else
|
|
58
|
-
return (0, utils_1.uint128)(volumeShifted);
|
|
59
|
-
}
|
|
60
|
-
/// @inheritdoc IDataStorageOperator
|
|
61
|
-
static window() {
|
|
62
|
-
return DataStorage_1.DataStorage.WINDOW;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
exports.DataStorageOperator = DataStorageOperator;
|
|
66
|
-
//# sourceMappingURL=DataStorageOperator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"DataStorageOperator.js","sourceRoot":"","sources":["../../../../src/dex/algebra/lib/DataStorageOperator.ts"],"names":[],"mappings":";;;AAAA,iDAAuC;AAEvC,+CAA4D;AAC5D,gEAA2D;AAC3D,0CAAyC;AACzC,iCAA8B;AAE9B,4JAA4J;AAE5J,MAAM,wBAAwB,GAAG,MAAM,IAAI,EAAE,CAAC,CAAC,kDAAkD;AAEjG,MAAa,mBAAmB;IAC9B,oCAAoC;IACpC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UAAU,EAChB,IAAI,EACJ,IAAI,EACJ,KAAK,EACL,SAAS,CACV,CAAC;IACJ,CAAC;IAED,oCAAoC;IACpC,MAAM,CAAC,KAAK,CACV,KAAgB,EAChB,KAAa,EACb,cAAsB,EACtB,IAAY,EACZ,SAAiB,EACjB,kBAA0B;QAE1B,OAAO,yBAAW,CAAC,KAAK,CACtB,KAAK,CAAC,UAAU,EAChB,KAAK,EACL,cAAc,EACd,IAAI,EACJ,SAAS,EACT,kBAAkB,CACnB,CAAC;IACJ,CAAC;IAED,oCAAoC;IACpC,MAAM,CAAC,2BAA2B,CAChC,SAAiB,EACjB,OAAe,EACf,OAAe;QAEf,IAAI,MAAM,GAAG,WAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,WAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAC3D,IAAI,aAAa,CAAC;QAClB,IAAI,MAAM,IAAI,EAAE,IAAI,IAAI;YACtB,aAAa,GAAG,iCAAc,GAAG,CAAC,SAAS,GAAG,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;;YAChE,aAAa,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,GAAG,CAAC,SAAS,GAAG,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QACzE,IAAI,aAAa,IAAI,wBAAwB;YAC3C,OAAO,MAAM,CAAC,wBAAwB,CAAC,CAAC;;YACrC,OAAO,IAAA,eAAO,EAAC,aAAa,CAAC,CAAC;IACrC,CAAC;IAED,oCAAoC;IACpC,MAAM,CAAC,MAAM;QACX,OAAO,yBAAW,CAAC,MAAM,CAAC;IAC5B,CAAC;CAaF;AAvID,kDAuIC"}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Sqrt = void 0;
|
|
4
|
-
const utils_1 = require("../../../utils");
|
|
5
|
-
// TO COMPARE WITH https://github.com/cryptoalgebra/Algebra/blob/509f5bbb80b81daef1d5c15ea8695b5d039630cc/src/core/contracts/libraries/Sqrt.sol#L2-L3
|
|
6
|
-
class Sqrt {
|
|
7
|
-
/// @notice Gets the square root of the absolute value of the parameter
|
|
8
|
-
static sqrtAbs(_x) {
|
|
9
|
-
let result;
|
|
10
|
-
// get abs value
|
|
11
|
-
let mask = _x >> (256n - 1n);
|
|
12
|
-
let x = (0, utils_1.uint256)((_x ^ mask) - mask);
|
|
13
|
-
if (x == 0n)
|
|
14
|
-
result = 0n;
|
|
15
|
-
else {
|
|
16
|
-
let xx = x;
|
|
17
|
-
let r = 1n;
|
|
18
|
-
if (xx >= 0x100000000000000000000000000000000n) {
|
|
19
|
-
xx >>= 128n;
|
|
20
|
-
r <<= 64n;
|
|
21
|
-
}
|
|
22
|
-
if (xx >= 0x10000000000000000n) {
|
|
23
|
-
xx >>= 64n;
|
|
24
|
-
r <<= 32n;
|
|
25
|
-
}
|
|
26
|
-
Sqrt;
|
|
27
|
-
if (xx >= 0x100000000n) {
|
|
28
|
-
xx >>= 32n;
|
|
29
|
-
r <<= 16n;
|
|
30
|
-
}
|
|
31
|
-
if (xx >= 0x10000n) {
|
|
32
|
-
xx >>= 16n;
|
|
33
|
-
r <<= 8n;
|
|
34
|
-
}
|
|
35
|
-
if (xx >= 0x100n) {
|
|
36
|
-
xx >>= 8n;
|
|
37
|
-
r <<= 4n;
|
|
38
|
-
}
|
|
39
|
-
if (xx >= 0x10n) {
|
|
40
|
-
xx >>= 4n;
|
|
41
|
-
r <<= 2n;
|
|
42
|
-
}
|
|
43
|
-
if (xx >= 0x8n) {
|
|
44
|
-
r <<= 1n;
|
|
45
|
-
}
|
|
46
|
-
r = (r + x / r) >> 1n;
|
|
47
|
-
r = (r + x / r) >> 1n;
|
|
48
|
-
r = (r + x / r) >> 1n;
|
|
49
|
-
r = (r + x / r) >> 1n;
|
|
50
|
-
r = (r + x / r) >> 1n;
|
|
51
|
-
r = (r + x / r) >> 1n;
|
|
52
|
-
r = (r + x / r) >> 1n; // @dev Seven iterations should be enough.
|
|
53
|
-
let r1 = x / r;
|
|
54
|
-
result = r < r1 ? r : r1;
|
|
55
|
-
}
|
|
56
|
-
return result;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
exports.Sqrt = Sqrt;
|
|
60
|
-
//# sourceMappingURL=Sqrt.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Sqrt.js","sourceRoot":"","sources":["../../../../src/dex/algebra/lib/Sqrt.ts"],"names":[],"mappings":";;;AAAA,0CAAyC;AAEzC,qJAAqJ;AACrJ,MAAa,IAAI;IACf,uEAAuE;IACvE,MAAM,CAAC,OAAO,CAAC,EAAU;QACvB,IAAI,MAAM,CAAC;QAEX,gBAAgB;QAChB,IAAI,IAAI,GAAG,EAAE,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC,CAAC;QAC7B,IAAI,CAAC,GAAG,IAAA,eAAO,EAAC,CAAC,EAAE,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;QACpC,IAAI,CAAC,IAAI,EAAE;YAAE,MAAM,GAAG,EAAE,CAAC;aACpB;YACH,IAAI,EAAE,GAAG,CAAC,CAAC;YACX,IAAI,CAAC,GAAG,EAAE,CAAC;YACX,IAAI,EAAE,IAAI,oCAAoC,EAAE;gBAC9C,EAAE,KAAK,IAAI,CAAC;gBACZ,CAAC,KAAK,GAAG,CAAC;aACX;YACD,IAAI,EAAE,IAAI,oBAAoB,EAAE;gBAC9B,EAAE,KAAK,GAAG,CAAC;gBACX,CAAC,KAAK,GAAG,CAAC;aACX;YACD,IAAI,CAAC;YACL,IAAI,EAAE,IAAI,YAAY,EAAE;gBACtB,EAAE,KAAK,GAAG,CAAC;gBACX,CAAC,KAAK,GAAG,CAAC;aACX;YACD,IAAI,EAAE,IAAI,QAAQ,EAAE;gBAClB,EAAE,KAAK,GAAG,CAAC;gBACX,CAA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