@skate-org/amm-evm-v2 0.4.1 → 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/dist/index.js CHANGED
@@ -120,350 +120,6 @@ function wrapWriteError(err, functionName) {
120
120
  return new EvmWriteError(functionName, message, { cause: err });
121
121
  }
122
122
 
123
- // src/math/tickMath.ts
124
- var MAX_UINT256 = (1n << 256n) - 1n;
125
- var Q160_MASK = (1n << 160n) - 1n;
126
- var Q256_MOD = 1n << 256n;
127
- var MIN_TICK = -887272;
128
- var MAX_TICK = 887272;
129
- var MIN_SQRT_RATIO = 4295128739n;
130
- var MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342n;
131
- function getSqrtRatioAtTick(tick) {
132
- if (!Number.isInteger(tick)) {
133
- throw new Error("TickMath: tick must be an integer");
134
- }
135
- const absTick = BigInt(tick < 0 ? -tick : tick);
136
- if (absTick > BigInt(MAX_TICK)) {
137
- throw new Error("T");
138
- }
139
- let ratio = (absTick & 0x1n) !== 0n ? 0xfffcb933bd6fad37aa2d162d1a594001n : 0x100000000000000000000000000000000n;
140
- if ((absTick & 0x2n) !== 0n)
141
- ratio = (ratio * 0xfff97272373d413259a46990580e213an & MAX_UINT256) >> 128n;
142
- if ((absTick & 0x4n) !== 0n)
143
- ratio = (ratio * 0xfff2e50f5f656932ef12357cf3c7fdccn & MAX_UINT256) >> 128n;
144
- if ((absTick & 0x8n) !== 0n)
145
- ratio = (ratio * 0xffe5caca7e10e4e61c3624eaa0941cd0n & MAX_UINT256) >> 128n;
146
- if ((absTick & 0x10n) !== 0n)
147
- ratio = (ratio * 0xffcb9843d60f6159c9db58835c926644n & MAX_UINT256) >> 128n;
148
- if ((absTick & 0x20n) !== 0n)
149
- ratio = (ratio * 0xff973b41fa98c081472e6896dfb254c0n & MAX_UINT256) >> 128n;
150
- if ((absTick & 0x40n) !== 0n)
151
- ratio = (ratio * 0xff2ea16466c96a3843ec78b326b52861n & MAX_UINT256) >> 128n;
152
- if ((absTick & 0x80n) !== 0n)
153
- ratio = (ratio * 0xfe5dee046a99a2a811c461f1969c3053n & MAX_UINT256) >> 128n;
154
- if ((absTick & 0x100n) !== 0n)
155
- ratio = (ratio * 0xfcbe86c7900a88aedcffc83b479aa3a4n & MAX_UINT256) >> 128n;
156
- if ((absTick & 0x200n) !== 0n)
157
- ratio = (ratio * 0xf987a7253ac413176f2b074cf7815e54n & MAX_UINT256) >> 128n;
158
- if ((absTick & 0x400n) !== 0n)
159
- ratio = (ratio * 0xf3392b0822b70005940c7a398e4b70f3n & MAX_UINT256) >> 128n;
160
- if ((absTick & 0x800n) !== 0n)
161
- ratio = (ratio * 0xe7159475a2c29b7443b29c7fa6e889d9n & MAX_UINT256) >> 128n;
162
- if ((absTick & 0x1000n) !== 0n)
163
- ratio = (ratio * 0xd097f3bdfd2022b8845ad8f792aa5825n & MAX_UINT256) >> 128n;
164
- if ((absTick & 0x2000n) !== 0n)
165
- ratio = (ratio * 0xa9f746462d870fdf8a65dc1f90e061e5n & MAX_UINT256) >> 128n;
166
- if ((absTick & 0x4000n) !== 0n)
167
- ratio = (ratio * 0x70d869a156d2a1b890bb3df62baf32f7n & MAX_UINT256) >> 128n;
168
- if ((absTick & 0x8000n) !== 0n)
169
- ratio = (ratio * 0x31be135f97d08fd981231505542fcfa6n & MAX_UINT256) >> 128n;
170
- if ((absTick & 0x10000n) !== 0n)
171
- ratio = (ratio * 0x9aa508b5b7a84e1c677de54f3e99bc9n & MAX_UINT256) >> 128n;
172
- if ((absTick & 0x20000n) !== 0n)
173
- ratio = (ratio * 0x5d6af8dedb81196699c329225ee604n & MAX_UINT256) >> 128n;
174
- if ((absTick & 0x40000n) !== 0n)
175
- ratio = (ratio * 0x2216e584f5fa1ea926041bedfe98n & MAX_UINT256) >> 128n;
176
- if ((absTick & 0x80000n) !== 0n)
177
- ratio = (ratio * 0x48a170391f7dc42444e8fa2n & MAX_UINT256) >> 128n;
178
- if (tick > 0) ratio = MAX_UINT256 / ratio;
179
- const shifted = ratio >> 32n;
180
- const remainder = ratio & (1n << 32n) - 1n;
181
- const sqrtPriceX96 = remainder === 0n ? shifted : shifted + 1n;
182
- return sqrtPriceX96 & Q160_MASK;
183
- }
184
- function getTickAtSqrtRatio(sqrtPriceX96) {
185
- if (sqrtPriceX96 < MIN_SQRT_RATIO || sqrtPriceX96 >= MAX_SQRT_RATIO) {
186
- throw new Error("R");
187
- }
188
- const ratio = sqrtPriceX96 << 32n;
189
- let r = ratio;
190
- let msb = 0n;
191
- {
192
- const f = r > 0xffffffffffffffffffffffffffffffffn ? 128n : 0n;
193
- msb |= f;
194
- r >>= f;
195
- }
196
- {
197
- const f = r > 0xffffffffffffffffn ? 64n : 0n;
198
- msb |= f;
199
- r >>= f;
200
- }
201
- {
202
- const f = r > 0xffffffffn ? 32n : 0n;
203
- msb |= f;
204
- r >>= f;
205
- }
206
- {
207
- const f = r > 0xffffn ? 16n : 0n;
208
- msb |= f;
209
- r >>= f;
210
- }
211
- {
212
- const f = r > 0xffn ? 8n : 0n;
213
- msb |= f;
214
- r >>= f;
215
- }
216
- {
217
- const f = r > 0xfn ? 4n : 0n;
218
- msb |= f;
219
- r >>= f;
220
- }
221
- {
222
- const f = r > 0x3n ? 2n : 0n;
223
- msb |= f;
224
- r >>= f;
225
- }
226
- {
227
- const f = r > 0x1n ? 1n : 0n;
228
- msb |= f;
229
- }
230
- if (msb >= 128n) r = ratio >> msb - 127n;
231
- else r = ratio << 127n - msb & MAX_UINT256;
232
- const offset = msb - 128n;
233
- let log_2 = offset << 64n & MAX_UINT256;
234
- for (let i = 0; i < 14; i++) {
235
- r = r * r >> 127n;
236
- const f = r >> 128n;
237
- log_2 |= f << BigInt(63 - i);
238
- r >>= f;
239
- }
240
- const log_2_signed = toInt256(log_2);
241
- const log_sqrt10001 = log_2_signed * 255738958999603826347141n;
242
- const tickLowBig = log_sqrt10001 - 3402992956809132418596140100660247210n >> 128n;
243
- const tickHiBig = log_sqrt10001 + 291339464771989622907027621153398088495n >> 128n;
244
- const tickLow = toInt24(tickLowBig);
245
- const tickHi = toInt24(tickHiBig);
246
- if (tickLow === tickHi) return tickLow;
247
- return getSqrtRatioAtTick(tickHi) <= sqrtPriceX96 ? tickHi : tickLow;
248
- }
249
- function toInt256(x) {
250
- const masked = x & MAX_UINT256;
251
- return masked >= 1n << 255n ? masked - Q256_MOD : masked;
252
- }
253
- function toInt24(x) {
254
- const mask = (1n << 24n) - 1n;
255
- const truncated = x & mask;
256
- const signed = truncated >= 1n << 23n ? truncated - (1n << 24n) : truncated;
257
- return Number(signed);
258
- }
259
-
260
- // src/math/fullMath.ts
261
- var MAX_UINT2562 = (1n << 256n) - 1n;
262
- function mulDiv(a, b, denom) {
263
- if (denom === 0n) {
264
- throw new Error("FullMath: division by zero");
265
- }
266
- if (a < 0n || b < 0n || denom < 0n) {
267
- throw new Error("FullMath: negative input");
268
- }
269
- const product = a * b;
270
- const result = product / denom;
271
- if (result > MAX_UINT2562) {
272
- throw new Error("FullMath: result overflows uint256");
273
- }
274
- return result;
275
- }
276
- function mulDivRoundingUp(a, b, denom) {
277
- if (denom === 0n) {
278
- throw new Error("FullMath: division by zero");
279
- }
280
- if (a < 0n || b < 0n || denom < 0n) {
281
- throw new Error("FullMath: negative input");
282
- }
283
- const product = a * b;
284
- const quotient = product / denom;
285
- if (quotient > MAX_UINT2562) {
286
- throw new Error("FullMath: result overflows uint256");
287
- }
288
- if (product % denom !== 0n) {
289
- if (quotient === MAX_UINT2562) {
290
- throw new Error("FullMath: rounding up overflows uint256");
291
- }
292
- return quotient + 1n;
293
- }
294
- return quotient;
295
- }
296
-
297
- // src/math/sqrtPriceMath.ts
298
- var MAX_UINT128 = (1n << 128n) - 1n;
299
- var MAX_UINT160 = (1n << 160n) - 1n;
300
- var MAX_UINT2563 = (1n << 256n) - 1n;
301
- var Q96_RESOLUTION = 96n;
302
- var Q96 = 1n << 96n;
303
- function assertUint160(x, name) {
304
- if (x < 0n || x > MAX_UINT160) {
305
- throw new Error(`SqrtPriceMath: ${name} out of uint160 range`);
306
- }
307
- }
308
- function assertUint128(x, name) {
309
- if (x < 0n || x > MAX_UINT128) {
310
- throw new Error(`SqrtPriceMath: ${name} out of uint128 range`);
311
- }
312
- }
313
- function assertUint256(x, name) {
314
- if (x < 0n || x > MAX_UINT2563) {
315
- throw new Error(`SqrtPriceMath: ${name} out of uint256 range`);
316
- }
317
- }
318
- function divRoundingUp(x, y) {
319
- if (y === 0n) throw new Error("SqrtPriceMath: div by zero");
320
- return x / y + (x % y === 0n ? 0n : 1n);
321
- }
322
- function toUint160(x) {
323
- if (x > MAX_UINT160) throw new Error("SqrtPriceMath: toUint160 overflow");
324
- return x;
325
- }
326
- function getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amount, add) {
327
- assertUint160(sqrtPX96, "sqrtPX96");
328
- assertUint128(liquidity, "liquidity");
329
- assertUint256(amount, "amount");
330
- if (amount === 0n) return sqrtPX96;
331
- const numerator1 = liquidity << Q96_RESOLUTION;
332
- if (add) {
333
- const product = amount * sqrtPX96 & MAX_UINT2563;
334
- const noOverflow = amount === 0n || product / amount === sqrtPX96;
335
- if (noOverflow) {
336
- const denominator = numerator1 + product;
337
- if (denominator >= numerator1) {
338
- return toUint160(mulDivRoundingUp(numerator1, sqrtPX96, denominator));
339
- }
340
- }
341
- if (sqrtPX96 === 0n) throw new Error("SqrtPriceMath: sqrtPX96 == 0");
342
- const denom = numerator1 / sqrtPX96 + amount;
343
- return toUint160(divRoundingUp(numerator1, denom));
344
- } else {
345
- const product = amount * sqrtPX96 & MAX_UINT2563;
346
- const noOverflow = amount === 0n || product / amount === sqrtPX96;
347
- if (!noOverflow || numerator1 <= product) {
348
- throw new Error("SqrtPriceMath: getNextSqrtPriceFromAmount0 denom <= 0");
349
- }
350
- const denominator = numerator1 - product;
351
- return toUint160(mulDivRoundingUp(numerator1, sqrtPX96, denominator));
352
- }
353
- }
354
- function getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amount, add) {
355
- assertUint160(sqrtPX96, "sqrtPX96");
356
- assertUint128(liquidity, "liquidity");
357
- assertUint256(amount, "amount");
358
- if (add) {
359
- const quotient = amount <= MAX_UINT160 ? (amount << Q96_RESOLUTION) / liquidity : mulDiv(amount, Q96, liquidity);
360
- return toUint160(sqrtPX96 + quotient);
361
- } else {
362
- const quotient = amount <= MAX_UINT160 ? divRoundingUp(amount << Q96_RESOLUTION, liquidity) : mulDivRoundingUp(amount, Q96, liquidity);
363
- if (sqrtPX96 <= quotient) {
364
- throw new Error("SqrtPriceMath: sqrtPX96 <= quotient");
365
- }
366
- return sqrtPX96 - quotient;
367
- }
368
- }
369
- function getNextSqrtPriceFromInput(sqrtPX96, liquidity, amountIn, zeroForOne) {
370
- if (sqrtPX96 <= 0n) throw new Error("SqrtPriceMath: sqrtPX96 must be > 0");
371
- if (liquidity <= 0n)
372
- throw new Error("SqrtPriceMath: liquidity must be > 0");
373
- return zeroForOne ? getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true) : getNextSqrtPriceFromAmount1RoundingDown(
374
- sqrtPX96,
375
- liquidity,
376
- amountIn,
377
- true
378
- );
379
- }
380
- function getNextSqrtPriceFromOutput(sqrtPX96, liquidity, amountOut, zeroForOne) {
381
- if (sqrtPX96 <= 0n) throw new Error("SqrtPriceMath: sqrtPX96 must be > 0");
382
- if (liquidity <= 0n)
383
- throw new Error("SqrtPriceMath: liquidity must be > 0");
384
- return zeroForOne ? getNextSqrtPriceFromAmount1RoundingDown(
385
- sqrtPX96,
386
- liquidity,
387
- amountOut,
388
- false
389
- ) : getNextSqrtPriceFromAmount0RoundingUp(
390
- sqrtPX96,
391
- liquidity,
392
- amountOut,
393
- false
394
- );
395
- }
396
- function getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, roundUp) {
397
- assertUint160(sqrtRatioAX96, "sqrtRatioAX96");
398
- assertUint160(sqrtRatioBX96, "sqrtRatioBX96");
399
- assertUint128(liquidity, "liquidity");
400
- let a = sqrtRatioAX96;
401
- let b = sqrtRatioBX96;
402
- if (a > b) {
403
- [a, b] = [b, a];
404
- }
405
- if (a === 0n) throw new Error("SqrtPriceMath: sqrtRatio must be > 0");
406
- const numerator1 = liquidity << Q96_RESOLUTION;
407
- const numerator2 = b - a;
408
- if (roundUp) {
409
- return divRoundingUp(mulDivRoundingUp(numerator1, numerator2, b), a);
410
- }
411
- return mulDiv(numerator1, numerator2, b) / a;
412
- }
413
- function getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, roundUp) {
414
- assertUint160(sqrtRatioAX96, "sqrtRatioAX96");
415
- assertUint160(sqrtRatioBX96, "sqrtRatioBX96");
416
- assertUint128(liquidity, "liquidity");
417
- let a = sqrtRatioAX96;
418
- let b = sqrtRatioBX96;
419
- if (a > b) {
420
- [a, b] = [b, a];
421
- }
422
- return roundUp ? mulDivRoundingUp(liquidity, b - a, Q96) : mulDiv(liquidity, b - a, Q96);
423
- }
424
- function getAmount0DeltaSigned(sqrtRatioAX96, sqrtRatioBX96, liquidity) {
425
- const INT128_MIN2 = -(1n << 127n);
426
- const INT128_MAX2 = (1n << 127n) - 1n;
427
- if (liquidity < INT128_MIN2 || liquidity > INT128_MAX2) {
428
- throw new Error("SqrtPriceMath: liquidity out of int128 range");
429
- }
430
- if (liquidity < 0n) {
431
- return -getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, -liquidity, false);
432
- }
433
- return getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, true);
434
- }
435
- function getAmount1DeltaSigned(sqrtRatioAX96, sqrtRatioBX96, liquidity) {
436
- const INT128_MIN2 = -(1n << 127n);
437
- const INT128_MAX2 = (1n << 127n) - 1n;
438
- if (liquidity < INT128_MIN2 || liquidity > INT128_MAX2) {
439
- throw new Error("SqrtPriceMath: liquidity out of int128 range");
440
- }
441
- if (liquidity < 0n) {
442
- return -getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, -liquidity, false);
443
- }
444
- return getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, true);
445
- }
446
-
447
- // src/math/liquidityMath.ts
448
- var MAX_UINT1282 = (1n << 128n) - 1n;
449
- var INT128_MAX = (1n << 127n) - 1n;
450
- var INT128_MIN = -(1n << 127n);
451
- function addDelta(x, y) {
452
- if (x < 0n || x > MAX_UINT1282) {
453
- throw new Error("LiquidityMath: x out of uint128 range");
454
- }
455
- if (y < INT128_MIN || y > INT128_MAX) {
456
- throw new Error("LiquidityMath: y out of int128 range");
457
- }
458
- const z = x + y;
459
- if (y < 0n) {
460
- if (z < 0n) throw new Error("LS");
461
- } else {
462
- if (z > MAX_UINT1282) throw new Error("LA");
463
- }
464
- return z;
465
- }
466
-
467
123
  // src/kernel/reader.ts
