@sundaeswap/math 0.5.0 → 0.6.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.
- package/dist/cjs/PoolMath/ConcentratedLiquidityPool.js +655 -160
- package/dist/cjs/PoolMath/ConcentratedLiquidityPool.js.map +1 -1
- package/dist/cjs/PoolMath/StableSwapsPool.js +8 -8
- package/dist/cjs/PoolMath/StableSwapsPool.js.map +1 -1
- package/dist/esm/PoolMath/ConcentratedLiquidityPool.js +605 -137
- package/dist/esm/PoolMath/ConcentratedLiquidityPool.js.map +1 -1
- package/dist/esm/PoolMath/StableSwapsPool.js +8 -8
- package/dist/esm/PoolMath/StableSwapsPool.js.map +1 -1
- package/dist/types/PoolMath/ConcentratedLiquidityPool.d.ts +231 -45
- package/dist/types/PoolMath/ConcentratedLiquidityPool.d.ts.map +1 -1
- package/dist/types/PoolMath/StableSwapsPool.d.ts +6 -5
- package/dist/types/PoolMath/StableSwapsPool.d.ts.map +1 -1
- package/dist/types/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/PoolMath/ConcentratedLiquidityPool.ts +712 -212
- package/src/PoolMath/StableSwapsPool.ts +8 -8
- package/src/PoolMath/__tests__/ConcentratedLiquidityPool.test.ts +189 -196
- package/src/PoolMath/__tests__/StableSwapsPool.test.ts +76 -23
|
@@ -103,6 +103,59 @@ describe("getPrice()", () => {
|
|
|
103
103
|
Math.abs(normalCppPrice.toNumber() - 1.0),
|
|
104
104
|
);
|
|
105
105
|
});
|
|
106
|
+
|
|
107
|
+
// |dx/dy| of the on-chain invariant 4A(x + y) + D = 4AD + D^3/(4xy), as an
|
|
108
|
+
// exact rational on the scaled reserves. Independent of getPrice's integer
|
|
109
|
+
// arithmetic, so it pins both the amplification term and the orientation.
|
|
110
|
+
const exactSpot = (aReserve: bigint, bReserve: bigint, laf: bigint) => {
|
|
111
|
+
const d = getSumInvariant(laf, aReserve, bReserve);
|
|
112
|
+
const x = aReserve * RESERVE_PRECISION;
|
|
113
|
+
const y = bReserve * RESERVE_PRECISION;
|
|
114
|
+
const dCubed = d * d * d;
|
|
115
|
+
return new Fraction(
|
|
116
|
+
x * (16n * laf * x * y * y + dCubed),
|
|
117
|
+
y * (16n * laf * x * x * y + dCubed),
|
|
118
|
+
);
|
|
119
|
+
};
|
|
120
|
+
const relativeError = (actual: Fraction, expected: Fraction) =>
|
|
121
|
+
Math.abs(actual.toNumber() / expected.toNumber() - 1);
|
|
122
|
+
|
|
123
|
+
const offParPools: [bigint, bigint, bigint][] = [
|
|
124
|
+
[250275094796n, 250515678047n, 500n],
|
|
125
|
+
[1_000_000_000n, 1_100_000_000n, 100n],
|
|
126
|
+
[2_000_000_000n, 1_000_000_000n, 50n],
|
|
127
|
+
[5_000_000_000n, 1_000_000_000n, 10n],
|
|
128
|
+
[1_010_000_000n, 990_000_250n, 200n],
|
|
129
|
+
];
|
|
130
|
+
|
|
131
|
+
test.each(offParPools)(
|
|
132
|
+
"matches the derivative of the on-chain invariant (a=%s, b=%s, laf=%s)",
|
|
133
|
+
(aReserve, bReserve, laf) => {
|
|
134
|
+
const price = getPrice(aReserve, bReserve, laf);
|
|
135
|
+
expect(
|
|
136
|
+
relativeError(price, exactSpot(aReserve, bReserve, laf)),
|
|
137
|
+
).toBeLessThan(1e-9);
|
|
138
|
+
},
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
test.each(offParPools)(
|
|
142
|
+
"agrees with a central finite difference of getNewY (a=%s, b=%s, laf=%s)",
|
|
143
|
+
(aReserve, bReserve, laf) => {
|
|
144
|
+
const dA = 1000n;
|
|
145
|
+
const d = getSumInvariant(laf, aReserve, bReserve);
|
|
146
|
+
const lowY = getNewY(aReserve + dA, laf, d);
|
|
147
|
+
const highY = getNewY(aReserve - dA, laf, d);
|
|
148
|
+
const slope = new Fraction(dA * 2n * RESERVE_PRECISION, highY - lowY);
|
|
149
|
+
expect(
|
|
150
|
+
relativeError(getPrice(aReserve, bReserve, laf), slope),
|
|
151
|
+
).toBeLessThan(1e-9);
|
|
152
|
+
},
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
test("issue #172: preview pool 488fe776 at laf 500", () => {
|
|
156
|
+
const price = getPrice(250275094796n, 250515678047n, 500n);
|
|
157
|
+
expect(price.toNumber()).toBeCloseTo(0.999999040146, 11);
|
|
158
|
+
});
|
|
106
159
|
});
|
|
107
160
|
|
|
108
161
|
describe("getSwapOutput(usdc,)", () => {
|
|
@@ -199,7 +252,7 @@ describe("getSwapOutput(usdc,)", () => {
|
|
|
199
252
|
out: 0n,
|
|
200
253
|
outReserve: [3n, 2n],
|
|
201
254
|
lpFee: 0n,
|
|
202
|
-
impact: new Fraction(0n,
|
|
255
|
+
impact: new Fraction(0n, 1n),
|
|
203
256
|
},
|
|
204
257
|
{
|
|
205
258
|
id: 2,
|
|
@@ -209,7 +262,7 @@ describe("getSwapOutput(usdc,)", () => {
|
|
|
209
262
|
out: 1n,
|
|
210
263
|
outReserve: [3n, 2n],
|
|
211
264
|
lpFee: 0n,
|
|
212
|
-
impact: new Fraction(
|
|
265
|
+
impact: new Fraction(31n, 250000n),
|
|
213
266
|
},
|
|
214
267
|
{
|
|
215
268
|
id: 3,
|
|
@@ -219,7 +272,7 @@ describe("getSwapOutput(usdc,)", () => {
|
|
|
219
272
|
out: 2n,
|
|
220
273
|
outReserve: [102n, 1n],
|
|
221
274
|
lpFee: 0n,
|
|
222
|
-
impact: new Fraction(
|
|
275
|
+
impact: new Fraction(490001n, 500000n),
|
|
223
276
|
},
|
|
224
277
|
{
|
|
225
278
|
id: 4,
|
|
@@ -229,7 +282,7 @@ describe("getSwapOutput(usdc,)", () => {
|
|
|
229
282
|
out: 99n,
|
|
230
283
|
outReserve: [102n, 1n],
|
|
231
284
|
lpFee: 0n,
|
|
232
|
-
impact: new Fraction(
|
|
285
|
+
impact: new Fraction(14333n, 100000n),
|
|
233
286
|
},
|
|
234
287
|
{
|
|
235
288
|
id: 5,
|
|
@@ -260,7 +313,7 @@ describe("getSwapOutput(usdc,)", () => {
|
|
|
260
313
|
out: 0n,
|
|
261
314
|
outReserve: [3n, 2n],
|
|
262
315
|
lpFee: 0n,
|
|
263
|
-
impact: new Fraction(0n,
|
|
316
|
+
impact: new Fraction(0n, 1n),
|
|
264
317
|
},
|
|
265
318
|
{
|
|
266
319
|
id: 8,
|
|
@@ -270,7 +323,7 @@ describe("getSwapOutput(usdc,)", () => {
|
|
|
270
323
|
out: 0n,
|
|
271
324
|
outReserve: [3n, 3n],
|
|
272
325
|
lpFee: 1n,
|
|
273
|
-
impact: new Fraction(
|
|
326
|
+
impact: new Fraction(31n, 250000n),
|
|
274
327
|
},
|
|
275
328
|
{
|
|
276
329
|
id: 9,
|
|
@@ -280,7 +333,7 @@ describe("getSwapOutput(usdc,)", () => {
|
|
|
280
333
|
out: 0n,
|
|
281
334
|
outReserve: [3n, 6n],
|
|
282
335
|
lpFee: 1n,
|
|
283
|
-
impact: new Fraction(
|
|
336
|
+
impact: new Fraction(249n, 1000000n),
|
|
284
337
|
},
|
|
285
338
|
{
|
|
286
339
|
id: 10,
|
|
@@ -290,7 +343,7 @@ describe("getSwapOutput(usdc,)", () => {
|
|
|
290
343
|
out: 1n,
|
|
291
344
|
outReserve: [102n, 2n],
|
|
292
345
|
lpFee: 1n,
|
|
293
|
-
impact: new Fraction(
|
|
346
|
+
impact: new Fraction(990001n, 1000000n),
|
|
294
347
|
},
|
|
295
348
|
{
|
|
296
349
|
id: 11,
|
|
@@ -300,7 +353,7 @@ describe("getSwapOutput(usdc,)", () => {
|
|
|
300
353
|
out: 196n,
|
|
301
354
|
outReserve: [202n, 3n],
|
|
302
355
|
lpFee: 1n,
|
|
303
|
-
impact: new Fraction(
|
|
356
|
+
impact: new Fraction(393153n, 1000000n),
|
|
304
357
|
},
|
|
305
358
|
{
|
|
306
359
|
id: 12,
|
|
@@ -332,7 +385,7 @@ describe("getSwapOutput(usdc,)", () => {
|
|
|
332
385
|
out: 2139306697n,
|
|
333
386
|
outReserve: [7044962984n, 670265463643n],
|
|
334
387
|
lpFee: 21829661n,
|
|
335
|
-
impact: new Fraction(
|
|
388
|
+
impact: new Fraction(101761n, 1000000n),
|
|
336
389
|
},
|
|
337
390
|
{
|
|
338
391
|
id: 15,
|
|
@@ -342,7 +395,7 @@ describe("getSwapOutput(usdc,)", () => {
|
|
|
342
395
|
out: 1024031n,
|
|
343
396
|
outReserve: [3696261028076n, 77871392800159n],
|
|
344
397
|
lpFee: 3091n,
|
|
345
|
-
impact: new Fraction(
|
|
398
|
+
impact: new Fraction(6001n, 1000000n),
|
|
346
399
|
},
|
|
347
400
|
] as {
|
|
348
401
|
input: bigint;
|
|
@@ -466,31 +519,31 @@ describe("getSwapInput", () => {
|
|
|
466
519
|
input: 1n,
|
|
467
520
|
reserves: [1n, 2n],
|
|
468
521
|
fee: zeroPct,
|
|
469
|
-
impact: new Fraction(0n,
|
|
522
|
+
impact: new Fraction(0n, 4000n),
|
|
470
523
|
},
|
|
471
524
|
{
|
|
472
525
|
input: 100n,
|
|
473
526
|
reserves: [1n, 2n],
|
|
474
527
|
fee: zeroPct,
|
|
475
|
-
impact: new Fraction(0n,
|
|
528
|
+
impact: new Fraction(0n, 4000n),
|
|
476
529
|
},
|
|
477
530
|
{
|
|
478
531
|
input: 100n,
|
|
479
532
|
reserves: [1n, 100n],
|
|
480
533
|
fee: zeroPct,
|
|
481
|
-
impact: new Fraction(245025n,
|
|
534
|
+
impact: new Fraction(245025n, 643500n),
|
|
482
535
|
},
|
|
483
536
|
{
|
|
484
537
|
input: 100n,
|
|
485
538
|
reserves: [100n, 100n],
|
|
486
539
|
fee: zeroPct,
|
|
487
|
-
impact: new Fraction(
|
|
540
|
+
impact: new Fraction(800400n, 40020000n),
|
|
488
541
|
},
|
|
489
542
|
{
|
|
490
543
|
input: 50n,
|
|
491
544
|
reserves: [100n, 100n],
|
|
492
545
|
fee: zeroPct,
|
|
493
|
-
impact: new Fraction(
|
|
546
|
+
impact: new Fraction(400200n, 20010000n),
|
|
494
547
|
},
|
|
495
548
|
|
|
496
549
|
// Simple boundary cases
|
|
@@ -498,25 +551,25 @@ describe("getSwapInput", () => {
|
|
|
498
551
|
input: 10n,
|
|
499
552
|
reserves: [10n, 30n],
|
|
500
553
|
fee: threePct,
|
|
501
|
-
impact: new Fraction(
|
|
554
|
+
impact: new Fraction(40315n, 400450n),
|
|
502
555
|
},
|
|
503
556
|
{
|
|
504
557
|
input: 100n,
|
|
505
558
|
reserves: [1n, 100n],
|
|
506
559
|
fee: threePct,
|
|
507
|
-
impact: new Fraction(
|
|
560
|
+
impact: new Fraction(257100n, 643500n),
|
|
508
561
|
},
|
|
509
562
|
{
|
|
510
563
|
input: 100n,
|
|
511
564
|
reserves: [100n, 100n],
|
|
512
565
|
fee: threePct,
|
|
513
|
-
impact: new Fraction(
|
|
566
|
+
impact: new Fraction(2001000n, 40020000n),
|
|
514
567
|
},
|
|
515
568
|
{
|
|
516
569
|
input: 50n,
|
|
517
570
|
reserves: [100n, 100n],
|
|
518
571
|
fee: threePct,
|
|
519
|
-
impact: new Fraction(
|
|
572
|
+
impact: new Fraction(1200600n, 20010000n),
|
|
520
573
|
},
|
|
521
574
|
|
|
522
575
|
// // Real world examples
|
|
@@ -524,19 +577,19 @@ describe("getSwapInput", () => {
|
|
|
524
577
|
input: 5n,
|
|
525
578
|
reserves: [84159832107n, 123172010729n],
|
|
526
579
|
fee: fivePct,
|
|
527
|
-
impact: new Fraction(
|
|
580
|
+
impact: new Fraction(337126570533235n, 1684271322211955n),
|
|
528
581
|
},
|
|
529
582
|
{
|
|
530
583
|
input: 100n,
|
|
531
584
|
reserves: [84159832107n, 123172010729n],
|
|
532
585
|
fee: fivePct,
|
|
533
|
-
impact: new Fraction(
|
|
586
|
+
impact: new Fraction(1690738591869500n, 33685426444239100n),
|
|
534
587
|
},
|
|
535
588
|
{
|
|
536
589
|
input: 1291591603n,
|
|
537
590
|
reserves: [5753371381n, 672426600000n],
|
|
538
591
|
fee: onePct,
|
|
539
|
-
impact: new Fraction(
|
|
592
|
+
impact: new Fraction(5112429850089508046677n, 55212615531111655078080n),
|
|
540
593
|
},
|
|
541
594
|
] as {
|
|
542
595
|
input: bigint;
|