@skate-org/amm-evm-v2 1.0.0 → 2.0.0-alpha.2
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/CHANGELOG.md +24 -0
- package/dist/index.cjs +6 -384
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -362
- package/dist/index.js.map +1 -1
- package/dist/kernel/reader.d.ts.map +1 -1
- package/dist/periphery/reader.d.ts.map +1 -1
- package/package.json +2 -2
- package/dist/math/fullMath.d.ts +0 -28
- package/dist/math/fullMath.d.ts.map +0 -1
- package/dist/math/index.d.ts +0 -5
- package/dist/math/index.d.ts.map +0 -1
- package/dist/math/liquidityMath.d.ts +0 -18
- package/dist/math/liquidityMath.d.ts.map +0 -1
- package/dist/math/sqrtPriceMath.d.ts +0 -62
- package/dist/math/sqrtPriceMath.d.ts.map +0 -1
- package/dist/math/tickMath.d.ts +0 -36
- package/dist/math/tickMath.d.ts.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# @skate-org/amm-evm-v2
|
|
2
2
|
|
|
3
|
+
## 2.0.0-alpha.2 — 2026-05-05
|
|
4
|
+
|
|
5
|
+
Plan C of the SVM/Sui integration. Adds `@skate-org/amm-sui-v2` to
|
|
6
|
+
the workspace; this package's source is unchanged versus
|
|
7
|
+
`2.0.0-alpha.1` (re-versioned in lockstep).
|
|
8
|
+
|
|
9
|
+
## 2.0.0-alpha.1 — 2026-05-05
|
|
10
|
+
|
|
11
|
+
Plan B of the SVM/Sui integration. Adds `@skate-org/amm-svm-v2` to
|
|
12
|
+
the workspace; this package's source is unchanged versus
|
|
13
|
+
`2.0.0-alpha.0` (re-versioned in lockstep).
|
|
14
|
+
|
|
15
|
+
## 2.0.0-alpha.0 — 2026-05-05
|
|
16
|
+
|
|
17
|
+
Plan A of the SVM/Sui integration.
|
|
18
|
+
|
|
19
|
+
- **Breaking:** uniswap-v3 math no longer ships from
|
|
20
|
+
`@skate-org/amm-evm-v2`. Import from `@skate-org/amm-core-v2`
|
|
21
|
+
instead. All function names unchanged. Top-level barrel re-exports
|
|
22
|
+
via the umbrella (`@skate-org/amm-v2`) continue to work without
|
|
23
|
+
callsite changes.
|
|
24
|
+
- Removed: `packages/evm/src/math/` directory and corresponding
|
|
25
|
+
tests (relocated to `core`).
|
|
26
|
+
|
|
3
27
|
## 1.0.0 — 2026-05-04
|
|
4
28
|
|
|
5
29
|
Realigning all four `@skate-org/amm-*-v2` packages at a single version
|
package/dist/index.cjs
CHANGED
|
@@ -24,30 +24,13 @@ __export(index_exports, {
|
|
|
24
24
|
EvmReadError: () => EvmReadError,
|
|
25
25
|
EvmWriteError: () => EvmWriteError,
|
|
26
26
|
KERNEL_EVENT_NAMES: () => KERNEL_EVENT_NAMES,
|
|
27
|
-
MAX_SQRT_RATIO: () => MAX_SQRT_RATIO,
|
|
28
|
-
MAX_TICK: () => MAX_TICK,
|
|
29
|
-
MIN_SQRT_RATIO: () => MIN_SQRT_RATIO,
|
|
30
|
-
MIN_TICK: () => MIN_TICK,
|
|
31
27
|
PERIPHERY_EVENT_NAMES: () => PERIPHERY_EVENT_NAMES,
|
|
32
|
-
addDelta: () => addDelta,
|
|
33
28
|
computePositionAmounts: () => computePositionAmounts,
|
|
34
29
|
createDevConfig: () => createDevConfig,
|
|
35
30
|
decodeTickBitmapWord: () => decodeTickBitmapWord,
|
|
36
|
-
getAmount0Delta: () => getAmount0Delta,
|
|
37
|
-
getAmount0DeltaSigned: () => getAmount0DeltaSigned,
|
|
38
|
-
getAmount1Delta: () => getAmount1Delta,
|
|
39
|
-
getAmount1DeltaSigned: () => getAmount1DeltaSigned,
|
|
40
31
|
getKernelPublicClient: () => getKernelPublicClient,
|
|
41
|
-
getNextSqrtPriceFromAmount0RoundingUp: () => getNextSqrtPriceFromAmount0RoundingUp,
|
|
42
|
-
getNextSqrtPriceFromAmount1RoundingDown: () => getNextSqrtPriceFromAmount1RoundingDown,
|
|
43
|
-
getNextSqrtPriceFromInput: () => getNextSqrtPriceFromInput,
|
|
44
|
-
getNextSqrtPriceFromOutput: () => getNextSqrtPriceFromOutput,
|
|
45
32
|
getSourcePublicClient: () => getSourcePublicClient,
|
|
46
|
-
getSqrtRatioAtTick: () => getSqrtRatioAtTick,
|
|
47
|
-
getTickAtSqrtRatio: () => getTickAtSqrtRatio,
|
|
48
33
|
megaethChain: () => megaethChain,
|
|
49
|
-
mulDiv: () => mulDiv,
|
|
50
|
-
mulDivRoundingUp: () => mulDivRoundingUp,
|
|
51
34
|
parseKernelEventLog: () => parseKernelEventLog,
|
|
52
35
|
parsePeripheryEventLog: () => parsePeripheryEventLog,
|
|
53
36
|
readPeripheryPool: () => readPeripheryPool,
|
|
@@ -176,350 +159,6 @@ function wrapWriteError(err, functionName) {
|
|
|
176
159
|
return new EvmWriteError(functionName, message, { cause: err });
|
|
177
160
|
}
|
|
178
161
|
|
|
179
|
-
// src/math/tickMath.ts
|
|
180
|
-
var MAX_UINT256 = (1n << 256n) - 1n;
|
|
181
|
-
var Q160_MASK = (1n << 160n) - 1n;
|
|
182
|
-
var Q256_MOD = 1n << 256n;
|
|
183
|
-
var MIN_TICK = -887272;
|
|
184
|
-
var MAX_TICK = 887272;
|
|
185
|
-
var MIN_SQRT_RATIO = 4295128739n;
|
|
186
|
-
var MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342n;
|
|
187
|
-
function getSqrtRatioAtTick(tick) {
|
|
188
|
-
if (!Number.isInteger(tick)) {
|
|
189
|
-
throw new Error("TickMath: tick must be an integer");
|
|
190
|
-
}
|
|
191
|
-
const absTick = BigInt(tick < 0 ? -tick : tick);
|
|
192
|
-
if (absTick > BigInt(MAX_TICK)) {
|
|
193
|
-
throw new Error("T");
|
|
194
|
-
}
|
|
195
|
-
let ratio = (absTick & 0x1n) !== 0n ? 0xfffcb933bd6fad37aa2d162d1a594001n : 0x100000000000000000000000000000000n;
|
|
196
|
-
if ((absTick & 0x2n) !== 0n)
|
|
197
|
-
ratio = (ratio * 0xfff97272373d413259a46990580e213an & MAX_UINT256) >> 128n;
|
|
198
|
-
if ((absTick & 0x4n) !== 0n)
|
|
199
|
-
ratio = (ratio * 0xfff2e50f5f656932ef12357cf3c7fdccn & MAX_UINT256) >> 128n;
|
|
200
|
-
if ((absTick & 0x8n) !== 0n)
|
|
201
|
-
ratio = (ratio * 0xffe5caca7e10e4e61c3624eaa0941cd0n & MAX_UINT256) >> 128n;
|
|
202
|
-
if ((absTick & 0x10n) !== 0n)
|
|
203
|
-
ratio = (ratio * 0xffcb9843d60f6159c9db58835c926644n & MAX_UINT256) >> 128n;
|
|
204
|
-
if ((absTick & 0x20n) !== 0n)
|
|
205
|
-
ratio = (ratio * 0xff973b41fa98c081472e6896dfb254c0n & MAX_UINT256) >> 128n;
|
|
206
|
-
if ((absTick & 0x40n) !== 0n)
|
|
207
|
-
ratio = (ratio * 0xff2ea16466c96a3843ec78b326b52861n & MAX_UINT256) >> 128n;
|
|
208
|
-
if ((absTick & 0x80n) !== 0n)
|
|
209
|
-
ratio = (ratio * 0xfe5dee046a99a2a811c461f1969c3053n & MAX_UINT256) >> 128n;
|
|
210
|
-
if ((absTick & 0x100n) !== 0n)
|
|
211
|
-
ratio = (ratio * 0xfcbe86c7900a88aedcffc83b479aa3a4n & MAX_UINT256) >> 128n;
|
|
212
|
-
if ((absTick & 0x200n) !== 0n)
|
|
213
|
-
ratio = (ratio * 0xf987a7253ac413176f2b074cf7815e54n & MAX_UINT256) >> 128n;
|
|
214
|
-
if ((absTick & 0x400n) !== 0n)
|
|
215
|
-
ratio = (ratio * 0xf3392b0822b70005940c7a398e4b70f3n & MAX_UINT256) >> 128n;
|
|
216
|
-
if ((absTick & 0x800n) !== 0n)
|
|
217
|
-
ratio = (ratio * 0xe7159475a2c29b7443b29c7fa6e889d9n & MAX_UINT256) >> 128n;
|
|
218
|
-
if ((absTick & 0x1000n) !== 0n)
|
|
219
|
-
ratio = (ratio * 0xd097f3bdfd2022b8845ad8f792aa5825n & MAX_UINT256) >> 128n;
|
|
220
|
-
if ((absTick & 0x2000n) !== 0n)
|
|
221
|
-
ratio = (ratio * 0xa9f746462d870fdf8a65dc1f90e061e5n & MAX_UINT256) >> 128n;
|
|
222
|
-
if ((absTick & 0x4000n) !== 0n)
|
|
223
|
-
ratio = (ratio * 0x70d869a156d2a1b890bb3df62baf32f7n & MAX_UINT256) >> 128n;
|
|
224
|
-
if ((absTick & 0x8000n) !== 0n)
|
|
225
|
-
ratio = (ratio * 0x31be135f97d08fd981231505542fcfa6n & MAX_UINT256) >> 128n;
|
|
226
|
-
if ((absTick & 0x10000n) !== 0n)
|
|
227
|
-
ratio = (ratio * 0x9aa508b5b7a84e1c677de54f3e99bc9n & MAX_UINT256) >> 128n;
|
|
228
|
-
if ((absTick & 0x20000n) !== 0n)
|
|
229
|
-
ratio = (ratio * 0x5d6af8dedb81196699c329225ee604n & MAX_UINT256) >> 128n;
|
|
230
|
-
if ((absTick & 0x40000n) !== 0n)
|
|
231
|
-
ratio = (ratio * 0x2216e584f5fa1ea926041bedfe98n & MAX_UINT256) >> 128n;
|
|
232
|
-
if ((absTick & 0x80000n) !== 0n)
|
|
233
|
-
ratio = (ratio * 0x48a170391f7dc42444e8fa2n & MAX_UINT256) >> 128n;
|
|
234
|
-
if (tick > 0) ratio = MAX_UINT256 / ratio;
|
|
235
|
-
const shifted = ratio >> 32n;
|
|
236
|
-
const remainder = ratio & (1n << 32n) - 1n;
|
|
237
|
-
const sqrtPriceX96 = remainder === 0n ? shifted : shifted + 1n;
|
|
238
|
-
return sqrtPriceX96 & Q160_MASK;
|
|
239
|
-
}
|
|
240
|
-
function getTickAtSqrtRatio(sqrtPriceX96) {
|
|
241
|
-
if (sqrtPriceX96 < MIN_SQRT_RATIO || sqrtPriceX96 >= MAX_SQRT_RATIO) {
|
|
242
|
-
throw new Error("R");
|
|
243
|
-
}
|
|
244
|
-
const ratio = sqrtPriceX96 << 32n;
|
|
245
|
-
let r = ratio;
|
|
246
|
-
let msb = 0n;
|
|
247
|
-
{
|
|
248
|
-
const f = r > 0xffffffffffffffffffffffffffffffffn ? 128n : 0n;
|
|
249
|
-
msb |= f;
|
|
250
|
-
r >>= f;
|
|
251
|
-
}
|
|
252
|
-
{
|
|
253
|
-
const f = r > 0xffffffffffffffffn ? 64n : 0n;
|
|
254
|
-
msb |= f;
|
|
255
|
-
r >>= f;
|
|
256
|
-
}
|
|
257
|
-
{
|
|
258
|
-
const f = r > 0xffffffffn ? 32n : 0n;
|
|
259
|
-
msb |= f;
|
|
260
|
-
r >>= f;
|
|
261
|
-
}
|
|
262
|
-
{
|
|
263
|
-
const f = r > 0xffffn ? 16n : 0n;
|
|
264
|
-
msb |= f;
|
|
265
|
-
r >>= f;
|
|
266
|
-
}
|
|
267
|
-
{
|
|
268
|
-
const f = r > 0xffn ? 8n : 0n;
|
|
269
|
-
msb |= f;
|
|
270
|
-
r >>= f;
|
|
271
|
-
}
|
|
272
|
-
{
|
|
273
|
-
const f = r > 0xfn ? 4n : 0n;
|
|
274
|
-
msb |= f;
|
|
275
|
-
r >>= f;
|
|
276
|
-
}
|
|
277
|
-
{
|
|
278
|
-
const f = r > 0x3n ? 2n : 0n;
|
|
279
|
-
msb |= f;
|
|
280
|
-
r >>= f;
|
|
281
|
-
}
|
|
282
|
-
{
|
|
283
|
-
const f = r > 0x1n ? 1n : 0n;
|
|
284
|
-
msb |= f;
|
|
285
|
-
}
|
|
286
|
-
if (msb >= 128n) r = ratio >> msb - 127n;
|
|
287
|
-
else r = ratio << 127n - msb & MAX_UINT256;
|
|
288
|
-
const offset = msb - 128n;
|
|
289
|
-
let log_2 = offset << 64n & MAX_UINT256;
|
|
290
|
-
for (let i = 0; i < 14; i++) {
|
|
291
|
-
r = r * r >> 127n;
|
|
292
|
-
const f = r >> 128n;
|
|
293
|
-
log_2 |= f << BigInt(63 - i);
|
|
294
|
-
r >>= f;
|
|
295
|
-
}
|
|
296
|
-
const log_2_signed = toInt256(log_2);
|
|
297
|
-
const log_sqrt10001 = log_2_signed * 255738958999603826347141n;
|
|
298
|
-
const tickLowBig = log_sqrt10001 - 3402992956809132418596140100660247210n >> 128n;
|
|
299
|
-
const tickHiBig = log_sqrt10001 + 291339464771989622907027621153398088495n >> 128n;
|
|
300
|
-
const tickLow = toInt24(tickLowBig);
|
|
301
|
-
const tickHi = toInt24(tickHiBig);
|
|
302
|
-
if (tickLow === tickHi) return tickLow;
|
|
303
|
-
return getSqrtRatioAtTick(tickHi) <= sqrtPriceX96 ? tickHi : tickLow;
|
|
304
|
-
}
|
|
305
|
-
function toInt256(x) {
|
|
306
|
-
const masked = x & MAX_UINT256;
|
|
307
|
-
return masked >= 1n << 255n ? masked - Q256_MOD : masked;
|
|
308
|
-
}
|
|
309
|
-
function toInt24(x) {
|
|
310
|
-
const mask = (1n << 24n) - 1n;
|
|
311
|
-
const truncated = x & mask;
|
|
312
|
-
const signed = truncated >= 1n << 23n ? truncated - (1n << 24n) : truncated;
|
|
313
|
-
return Number(signed);
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
// src/math/fullMath.ts
|
|
317
|
-
var MAX_UINT2562 = (1n << 256n) - 1n;
|
|
318
|
-
function mulDiv(a, b, denom) {
|
|
319
|
-
if (denom === 0n) {
|
|
320
|
-
throw new Error("FullMath: division by zero");
|
|
321
|
-
}
|
|
322
|
-
if (a < 0n || b < 0n || denom < 0n) {
|
|
323
|
-
throw new Error("FullMath: negative input");
|
|
324
|
-
}
|
|
325
|
-
const product = a * b;
|
|
326
|
-
const result = product / denom;
|
|
327
|
-
if (result > MAX_UINT2562) {
|
|
328
|
-
throw new Error("FullMath: result overflows uint256");
|
|
329
|
-
}
|
|
330
|
-
return result;
|
|
331
|
-
}
|
|
332
|
-
function mulDivRoundingUp(a, b, denom) {
|
|
333
|
-
if (denom === 0n) {
|
|
334
|
-
throw new Error("FullMath: division by zero");
|
|
335
|
-
}
|
|
336
|
-
if (a < 0n || b < 0n || denom < 0n) {
|
|
337
|
-
throw new Error("FullMath: negative input");
|
|
338
|
-
}
|
|
339
|
-
const product = a * b;
|
|
340
|
-
const quotient = product / denom;
|
|
341
|
-
if (quotient > MAX_UINT2562) {
|
|
342
|
-
throw new Error("FullMath: result overflows uint256");
|
|
343
|
-
}
|
|
344
|
-
if (product % denom !== 0n) {
|
|
345
|
-
if (quotient === MAX_UINT2562) {
|
|
346
|
-
throw new Error("FullMath: rounding up overflows uint256");
|
|
347
|
-
}
|
|
348
|
-
return quotient + 1n;
|
|
349
|
-
}
|
|
350
|
-
return quotient;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
// src/math/sqrtPriceMath.ts
|
|
354
|
-
var MAX_UINT128 = (1n << 128n) - 1n;
|
|
355
|
-
var MAX_UINT160 = (1n << 160n) - 1n;
|
|
356
|
-
var MAX_UINT2563 = (1n << 256n) - 1n;
|
|
357
|
-
var Q96_RESOLUTION = 96n;
|
|
358
|
-
var Q96 = 1n << 96n;
|
|
359
|
-
function assertUint160(x, name) {
|
|
360
|
-
if (x < 0n || x > MAX_UINT160) {
|
|
361
|
-
throw new Error(`SqrtPriceMath: ${name} out of uint160 range`);
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
function assertUint128(x, name) {
|
|
365
|
-
if (x < 0n || x > MAX_UINT128) {
|
|
366
|
-
throw new Error(`SqrtPriceMath: ${name} out of uint128 range`);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
function assertUint256(x, name) {
|
|
370
|
-
if (x < 0n || x > MAX_UINT2563) {
|
|
371
|
-
throw new Error(`SqrtPriceMath: ${name} out of uint256 range`);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
function divRoundingUp(x, y) {
|
|
375
|
-
if (y === 0n) throw new Error("SqrtPriceMath: div by zero");
|
|
376
|
-
return x / y + (x % y === 0n ? 0n : 1n);
|
|
377
|
-
}
|
|
378
|
-
function toUint160(x) {
|
|
379
|
-
if (x > MAX_UINT160) throw new Error("SqrtPriceMath: toUint160 overflow");
|
|
380
|
-
return x;
|
|
381
|
-
}
|
|
382
|
-
function getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amount, add) {
|
|
383
|
-
assertUint160(sqrtPX96, "sqrtPX96");
|
|
384
|
-
assertUint128(liquidity, "liquidity");
|
|
385
|
-
assertUint256(amount, "amount");
|
|
386
|
-
if (amount === 0n) return sqrtPX96;
|
|
387
|
-
const numerator1 = liquidity << Q96_RESOLUTION;
|
|
388
|
-
if (add) {
|
|
389
|
-
const product = amount * sqrtPX96 & MAX_UINT2563;
|
|
390
|
-
const noOverflow = amount === 0n || product / amount === sqrtPX96;
|
|
391
|
-
if (noOverflow) {
|
|
392
|
-
const denominator = numerator1 + product;
|
|
393
|
-
if (denominator >= numerator1) {
|
|
394
|
-
return toUint160(mulDivRoundingUp(numerator1, sqrtPX96, denominator));
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
if (sqrtPX96 === 0n) throw new Error("SqrtPriceMath: sqrtPX96 == 0");
|
|
398
|
-
const denom = numerator1 / sqrtPX96 + amount;
|
|
399
|
-
return toUint160(divRoundingUp(numerator1, denom));
|
|
400
|
-
} else {
|
|
401
|
-
const product = amount * sqrtPX96 & MAX_UINT2563;
|
|
402
|
-
const noOverflow = amount === 0n || product / amount === sqrtPX96;
|
|
403
|
-
if (!noOverflow || numerator1 <= product) {
|
|
404
|
-
throw new Error("SqrtPriceMath: getNextSqrtPriceFromAmount0 denom <= 0");
|
|
405
|
-
}
|
|
406
|
-
const denominator = numerator1 - product;
|
|
407
|
-
return toUint160(mulDivRoundingUp(numerator1, sqrtPX96, denominator));
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
function getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amount, add) {
|
|
411
|
-
assertUint160(sqrtPX96, "sqrtPX96");
|
|
412
|
-
assertUint128(liquidity, "liquidity");
|
|
413
|
-
assertUint256(amount, "amount");
|
|
414
|
-
if (add) {
|
|
415
|
-
const quotient = amount <= MAX_UINT160 ? (amount << Q96_RESOLUTION) / liquidity : mulDiv(amount, Q96, liquidity);
|
|
416
|
-
return toUint160(sqrtPX96 + quotient);
|
|
417
|
-
} else {
|
|
418
|
-
const quotient = amount <= MAX_UINT160 ? divRoundingUp(amount << Q96_RESOLUTION, liquidity) : mulDivRoundingUp(amount, Q96, liquidity);
|
|
419
|
-
if (sqrtPX96 <= quotient) {
|
|
420
|
-
throw new Error("SqrtPriceMath: sqrtPX96 <= quotient");
|
|
421
|
-
}
|
|
422
|
-
return sqrtPX96 - quotient;
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
function getNextSqrtPriceFromInput(sqrtPX96, liquidity, amountIn, zeroForOne) {
|
|
426
|
-
if (sqrtPX96 <= 0n) throw new Error("SqrtPriceMath: sqrtPX96 must be > 0");
|
|
427
|
-
if (liquidity <= 0n)
|
|
428
|
-
throw new Error("SqrtPriceMath: liquidity must be > 0");
|
|
429
|
-
return zeroForOne ? getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true) : getNextSqrtPriceFromAmount1RoundingDown(
|
|
430
|
-
sqrtPX96,
|
|
431
|
-
liquidity,
|
|
432
|
-
amountIn,
|
|
433
|
-
true
|
|
434
|
-
);
|
|
435
|
-
}
|
|
436
|
-
function getNextSqrtPriceFromOutput(sqrtPX96, liquidity, amountOut, zeroForOne) {
|
|
437
|
-
if (sqrtPX96 <= 0n) throw new Error("SqrtPriceMath: sqrtPX96 must be > 0");
|
|
438
|
-
if (liquidity <= 0n)
|
|
439
|
-
throw new Error("SqrtPriceMath: liquidity must be > 0");
|
|
440
|
-
return zeroForOne ? getNextSqrtPriceFromAmount1RoundingDown(
|
|
441
|
-
sqrtPX96,
|
|
442
|
-
liquidity,
|
|
443
|
-
amountOut,
|
|
444
|
-
false
|
|
445
|
-
) : getNextSqrtPriceFromAmount0RoundingUp(
|
|
446
|
-
sqrtPX96,
|
|
447
|
-
liquidity,
|
|
448
|
-
amountOut,
|
|
449
|
-
false
|
|
450
|
-
);
|
|
451
|
-
}
|
|
452
|
-
function getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, roundUp) {
|
|
453
|
-
assertUint160(sqrtRatioAX96, "sqrtRatioAX96");
|
|
454
|
-
assertUint160(sqrtRatioBX96, "sqrtRatioBX96");
|
|
455
|
-
assertUint128(liquidity, "liquidity");
|
|
456
|
-
let a = sqrtRatioAX96;
|
|
457
|
-
let b = sqrtRatioBX96;
|
|
458
|
-
if (a > b) {
|
|
459
|
-
[a, b] = [b, a];
|
|
460
|
-
}
|
|
461
|
-
if (a === 0n) throw new Error("SqrtPriceMath: sqrtRatio must be > 0");
|
|
462
|
-
const numerator1 = liquidity << Q96_RESOLUTION;
|
|
463
|
-
const numerator2 = b - a;
|
|
464
|
-
if (roundUp) {
|
|
465
|
-
return divRoundingUp(mulDivRoundingUp(numerator1, numerator2, b), a);
|
|
466
|
-
}
|
|
467
|
-
return mulDiv(numerator1, numerator2, b) / a;
|
|
468
|
-
}
|
|
469
|
-
function getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, roundUp) {
|
|
470
|
-
assertUint160(sqrtRatioAX96, "sqrtRatioAX96");
|
|
471
|
-
assertUint160(sqrtRatioBX96, "sqrtRatioBX96");
|
|
472
|
-
assertUint128(liquidity, "liquidity");
|
|
473
|
-
let a = sqrtRatioAX96;
|
|
474
|
-
let b = sqrtRatioBX96;
|
|
475
|
-
if (a > b) {
|
|
476
|
-
[a, b] = [b, a];
|
|
477
|
-
}
|
|
478
|
-
return roundUp ? mulDivRoundingUp(liquidity, b - a, Q96) : mulDiv(liquidity, b - a, Q96);
|
|
479
|
-
}
|
|
480
|
-
function getAmount0DeltaSigned(sqrtRatioAX96, sqrtRatioBX96, liquidity) {
|
|
481
|
-
const INT128_MIN2 = -(1n << 127n);
|
|
482
|
-
const INT128_MAX2 = (1n << 127n) - 1n;
|
|
483
|
-
if (liquidity < INT128_MIN2 || liquidity > INT128_MAX2) {
|
|
484
|
-
throw new Error("SqrtPriceMath: liquidity out of int128 range");
|
|
485
|
-
}
|
|
486
|
-
if (liquidity < 0n) {
|
|
487
|
-
return -getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, -liquidity, false);
|
|
488
|
-
}
|
|
489
|
-
return getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, true);
|
|
490
|
-
}
|
|
491
|
-
function getAmount1DeltaSigned(sqrtRatioAX96, sqrtRatioBX96, liquidity) {
|
|
492
|
-
const INT128_MIN2 = -(1n << 127n);
|
|
493
|
-
const INT128_MAX2 = (1n << 127n) - 1n;
|
|
494
|
-
if (liquidity < INT128_MIN2 || liquidity > INT128_MAX2) {
|
|
495
|
-
throw new Error("SqrtPriceMath: liquidity out of int128 range");
|
|
496
|
-
}
|
|
497
|
-
if (liquidity < 0n) {
|
|
498
|
-
return -getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, -liquidity, false);
|
|
499
|
-
}
|
|
500
|
-
return getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, true);
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
// src/math/liquidityMath.ts
|
|
504
|
-
var MAX_UINT1282 = (1n << 128n) - 1n;
|
|
505
|
-
var INT128_MAX = (1n << 127n) - 1n;
|
|
506
|
-
var INT128_MIN = -(1n << 127n);
|
|
507
|
-
function addDelta(x, y) {
|
|
508
|
-
if (x < 0n || x > MAX_UINT1282) {
|
|
509
|
-
throw new Error("LiquidityMath: x out of uint128 range");
|
|
510
|
-
}
|
|
511
|
-
if (y < INT128_MIN || y > INT128_MAX) {
|
|
512
|
-
throw new Error("LiquidityMath: y out of int128 range");
|
|
513
|
-
}
|
|
514
|
-
const z = x + y;
|
|
515
|
-
if (y < 0n) {
|
|
516
|
-
if (z < 0n) throw new Error("LS");
|
|
517
|
-
} else {
|
|
518
|
-
if (z > MAX_UINT1282) throw new Error("LA");
|
|
519
|
-
}
|
|
520
|
-
return z;
|
|
521
|
-
}
|
|
522
|
-
|
|
523
162
|
// src/kernel/reader.ts
|
|
524
163
|
var import_amm_bindings = require("@skate-org/amm-bindings");
|
|
525
164
|
var import_amm_core_v23 = require("@skate-org/amm-core-v2");
|
|
@@ -628,28 +267,28 @@ async function computePositionAmounts(tokenId, mode, client) {
|
|
|
628
267
|
const c = resolveClient(mode, client);
|
|
629
268
|
const position = await readPosition(tokenId, mode, c);
|
|
630
269
|
const { slot0 } = await readPool(position.pool, mode, c);
|
|
631
|
-
const sqrtLower = getSqrtRatioAtTick(position.tickLower);
|
|
632
|
-
const sqrtUpper = getSqrtRatioAtTick(position.tickUpper);
|
|
270
|
+
const sqrtLower = (0, import_amm_core_v23.getSqrtRatioAtTick)(position.tickLower);
|
|
271
|
+
const sqrtUpper = (0, import_amm_core_v23.getSqrtRatioAtTick)(position.tickUpper);
|
|
633
272
|
if (slot0.tick < position.tickLower) {
|
|
634
273
|
return {
|
|
635
|
-
amount0: getAmount0Delta(sqrtLower, sqrtUpper, position.liquidity, false),
|
|
274
|
+
amount0: (0, import_amm_core_v23.getAmount0Delta)(sqrtLower, sqrtUpper, position.liquidity, false),
|
|
636
275
|
amount1: 0n
|
|
637
276
|
};
|
|
638
277
|
}
|
|
639
278
|
if (slot0.tick >= position.tickUpper) {
|
|
640
279
|
return {
|
|
641
280
|
amount0: 0n,
|
|
642
|
-
amount1: getAmount1Delta(sqrtLower, sqrtUpper, position.liquidity, false)
|
|
281
|
+
amount1: (0, import_amm_core_v23.getAmount1Delta)(sqrtLower, sqrtUpper, position.liquidity, false)
|
|
643
282
|
};
|
|
644
283
|
}
|
|
645
284
|
return {
|
|
646
|
-
amount0: getAmount0Delta(
|
|
285
|
+
amount0: (0, import_amm_core_v23.getAmount0Delta)(
|
|
647
286
|
slot0.sqrtPriceX96,
|
|
648
287
|
sqrtUpper,
|
|
649
288
|
position.liquidity,
|
|
650
289
|
false
|
|
651
290
|
),
|
|
652
|
-
amount1: getAmount1Delta(
|
|
291
|
+
amount1: (0, import_amm_core_v23.getAmount1Delta)(
|
|
653
292
|
sqrtLower,
|
|
654
293
|
slot0.sqrtPriceX96,
|
|
655
294
|
position.liquidity,
|
|
@@ -956,30 +595,13 @@ function watchPeripheryEvents(client, address, opts) {
|
|
|
956
595
|
EvmReadError,
|
|
957
596
|
EvmWriteError,
|
|
958
597
|
KERNEL_EVENT_NAMES,
|
|
959
|
-
MAX_SQRT_RATIO,
|
|
960
|
-
MAX_TICK,
|
|
961
|
-
MIN_SQRT_RATIO,
|
|
962
|
-
MIN_TICK,
|
|
963
598
|
PERIPHERY_EVENT_NAMES,
|
|
964
|
-
addDelta,
|
|
965
599
|
computePositionAmounts,
|
|
966
600
|
createDevConfig,
|
|
967
601
|
decodeTickBitmapWord,
|
|
968
|
-
getAmount0Delta,
|
|
969
|
-
getAmount0DeltaSigned,
|
|
970
|
-
getAmount1Delta,
|
|
971
|
-
getAmount1DeltaSigned,
|
|
972
602
|
getKernelPublicClient,
|
|
973
|
-
getNextSqrtPriceFromAmount0RoundingUp,
|
|
974
|
-
getNextSqrtPriceFromAmount1RoundingDown,
|
|
975
|
-
getNextSqrtPriceFromInput,
|
|
976
|
-
getNextSqrtPriceFromOutput,
|
|
977
603
|
getSourcePublicClient,
|
|
978
|
-
getSqrtRatioAtTick,
|
|
979
|
-
getTickAtSqrtRatio,
|
|
980
604
|
megaethChain,
|
|
981
|
-
mulDiv,
|
|
982
|
-
mulDivRoundingUp,
|
|
983
605
|
parseKernelEventLog,
|
|
984
606
|
parsePeripheryEventLog,
|
|
985
607
|
readPeripheryPool,
|