468
124
  import {
469
125
  KernelManagerABI,
@@ -471,7 +127,10 @@ import {
471
127
  } from "@skate-org/amm-bindings";
472
128
  import {
473
129
  KernelManagerAddress,
474
- normalizeMode as normalizeMode2
130
+ normalizeMode as normalizeMode2,
131
+ getAmount0Delta,
132
+ getAmount1Delta,
133
+ getSqrtRatioAtTick
475
134
  } from "@skate-org/amm-core-v2";
476
135
  function resolveClient(mode, client) {
477
136
  return client ?? getKernelPublicClient(mode);
@@ -911,30 +570,13 @@ export {
911
570
  EvmReadError,
912
571
  EvmWriteError,
913
572
  KERNEL_EVENT_NAMES,
914
- MAX_SQRT_RATIO,
915
- MAX_TICK,
916
- MIN_SQRT_RATIO,
917
- MIN_TICK,
918
573
  PERIPHERY_EVENT_NAMES,
919
- addDelta,
920
574
  computePositionAmounts,
921
575
  createDevConfig,
922
576
  decodeTickBitmapWord,
923
- getAmount0Delta,
924
- getAmount0DeltaSigned,
925
- getAmount1Delta,
926
- getAmount1DeltaSigned,
927
577
  getKernelPublicClient,
928
- getNextSqrtPriceFromAmount0RoundingUp,
929
- getNextSqrtPriceFromAmount1RoundingDown,
930
- getNextSqrtPriceFromInput,
931
- getNextSqrtPriceFromOutput,
932
578
  getSourcePublicClient,
933
- getSqrtRatioAtTick,
934
- getTickAtSqrtRatio,
935
579
  megaethChain,
936
- mulDiv,
937
- mulDivRoundingUp,
938
580
  parseKernelEventLog,
939
581
  parsePeripheryEventLog,
940
582
  readPeripheryPool,