@ekubo/sdk 0.0.8-alpha.0

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.
@@ -0,0 +1 @@
1
+ export * from "./math";
package/dist/index.js ADDED
@@ -0,0 +1,849 @@
1
+ // src/math/constants.ts
2
+ var MAX_U256 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn;
3
+ var MAX_U128 = 0xffffffffffffffffffffffffffffffffn;
4
+
5
+ // src/math/delta.ts
6
+ function amount0Delta(sqrtRatioA, sqrtRatioB, liquidity, roundUp) {
7
+ if (liquidity === 0n || sqrtRatioA === sqrtRatioB)
8
+ return 0n;
9
+ const [lower, upper] = sqrtRatioA < sqrtRatioB ? [sqrtRatioA, sqrtRatioB] : [sqrtRatioB, sqrtRatioA];
10
+ const numerator = (liquidity << 128n) * (upper - lower);
11
+ let result0 = numerator / upper;
12
+ if (roundUp && numerator % upper !== 0n) {
13
+ result0++;
14
+ }
15
+ if (result0 > MAX_U256) {
16
+ throw new Error("AMOUNT0_DELTA_OVERFLOW_U256");
17
+ }
18
+ let result = result0 / lower;
19
+ if (roundUp && result % lower !== 0n) {
20
+ result++;
21
+ }
22
+ if (result > MAX_U128) {
23
+ throw new Error("AMOUNT0_DELTA_OVERFLOW_U128");
24
+ }
25
+ return result;
26
+ }
27
+ var TWO_POW_128 = 0x100000000000000000000000000000000n;
28
+ function amount1Delta(sqrtRatioA, sqrtRatioB, liquidity, roundUp) {
29
+ if (liquidity === 0n || sqrtRatioA === sqrtRatioB)
30
+ return 0n;
31
+ const [lower, upper] = sqrtRatioA < sqrtRatioB ? [sqrtRatioA, sqrtRatioB] : [sqrtRatioB, sqrtRatioA];
32
+ const result = liquidity * (upper - lower);
33
+ if (result > MAX_U256) {
34
+ throw new Error("AMOUNT1_DELTA_OVERFLOW_U256");
35
+ }
36
+ if (roundUp && result % TWO_POW_128 !== 0n) {
37
+ const delta = result / TWO_POW_128 + 1n;
38
+ if (delta > MAX_U128) {
39
+ throw new Error("AMOUNT1_DELTA_OVERFLOW_U128");
40
+ }
41
+ return delta;
42
+ } else {
43
+ return result >> 128n;
44
+ }
45
+ }
46
+
47
+ // src/math/msb.ts
48
+ var MAX = 1n << 128n;
49
+ function msb(x) {
50
+ if (x >= MAX) {
51
+ throw new Error("x too large");
52
+ }
53
+ if (x <= 0n) {
54
+ throw new Error("x must be positive");
55
+ }
56
+ let res = 0;
57
+ if (x >= 0x10000000000000000n) {
58
+ x = x >> 64n;
59
+ res += 64;
60
+ }
61
+ if (x >= 0x100000000n) {
62
+ x = x >> 32n;
63
+ res += 32;
64
+ }
65
+ if (x >= 0x10000n) {
66
+ x = x >> 16n;
67
+ res += 16;
68
+ }
69
+ if (x >= 0x100n) {
70
+ x = x >> 8n;
71
+ res += 8;
72
+ }
73
+ if (x >= 0x10n) {
74
+ x = x >> 4n;
75
+ res += 4;
76
+ }
77
+ if (x >= 0x04n) {
78
+ x = x >> 2n;
79
+ res += 2;
80
+ }
81
+ if (x >= 0x02n) {
82
+ res += 1;
83
+ }
84
+ return res;
85
+ }
86
+
87
+ // src/math/price.ts
88
+ var toBig = (value, name) => {
89
+ if (typeof value === "bigint")
90
+ return value;
91
+ try {
92
+ return BigInt(value);
93
+ } catch (err) {
94
+ const printed = value === null || value === undefined ? String(value) : value.toString();
95
+ const ctor = value && typeof value === "object" && "constructor" in value ? value.constructor.name : undefined;
96
+ throw new TypeError(`${name} must be bigint-compatible, received ${printed} (type ${typeof value}${ctor ? `, ctor ${ctor}` : ""})`);
97
+ }
98
+ };
99
+ function nextSqrtRatioFromAmount0(sqrtRatio, liquidity, amount0) {
100
+ const sqrtRatioFixed = toBig(sqrtRatio, "sqrtRatio");
101
+ const liquidityFixed = toBig(liquidity, "liquidity");
102
+ const amount0Fixed = toBig(amount0, "amount0");
103
+ if (amount0Fixed === 0n)
104
+ return sqrtRatioFixed;
105
+ if (liquidityFixed === 0n)
106
+ throw new Error("NO_LIQUIDITY");
107
+ const numerator1 = liquidityFixed << 128n;
108
+ if (amount0Fixed < 0n) {
109
+ const product = amount0Fixed * -1n * sqrtRatioFixed;
110
+ if (product >= MAX_U256) {
111
+ return null;
112
+ }
113
+ const denominator = numerator1 - product;
114
+ if (denominator < 0n) {
115
+ return null;
116
+ }
117
+ const num = numerator1 * sqrtRatioFixed;
118
+ const result = num / denominator + (num % denominator === 0n ? 0n : 1n);
119
+ if (result > MAX_U256) {
120
+ return null;
121
+ }
122
+ return result;
123
+ } else {
124
+ const denomP1 = numerator1 / sqrtRatioFixed;
125
+ const denom = denomP1 + amount0Fixed;
126
+ const quotient = numerator1 / denom;
127
+ const remainder = numerator1 % denom;
128
+ if (remainder === 0n)
129
+ return quotient;
130
+ const sum = quotient + 1n;
131
+ if (sum > MAX_U256)
132
+ return null;
133
+ return sum;
134
+ }
135
+ }
136
+ function nextSqrtRatioFromAmount1(sqrtRatio, liquidity, amount1) {
137
+ const sqrtRatioFixed = toBig(sqrtRatio, "sqrtRatio");
138
+ const liquidityFixed = toBig(liquidity, "liquidity");
139
+ const amount1Fixed = toBig(amount1, "amount1");
140
+ if (amount1Fixed === 0n)
141
+ return sqrtRatioFixed;
142
+ if (liquidityFixed === 0n)
143
+ throw new Error("NO_LIQUIDITY");
144
+ const amountShifted = amount1Fixed << 128n;
145
+ const quotient = amountShifted / liquidityFixed;
146
+ const remainder = amountShifted % liquidityFixed;
147
+ if (amount1Fixed < 0n) {
148
+ const res = sqrtRatioFixed + quotient;
149
+ if (res < 0n) {
150
+ return null;
151
+ }
152
+ if (remainder === 0n) {
153
+ return res;
154
+ } else {
155
+ if (res != 0n) {
156
+ return res - 1n;
157
+ } else {
158
+ return null;
159
+ }
160
+ }
161
+ } else {
162
+ const res = sqrtRatioFixed + quotient;
163
+ if (res > MAX_U256) {
164
+ return null;
165
+ } else {
166
+ return res;
167
+ }
168
+ }
169
+ }
170
+
171
+ // src/math/swap.ts
172
+ function isPriceIncreasing(amount, isToken1) {
173
+ return amount < 0n !== isToken1;
174
+ }
175
+ function noOp(sqrtRatioNext) {
176
+ return {
177
+ consumedAmount: 0n,
178
+ calculatedAmount: 0n,
179
+ sqrtRatioNext,
180
+ feeAmount: 0n
181
+ };
182
+ }
183
+ function amountBeforeFee(amount, fee) {
184
+ if (fee === 0n)
185
+ return amount;
186
+ const num = amount << 128n;
187
+ const denom = (1n << 128n) - fee;
188
+ const val = num / denom;
189
+ const result = val + (num % denom !== 0n ? 1n : 0n);
190
+ if (result > MAX_U128)
191
+ throw new Error("AMOUNT_BEFORE_FEE_OVERFLOW");
192
+ return result;
193
+ }
194
+ function computeFee(amount, fee) {
195
+ const num = amount * fee;
196
+ const denom = 2n ** 128n;
197
+ if (num % denom !== 0n) {
198
+ return num / denom + 1n;
199
+ } else {
200
+ return num / denom;
201
+ }
202
+ }
203
+ function computeStep({
204
+ sqrtRatio,
205
+ liquidity,
206
+ sqrtRatioLimit,
207
+ amount,
208
+ isToken1,
209
+ fee
210
+ }) {
211
+ if (amount === 0n || sqrtRatio === sqrtRatioLimit) {
212
+ return noOp(sqrtRatio);
213
+ }
214
+ const increasing = isPriceIncreasing(amount, isToken1);
215
+ if (sqrtRatioLimit < sqrtRatio === increasing) {
216
+ throw new Error("computeStep: wrong direction");
217
+ }
218
+ if (liquidity === 0n) {
219
+ return noOp(sqrtRatioLimit);
220
+ }
221
+ let priceImpactAmount;
222
+ if (amount < 0n) {
223
+ priceImpactAmount = amount;
224
+ } else {
225
+ priceImpactAmount = amount - computeFee(amount, fee);
226
+ }
227
+ let sqrtRatioNextFromAmount;
228
+ if (isToken1) {
229
+ sqrtRatioNextFromAmount = nextSqrtRatioFromAmount1(sqrtRatio, liquidity, priceImpactAmount);
230
+ } else {
231
+ sqrtRatioNextFromAmount = nextSqrtRatioFromAmount0(sqrtRatio, liquidity, priceImpactAmount);
232
+ }
233
+ if (sqrtRatioNextFromAmount === null || sqrtRatioNextFromAmount > sqrtRatioLimit === increasing) {
234
+ const [specifiedAmountDelta, calculatedAmountDelta] = isToken1 ? [
235
+ amount1Delta(sqrtRatioLimit, sqrtRatio, liquidity, amount >= 0n) * (amount < 0n ? -1n : 1n),
236
+ amount0Delta(sqrtRatioLimit, sqrtRatio, liquidity, amount < 0n)
237
+ ] : [
238
+ amount0Delta(sqrtRatioLimit, sqrtRatio, liquidity, amount >= 0n) * (amount < 0n ? -1n : 1n),
239
+ amount1Delta(sqrtRatioLimit, sqrtRatio, liquidity, amount < 0n)
240
+ ];
241
+ if (amount < 0n) {
242
+ const beforeFee = amountBeforeFee(calculatedAmountDelta, fee);
243
+ return {
244
+ consumedAmount: specifiedAmountDelta,
245
+ calculatedAmount: beforeFee,
246
+ feeAmount: beforeFee - calculatedAmountDelta,
247
+ sqrtRatioNext: sqrtRatioLimit
248
+ };
249
+ } else {
250
+ const beforeFee = amountBeforeFee(specifiedAmountDelta, fee);
251
+ return {
252
+ consumedAmount: beforeFee,
253
+ calculatedAmount: calculatedAmountDelta,
254
+ feeAmount: beforeFee - specifiedAmountDelta,
255
+ sqrtRatioNext: sqrtRatioLimit
256
+ };
257
+ }
258
+ }
259
+ if (sqrtRatioNextFromAmount === sqrtRatio) {
260
+ return {
261
+ consumedAmount: amount,
262
+ calculatedAmount: 0n,
263
+ feeAmount: amount,
264
+ sqrtRatioNext: sqrtRatio
265
+ };
266
+ }
267
+ const calculatedAmountExcludingFee = isToken1 ? amount0Delta(sqrtRatioNextFromAmount, sqrtRatio, liquidity, amount < 0n) : amount1Delta(sqrtRatioNextFromAmount, sqrtRatio, liquidity, amount < 0n);
268
+ if (amount < 0n) {
269
+ const includingFee = amountBeforeFee(calculatedAmountExcludingFee, fee);
270
+ return {
271
+ consumedAmount: amount,
272
+ calculatedAmount: includingFee,
273
+ sqrtRatioNext: sqrtRatioNextFromAmount,
274
+ feeAmount: includingFee - calculatedAmountExcludingFee
275
+ };
276
+ } else {
277
+ return {
278
+ consumedAmount: amount,
279
+ calculatedAmount: calculatedAmountExcludingFee,
280
+ sqrtRatioNext: sqrtRatioNextFromAmount,
281
+ feeAmount: amount - priceImpactAmount
282
+ };
283
+ }
284
+ }
285
+
286
+ // src/math/tick.ts
287
+ var EVM_MIN_TICK = -88722835;
288
+ var EVM_MAX_TICK = 88722835;
289
+ var EVM_MAX_TICK_SPACING = 698605;
290
+ var EVM_MIN_SQRT_RATIO = 18447191164202170524n;
291
+ var EVM_MAX_SQRT_RATIO = 6276949602062853172742588666607187473671941430179807625216n;
292
+ var STARKNET_MIN_TICK = -88722883;
293
+ var STARKNET_MAX_TICK = 88722883;
294
+ var STARKNET_MAX_SQRT_RATIO = 6277100250585753475930931601400621808602321654880405518632n;
295
+ var STARKNET_MIN_SQRT_RATIO = 18446748437148339061n;
296
+ var STARKNET_MAX_TICK_SPACING = 354892;
297
+ var CHAIN_PARAMS = {
298
+ evm: {
299
+ MIN_TICK: EVM_MIN_TICK,
300
+ MAX_TICK: EVM_MAX_TICK,
301
+ MIN_SQRT_RATIO: EVM_MIN_SQRT_RATIO,
302
+ MAX_SQRT_RATIO: EVM_MAX_SQRT_RATIO,
303
+ MAX_TICK_SPACING: EVM_MAX_TICK_SPACING
304
+ },
305
+ starknet: {
306
+ MIN_TICK: STARKNET_MIN_TICK,
307
+ MAX_TICK: STARKNET_MAX_TICK,
308
+ MIN_SQRT_RATIO: STARKNET_MIN_SQRT_RATIO,
309
+ MAX_SQRT_RATIO: STARKNET_MAX_SQRT_RATIO,
310
+ MAX_TICK_SPACING: STARKNET_MAX_TICK_SPACING
311
+ }
312
+ };
313
+ var SQRT_RATIO_FLOAT_BITMASK = 0xc00000000000000000000000n;
314
+ var SQRT_RATIO_FLOAT_NOT_BITMASK = 0x3fffffffffffffffffffffffn;
315
+ var SQRT_RATIO_FLOAT_EXPONENTS = [0n, 32n, 64n, 96n];
316
+ var TWO_POW_160 = 1n << 160n;
317
+ var TWO_POW_1282 = 1n << 128n;
318
+ var TWO_POW_96 = 1n << 96n;
319
+ function toSqrtRatio(tick, chain) {
320
+ const MIN_TICK = CHAIN_PARAMS[chain].MIN_TICK;
321
+ const MAX_TICK = CHAIN_PARAMS[chain].MAX_TICK;
322
+ if (tick < MIN_TICK || tick > MAX_TICK)
323
+ throw new Error(`Invalid tick: ${tick}`);
324
+ let sign = tick < 0;
325
+ tick = Math.abs(tick);
326
+ let ratio = 0x100000000000000000000000000000000n;
327
+ if ((tick & 1) != 0) {
328
+ ratio = 0xfffff79c8499329c7cbb2510d893283bn;
329
+ }
330
+ if ((tick & 2) != 0) {
331
+ ratio = ratio * 0xffffef390978c398134b4ff3764fe410n >> 128n;
332
+ }
333
+ if ((tick & 4) != 0) {
334
+ ratio = ratio * 0xffffde72140b00a354bd3dc828e976c9n >> 128n;
335
+ }
336
+ if ((tick & 8) != 0) {
337
+ ratio = ratio * 0xffffbce42c7be6c998ad6318193c0b18n >> 128n;
338
+ }
339
+ if ((tick & 16) != 0) {
340
+ ratio = ratio * 0xffff79c86a8f6150a32d9778eceef97cn >> 128n;
341
+ }
342
+ if ((tick & 32) != 0) {
343
+ ratio = ratio * 0xfffef3911b7cff24ba1b3dbb5f8f5974n >> 128n;
344
+ }
345
+ if ((tick & 64) != 0) {
346
+ ratio = ratio * 0xfffde72350725cc4ea8feece3b5f13c8n >> 128n;
347
+ }
348
+ if ((tick & 128) != 0) {
349
+ ratio = ratio * 0xfffbce4b06c196e9247ac87695d53c60n >> 128n;
350
+ }
351
+ if ((tick & 256) != 0) {
352
+ ratio = ratio * 0xfff79ca7a4d1bf1ee8556cea23cdbaa5n >> 128n;
353
+ }
354
+ if ((tick & 512) != 0) {
355
+ ratio = ratio * 0xffef3995a5b6a6267530f207142a5764n >> 128n;
356
+ }
357
+ if ((tick & 1024) != 0) {
358
+ ratio = ratio * 0xffde7444b28145508125d10077ba83b8n >> 128n;
359
+ }
360
+ if ((tick & 2048) != 0) {
361
+ ratio = ratio * 0xffbceceeb791747f10df216f2e53ec57n >> 128n;
362
+ }
363
+ if ((tick & 4096) != 0) {
364
+ ratio = ratio * 0xff79eb706b9a64c6431d76e63531e929n >> 128n;
365
+ }
366
+ if ((tick & 8192) != 0) {
367
+ ratio = ratio * 0xfef41d1a5f2ae3a20676bec6f7f9459an >> 128n;
368
+ }
369
+ if ((tick & 16384) != 0) {
370
+ ratio = ratio * 0xfde95287d26d81bea159c37073122c73n >> 128n;
371
+ }
372
+ if ((tick & 32768) != 0) {
373
+ ratio = ratio * 0xfbd701c7cbc4c8a6bb81efd232d1e4e7n >> 128n;
374
+ }
375
+ if ((tick & 65536) != 0) {
376
+ ratio = ratio * 0xf7bf5211c72f5185f372aeb1d48f937en >> 128n;
377
+ }
378
+ if ((tick & 131072) != 0) {
379
+ ratio = ratio * 0xefc2bf59df33ecc28125cf78ec4f167fn >> 128n;
380
+ }
381
+ if ((tick & 262144) != 0) {
382
+ ratio = ratio * 0xe08d35706200796273f0b3a981d90cfdn >> 128n;
383
+ }
384
+ if ((tick & 524288) != 0) {
385
+ ratio = ratio * 0xc4f76b68947482dc198a48a54348c4edn >> 128n;
386
+ }
387
+ if ((tick & 1048576) != 0) {
388
+ ratio = ratio * 0x978bcb9894317807e5fa4498eee7c0fan >> 128n;
389
+ }
390
+ if ((tick & 2097152) != 0) {
391
+ ratio = ratio * 0x59b63684b86e9f486ec54727371ba6can >> 128n;
392
+ }
393
+ if ((tick & 4194304) != 0) {
394
+ ratio = ratio * 0x1f703399d88f6aa83a28b22d4a1f56e3n >> 128n;
395
+ }
396
+ if ((tick & 8388608) != 0) {
397
+ ratio = ratio * 0x3dc5dac7376e20fc8679758d1bcdcfcn >> 128n;
398
+ }
399
+ if ((tick & 16777216) != 0) {
400
+ ratio = ratio * 0xee7e32d61fdb0a5e622b820f681d0n >> 128n;
401
+ }
402
+ if ((tick & 33554432) != 0) {
403
+ ratio = ratio * 0xde2ee4bc381afa7089aa84bb66n >> 128n;
404
+ }
405
+ if ((tick & 67108864) != 0) {
406
+ ratio = ratio * 0xc0d55d4d7152c25fb139n >> 128n;
407
+ }
408
+ if (tick > 0 && !sign) {
409
+ ratio = MAX_U256 / ratio;
410
+ }
411
+ ratio = chain === "evm" ? ratio >= TWO_POW_160 ? ratio >> 98n << 98n : ratio >= TWO_POW_1282 ? ratio >> 66n << 66n : ratio >= TWO_POW_96 ? ratio >> 34n << 34n : ratio >> 2n << 2n : ratio;
412
+ return ratio;
413
+ }
414
+ var logBase = Math.log(1.0000005);
415
+ function floatSqrtRatioToFixed(sqrtRatioFloat) {
416
+ const exponent = (sqrtRatioFloat & SQRT_RATIO_FLOAT_BITMASK) >> 89n;
417
+ const mantissa = sqrtRatioFloat & SQRT_RATIO_FLOAT_NOT_BITMASK;
418
+ return mantissa << 2n + exponent;
419
+ }
420
+ function fixedSqrtRatioToFloat(sqrtRatio) {
421
+ if (sqrtRatio < 0n)
422
+ throw new Error("Sqrt ratio must be non-negative");
423
+ for (const exponent of SQRT_RATIO_FLOAT_EXPONENTS) {
424
+ const shift = 2n + exponent;
425
+ const mantissa = sqrtRatio >> shift;
426
+ if (mantissa <= SQRT_RATIO_FLOAT_NOT_BITMASK) {
427
+ return mantissa | exponent << 89n;
428
+ }
429
+ }
430
+ throw new Error("Sqrt ratio too large to encode as float");
431
+ }
432
+
433
+ // src/math/twamm.ts
434
+ var EXPONENT_LIMIT = 1623313478486440542208n;
435
+ function calculateNextSqrtRatio(sqrtRatio, liquidity, token0SaleRate, token1SaleRate, timeElapsed, fee) {
436
+ let sqrtSaleRatio = sqrt((token1SaleRate << 128n) / token0SaleRate) << 64n;
437
+ if (liquidity === 0n) {
438
+ return sqrtSaleRatio;
439
+ }
440
+ const sRate = sqrt(token1SaleRate * token0SaleRate) * ((1n << 128n) - fee) / (1n << 128n);
441
+ const roundUp = sqrtRatio > sqrtSaleRatio;
442
+ const exponent = div(0x200000000n * timeElapsed * sRate, liquidity, roundUp);
443
+ if (exponent > EXPONENT_LIMIT) {
444
+ return sqrtSaleRatio;
445
+ }
446
+ const e = exp(exponent);
447
+ const [num, sign] = roundUp ? [sqrtRatio - sqrtSaleRatio, true] : [sqrtSaleRatio - sqrtRatio, false];
448
+ const c = div(num << 128n, sqrtSaleRatio + sqrtRatio, roundUp);
449
+ const [term1, term2] = [e - c, e + c];
450
+ const scale = sign ? div(term2 << 128n, term1, roundUp) : div(term1 << 128n, term2, roundUp);
451
+ return sqrtSaleRatio * scale >> 128n;
452
+ }
453
+ function div(x, y, round) {
454
+ const quotient = x / y;
455
+ const remainder = x % y;
456
+ return quotient + (remainder !== 0n && round ? 1n : 0n);
457
+ }
458
+ function exp(x) {
459
+ if (x >= 36893488147419103000) {
460
+ let half = exp(x / 2n);
461
+ return half * half >> 128n;
462
+ } else {
463
+ return expInner(x);
464
+ }
465
+ }
466
+ function expInner(x) {
467
+ if (x >= 0x20000000000000000n) {
468
+ throw new Error("Invalid input");
469
+ }
470
+ let ratio = 0x100000000000000000000000000000000n;
471
+ if ((x & 0x1n) != 0n) {
472
+ ratio = 0xffffffffffffffff0000000000000000n;
473
+ }
474
+ if ((x & 0x2n) != 0n) {
475
+ ratio = ratio * 0xfffffffffffffffe0000000000000002n >> 128n;
476
+ }
477
+ if ((x & 0x4n) != 0n) {
478
+ ratio = ratio * 0xfffffffffffffffc0000000000000008n >> 128n;
479
+ }
480
+ if ((x & 0x8n) != 0n) {
481
+ ratio = ratio * 0xfffffffffffffff80000000000000020n >> 128n;
482
+ }
483
+ if ((x & 0x10n) != 0n) {
484
+ ratio = ratio * 0xfffffffffffffff00000000000000080n >> 128n;
485
+ }
486
+ if ((x & 0x20n) != 0n) {
487
+ ratio = ratio * 0xffffffffffffffe00000000000000200n >> 128n;
488
+ }
489
+ if ((x & 0x40n) != 0n) {
490
+ ratio = ratio * 0xffffffffffffffc00000000000000800n >> 128n;
491
+ }
492
+ if ((x & 0x80n) != 0n) {
493
+ ratio = ratio * 0xffffffffffffff800000000000002000n >> 128n;
494
+ }
495
+ if ((x & 0x100n) != 0n) {
496
+ ratio = ratio * 0xffffffffffffff000000000000008000n >> 128n;
497
+ }
498
+ if ((x & 0x200n) != 0n) {
499
+ ratio = ratio * 0xfffffffffffffe000000000000020000n >> 128n;
500
+ }
501
+ if ((x & 0x400n) != 0n) {
502
+ ratio = ratio * 0xfffffffffffffc000000000000080000n >> 128n;
503
+ }
504
+ if ((x & 0x800n) != 0n) {
505
+ ratio = ratio * 0xfffffffffffff8000000000000200000n >> 128n;
506
+ }
507
+ if ((x & 0x1000n) != 0n) {
508
+ ratio = ratio * 0xfffffffffffff0000000000000800000n >> 128n;
509
+ }
510
+ if ((x & 0x2000n) != 0n) {
511
+ ratio = ratio * 0xffffffffffffe0000000000002000000n >> 128n;
512
+ }
513
+ if ((x & 0x4000n) != 0n) {
514
+ ratio = ratio * 0xffffffffffffc0000000000008000000n >> 128n;
515
+ }
516
+ if ((x & 0x8000n) != 0n) {
517
+ ratio = ratio * 0xffffffffffff80000000000020000000n >> 128n;
518
+ }
519
+ if ((x & 0x10000n) != 0n) {
520
+ ratio = ratio * 0xffffffffffff00000000000080000000n >> 128n;
521
+ }
522
+ if ((x & 0x20000n) != 0n) {
523
+ ratio = ratio * 0xfffffffffffe00000000000200000000n >> 128n;
524
+ }
525
+ if ((x & 0x40000n) != 0n) {
526
+ ratio = ratio * 0xfffffffffffc00000000000800000000n >> 128n;
527
+ }
528
+ if ((x & 0x80000n) != 0n) {
529
+ ratio = ratio * 0xfffffffffff800000000002000000000n >> 128n;
530
+ }
531
+ if ((x & 0x100000n) != 0n) {
532
+ ratio = ratio * 0xfffffffffff000000000008000000000n >> 128n;
533
+ }
534
+ if ((x & 0x200000n) != 0n) {
535
+ ratio = ratio * 0xffffffffffe000000000020000000000n >> 128n;
536
+ }
537
+ if ((x & 0x400000n) != 0n) {
538
+ ratio = ratio * 0xffffffffffc00000000007ffffffffffn >> 128n;
539
+ }
540
+ if ((x & 0x800000n) != 0n) {
541
+ ratio = ratio * 0xffffffffff80000000001ffffffffffbn >> 128n;
542
+ }
543
+ if ((x & 0x1000000n) != 0n) {
544
+ ratio = ratio * 0xffffffffff00000000007fffffffffd5n >> 128n;
545
+ }
546
+ if ((x & 0x2000000n) != 0n) {
547
+ ratio = ratio * 0xfffffffffe0000000001fffffffffeabn >> 128n;
548
+ }
549
+ if ((x & 0x4000000n) != 0n) {
550
+ ratio = ratio * 0xfffffffffc0000000007fffffffff555n >> 128n;
551
+ }
552
+ if ((x & 0x8000000n) != 0n) {
553
+ ratio = ratio * 0xfffffffff8000000001fffffffffaaabn >> 128n;
554
+ }
555
+ if ((x & 0x10000000n) != 0n) {
556
+ ratio = ratio * 0xfffffffff0000000007ffffffffd5555n >> 128n;
557
+ }
558
+ if ((x & 0x20000000n) != 0n) {
559
+ ratio = ratio * 0xffffffffe000000001ffffffffeaaaabn >> 128n;
560
+ }
561
+ if ((x & 0x40000000n) != 0n) {
562
+ ratio = ratio * 0xffffffffc000000007ffffffff555555n >> 128n;
563
+ }
564
+ if ((x & 0x80000000n) != 0n) {
565
+ ratio = ratio * 0xffffffff800000001ffffffffaaaaaabn >> 128n;
566
+ }
567
+ if ((x & 0x100000000n) != 0n) {
568
+ ratio = ratio * 0xffffffff000000007fffffffd5555555n >> 128n;
569
+ }
570
+ if ((x & 0x200000000n) != 0n) {
571
+ ratio = ratio * 0xfffffffe00000001fffffffeaaaaaaabn >> 128n;
572
+ }
573
+ if ((x & 0x400000000n) != 0n) {
574
+ ratio = ratio * 0xfffffffc00000007fffffff555555560n >> 128n;
575
+ }
576
+ if ((x & 0x800000000n) != 0n) {
577
+ ratio = ratio * 0xfffffff80000001fffffffaaaaaaab55n >> 128n;
578
+ }
579
+ if ((x & 0x1000000000n) != 0n) {
580
+ ratio = ratio * 0xfffffff00000007ffffffd5555556000n >> 128n;
581
+ }
582
+ if ((x & 0x2000000000n) != 0n) {
583
+ ratio = ratio * 0xffffffe0000001ffffffeaaaaaab5555n >> 128n;
584
+ }
585
+ if ((x & 0x4000000000n) != 0n) {
586
+ ratio = ratio * 0xffffffc0000007ffffff555555600000n >> 128n;
587
+ }
588
+ if ((x & 0x8000000000n) != 0n) {
589
+ ratio = ratio * 0xffffff8000001ffffffaaaaaab555555n >> 128n;
590
+ }
591
+ if ((x & 0x10000000000n) != 0n) {
592
+ ratio = ratio * 0xffffff0000007fffffd555555ffffffen >> 128n;
593
+ }
594
+ if ((x & 0x20000000000n) != 0n) {
595
+ ratio = ratio * 0xfffffe000001fffffeaaaaab55555511n >> 128n;
596
+ }
597
+ if ((x & 0x40000000000n) != 0n) {
598
+ ratio = ratio * 0xfffffc000007fffff555555ffffff777n >> 128n;
599
+ }
600
+ if ((x & 0x80000000000n) != 0n) {
601
+ ratio = ratio * 0xfffff800001fffffaaaaab5555544444n >> 128n;
602
+ }
603
+ if ((x & 0x100000000000n) != 0n) {
604
+ ratio = ratio * 0xfffff000007ffffd55555fffffddddden >> 128n;
605
+ }
606
+ if ((x & 0x200000000000n) != 0n) {
607
+ ratio = ratio * 0xffffe00001ffffeaaaab555551111128n >> 128n;
608
+ }
609
+ if ((x & 0x400000000000n) != 0n) {
610
+ ratio = ratio * 0xffffc00007ffff55555fffff77777d28n >> 128n;
611
+ }
612
+ if ((x & 0x800000000000n) != 0n) {
613
+ ratio = ratio * 0xffff80001ffffaaaab5555444445b05bn >> 128n;
614
+ }
615
+ if ((x & 0x1000000000000n) != 0n) {
616
+ ratio = ratio * 0xffff00007fffd5555ffffdddde38e381n >> 128n;
617
+ }
618
+ if ((x & 0x2000000000000n) != 0n) {
619
+ ratio = ratio * 0xfffe0001fffeaaab5555111127d276a7n >> 128n;
620
+ }
621
+ if ((x & 0x4000000000000n) != 0n) {
622
+ ratio = ratio * 0xfffc0007fff5555ffff7777d27cf3cf5n >> 128n;
623
+ }
624
+ if ((x & 0x8000000000000n) != 0n) {
625
+ ratio = ratio * 0xfff8001fffaaab55544445b0596597f9n >> 128n;
626
+ }
627
+ if ((x & 0x10000000000000n) != 0n) {
628
+ ratio = ratio * 0xfff0007ffd555fffddde38e2be2d82d5n >> 128n;
629
+ }
630
+ if ((x & 0x20000000000000n) != 0n) {
631
+ ratio = ratio * 0xffe001ffeaab55511127d21522f2295cn >> 128n;
632
+ }
633
+ if ((x & 0x40000000000000n) != 0n) {
634
+ ratio = ratio * 0xffc007ff555fff777d279e7b87acece0n >> 128n;
635
+ }
636
+ if ((x & 0x80000000000000n) != 0n) {
637
+ ratio = ratio * 0xff801ffaab554445b04105b043e8f48dn >> 128n;
638
+ }
639
+ if ((x & 0x100000000000000n) != 0n) {
640
+ ratio = ratio * 0xff007fd55ffdde38d68f08c257e0ce3fn >> 128n;
641
+ }
642
+ if ((x & 0x200000000000000n) != 0n) {
643
+ ratio = ratio * 0xfe01feab551127cbfe5f89994c44216fn >> 128n;
644
+ }
645
+ if ((x & 0x400000000000000n) != 0n) {
646
+ ratio = ratio * 0xfc07f55ff77d2493e885eeaa756ad523n >> 128n;
647
+ }
648
+ if ((x & 0x800000000000000n) != 0n) {
649
+ ratio = ratio * 0xf81fab5445aebc8a58055fcbbb139ae9n >> 128n;
650
+ }
651
+ if ((x & 0x1000000000000000n) != 0n) {
652
+ ratio = ratio * 0xf07d5fde38151e72f18ff03049ac5d7fn >> 128n;
653
+ }
654
+ if ((x & 0x2000000000000000n) != 0n) {
655
+ ratio = ratio * 0xe1eb51276c110c3c3eb1269f2f5d4afbn >> 128n;
656
+ }
657
+ if ((x & 0x4000000000000000n) != 0n) {
658
+ ratio = ratio * 0xc75f7cf564105743415cbc9d6368f3b9n >> 128n;
659
+ }
660
+ if ((x & 0x8000000000000000n) != 0n) {
661
+ ratio = ratio * 0x9b4597e37cb04ff3d675a35530cdd768n >> 128n;
662
+ }
663
+ if ((x & 0x10000000000000000n) != 0n) {
664
+ ratio = ratio * 0x5e2d58d8b3bcdf1abadec7829054f90en >> 128n;
665
+ }
666
+ if (x != 0n) {
667
+ ratio = MAX_U256 / ratio;
668
+ }
669
+ return ratio;
670
+ }
671
+ function sqrt(x) {
672
+ if (x < 0n) {
673
+ throw new Error("Square root of negative numbers is not supported.");
674
+ }
675
+ if (x < 2n) {
676
+ return x;
677
+ }
678
+ let x0 = x / 2n;
679
+ let x1 = (x0 + x / x0) / 2n;
680
+ while (x0 > x1) {
681
+ x0 = x1;
682
+ x1 = (x0 + x / x0) / 2n;
683
+ }
684
+ return x0;
685
+ }
686
+
687
+ // src/math/liquidity.ts
688
+ var Q128 = 1n << 128n;
689
+ function orderBounds(a, b) {
690
+ return a < b ? [a, b] : [b, a];
691
+ }
692
+ function maxLiquidityForBaseToken({
693
+ sqrtPriceLower,
694
+ sqrtPriceUpper,
695
+ amount
696
+ }) {
697
+ const [lower, upper] = orderBounds(sqrtPriceLower, sqrtPriceUpper);
698
+ if (amount === 0n || lower === upper)
699
+ return 0n;
700
+ return amount * lower * upper / (upper - lower) / Q128;
701
+ }
702
+ function maxLiquidityForQuoteToken({
703
+ sqrtPriceLower,
704
+ sqrtPriceUpper,
705
+ amount
706
+ }) {
707
+ const [lower, upper] = orderBounds(sqrtPriceLower, sqrtPriceUpper);
708
+ if (amount === 0n || lower === upper)
709
+ return 0n;
710
+ return amount * Q128 / (upper - lower);
711
+ }
712
+ function maxLiquidityForTokenAmounts({
713
+ sqrtPrice,
714
+ sqrtPriceLower,
715
+ sqrtPriceUpper,
716
+ amountBase,
717
+ amountQuote
718
+ }) {
719
+ const [lower, upper] = orderBounds(sqrtPriceLower, sqrtPriceUpper);
720
+ if (sqrtPrice <= lower) {
721
+ return maxLiquidityForBaseToken({ sqrtPriceLower: lower, sqrtPriceUpper: upper, amount: amountBase });
722
+ } else if (sqrtPrice < upper) {
723
+ const liquidityBase = maxLiquidityForBaseToken({
724
+ sqrtPriceLower: sqrtPrice,
725
+ sqrtPriceUpper: upper,
726
+ amount: amountBase
727
+ });
728
+ const liquidityQuote = maxLiquidityForQuoteToken({
729
+ sqrtPriceLower: lower,
730
+ sqrtPriceUpper: sqrtPrice,
731
+ amount: amountQuote
732
+ });
733
+ return liquidityBase < liquidityQuote ? liquidityBase : liquidityQuote;
734
+ } else {
735
+ return maxLiquidityForQuoteToken({ sqrtPriceLower: lower, sqrtPriceUpper: upper, amount: amountQuote });
736
+ }
737
+ }
738
+ function liquidityToAmountBase({
739
+ sqrtPriceLower,
740
+ sqrtPriceUpper,
741
+ liquidity
742
+ }) {
743
+ const [lower, upper] = orderBounds(sqrtPriceLower, sqrtPriceUpper);
744
+ return amount0Delta(lower, upper, liquidity, false);
745
+ }
746
+ function liquidityToAmountQuote({
747
+ sqrtPriceLower,
748
+ sqrtPriceUpper,
749
+ liquidity
750
+ }) {
751
+ const [lower, upper] = orderBounds(sqrtPriceLower, sqrtPriceUpper);
752
+ return amount1Delta(lower, upper, liquidity, false);
753
+ }
754
+ function maxLiquidityForSpecifiedAmount({
755
+ sqrtPrice,
756
+ sqrtPriceUpper,
757
+ sqrtPriceLower,
758
+ amount
759
+ }) {
760
+ const [lower, upper] = orderBounds(sqrtPriceLower, sqrtPriceUpper);
761
+ if (sqrtPrice <= lower) {
762
+ return "base" in amount ? maxLiquidityForBaseToken({ sqrtPriceLower: lower, sqrtPriceUpper: upper, amount: amount.base }) : 0n;
763
+ } else if (sqrtPrice < upper) {
764
+ return "base" in amount ? maxLiquidityForBaseToken({ sqrtPriceLower: sqrtPrice, sqrtPriceUpper: upper, amount: amount.base }) : maxLiquidityForQuoteToken({ sqrtPriceLower: lower, sqrtPriceUpper: sqrtPrice, amount: amount.quote });
765
+ } else {
766
+ return "quote" in amount ? maxLiquidityForQuoteToken({ sqrtPriceLower: lower, sqrtPriceUpper: upper, amount: amount.quote }) : 0n;
767
+ }
768
+ }
769
+ function amountsFromSpecifiedAmount({
770
+ sqrtPrice,
771
+ sqrtPriceUpper,
772
+ sqrtPriceLower,
773
+ amount
774
+ }) {
775
+ const [lower, upper] = orderBounds(sqrtPriceLower, sqrtPriceUpper);
776
+ const maxLiquidity = maxLiquidityForSpecifiedAmount({
777
+ sqrtPrice,
778
+ sqrtPriceLower: lower,
779
+ sqrtPriceUpper: upper,
780
+ amount
781
+ });
782
+ if ("base" in amount) {
783
+ if (sqrtPrice < lower) {
784
+ return {
785
+ maxLiquidity,
786
+ base: amount.base,
787
+ quote: liquidityToAmountQuote({ liquidity: maxLiquidity, sqrtPriceLower: lower, sqrtPriceUpper: upper })
788
+ };
789
+ } else if (sqrtPrice < upper) {
790
+ return {
791
+ maxLiquidity,
792
+ base: amount.base,
793
+ quote: liquidityToAmountQuote({ liquidity: maxLiquidity, sqrtPriceLower: sqrtPrice, sqrtPriceUpper: upper })
794
+ };
795
+ } else {
796
+ return { maxLiquidity, base: amount.base, quote: 0n };
797
+ }
798
+ } else {
799
+ if (sqrtPrice < lower) {
800
+ return { maxLiquidity, quote: amount.quote, base: 0n };
801
+ } else if (sqrtPrice < upper) {
802
+ return {
803
+ maxLiquidity,
804
+ quote: amount.quote,
805
+ base: liquidityToAmountBase({ liquidity: maxLiquidity, sqrtPriceLower: lower, sqrtPriceUpper: sqrtPrice })
806
+ };
807
+ } else {
808
+ return {
809
+ maxLiquidity,
810
+ quote: amount.quote,
811
+ base: liquidityToAmountBase({ liquidity: maxLiquidity, sqrtPriceLower: lower, sqrtPriceUpper: upper })
812
+ };
813
+ }
814
+ }
815
+ }
816
+ export {
817
+ toSqrtRatio,
818
+ nextSqrtRatioFromAmount1,
819
+ nextSqrtRatioFromAmount0,
820
+ msb,
821
+ maxLiquidityForTokenAmounts,
822
+ maxLiquidityForSpecifiedAmount,
823
+ maxLiquidityForQuoteToken,
824
+ maxLiquidityForBaseToken,
825
+ liquidityToAmountQuote,
826
+ liquidityToAmountBase,
827
+ floatSqrtRatioToFixed,
828
+ fixedSqrtRatioToFloat,
829
+ computeStep,
830
+ calculateNextSqrtRatio,
831
+ amountsFromSpecifiedAmount,
832
+ amount1Delta,
833
+ amount0Delta,
834
+ STARKNET_MIN_TICK,
835
+ STARKNET_MIN_SQRT_RATIO,
836
+ STARKNET_MAX_TICK_SPACING,
837
+ STARKNET_MAX_TICK,
838
+ STARKNET_MAX_SQRT_RATIO,
839
+ MAX_U256,
840
+ MAX_U128,
841
+ EVM_MIN_TICK,
842
+ EVM_MIN_SQRT_RATIO,
843
+ EVM_MAX_TICK_SPACING,
844
+ EVM_MAX_TICK,
845
+ EVM_MAX_SQRT_RATIO
846
+ };
847
+
848
+ //# debugId=638B62C2E6A8A81B64756E2164756E21
849
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,17 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/math/constants.ts", "../src/math/delta.ts", "../src/math/msb.ts", "../src/math/price.ts", "../src/math/swap.ts", "../src/math/tick.ts", "../src/math/twamm.ts", "../src/math/liquidity.ts"],
4
+ "sourcesContent": [
5
+ "export const MAX_U256 =\n 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn;\nexport const MAX_U128 = 0xffffffffffffffffffffffffffffffffn;\n",
6
+ "import { MAX_U128, MAX_U256 } from \"./constants\";\n\nexport function amount0Delta(\n sqrtRatioA: bigint,\n sqrtRatioB: bigint,\n liquidity: bigint,\n roundUp: boolean,\n): bigint {\n if (liquidity === 0n || sqrtRatioA === sqrtRatioB) return 0n;\n\n const [lower, upper] =\n sqrtRatioA < sqrtRatioB\n ? [sqrtRatioA, sqrtRatioB]\n : [sqrtRatioB, sqrtRatioA];\n\n const numerator = (liquidity << 128n) * (upper - lower);\n\n let result0 = numerator / upper;\n if (roundUp && numerator % upper !== 0n) {\n result0++;\n }\n\n if (result0 > MAX_U256) {\n throw new Error(\"AMOUNT0_DELTA_OVERFLOW_U256\");\n }\n let result = result0 / lower;\n if (roundUp && result % lower !== 0n) {\n result++;\n }\n\n if (result > MAX_U128) {\n throw new Error(\"AMOUNT0_DELTA_OVERFLOW_U128\");\n }\n\n return result;\n}\n\nconst TWO_POW_128 = 0x100000000000000000000000000000000n;\n\nexport function amount1Delta(\n sqrtRatioA: bigint,\n sqrtRatioB: bigint,\n liquidity: bigint,\n roundUp: boolean,\n): bigint {\n if (liquidity === 0n || sqrtRatioA === sqrtRatioB) return 0n;\n\n const [lower, upper] =\n sqrtRatioA < sqrtRatioB\n ? [sqrtRatioA, sqrtRatioB]\n : [sqrtRatioB, sqrtRatioA];\n\n const result = liquidity * (upper - lower);\n\n if (result > MAX_U256) {\n throw new Error(\"AMOUNT1_DELTA_OVERFLOW_U256\");\n }\n\n if (roundUp && result % TWO_POW_128 !== 0n) {\n const delta = result / TWO_POW_128 + 1n;\n if (delta > MAX_U128) {\n throw new Error(\"AMOUNT1_DELTA_OVERFLOW_U128\");\n }\n return delta;\n } else {\n return result >> 128n;\n }\n}\n",
7
+ "const MAX = 1n << 128n;\n\nexport default function msb(x: bigint): number {\n if (x >= MAX) {\n throw new Error(\"x too large\");\n }\n if (x <= 0n) {\n throw new Error(\"x must be positive\");\n }\n let res = 0;\n if (x >= 0x10000000000000000n) {\n x = x >> 64n;\n res += 64;\n }\n if (x >= 0x100000000n) {\n x = x >> 32n;\n res += 32;\n }\n if (x >= 0x10000n) {\n x = x >> 16n;\n res += 16;\n }\n if (x >= 0x100n) {\n x = x >> 8n;\n res += 8;\n }\n if (x >= 0x10n) {\n x = x >> 4n;\n res += 4;\n }\n if (x >= 0x04n) {\n x = x >> 2n;\n res += 2;\n }\n if (x >= 0x02n) {\n res += 1;\n }\n return res;\n}\n",
8
+ "import { MAX_U256 } from \"./constants\";\n\nconst toBig = (value: unknown, name: string): bigint => {\n if (typeof value === \"bigint\") return value;\n try {\n return BigInt(value as any);\n } catch (err) {\n const printed =\n value === null || value === undefined ? String(value) : value.toString();\n const ctor =\n value && typeof value === \"object\" && \"constructor\" in value\n ? (value as { constructor: { name: string } }).constructor.name\n : undefined;\n throw new TypeError(\n `${name} must be bigint-compatible, received ${printed} (type ${typeof value}${\n ctor ? `, ctor ${ctor}` : \"\"\n })`,\n );\n }\n};\n\nexport function nextSqrtRatioFromAmount0(\n sqrtRatio: bigint,\n liquidity: bigint,\n amount0: bigint,\n): bigint | null {\n const sqrtRatioFixed = toBig(sqrtRatio, \"sqrtRatio\");\n const liquidityFixed = toBig(liquidity, \"liquidity\");\n const amount0Fixed = toBig(amount0, \"amount0\");\n\n if (amount0Fixed === 0n) return sqrtRatioFixed;\n\n if (liquidityFixed === 0n) throw new Error(\"NO_LIQUIDITY\");\n\n const numerator1 = liquidityFixed << 128n;\n\n // because quotient is rounded down, this price movement is also rounded towards sqrt_ratio\n if (amount0Fixed < 0n) {\n const product = amount0Fixed * -1n * sqrtRatioFixed;\n\n if (product >= MAX_U256) {\n return null;\n }\n\n const denominator = numerator1 - product;\n\n if (denominator < 0n) {\n return null;\n }\n\n const num = numerator1 * sqrtRatioFixed;\n\n const result = num / denominator + (num % denominator === 0n ? 0n : 1n);\n\n if (result > MAX_U256) {\n return null;\n }\n\n return result;\n } else {\n const denomP1 = numerator1 / sqrtRatioFixed;\n\n const denom = denomP1 + amount0Fixed;\n const quotient = numerator1 / denom;\n const remainder = numerator1 % denom;\n\n if (remainder === 0n) return quotient;\n const sum = quotient + 1n;\n if (sum > MAX_U256) return null;\n return sum;\n }\n}\n\nexport function nextSqrtRatioFromAmount1(\n sqrtRatio: bigint,\n liquidity: bigint,\n amount1: bigint,\n): bigint | null {\n const sqrtRatioFixed = toBig(sqrtRatio, \"sqrtRatio\");\n const liquidityFixed = toBig(liquidity, \"liquidity\");\n const amount1Fixed = toBig(amount1, \"amount1\");\n\n if (amount1Fixed === 0n) return sqrtRatioFixed;\n\n if (liquidityFixed === 0n) throw new Error(\"NO_LIQUIDITY\");\n\n const amountShifted = amount1Fixed << 128n;\n const quotient = amountShifted / liquidityFixed;\n const remainder = amountShifted % liquidityFixed;\n\n // because quotient is rounded down, this price movement is also rounded towards sqrt_ratio\n if (amount1Fixed < 0n) {\n // adding amount1, taking out amount0\n const res = sqrtRatioFixed + quotient;\n if (res < 0n) {\n return null;\n }\n if (remainder === 0n) {\n return res;\n } else {\n if (res != 0n) {\n return res - 1n;\n } else {\n return null;\n }\n }\n } else {\n // adding amount1, taking out amount0, price goes up\n const res = sqrtRatioFixed + quotient;\n if (res > MAX_U256) {\n return null;\n } else {\n return res;\n }\n }\n}\n",
9
+ "import { nextSqrtRatioFromAmount0, nextSqrtRatioFromAmount1 } from \"./price\";\nimport { amount0Delta, amount1Delta } from \"./delta\";\nimport { MAX_U128 } from \"./constants\";\n\ninterface SwapResult {\n consumedAmount: bigint;\n calculatedAmount: bigint;\n sqrtRatioNext: bigint;\n feeAmount: bigint;\n}\n\nexport function isPriceIncreasing(amount: bigint, isToken1: boolean): boolean {\n return amount < 0n !== isToken1;\n}\n\nfunction noOp(sqrtRatioNext: bigint): SwapResult {\n return {\n consumedAmount: 0n,\n calculatedAmount: 0n,\n sqrtRatioNext,\n feeAmount: 0n,\n };\n}\n\nexport function amountBeforeFee(amount: bigint, fee: bigint): bigint {\n if (fee === 0n) return amount;\n const num = amount << 128n;\n const denom = (1n << 128n) - fee;\n const val = num / denom;\n const result = val + (num % denom !== 0n ? 1n : 0n);\n if (result > MAX_U128) throw new Error(\"AMOUNT_BEFORE_FEE_OVERFLOW\");\n return result;\n}\n\nexport function computeFee(amount: bigint, fee: bigint) {\n const num = amount * fee;\n const denom = 2n ** 128n;\n if (num % denom !== 0n) {\n return num / denom + 1n;\n } else {\n return num / denom;\n }\n}\n\nexport function computeStep({\n sqrtRatio,\n liquidity,\n sqrtRatioLimit,\n amount,\n isToken1,\n fee,\n}: {\n sqrtRatio: bigint;\n liquidity: bigint;\n sqrtRatioLimit: bigint;\n amount: bigint;\n isToken1: boolean;\n fee: bigint;\n}): SwapResult {\n if (amount === 0n || sqrtRatio === sqrtRatioLimit) {\n return noOp(sqrtRatio);\n }\n\n const increasing = isPriceIncreasing(amount, isToken1);\n\n if (sqrtRatioLimit < sqrtRatio === increasing) {\n throw new Error(\"computeStep: wrong direction\");\n }\n\n if (liquidity === 0n) {\n return noOp(sqrtRatioLimit);\n }\n\n let priceImpactAmount: bigint;\n if (amount < 0n) {\n priceImpactAmount = amount;\n } else {\n priceImpactAmount = amount - computeFee(amount, fee);\n }\n\n let sqrtRatioNextFromAmount: bigint | null;\n if (isToken1) {\n sqrtRatioNextFromAmount = nextSqrtRatioFromAmount1(\n sqrtRatio,\n liquidity,\n priceImpactAmount,\n );\n } else {\n sqrtRatioNextFromAmount = nextSqrtRatioFromAmount0(\n sqrtRatio,\n liquidity,\n priceImpactAmount,\n );\n }\n\n if (\n sqrtRatioNextFromAmount === null ||\n sqrtRatioNextFromAmount > sqrtRatioLimit === increasing\n ) {\n const [specifiedAmountDelta, calculatedAmountDelta]: [bigint, bigint] =\n isToken1\n ? [\n amount1Delta(sqrtRatioLimit, sqrtRatio, liquidity, amount >= 0n) *\n (amount < 0n ? -1n : 1n),\n amount0Delta(sqrtRatioLimit, sqrtRatio, liquidity, amount < 0n),\n ]\n : [\n amount0Delta(sqrtRatioLimit, sqrtRatio, liquidity, amount >= 0n) *\n (amount < 0n ? -1n : 1n),\n amount1Delta(sqrtRatioLimit, sqrtRatio, liquidity, amount < 0n),\n ];\n\n if (amount < 0n) {\n const beforeFee = amountBeforeFee(calculatedAmountDelta, fee);\n return {\n consumedAmount: specifiedAmountDelta,\n calculatedAmount: beforeFee,\n feeAmount: beforeFee - calculatedAmountDelta,\n sqrtRatioNext: sqrtRatioLimit,\n };\n } else {\n const beforeFee = amountBeforeFee(specifiedAmountDelta, fee);\n return {\n consumedAmount: beforeFee,\n calculatedAmount: calculatedAmountDelta,\n feeAmount: beforeFee - specifiedAmountDelta,\n sqrtRatioNext: sqrtRatioLimit,\n };\n }\n }\n\n if (sqrtRatioNextFromAmount === sqrtRatio) {\n return {\n consumedAmount: amount,\n calculatedAmount: 0n,\n feeAmount: amount,\n sqrtRatioNext: sqrtRatio,\n };\n }\n\n const calculatedAmountExcludingFee = isToken1\n ? amount0Delta(sqrtRatioNextFromAmount, sqrtRatio, liquidity, amount < 0n)\n : amount1Delta(sqrtRatioNextFromAmount, sqrtRatio, liquidity, amount < 0n);\n\n if (amount < 0n) {\n const includingFee = amountBeforeFee(calculatedAmountExcludingFee, fee);\n return {\n consumedAmount: amount,\n calculatedAmount: includingFee,\n sqrtRatioNext: sqrtRatioNextFromAmount,\n feeAmount: includingFee - calculatedAmountExcludingFee,\n };\n } else {\n return {\n consumedAmount: amount,\n calculatedAmount: calculatedAmountExcludingFee,\n sqrtRatioNext: sqrtRatioNextFromAmount,\n feeAmount: amount - priceImpactAmount,\n };\n }\n}\n",
10
+ "import { Chain } from \"./chain\";\nimport { MAX_U256 } from \"./constants\";\n\nexport const EVM_MIN_TICK = -88722835 as const;\nexport const EVM_MAX_TICK = 88722835 as const;\nexport const EVM_MAX_TICK_SPACING = 698605 as const;\nexport const EVM_MIN_SQRT_RATIO: bigint = 18447191164202170524n as const;\nexport const EVM_MAX_SQRT_RATIO: bigint =\n 6276949602062853172742588666607187473671941430179807625216n as const;\n\nexport const STARKNET_MIN_TICK = -88722883 as const;\nexport const STARKNET_MAX_TICK = 88722883 as const;\nexport const STARKNET_MAX_SQRT_RATIO: bigint =\n 6277100250585753475930931601400621808602321654880405518632n as const;\nexport const STARKNET_MIN_SQRT_RATIO: bigint = 18446748437148339061n as const;\nexport const STARKNET_MAX_TICK_SPACING = 354892 as const;\n\nexport const CHAIN_PARAMS: Record<\n Chain,\n {\n MIN_TICK: number;\n MAX_TICK: number;\n MIN_SQRT_RATIO: bigint;\n MAX_SQRT_RATIO: bigint;\n MAX_TICK_SPACING: number;\n }\n> = {\n evm: {\n MIN_TICK: EVM_MIN_TICK,\n MAX_TICK: EVM_MAX_TICK,\n MIN_SQRT_RATIO: EVM_MIN_SQRT_RATIO,\n MAX_SQRT_RATIO: EVM_MAX_SQRT_RATIO,\n MAX_TICK_SPACING: EVM_MAX_TICK_SPACING,\n },\n starknet: {\n MIN_TICK: STARKNET_MIN_TICK,\n MAX_TICK: STARKNET_MAX_TICK,\n MIN_SQRT_RATIO: STARKNET_MIN_SQRT_RATIO,\n MAX_SQRT_RATIO: STARKNET_MAX_SQRT_RATIO,\n MAX_TICK_SPACING: STARKNET_MAX_TICK_SPACING,\n },\n};\n\n// sqrt ratio float encoding (96-bit custom float used on mainnet)\nconst SQRT_RATIO_FLOAT_BITMASK = 0xc00000000000000000000000n;\nconst SQRT_RATIO_FLOAT_NOT_BITMASK = 0x3fffffffffffffffffffffffn;\nconst SQRT_RATIO_FLOAT_EXPONENTS: readonly bigint[] = [0n, 32n, 64n, 96n];\n\nconst TWO_POW_160 = 1n << 160n;\nconst TWO_POW_128 = 1n << 128n;\nconst TWO_POW_96 = 1n << 96n;\n\nexport function toSqrtRatio(tick: number, chain: Chain): bigint {\n const MIN_TICK = CHAIN_PARAMS[chain].MIN_TICK;\n const MAX_TICK = CHAIN_PARAMS[chain].MAX_TICK;\n\n if (tick < MIN_TICK || tick > MAX_TICK)\n throw new Error(`Invalid tick: ${tick}`);\n let sign = tick < 0;\n tick = Math.abs(tick);\n let ratio = 0x100000000000000000000000000000000n;\n if ((tick & 0x1) != 0) {\n ratio = 0xfffff79c8499329c7cbb2510d893283bn;\n }\n if ((tick & 0x2) != 0) {\n ratio = (ratio * 0xffffef390978c398134b4ff3764fe410n) >> 128n;\n }\n if ((tick & 0x4) != 0) {\n ratio = (ratio * 0xffffde72140b00a354bd3dc828e976c9n) >> 128n;\n }\n if ((tick & 0x8) != 0) {\n ratio = (ratio * 0xffffbce42c7be6c998ad6318193c0b18n) >> 128n;\n }\n if ((tick & 0x10) != 0) {\n ratio = (ratio * 0xffff79c86a8f6150a32d9778eceef97cn) >> 128n;\n }\n if ((tick & 0x20) != 0) {\n ratio = (ratio * 0xfffef3911b7cff24ba1b3dbb5f8f5974n) >> 128n;\n }\n if ((tick & 0x40) != 0) {\n ratio = (ratio * 0xfffde72350725cc4ea8feece3b5f13c8n) >> 128n;\n }\n if ((tick & 0x80) != 0) {\n ratio = (ratio * 0xfffbce4b06c196e9247ac87695d53c60n) >> 128n;\n }\n if ((tick & 0x100) != 0) {\n ratio = (ratio * 0xfff79ca7a4d1bf1ee8556cea23cdbaa5n) >> 128n;\n }\n if ((tick & 0x200) != 0) {\n ratio = (ratio * 0xffef3995a5b6a6267530f207142a5764n) >> 128n;\n }\n if ((tick & 0x400) != 0) {\n ratio = (ratio * 0xffde7444b28145508125d10077ba83b8n) >> 128n;\n }\n if ((tick & 0x800) != 0) {\n ratio = (ratio * 0xffbceceeb791747f10df216f2e53ec57n) >> 128n;\n }\n if ((tick & 0x1000) != 0) {\n ratio = (ratio * 0xff79eb706b9a64c6431d76e63531e929n) >> 128n;\n }\n if ((tick & 0x2000) != 0) {\n ratio = (ratio * 0xfef41d1a5f2ae3a20676bec6f7f9459an) >> 128n;\n }\n if ((tick & 0x4000) != 0) {\n ratio = (ratio * 0xfde95287d26d81bea159c37073122c73n) >> 128n;\n }\n if ((tick & 0x8000) != 0) {\n ratio = (ratio * 0xfbd701c7cbc4c8a6bb81efd232d1e4e7n) >> 128n;\n }\n if ((tick & 0x10000) != 0) {\n ratio = (ratio * 0xf7bf5211c72f5185f372aeb1d48f937en) >> 128n;\n }\n if ((tick & 0x20000) != 0) {\n ratio = (ratio * 0xefc2bf59df33ecc28125cf78ec4f167fn) >> 128n;\n }\n if ((tick & 0x40000) != 0) {\n ratio = (ratio * 0xe08d35706200796273f0b3a981d90cfdn) >> 128n;\n }\n if ((tick & 0x80000) != 0) {\n ratio = (ratio * 0xc4f76b68947482dc198a48a54348c4edn) >> 128n;\n }\n if ((tick & 0x100000) != 0) {\n ratio = (ratio * 0x978bcb9894317807e5fa4498eee7c0fan) >> 128n;\n }\n if ((tick & 0x200000) != 0) {\n ratio = (ratio * 0x59b63684b86e9f486ec54727371ba6can) >> 128n;\n }\n if ((tick & 0x400000) != 0) {\n ratio = (ratio * 0x1f703399d88f6aa83a28b22d4a1f56e3n) >> 128n;\n }\n if ((tick & 0x800000) != 0) {\n ratio = (ratio * 0x3dc5dac7376e20fc8679758d1bcdcfcn) >> 128n;\n }\n if ((tick & 0x1000000) != 0) {\n ratio = (ratio * 0xee7e32d61fdb0a5e622b820f681d0n) >> 128n;\n }\n if ((tick & 0x2000000) != 0) {\n ratio = (ratio * 0xde2ee4bc381afa7089aa84bb66n) >> 128n;\n }\n if ((tick & 0x4000000) != 0) {\n ratio = (ratio * 0xc0d55d4d7152c25fb139n) >> 128n;\n }\n\n if (tick > 0 && !sign) {\n ratio = MAX_U256 / ratio;\n }\n\n ratio =\n chain === \"evm\"\n ? ratio >= TWO_POW_160\n ? (ratio >> 98n) << 98n\n : ratio >= TWO_POW_128\n ? (ratio >> 66n) << 66n\n : ratio >= TWO_POW_96\n ? (ratio >> 34n) << 34n\n : (ratio >> 2n) << 2n\n : ratio;\n\n return ratio;\n}\n\nconst logBase = Math.log(1.0000005);\n\nexport function approximateNumberOfTickSpacingsCrossed(\n sqrtRatioStart: bigint,\n sqrtRatioEnd: bigint,\n tickSpacing: number,\n): number {\n const logPriceDiff =\n Math.log(Number(sqrtRatioEnd) / Number(sqrtRatioStart)) / logBase;\n\n return Math.floor(Math.abs(logPriceDiff / (tickSpacing * 251)));\n}\n\n// Convert the on-chain float sqrt ratio to the fixed-point representation used in math.\nexport function floatSqrtRatioToFixed(sqrtRatioFloat: bigint): bigint {\n const exponent = (sqrtRatioFloat & SQRT_RATIO_FLOAT_BITMASK) >> 89n;\n const mantissa = sqrtRatioFloat & SQRT_RATIO_FLOAT_NOT_BITMASK;\n return mantissa << (2n + exponent);\n}\n\n// Convert a fixed-point sqrt ratio to the compact float format expected on mainnet.\nexport function fixedSqrtRatioToFloat(sqrtRatio: bigint): bigint {\n if (sqrtRatio < 0n) throw new Error(\"Sqrt ratio must be non-negative\");\n\n for (const exponent of SQRT_RATIO_FLOAT_EXPONENTS) {\n const shift = 2n + exponent;\n const mantissa = sqrtRatio >> shift;\n if (mantissa <= SQRT_RATIO_FLOAT_NOT_BITMASK) {\n return mantissa | (exponent << 89n);\n }\n }\n\n throw new Error(\"Sqrt ratio too large to encode as float\");\n}\n",
11
+ "import { MAX_U256 } from \"./constants\";\n\n// ~ ln(2**128) * 2**64\nexport const EXPONENT_LIMIT: bigint = 1623313478486440542208n;\n\nexport function calculateNextSqrtRatio(\n sqrtRatio: bigint,\n liquidity: bigint,\n token0SaleRate: bigint,\n token1SaleRate: bigint,\n timeElapsed: bigint,\n fee: bigint,\n): bigint {\n let sqrtSaleRatio = sqrt((token1SaleRate << 128n) / token0SaleRate) << 64n;\n\n if (liquidity === 0n) {\n return sqrtSaleRatio;\n }\n\n const sRate =\n (sqrt(token1SaleRate * token0SaleRate) * ((1n << 128n) - fee)) /\n (1n << 128n);\n\n const roundUp = sqrtRatio > sqrtSaleRatio;\n\n const exponent = div(0x200000000n * timeElapsed * sRate, liquidity, roundUp);\n\n if (exponent > EXPONENT_LIMIT) {\n return sqrtSaleRatio;\n }\n\n const e = exp(exponent);\n\n const [num, sign] = roundUp\n ? [sqrtRatio - sqrtSaleRatio, true]\n : [sqrtSaleRatio - sqrtRatio, false];\n\n const c = div(num << 128n, sqrtSaleRatio + sqrtRatio, roundUp);\n\n const [term1, term2] = [e - c, e + c];\n const scale = sign\n ? div(term2 << 128n, term1, roundUp)\n : div(term1 << 128n, term2, roundUp);\n\n return (sqrtSaleRatio * scale) >> 128n;\n}\n\nfunction div(x: bigint, y: bigint, round: boolean): bigint {\n const quotient = x / y;\n const remainder: bigint = x % y;\n return quotient + (remainder !== 0n && round ? 1n : 0n);\n}\n\n// Computes e^x where x is a fixed point 64.64 number and the result is a fixed point 128.128 number\nexport function exp(x: bigint): bigint {\n if (x >= 0x20000000000000000) {\n let half = exp(x / 2n);\n return (half * half) >> 128n;\n } else {\n return expInner(x);\n }\n}\n\n// Computes e^x where x is a fixed point 64.64 number that is less than the real number 2\nfunction expInner(x: bigint): bigint {\n if (x >= 0x20000000000000000n) {\n throw new Error(\"Invalid input\");\n }\n\n let ratio = 0x100000000000000000000000000000000n;\n\n if ((x & 0x1n) != 0n) {\n ratio = 0xffffffffffffffff0000000000000000n;\n }\n if ((x & 0x2n) != 0n) {\n ratio = (ratio * 0xfffffffffffffffe0000000000000002n) >> 128n;\n }\n if ((x & 0x4n) != 0n) {\n ratio = (ratio * 0xfffffffffffffffc0000000000000008n) >> 128n;\n }\n if ((x & 0x8n) != 0n) {\n ratio = (ratio * 0xfffffffffffffff80000000000000020n) >> 128n;\n }\n if ((x & 0x10n) != 0n) {\n ratio = (ratio * 0xfffffffffffffff00000000000000080n) >> 128n;\n }\n if ((x & 0x20n) != 0n) {\n ratio = (ratio * 0xffffffffffffffe00000000000000200n) >> 128n;\n }\n if ((x & 0x40n) != 0n) {\n ratio = (ratio * 0xffffffffffffffc00000000000000800n) >> 128n;\n }\n if ((x & 0x80n) != 0n) {\n ratio = (ratio * 0xffffffffffffff800000000000002000n) >> 128n;\n }\n if ((x & 0x100n) != 0n) {\n ratio = (ratio * 0xffffffffffffff000000000000008000n) >> 128n;\n }\n if ((x & 0x200n) != 0n) {\n ratio = (ratio * 0xfffffffffffffe000000000000020000n) >> 128n;\n }\n if ((x & 0x400n) != 0n) {\n ratio = (ratio * 0xfffffffffffffc000000000000080000n) >> 128n;\n }\n if ((x & 0x800n) != 0n) {\n ratio = (ratio * 0xfffffffffffff8000000000000200000n) >> 128n;\n }\n if ((x & 0x1000n) != 0n) {\n ratio = (ratio * 0xfffffffffffff0000000000000800000n) >> 128n;\n }\n if ((x & 0x2000n) != 0n) {\n ratio = (ratio * 0xffffffffffffe0000000000002000000n) >> 128n;\n }\n if ((x & 0x4000n) != 0n) {\n ratio = (ratio * 0xffffffffffffc0000000000008000000n) >> 128n;\n }\n if ((x & 0x8000n) != 0n) {\n ratio = (ratio * 0xffffffffffff80000000000020000000n) >> 128n;\n }\n if ((x & 0x10000n) != 0n) {\n ratio = (ratio * 0xffffffffffff00000000000080000000n) >> 128n;\n }\n if ((x & 0x20000n) != 0n) {\n ratio = (ratio * 0xfffffffffffe00000000000200000000n) >> 128n;\n }\n if ((x & 0x40000n) != 0n) {\n ratio = (ratio * 0xfffffffffffc00000000000800000000n) >> 128n;\n }\n if ((x & 0x80000n) != 0n) {\n ratio = (ratio * 0xfffffffffff800000000002000000000n) >> 128n;\n }\n if ((x & 0x100000n) != 0n) {\n ratio = (ratio * 0xfffffffffff000000000008000000000n) >> 128n;\n }\n if ((x & 0x200000n) != 0n) {\n ratio = (ratio * 0xffffffffffe000000000020000000000n) >> 128n;\n }\n if ((x & 0x400000n) != 0n) {\n ratio = (ratio * 0xffffffffffc00000000007ffffffffffn) >> 128n;\n }\n if ((x & 0x800000n) != 0n) {\n ratio = (ratio * 0xffffffffff80000000001ffffffffffbn) >> 128n;\n }\n if ((x & 0x1000000n) != 0n) {\n ratio = (ratio * 0xffffffffff00000000007fffffffffd5n) >> 128n;\n }\n if ((x & 0x2000000n) != 0n) {\n ratio = (ratio * 0xfffffffffe0000000001fffffffffeabn) >> 128n;\n }\n if ((x & 0x4000000n) != 0n) {\n ratio = (ratio * 0xfffffffffc0000000007fffffffff555n) >> 128n;\n }\n if ((x & 0x8000000n) != 0n) {\n ratio = (ratio * 0xfffffffff8000000001fffffffffaaabn) >> 128n;\n }\n if ((x & 0x10000000n) != 0n) {\n ratio = (ratio * 0xfffffffff0000000007ffffffffd5555n) >> 128n;\n }\n if ((x & 0x20000000n) != 0n) {\n ratio = (ratio * 0xffffffffe000000001ffffffffeaaaabn) >> 128n;\n }\n if ((x & 0x40000000n) != 0n) {\n ratio = (ratio * 0xffffffffc000000007ffffffff555555n) >> 128n;\n }\n if ((x & 0x80000000n) != 0n) {\n ratio = (ratio * 0xffffffff800000001ffffffffaaaaaabn) >> 128n;\n }\n if ((x & 0x100000000n) != 0n) {\n ratio = (ratio * 0xffffffff000000007fffffffd5555555n) >> 128n;\n }\n if ((x & 0x200000000n) != 0n) {\n ratio = (ratio * 0xfffffffe00000001fffffffeaaaaaaabn) >> 128n;\n }\n if ((x & 0x400000000n) != 0n) {\n ratio = (ratio * 0xfffffffc00000007fffffff555555560n) >> 128n;\n }\n if ((x & 0x800000000n) != 0n) {\n ratio = (ratio * 0xfffffff80000001fffffffaaaaaaab55n) >> 128n;\n }\n if ((x & 0x1000000000n) != 0n) {\n ratio = (ratio * 0xfffffff00000007ffffffd5555556000n) >> 128n;\n }\n if ((x & 0x2000000000n) != 0n) {\n ratio = (ratio * 0xffffffe0000001ffffffeaaaaaab5555n) >> 128n;\n }\n if ((x & 0x4000000000n) != 0n) {\n ratio = (ratio * 0xffffffc0000007ffffff555555600000n) >> 128n;\n }\n if ((x & 0x8000000000n) != 0n) {\n ratio = (ratio * 0xffffff8000001ffffffaaaaaab555555n) >> 128n;\n }\n if ((x & 0x10000000000n) != 0n) {\n ratio = (ratio * 0xffffff0000007fffffd555555ffffffen) >> 128n;\n }\n if ((x & 0x20000000000n) != 0n) {\n ratio = (ratio * 0xfffffe000001fffffeaaaaab55555511n) >> 128n;\n }\n if ((x & 0x40000000000n) != 0n) {\n ratio = (ratio * 0xfffffc000007fffff555555ffffff777n) >> 128n;\n }\n if ((x & 0x80000000000n) != 0n) {\n ratio = (ratio * 0xfffff800001fffffaaaaab5555544444n) >> 128n;\n }\n if ((x & 0x100000000000n) != 0n) {\n ratio = (ratio * 0xfffff000007ffffd55555fffffddddden) >> 128n;\n }\n if ((x & 0x200000000000n) != 0n) {\n ratio = (ratio * 0xffffe00001ffffeaaaab555551111128n) >> 128n;\n }\n if ((x & 0x400000000000n) != 0n) {\n ratio = (ratio * 0xffffc00007ffff55555fffff77777d28n) >> 128n;\n }\n if ((x & 0x800000000000n) != 0n) {\n ratio = (ratio * 0xffff80001ffffaaaab5555444445b05bn) >> 128n;\n }\n if ((x & 0x1000000000000n) != 0n) {\n ratio = (ratio * 0xffff00007fffd5555ffffdddde38e381n) >> 128n;\n }\n if ((x & 0x2000000000000n) != 0n) {\n ratio = (ratio * 0xfffe0001fffeaaab5555111127d276a7n) >> 128n;\n }\n if ((x & 0x4000000000000n) != 0n) {\n ratio = (ratio * 0xfffc0007fff5555ffff7777d27cf3cf5n) >> 128n;\n }\n if ((x & 0x8000000000000n) != 0n) {\n ratio = (ratio * 0xfff8001fffaaab55544445b0596597f9n) >> 128n;\n }\n if ((x & 0x10000000000000n) != 0n) {\n ratio = (ratio * 0xfff0007ffd555fffddde38e2be2d82d5n) >> 128n;\n }\n if ((x & 0x20000000000000n) != 0n) {\n ratio = (ratio * 0xffe001ffeaab55511127d21522f2295cn) >> 128n;\n }\n if ((x & 0x40000000000000n) != 0n) {\n ratio = (ratio * 0xffc007ff555fff777d279e7b87acece0n) >> 128n;\n }\n if ((x & 0x80000000000000n) != 0n) {\n ratio = (ratio * 0xff801ffaab554445b04105b043e8f48dn) >> 128n;\n }\n if ((x & 0x100000000000000n) != 0n) {\n ratio = (ratio * 0xff007fd55ffdde38d68f08c257e0ce3fn) >> 128n;\n }\n if ((x & 0x200000000000000n) != 0n) {\n ratio = (ratio * 0xfe01feab551127cbfe5f89994c44216fn) >> 128n;\n }\n if ((x & 0x400000000000000n) != 0n) {\n ratio = (ratio * 0xfc07f55ff77d2493e885eeaa756ad523n) >> 128n;\n }\n if ((x & 0x800000000000000n) != 0n) {\n ratio = (ratio * 0xf81fab5445aebc8a58055fcbbb139ae9n) >> 128n;\n }\n if ((x & 0x1000000000000000n) != 0n) {\n ratio = (ratio * 0xf07d5fde38151e72f18ff03049ac5d7fn) >> 128n;\n }\n if ((x & 0x2000000000000000n) != 0n) {\n ratio = (ratio * 0xe1eb51276c110c3c3eb1269f2f5d4afbn) >> 128n;\n }\n if ((x & 0x4000000000000000n) != 0n) {\n ratio = (ratio * 0xc75f7cf564105743415cbc9d6368f3b9n) >> 128n;\n }\n if ((x & 0x8000000000000000n) != 0n) {\n ratio = (ratio * 0x9b4597e37cb04ff3d675a35530cdd768n) >> 128n;\n }\n if ((x & 0x10000000000000000n) != 0n) {\n ratio = (ratio * 0x5e2d58d8b3bcdf1abadec7829054f90en) >> 128n;\n }\n\n if (x != 0n) {\n ratio = MAX_U256 / ratio;\n }\n\n return ratio;\n}\n\nexport function sqrt(x: bigint): bigint {\n if (x < 0n) {\n throw new Error(\"Square root of negative numbers is not supported.\");\n }\n if (x < 2n) {\n return x;\n }\n\n let x0: bigint = x / 2n;\n let x1: bigint = (x0 + x / x0) / 2n;\n\n while (x0 > x1) {\n x0 = x1;\n x1 = (x0 + x / x0) / 2n;\n }\n\n return x0;\n}\n",
12
+ "import { amount0Delta, amount1Delta } from \"./delta\";\n\nconst Q128 = 1n << 128n;\n\nfunction orderBounds(a: bigint, b: bigint): [bigint, bigint] {\n return a < b ? [a, b] : [b, a];\n}\n\nexport function maxLiquidityForBaseToken({\n sqrtPriceLower,\n sqrtPriceUpper,\n amount,\n}: {\n amount: bigint;\n sqrtPriceLower: bigint;\n sqrtPriceUpper: bigint;\n}): bigint {\n const [lower, upper] = orderBounds(sqrtPriceLower, sqrtPriceUpper);\n if (amount === 0n || lower === upper) return 0n;\n return (amount * lower * upper) / (upper - lower) / Q128;\n}\n\nexport function maxLiquidityForQuoteToken({\n sqrtPriceLower,\n sqrtPriceUpper,\n amount,\n}: {\n amount: bigint;\n sqrtPriceLower: bigint;\n sqrtPriceUpper: bigint;\n}): bigint {\n const [lower, upper] = orderBounds(sqrtPriceLower, sqrtPriceUpper);\n if (amount === 0n || lower === upper) return 0n;\n return (amount * Q128) / (upper - lower);\n}\n\nexport function maxLiquidityForTokenAmounts({\n sqrtPrice,\n sqrtPriceLower,\n sqrtPriceUpper,\n amountBase,\n amountQuote,\n}: {\n sqrtPrice: bigint;\n sqrtPriceLower: bigint;\n sqrtPriceUpper: bigint;\n amountBase: bigint;\n amountQuote: bigint;\n}): bigint {\n const [lower, upper] = orderBounds(sqrtPriceLower, sqrtPriceUpper);\n\n if (sqrtPrice <= lower) {\n return maxLiquidityForBaseToken({ sqrtPriceLower: lower, sqrtPriceUpper: upper, amount: amountBase });\n } else if (sqrtPrice < upper) {\n const liquidityBase = maxLiquidityForBaseToken({\n sqrtPriceLower: sqrtPrice,\n sqrtPriceUpper: upper,\n amount: amountBase,\n });\n const liquidityQuote = maxLiquidityForQuoteToken({\n sqrtPriceLower: lower,\n sqrtPriceUpper: sqrtPrice,\n amount: amountQuote,\n });\n return liquidityBase < liquidityQuote ? liquidityBase : liquidityQuote;\n } else {\n return maxLiquidityForQuoteToken({ sqrtPriceLower: lower, sqrtPriceUpper: upper, amount: amountQuote });\n }\n}\n\nexport function liquidityToAmountBase({\n sqrtPriceLower,\n sqrtPriceUpper,\n liquidity,\n}: {\n liquidity: bigint;\n sqrtPriceLower: bigint;\n sqrtPriceUpper: bigint;\n}): bigint {\n const [lower, upper] = orderBounds(sqrtPriceLower, sqrtPriceUpper);\n return amount0Delta(lower, upper, liquidity, false);\n}\n\nexport function liquidityToAmountQuote({\n sqrtPriceLower,\n sqrtPriceUpper,\n liquidity,\n}: {\n liquidity: bigint;\n sqrtPriceLower: bigint;\n sqrtPriceUpper: bigint;\n}): bigint {\n const [lower, upper] = orderBounds(sqrtPriceLower, sqrtPriceUpper);\n return amount1Delta(lower, upper, liquidity, false);\n}\n\nexport function maxLiquidityForSpecifiedAmount({\n sqrtPrice,\n sqrtPriceUpper,\n sqrtPriceLower,\n amount,\n}: {\n amount: { base: bigint } | { quote: bigint };\n sqrtPrice: bigint;\n sqrtPriceLower: bigint;\n sqrtPriceUpper: bigint;\n}): bigint {\n const [lower, upper] = orderBounds(sqrtPriceLower, sqrtPriceUpper);\n\n if (sqrtPrice <= lower) {\n return \"base\" in amount\n ? maxLiquidityForBaseToken({ sqrtPriceLower: lower, sqrtPriceUpper: upper, amount: amount.base })\n : 0n;\n } else if (sqrtPrice < upper) {\n return \"base\" in amount\n ? maxLiquidityForBaseToken({ sqrtPriceLower: sqrtPrice, sqrtPriceUpper: upper, amount: amount.base })\n : maxLiquidityForQuoteToken({ sqrtPriceLower: lower, sqrtPriceUpper: sqrtPrice, amount: amount.quote });\n } else {\n return \"quote\" in amount\n ? maxLiquidityForQuoteToken({ sqrtPriceLower: lower, sqrtPriceUpper: upper, amount: amount.quote })\n : 0n;\n }\n}\n\nexport function amountsFromSpecifiedAmount({\n sqrtPrice,\n sqrtPriceUpper,\n sqrtPriceLower,\n amount,\n}: {\n amount: { base: bigint } | { quote: bigint };\n sqrtPrice: bigint;\n sqrtPriceLower: bigint;\n sqrtPriceUpper: bigint;\n}): { base: bigint; quote: bigint; maxLiquidity: bigint } {\n const [lower, upper] = orderBounds(sqrtPriceLower, sqrtPriceUpper);\n const maxLiquidity = maxLiquidityForSpecifiedAmount({\n sqrtPrice,\n sqrtPriceLower: lower,\n sqrtPriceUpper: upper,\n amount,\n });\n\n if (\"base\" in amount) {\n if (sqrtPrice < lower) {\n return {\n maxLiquidity,\n base: amount.base,\n quote: liquidityToAmountQuote({ liquidity: maxLiquidity, sqrtPriceLower: lower, sqrtPriceUpper: upper }),\n };\n } else if (sqrtPrice < upper) {\n return {\n maxLiquidity,\n base: amount.base,\n quote: liquidityToAmountQuote({ liquidity: maxLiquidity, sqrtPriceLower: sqrtPrice, sqrtPriceUpper: upper }),\n };\n } else {\n return { maxLiquidity, base: amount.base, quote: 0n };\n }\n } else {\n if (sqrtPrice < lower) {\n return { maxLiquidity, quote: amount.quote, base: 0n };\n } else if (sqrtPrice < upper) {\n return {\n maxLiquidity,\n quote: amount.quote,\n base: liquidityToAmountBase({ liquidity: maxLiquidity, sqrtPriceLower: lower, sqrtPriceUpper: sqrtPrice }),\n };\n } else {\n return {\n maxLiquidity,\n quote: amount.quote,\n base: liquidityToAmountBase({ liquidity: maxLiquidity, sqrtPriceLower: lower, sqrtPriceUpper: upper }),\n };\n }\n }\n}\n"
13
+ ],
14
+ "mappings": ";AAAO,IAAM,WACX;AACK,IAAM,WAAW;;;ACAjB,SAAS,YAAY,CAC1B,YACA,YACA,WACA,SACQ;AAAA,EACR,IAAI,cAAc,MAAM,eAAe;AAAA,IAAY,OAAO;AAAA,EAE1D,OAAO,OAAO,SACZ,aAAa,aACT,CAAC,YAAY,UAAU,IACvB,CAAC,YAAY,UAAU;AAAA,EAE7B,MAAM,aAAa,aAAa,SAAS,QAAQ;AAAA,EAEjD,IAAI,UAAU,YAAY;AAAA,EAC1B,IAAI,WAAW,YAAY,UAAU,IAAI;AAAA,IACvC;AAAA,EACF;AAAA,EAEA,IAAI,UAAU,UAAU;AAAA,IACtB,MAAM,IAAI,MAAM,6BAA6B;AAAA,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ACd,MAAM,sBAAsB,EAAE,WAAW,cAAc,gBAAgB,OAAO,gBAAgB,MAAM,CAAC;AAAA,MACvG;AAAA;AAAA;AAAA;",
15
+ "debugId": "638B62C2E6A8A81B64756E2164756E21",
16
+ "names": []
17
+ }
@@ -0,0 +1 @@
1
+ export type Chain = "evm" | "starknet";
@@ -0,0 +1,2 @@
1
+ export declare const MAX_U256 = 115792089237316195423570985008687907853269984665640564039457584007913129639935n;
2
+ export declare const MAX_U128 = 340282366920938463463374607431768211455n;
@@ -0,0 +1,2 @@
1
+ export declare function amount0Delta(sqrtRatioA: bigint, sqrtRatioB: bigint, liquidity: bigint, roundUp: boolean): bigint;
2
+ export declare function amount1Delta(sqrtRatioA: bigint, sqrtRatioB: bigint, liquidity: bigint, roundUp: boolean): bigint;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,51 @@
1
+ export declare function maxLiquidityForBaseToken({ sqrtPriceLower, sqrtPriceUpper, amount, }: {
2
+ amount: bigint;
3
+ sqrtPriceLower: bigint;
4
+ sqrtPriceUpper: bigint;
5
+ }): bigint;
6
+ export declare function maxLiquidityForQuoteToken({ sqrtPriceLower, sqrtPriceUpper, amount, }: {
7
+ amount: bigint;
8
+ sqrtPriceLower: bigint;
9
+ sqrtPriceUpper: bigint;
10
+ }): bigint;
11
+ export declare function maxLiquidityForTokenAmounts({ sqrtPrice, sqrtPriceLower, sqrtPriceUpper, amountBase, amountQuote, }: {
12
+ sqrtPrice: bigint;
13
+ sqrtPriceLower: bigint;
14
+ sqrtPriceUpper: bigint;
15
+ amountBase: bigint;
16
+ amountQuote: bigint;
17
+ }): bigint;
18
+ export declare function liquidityToAmountBase({ sqrtPriceLower, sqrtPriceUpper, liquidity, }: {
19
+ liquidity: bigint;
20
+ sqrtPriceLower: bigint;
21
+ sqrtPriceUpper: bigint;
22
+ }): bigint;
23
+ export declare function liquidityToAmountQuote({ sqrtPriceLower, sqrtPriceUpper, liquidity, }: {
24
+ liquidity: bigint;
25
+ sqrtPriceLower: bigint;
26
+ sqrtPriceUpper: bigint;
27
+ }): bigint;
28
+ export declare function maxLiquidityForSpecifiedAmount({ sqrtPrice, sqrtPriceUpper, sqrtPriceLower, amount, }: {
29
+ amount: {
30
+ base: bigint;
31
+ } | {
32
+ quote: bigint;
33
+ };
34
+ sqrtPrice: bigint;
35
+ sqrtPriceLower: bigint;
36
+ sqrtPriceUpper: bigint;
37
+ }): bigint;
38
+ export declare function amountsFromSpecifiedAmount({ sqrtPrice, sqrtPriceUpper, sqrtPriceLower, amount, }: {
39
+ amount: {
40
+ base: bigint;
41
+ } | {
42
+ quote: bigint;
43
+ };
44
+ sqrtPrice: bigint;
45
+ sqrtPriceLower: bigint;
46
+ sqrtPriceUpper: bigint;
47
+ }): {
48
+ base: bigint;
49
+ quote: bigint;
50
+ maxLiquidity: bigint;
51
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export default function msb(x: bigint): number;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export declare function nextSqrtRatioFromAmount0(sqrtRatio: bigint, liquidity: bigint, amount0: bigint): bigint | null;
2
+ export declare function nextSqrtRatioFromAmount1(sqrtRatio: bigint, liquidity: bigint, amount1: bigint): bigint | null;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,18 @@
1
+ interface SwapResult {
2
+ consumedAmount: bigint;
3
+ calculatedAmount: bigint;
4
+ sqrtRatioNext: bigint;
5
+ feeAmount: bigint;
6
+ }
7
+ export declare function isPriceIncreasing(amount: bigint, isToken1: boolean): boolean;
8
+ export declare function amountBeforeFee(amount: bigint, fee: bigint): bigint;
9
+ export declare function computeFee(amount: bigint, fee: bigint): bigint;
10
+ export declare function computeStep({ sqrtRatio, liquidity, sqrtRatioLimit, amount, isToken1, fee, }: {
11
+ sqrtRatio: bigint;
12
+ liquidity: bigint;
13
+ sqrtRatioLimit: bigint;
14
+ amount: bigint;
15
+ isToken1: boolean;
16
+ fee: bigint;
17
+ }): SwapResult;
18
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,22 @@
1
+ import { Chain } from "./chain";
2
+ export declare const EVM_MIN_TICK: -88722835;
3
+ export declare const EVM_MAX_TICK: 88722835;
4
+ export declare const EVM_MAX_TICK_SPACING: 698605;
5
+ export declare const EVM_MIN_SQRT_RATIO: bigint;
6
+ export declare const EVM_MAX_SQRT_RATIO: bigint;
7
+ export declare const STARKNET_MIN_TICK: -88722883;
8
+ export declare const STARKNET_MAX_TICK: 88722883;
9
+ export declare const STARKNET_MAX_SQRT_RATIO: bigint;
10
+ export declare const STARKNET_MIN_SQRT_RATIO: bigint;
11
+ export declare const STARKNET_MAX_TICK_SPACING: 354892;
12
+ export declare const CHAIN_PARAMS: Record<Chain, {
13
+ MIN_TICK: number;
14
+ MAX_TICK: number;
15
+ MIN_SQRT_RATIO: bigint;
16
+ MAX_SQRT_RATIO: bigint;
17
+ MAX_TICK_SPACING: number;
18
+ }>;
19
+ export declare function toSqrtRatio(tick: number, chain: Chain): bigint;
20
+ export declare function approximateNumberOfTickSpacingsCrossed(sqrtRatioStart: bigint, sqrtRatioEnd: bigint, tickSpacing: number): number;
21
+ export declare function floatSqrtRatioToFixed(sqrtRatioFloat: bigint): bigint;
22
+ export declare function fixedSqrtRatioToFloat(sqrtRatio: bigint): bigint;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ export declare const EXPONENT_LIMIT: bigint;
2
+ export declare function calculateNextSqrtRatio(sqrtRatio: bigint, liquidity: bigint, token0SaleRate: bigint, token1SaleRate: bigint, timeElapsed: bigint, fee: bigint): bigint;
3
+ export declare function exp(x: bigint): bigint;
4
+ export declare function sqrt(x: bigint): bigint;
@@ -0,0 +1 @@
1
+ export {};
package/dist/math.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { MAX_U128, MAX_U256 } from "./math/constants";
2
+ import { amount0Delta, amount1Delta } from "./math/delta";
3
+ import msb from "./math/msb";
4
+ import { nextSqrtRatioFromAmount0, nextSqrtRatioFromAmount1 } from "./math/price";
5
+ import { computeStep } from "./math/swap";
6
+ import { EVM_MAX_SQRT_RATIO, EVM_MAX_TICK, EVM_MAX_TICK_SPACING, EVM_MIN_SQRT_RATIO, EVM_MIN_TICK, STARKNET_MAX_SQRT_RATIO, STARKNET_MAX_TICK, STARKNET_MAX_TICK_SPACING, STARKNET_MIN_SQRT_RATIO, STARKNET_MIN_TICK, toSqrtRatio, floatSqrtRatioToFixed, fixedSqrtRatioToFloat } from "./math/tick";
7
+ import { calculateNextSqrtRatio } from "./math/twamm";
8
+ import { maxLiquidityForBaseToken, maxLiquidityForQuoteToken, maxLiquidityForTokenAmounts, maxLiquidityForSpecifiedAmount, amountsFromSpecifiedAmount, liquidityToAmountBase, liquidityToAmountQuote } from "./math/liquidity";
9
+ export { amount0Delta, amount1Delta, msb, nextSqrtRatioFromAmount0, nextSqrtRatioFromAmount1, computeStep, toSqrtRatio, calculateNextSqrtRatio, floatSqrtRatioToFixed, fixedSqrtRatioToFloat, maxLiquidityForBaseToken, maxLiquidityForQuoteToken, maxLiquidityForTokenAmounts, maxLiquidityForSpecifiedAmount, amountsFromSpecifiedAmount, liquidityToAmountBase, liquidityToAmountQuote, MAX_U128, MAX_U256, EVM_MIN_TICK, EVM_MAX_TICK, EVM_MIN_SQRT_RATIO, EVM_MAX_SQRT_RATIO, EVM_MAX_TICK_SPACING, STARKNET_MIN_TICK, STARKNET_MAX_TICK, STARKNET_MIN_SQRT_RATIO, STARKNET_MAX_SQRT_RATIO, STARKNET_MAX_TICK_SPACING, };
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@ekubo/sdk",
3
+ "version": "0.0.8-alpha.0",
4
+ "private": false,
5
+ "description": "Shared math utilities for Ekubo (chain-agnostic)",
6
+ "type": "module",
7
+ "main": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "scripts": {
10
+ "test": "bun test",
11
+ "build": "bun build ./src/index.ts --outdir dist --format esm --sourcemap && tsc -p tsconfig.json",
12
+ "prepublishOnly": "rm -rf dist && bun run build"
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "author": "Moody Salem",
18
+ "license": "UNLICENSED",
19
+ "packageManager": "bun@1.3.3",
20
+ "devDependencies": {
21
+ "@types/bun": "^1.3.4",
22
+ "typescript": "^5.7.3"
23
+ }
24
+ }