@floorlaunchpad/sdk 0.4.1 → 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/CHANGELOG.md +83 -18
- package/README.md +0 -2
- package/dist/abi/functions.gen.js +1 -1
- package/dist/config/generated.js +77 -0
- package/dist/evm/chains.js +1 -24
- package/dist/evm/router.js +1 -25
- package/dist/evm/squid.js +3 -3
- package/dist/index.js +1 -0
- package/dist/indexer/bodies.js +4 -0
- package/dist/math/v2/book.js +40 -7
- package/dist/math/v2/create.js +1 -1
- package/dist/math/v2/relay.js +201 -1
- package/dist/math/v2/split.js +60 -15
- package/dist/math/v2/trade.js +16 -8
- package/dist/ton/addresses.js +36 -0
- package/dist/ton/book-check.js +23 -0
- package/dist/ton/book.js +119 -0
- package/dist/ton/cell.js +542 -0
- package/dist/ton/consts.generated.js +17 -0
- package/dist/ton/content.js +83 -0
- package/dist/ton/gas.js +198 -0
- package/dist/ton/governance.js +401 -0
- package/dist/ton/index.js +18 -0
- package/dist/ton/messages.js +340 -0
- package/dist/ton/ops.js +37 -0
- package/dist/ton/price.js +51 -0
- package/dist/ton/quote-entry.js +116 -0
- package/dist/ton/quote.js +25 -0
- package/dist/ton/reader.js +378 -0
- package/dist/ton/types.js +43 -0
- package/dist/ton/wallet.js +26 -0
- package/dist/v2/evm.js +3 -1
- package/dist/v2/layouts.js +5 -1
- package/dist/v2/router.js +2 -2
- package/dist/v2/solanaSettle.js +2 -0
- package/docs/aggregator.md +0 -22
- package/docs/bot.md +0 -8
- package/docs/cursor-pages.md +0 -2
- package/docs/frontend.md +0 -2
- package/docs/ton.md +107 -0
- package/docs/wallet.md +0 -2
- package/examples/floor-line.html +1 -1
- package/package.json +6 -1
- package/src/abi/functions.gen.ts +1 -1
- package/src/config/generated.ts +77 -0
- package/src/evm/chains.ts +3 -28
- package/src/evm/router.ts +1 -26
- package/src/evm/squid.ts +3 -3
- package/src/index.ts +1 -0
- package/src/indexer/bodies.ts +4 -0
- package/src/math/v2/book.ts +42 -7
- package/src/math/v2/create.ts +1 -1
- package/src/math/v2/relay.ts +208 -1
- package/src/math/v2/split.ts +70 -16
- package/src/math/v2/trade.ts +16 -8
- package/src/ton/addresses.ts +38 -0
- package/src/ton/book-check.ts +24 -0
- package/src/ton/book.ts +136 -0
- package/src/ton/cell.ts +547 -0
- package/src/ton/consts.generated.ts +17 -0
- package/src/ton/content.ts +97 -0
- package/src/ton/gas.ts +209 -0
- package/src/ton/governance.ts +452 -0
- package/src/ton/index.ts +18 -0
- package/src/ton/messages.ts +343 -0
- package/src/ton/ops.ts +37 -0
- package/src/ton/price.ts +54 -0
- package/src/ton/quote-entry.ts +138 -0
- package/src/ton/quote.ts +67 -0
- package/src/ton/reader.ts +420 -0
- package/src/ton/types.ts +201 -0
- package/src/ton/wallet.ts +30 -0
- package/src/v2/evm.ts +5 -1
- package/src/v2/layouts.ts +5 -1
- package/src/v2/router.ts +2 -2
- package/src/v2/solanaSettle.ts +2 -0
- package/src/v2/types.ts +4 -1
package/src/math/v2/relay.ts
CHANGED
|
@@ -115,6 +115,8 @@ export interface Relay {
|
|
|
115
115
|
laidK: bigint;
|
|
116
116
|
atF: bigint;
|
|
117
117
|
|
|
118
|
+
kAtF: bigint;
|
|
119
|
+
|
|
118
120
|
kept: number;
|
|
119
121
|
}
|
|
120
122
|
|
|
@@ -176,7 +178,7 @@ export function relay(b: Book, row: SplitRow, extraCk: bigint, circ: bigint, ckO
|
|
|
176
178
|
n++;
|
|
177
179
|
}
|
|
178
180
|
const nTop = n;
|
|
179
|
-
const r: Relay = { nC: 0, nK: 0, laidC: 0n, laidK: 0n, atF: 0n, kept };
|
|
181
|
+
const r: Relay = { nC: 0, nK: 0, laidC: 0n, laidK: 0n, atF: 0n, kAtF: 0n, kept };
|
|
180
182
|
let lk = pool;
|
|
181
183
|
if (room && n + 1 < BIDS_MAX) {
|
|
182
184
|
|
|
@@ -233,3 +235,208 @@ export function relay(b: Book, row: SplitRow, extraCk: bigint, circ: bigint, ckO
|
|
|
233
235
|
r.atF = lk;
|
|
234
236
|
return r;
|
|
235
237
|
}
|
|
238
|
+
|
|
239
|
+
export interface Pots {
|
|
240
|
+
c: bigint;
|
|
241
|
+
k: bigint;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function pieceTokens(g: RelaySeg): bigint {
|
|
245
|
+
return g.money === 0n ? 0n : ov(tokensBetween(ov(liquidityForMoney(g.lo, g.hi, g.money)), g.lo, g.hi, UP));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function kPiece(sqrtF: Sqrt, top: Sqrt, left: bigint, cap: bigint): RelaySeg {
|
|
249
|
+
const none: RelaySeg = { lo: 0n, hi: 0n, money: 0n, fIn: 0n };
|
|
250
|
+
if (left === 0n || cap === 0n || top <= sqrtF) return none;
|
|
251
|
+
const s0 = absorb(sqrtF, top, left);
|
|
252
|
+
const t = pieceTokens(s0);
|
|
253
|
+
if (t <= cap) return s0;
|
|
254
|
+
let l2 = ov(mulDiv(left, cap, t, DOWN));
|
|
255
|
+
for (let k = 0; k <= CUT_NUDGES; k++) {
|
|
256
|
+
if (l2 === 0n) break;
|
|
257
|
+
const s1 = absorb(sqrtF, top, l2);
|
|
258
|
+
if (pieceTokens(s1) <= cap) return s1;
|
|
259
|
+
l2--;
|
|
260
|
+
}
|
|
261
|
+
return none;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export function relayPots(b: Book, row: SplitRow, add: Pots, take: Pots, circ: bigint): Relay {
|
|
265
|
+
if (!b.kpot) return relay(b, row, u128(add.c + add.k), circ, u128(take.c + take.k));
|
|
266
|
+
const p = b.sqrtP;
|
|
267
|
+
const f = b.sqrtF;
|
|
268
|
+
const nc0 = b.nC;
|
|
269
|
+
const nb0 = b.nBands;
|
|
270
|
+
if (nb0 > nc0 + 1) fail("SqrtRange");
|
|
271
|
+
const kseg = nb0 === nc0 + 1 ? nc0 : null;
|
|
272
|
+
|
|
273
|
+
const kLive = kseg !== null && (b.bandTokens[kseg] > 0n || b.bandFClaim[kseg] > 0n) && p < b.bandEdge[kseg];
|
|
274
|
+
let floor = b.floor - b.ckAtF;
|
|
275
|
+
let cpool = u128(b.ckAtF - b.kAtF + add.c);
|
|
276
|
+
let kpool = u128(b.kAtF + add.k);
|
|
277
|
+
const zero = (): RelaySeg => ({ lo: 0n, hi: 0n, money: 0n, fIn: 0n });
|
|
278
|
+
const edge: Sqrt[] = Array(BIDS_MAX + 1).fill(0n);
|
|
279
|
+
const segs: RelaySeg[] = Array.from({ length: BIDS_MAX }, zero);
|
|
280
|
+
const tok: bigint[] = Array(BIDS_MAX).fill(0n);
|
|
281
|
+
const claim: bigint[] = Array(BIDS_MAX).fill(0n);
|
|
282
|
+
let n = 0;
|
|
283
|
+
let kept = 0;
|
|
284
|
+
|
|
285
|
+
const ndis = kLive ? nc0 : nb0;
|
|
286
|
+
for (let i = 0; i < ndis; i++) {
|
|
287
|
+
const isK = i >= nc0;
|
|
288
|
+
const lo = b.bandEdge[i + 1];
|
|
289
|
+
const hi = b.bandEdge[i];
|
|
290
|
+
const fi = b.bandF[i] < b.bandMoney[i] ? b.bandF[i] : b.bandMoney[i];
|
|
291
|
+
const prem = b.bandMoney[i] - fi;
|
|
292
|
+
if (isK) kpool = u128(kpool + prem);
|
|
293
|
+
else cpool = u128(cpool + prem);
|
|
294
|
+
floor = u128(floor + fi);
|
|
295
|
+
const klo = lo > p ? lo : p;
|
|
296
|
+
if (!isK && hi > klo && (b.bandTokens[i] > 0n || b.bandFClaim[i] > 0n)) {
|
|
297
|
+
if (n > 0 && edge[n] !== hi) {
|
|
298
|
+
segs[n] = { lo: hi, hi: edge[n], money: 0n, fIn: 0n };
|
|
299
|
+
edge[n + 1] = hi;
|
|
300
|
+
n++;
|
|
301
|
+
}
|
|
302
|
+
edge[n] = hi;
|
|
303
|
+
segs[n] = { lo: klo, hi, money: 0n, fIn: 0n };
|
|
304
|
+
tok[n] = b.bandTokens[i];
|
|
305
|
+
claim[n] = b.bandFClaim[i];
|
|
306
|
+
edge[n + 1] = klo;
|
|
307
|
+
n++;
|
|
308
|
+
kept++;
|
|
309
|
+
} else if (b.bandTokens[i] > 0n) {
|
|
310
|
+
|
|
311
|
+
const t = b.bandTokens[i];
|
|
312
|
+
let mine = t;
|
|
313
|
+
if (f !== 0n) {
|
|
314
|
+
const c = ov(flatTokensForMoney(f, b.bandFClaim[i], UP));
|
|
315
|
+
mine = c < t ? c : t;
|
|
316
|
+
}
|
|
317
|
+
b.floorTokens = u64(b.floorTokens + t);
|
|
318
|
+
b.ckFloorTokens = u64(b.ckFloorTokens + t - mine);
|
|
319
|
+
if (isK) b.kFloorTokens = u64(b.kFloorTokens + t - mine);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (take.c > cpool || take.k > kpool) fail("Overflow");
|
|
323
|
+
cpool -= take.c;
|
|
324
|
+
kpool -= take.k;
|
|
325
|
+
const r: Relay = { nC: 0, nK: 0, laidC: 0n, laidK: 0n, atF: 0n, kAtF: 0n, kept };
|
|
326
|
+
const nTop = n;
|
|
327
|
+
let nC = n;
|
|
328
|
+
let kOut = false;
|
|
329
|
+
if (kLive && kseg !== null) {
|
|
330
|
+
|
|
331
|
+
const klo = b.bandEdge[kseg + 1];
|
|
332
|
+
const khi = b.bandEdge[kseg];
|
|
333
|
+
if (n > 0 && edge[n] !== khi) {
|
|
334
|
+
segs[n] = { lo: khi, hi: edge[n], money: 0n, fIn: 0n };
|
|
335
|
+
edge[n + 1] = khi;
|
|
336
|
+
n++;
|
|
337
|
+
nC = n;
|
|
338
|
+
}
|
|
339
|
+
edge[n] = khi;
|
|
340
|
+
segs[n] = { lo: klo, hi: khi, money: b.bandMoney[kseg], fIn: b.bandF[kseg] };
|
|
341
|
+
tok[n] = b.bandTokens[kseg];
|
|
342
|
+
claim[n] = b.bandFClaim[kseg];
|
|
343
|
+
edge[n + 1] = klo;
|
|
344
|
+
n++;
|
|
345
|
+
kOut = true;
|
|
346
|
+
} else {
|
|
347
|
+
const room = p > f && f > 0n;
|
|
348
|
+
if (room && n > 0 && edge[n] > p) {
|
|
349
|
+
segs[n] = { lo: p, hi: edge[n], money: 0n, fIn: 0n };
|
|
350
|
+
edge[n + 1] = p;
|
|
351
|
+
n++;
|
|
352
|
+
}
|
|
353
|
+
if (room && n + 1 < BIDS_MAX) {
|
|
354
|
+
if (floor * (1n << 16n) >= f * f) fail("Overflow");
|
|
355
|
+
let backed: bigint | null = null;
|
|
356
|
+
let cap = circ;
|
|
357
|
+
let cs: RelaySeg[];
|
|
358
|
+
let ks: RelaySeg;
|
|
359
|
+
let lc: bigint;
|
|
360
|
+
for (;;) {
|
|
361
|
+
|
|
362
|
+
const c = mirror(b.ranges, b.nRanges, f, p, cpool, cap, BIDS_MAX - 1 - n, false);
|
|
363
|
+
cs = c.segs;
|
|
364
|
+
const ktop = cs.length > 0 ? cs[cs.length - 1].lo : p;
|
|
365
|
+
|
|
366
|
+
ks = kPiece(f, ktop, kpool, cap - c.tokens);
|
|
367
|
+
let fi = ks.fIn;
|
|
368
|
+
for (const g of cs) fi += g.fIn;
|
|
369
|
+
if (fi <= floor || cap === 0n) {
|
|
370
|
+
lc = c.left;
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
if (backed === null) backed = ov(flatTokensForMoney(f, floor, DOWN));
|
|
374
|
+
if (cap > backed) cap = backed;
|
|
375
|
+
else {
|
|
376
|
+
const short = ov(flatTokensForMoney(f, fi - floor, UP));
|
|
377
|
+
const d = short + BigInt(cs.length + 1) + 1n;
|
|
378
|
+
cap = cap > d ? cap - d : 0n;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
if (n === 0 && (cs.length > 0 || ks.money > 0n)) edge[0] = p;
|
|
382
|
+
cs.forEach((g, j) => {
|
|
383
|
+
segs[n + j] = g;
|
|
384
|
+
edge[n + j + 1] = g.lo;
|
|
385
|
+
r.laidC += g.money - g.fIn;
|
|
386
|
+
});
|
|
387
|
+
n += cs.length;
|
|
388
|
+
nC = n;
|
|
389
|
+
cpool = lc;
|
|
390
|
+
if (ks.money > 0n) {
|
|
391
|
+
segs[n] = ks;
|
|
392
|
+
edge[n + 1] = ks.lo;
|
|
393
|
+
r.laidK = ks.money - ks.fIn;
|
|
394
|
+
kpool -= r.laidK;
|
|
395
|
+
n++;
|
|
396
|
+
kOut = true;
|
|
397
|
+
}
|
|
398
|
+
} else nC = n;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
let fIn = 0n;
|
|
402
|
+
for (let i = nTop; i < n; i++) fIn += segs[i].fIn;
|
|
403
|
+
if (kLive) fIn -= segs[n - 1].fIn;
|
|
404
|
+
const atF = u128(cpool + kpool);
|
|
405
|
+
if (floor < fIn) fail("Overflow");
|
|
406
|
+
b.floor = u128(floor - fIn + atF);
|
|
407
|
+
b.ckAtF = atF;
|
|
408
|
+
b.kAtF = kpool;
|
|
409
|
+
b.nBands = n;
|
|
410
|
+
b.nC = kOut ? n - 1 : nC;
|
|
411
|
+
b.bandEdge = Array.from({ length: BIDS_MAX + 1 }, (_, i) => (n > 0 && i <= n ? edge[i] : 0n));
|
|
412
|
+
b.bandMoney = segs.map((g, i) => (i < n ? g.money : 0n));
|
|
413
|
+
b.bandF = segs.map((g, i) => (i < n ? g.fIn : 0n));
|
|
414
|
+
b.bandTokens = tok.map((t, i) => (i < n ? t : 0n));
|
|
415
|
+
b.bandFClaim = claim.map((c, i) => (i < n ? c : 0n));
|
|
416
|
+
r.nC = b.nC;
|
|
417
|
+
r.nK = kOut ? 1 : 0;
|
|
418
|
+
r.atF = atF;
|
|
419
|
+
r.kAtF = kpool;
|
|
420
|
+
return r;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
export function kpotUpgrade(b: Book, row: SplitRow, circ: bigint): void {
|
|
424
|
+
if (b.kpot) return;
|
|
425
|
+
b.kpot = true;
|
|
426
|
+
if (!b.graduated) return;
|
|
427
|
+
let kAt = b.ckAtF / 2n;
|
|
428
|
+
for (let i = b.nC; i < b.nBands; i++) {
|
|
429
|
+
const fi = b.bandF[i] < b.bandMoney[i] ? b.bandF[i] : b.bandMoney[i];
|
|
430
|
+
const prem = b.bandMoney[i] - fi;
|
|
431
|
+
|
|
432
|
+
b.floor = u128(b.floor + b.bandMoney[i]);
|
|
433
|
+
b.ckAtF = u128(b.ckAtF + prem);
|
|
434
|
+
kAt = u128(kAt + prem);
|
|
435
|
+
b.bandMoney[i] = 0n;
|
|
436
|
+
b.bandF[i] = 0n;
|
|
437
|
+
}
|
|
438
|
+
b.kAtF = kAt;
|
|
439
|
+
b.kFloorTokens = 0n;
|
|
440
|
+
b.nC = b.nBands;
|
|
441
|
+
relayPots(b, row, { c: 0n, k: 0n }, { c: 0n, k: 0n }, circ);
|
|
442
|
+
}
|
package/src/math/v2/split.ts
CHANGED
|
@@ -19,6 +19,23 @@ const max = (a: bigint, b: bigint): bigint => (a > b ? a : b);
|
|
|
19
19
|
const sat = (a: bigint, b: bigint): bigint => (a > b ? a - b : 0n);
|
|
20
20
|
const cdiv = (a: bigint, d: bigint): bigint => (a + d - 1n) / d;
|
|
21
21
|
|
|
22
|
+
export const shareSplit = (x: bigint, cShareBps: number): [bigint, bigint] => {
|
|
23
|
+
const c = cdiv(x * BigInt(cShareBps), 10_000n);
|
|
24
|
+
return [c, x - c];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const potsSplit = (x: bigint, cShareBps: number, gapC: bigint, gapK: bigint): [bigint, bigint] => {
|
|
28
|
+
let [c, k] = shareSplit(x, cShareBps);
|
|
29
|
+
if (c > gapC) {
|
|
30
|
+
k += c - gapC;
|
|
31
|
+
c = gapC;
|
|
32
|
+
} else if (k > gapK) {
|
|
33
|
+
c += k - gapK;
|
|
34
|
+
k = gapK;
|
|
35
|
+
}
|
|
36
|
+
return [c, k];
|
|
37
|
+
};
|
|
38
|
+
|
|
22
39
|
const flatUp = (s: Sqrt, n: bigint): bigint => ov(flatMoneyForTokens(s, n, UP));
|
|
23
40
|
|
|
24
41
|
export interface SplitRow {
|
|
@@ -72,7 +89,9 @@ export interface SplitIn {
|
|
|
72
89
|
|
|
73
90
|
fClaims: bigint;
|
|
74
91
|
|
|
75
|
-
|
|
92
|
+
c0: bigint;
|
|
93
|
+
|
|
94
|
+
k0: bigint;
|
|
76
95
|
|
|
77
96
|
held: bigint;
|
|
78
97
|
|
|
@@ -82,9 +101,16 @@ export interface SplitIn {
|
|
|
82
101
|
export interface Split {
|
|
83
102
|
|
|
84
103
|
dF: bigint;
|
|
104
|
+
|
|
85
105
|
ckAdd: bigint;
|
|
86
106
|
|
|
87
107
|
ckTake: bigint;
|
|
108
|
+
|
|
109
|
+
cAdd: bigint;
|
|
110
|
+
kAdd: bigint;
|
|
111
|
+
|
|
112
|
+
cTake: bigint;
|
|
113
|
+
kTake: bigint;
|
|
88
114
|
ms: bigint;
|
|
89
115
|
booked: [bigint, bigint, bigint, bigint];
|
|
90
116
|
sqrtFNew: Sqrt;
|
|
@@ -104,8 +130,16 @@ export function split(row: SplitRow, ranges: readonly Range[], inp: SplitIn, pie
|
|
|
104
130
|
|
|
105
131
|
const s0 = surplus(inp.sqrtF, fr + inp.fClaims, sr + inp.held);
|
|
106
132
|
const phi = BigInt(row.phiBps);
|
|
107
|
-
let
|
|
133
|
+
let cAdd = 0n;
|
|
134
|
+
let kAdd = 0n;
|
|
108
135
|
let ms = 0n;
|
|
136
|
+
const cs = BigInt(row.cShareBps);
|
|
137
|
+
|
|
138
|
+
const force = (x: bigint): void => {
|
|
139
|
+
const xc = cdiv(x * cs, 10_000n);
|
|
140
|
+
cAdd += xc;
|
|
141
|
+
kAdd += x - xc;
|
|
142
|
+
};
|
|
109
143
|
let topNeed = 0n;
|
|
110
144
|
const booked: [bigint, bigint, bigint, bigint] = [0n, 0n, 0n, 0n];
|
|
111
145
|
|
|
@@ -126,33 +160,53 @@ export function split(row: SplitRow, ranges: readonly Range[], inp: SplitIn, pie
|
|
|
126
160
|
const rem = pc.money - dF;
|
|
127
161
|
if (r.thetaBps >= 10_000) {
|
|
128
162
|
|
|
129
|
-
|
|
163
|
+
force(rem);
|
|
130
164
|
continue;
|
|
131
165
|
}
|
|
132
166
|
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
167
|
+
const t = cdiv(flatUp(pc.sqrtHi, sr) * BigInt(row.ckBps), 10_000n);
|
|
168
|
+
const tc = cdiv(t * cs, 10_000n);
|
|
169
|
+
const gapC = sat(tc, inp.c0 + cAdd);
|
|
170
|
+
const gapK = sat(t - tc, inp.k0 + kAdd);
|
|
171
|
+
const m = min(rem, gapC + gapK);
|
|
172
|
+
let mc = cdiv(m * cs, 10_000n);
|
|
173
|
+
let mk = m - mc;
|
|
174
|
+
|
|
175
|
+
if (mc > gapC) {
|
|
176
|
+
mk += mc - gapC;
|
|
177
|
+
mc = gapC;
|
|
178
|
+
} else if (mk > gapK) {
|
|
179
|
+
mc += mk - gapK;
|
|
180
|
+
mk = gapK;
|
|
181
|
+
}
|
|
182
|
+
cAdd += mc;
|
|
183
|
+
kAdd += mk;
|
|
184
|
+
let mm = rem - m;
|
|
136
185
|
if (r.msCapPpm > 0) {
|
|
137
186
|
const capm = (pc.money * BigInt(r.msCapPpm)) / 1_000_000n;
|
|
138
187
|
if (mm > capm) {
|
|
139
|
-
|
|
188
|
+
force(mm - capm);
|
|
140
189
|
mm = capm;
|
|
141
190
|
}
|
|
142
191
|
}
|
|
143
|
-
ckAdd += ck;
|
|
144
192
|
ms += mm;
|
|
145
193
|
}
|
|
146
194
|
|
|
147
|
-
let
|
|
195
|
+
let cTake = 0n;
|
|
196
|
+
let kTake = 0n;
|
|
148
197
|
const short = sat(topNeed, fr + inp.fClaims);
|
|
149
|
-
if (short > 0n && inp.
|
|
150
|
-
const
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
198
|
+
if (short > 0n && inp.c0 + inp.k0 + cAdd + kAdd > 0n) {
|
|
199
|
+
const x = min(short, cAdd + kAdd);
|
|
200
|
+
const [ac, ak] = potsSplit(x, 5_000, cAdd, kAdd);
|
|
201
|
+
let left = short - x;
|
|
202
|
+
const m2 = min(left, ms);
|
|
203
|
+
left -= m2;
|
|
204
|
+
const y = min(left, inp.c0 + inp.k0);
|
|
205
|
+
[cTake, kTake] = potsSplit(y, 5_000, inp.c0, inp.k0);
|
|
206
|
+
cAdd -= ac;
|
|
207
|
+
kAdd -= ak;
|
|
154
208
|
ms -= m2;
|
|
155
|
-
fr +=
|
|
209
|
+
fr += ac + ak + m2 + cTake + kTake;
|
|
156
210
|
}
|
|
157
211
|
|
|
158
212
|
booked[2] = (ms * BigInt(row.platformMsBps)) / 10_000n;
|
|
@@ -162,7 +216,7 @@ export function split(row: SplitRow, ranges: readonly Range[], inp: SplitIn, pie
|
|
|
162
216
|
const ratio = sqrtOfRatio(fr + inp.fClaims, inp.sNew + inp.held);
|
|
163
217
|
const capSqrt = ov(sqrtScaleDownBps(inp.sqrtP1, row.wcBps));
|
|
164
218
|
const sqrtFNew = max(inp.sqrtF, ratio === null ? inp.sqrtF : min(ratio, capSqrt));
|
|
165
|
-
return { dF: fr - inp.fTotal, ckAdd: u128(
|
|
219
|
+
return { dF: fr - inp.fTotal, ckAdd: u128(cAdd + kAdd), ckTake: cTake + kTake, cAdd: u128(cAdd), kAdd: u128(kAdd), cTake, kTake, ms: u128(ms), booked, sqrtFNew };
|
|
166
220
|
}
|
|
167
221
|
|
|
168
222
|
export const pieceEntersSplit = (r: Range): boolean => r.kind !== KIND_BONDING;
|
package/src/math/v2/trade.ts
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
import { fail } from "../index.ts";
|
|
4
4
|
import { U128_MAX } from "./wide.ts";
|
|
5
5
|
import {
|
|
6
|
-
Undo,
|
|
6
|
+
Undo, cMoney, ckHeld, cloneBook, kMoney, fClaims, fOwn, feesAt, feesNow, MAX_WALK_STEPS, moneyHeld, tokensHeld, walkDownAcc, walkUpFrom,
|
|
7
7
|
} from "./book.ts";
|
|
8
8
|
import type { Book } from "./book.ts";
|
|
9
9
|
import { FeeAcc, LEG_BACKING, cheaper, feeTotal, netFromGross } from "./fees.ts";
|
|
10
|
-
import {
|
|
10
|
+
import { kpotUpgrade, relayPots } from "./relay.ts";
|
|
11
11
|
import type { Relay } from "./relay.ts";
|
|
12
12
|
import { split } from "./split.ts";
|
|
13
13
|
import type { Split, SplitRow } from "./split.ts";
|
|
@@ -30,10 +30,15 @@ export interface BookSell {
|
|
|
30
30
|
relay: Relay;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
const noSplit = (): Split => ({ dF: 0n, ckAdd: 0n, ckTake: 0n, ms: 0n, booked: [0n, 0n, 0n, 0n], sqrtFNew: 0n });
|
|
34
|
-
const noRelay = (): Relay => ({ nC: 0, nK: 0, laidC: 0n, laidK: 0n, atF: 0n, kept: 0 });
|
|
33
|
+
const noSplit = (): Split => ({ dF: 0n, ckAdd: 0n, ckTake: 0n, cAdd: 0n, kAdd: 0n, cTake: 0n, kTake: 0n, ms: 0n, booked: [0n, 0n, 0n, 0n], sqrtFNew: 0n });
|
|
34
|
+
const noRelay = (): Relay => ({ nC: 0, nK: 0, laidC: 0n, laidK: 0n, atF: 0n, kAtF: 0n, kept: 0 });
|
|
35
35
|
const add = (a: bigint, b: bigint): bigint => (a + b > U128_MAX ? fail("Overflow") : a + b);
|
|
36
36
|
|
|
37
|
+
function upgrade(b: Book, row: SplitRow, sup: Supply): void {
|
|
38
|
+
if (b.kpot) return;
|
|
39
|
+
kpotUpgrade(b, row, b.graduated ? sCirc(b, sup) : 0n);
|
|
40
|
+
}
|
|
41
|
+
|
|
37
42
|
function commit(dst: Book, src: Book): void {
|
|
38
43
|
Object.assign(dst, src);
|
|
39
44
|
}
|
|
@@ -45,6 +50,8 @@ function buyOn(book: Book, side: Side, gross: bigint, minTokens: bigint, walkSte
|
|
|
45
50
|
const budget = netFromGross(gross, ppm);
|
|
46
51
|
if (budget === 0n) fail("NothingToDo");
|
|
47
52
|
const w = cloneBook(book);
|
|
53
|
+
|
|
54
|
+
if (side !== null) upgrade(w, side.row, side.sup);
|
|
48
55
|
const u = new Undo();
|
|
49
56
|
|
|
50
57
|
const acc = new FeeAcc(ppm);
|
|
@@ -70,12 +77,12 @@ function buyOn(book: Book, side: Side, gross: bigint, minTokens: bigint, walkSte
|
|
|
70
77
|
|
|
71
78
|
if (u.bought.length > 0) {
|
|
72
79
|
sp = split(side.row, w.ranges.slice(0, w.nRanges), {
|
|
73
|
-
sqrtF: w.sqrtF, sqrtP1: w.sqrtP, fTotal: fOwn(w), fClaims: fClaims(w),
|
|
80
|
+
sqrtF: w.sqrtF, sqrtP1: w.sqrtP, fTotal: fOwn(w), fClaims: fClaims(w), c0: cMoney(w), k0: kMoney(w), held: ckHeld(w), sNew,
|
|
74
81
|
}, u.bought);
|
|
75
82
|
w.floor = add(w.floor, sp.dF);
|
|
76
83
|
w.sqrtF = sp.sqrtFNew;
|
|
77
84
|
}
|
|
78
|
-
rl =
|
|
85
|
+
rl = relayPots(w, side.row, { c: sp.cAdd, k: sp.kAdd }, { c: sp.cTake, k: sp.kTake }, sNew);
|
|
79
86
|
}
|
|
80
87
|
const r: BookBuy = {
|
|
81
88
|
gross, net: walked.money, refund: gross - paid, tokens: walked.tokens, fees, fromResale: walked.fromResale, fromFirst: walked.fromFirst,
|
|
@@ -88,8 +95,9 @@ function buyOn(book: Book, side: Side, gross: bigint, minTokens: bigint, walkSte
|
|
|
88
95
|
|
|
89
96
|
function sellOn(book: Book, side: Side, tokens: bigint, minOut: bigint, keep = true): BookSell {
|
|
90
97
|
if (tokens === 0n) fail("NothingToDo");
|
|
91
|
-
const ppm = feesNow(book);
|
|
92
98
|
const w = cloneBook(book);
|
|
99
|
+
if (side !== null) upgrade(w, side.row, side.sup);
|
|
100
|
+
const ppm = feesNow(w);
|
|
93
101
|
|
|
94
102
|
const acc = new FeeAcc(ppm);
|
|
95
103
|
const walked = walkDownAcc(w, tokens, acc);
|
|
@@ -100,7 +108,7 @@ function sellOn(book: Book, side: Side, tokens: bigint, minOut: bigint, keep = t
|
|
|
100
108
|
if (out < minOut) fail("SlippageExceeded");
|
|
101
109
|
w.floor = add(w.floor, fees[LEG_BACKING]);
|
|
102
110
|
|
|
103
|
-
const rl = w.graduated && side !== null ?
|
|
111
|
+
const rl = w.graduated && side !== null ? relayPots(w, side.row, { c: 0n, k: 0n }, { c: 0n, k: 0n }, sCirc(w, side.sup)) : noRelay();
|
|
104
112
|
const r: BookSell = {
|
|
105
113
|
tokensIn: tokens, unfilled: tokens - walked.tokens, gross: walked.money, fees, out, steps: walked.steps, sqrtBefore: book.sqrtP, sqrtAfter: w.sqrtP,
|
|
106
114
|
fromF: walked.fromF, fromK: walked.fromK, fromC: walked.fromC, relay: rl,
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { Address, beginCell, contractAddress, EMPTY_CELL } from "./cell.ts";
|
|
4
|
+
import type { Cell } from "./cell.ts";
|
|
5
|
+
import type { AddressLike, TonChain, TonCodes } from "./types.ts";
|
|
6
|
+
|
|
7
|
+
export function addr(a: AddressLike): Address {
|
|
8
|
+
return typeof a === "string" ? Address.parse(a) : a;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const factoryData = (authority: AddressLike, salt: bigint): Cell => beginCell().storeAddress(addr(authority)).storeUint(salt, 64).endCell();
|
|
12
|
+
export const launchData = (factory: AddressLike, id: bigint): Cell => beginCell().storeAddress(addr(factory)).storeUint(id, 64).endCell();
|
|
13
|
+
|
|
14
|
+
export const walletData = (owner: AddressLike, master: AddressLike, position: Cell = EMPTY_CELL): Cell =>
|
|
15
|
+
beginCell().storeCoins(0).storeAddress(addr(owner)).storeAddress(addr(master)).storeRef(position).endCell();
|
|
16
|
+
export const accountData = (launch: AddressLike, owner: AddressLike): Cell => beginCell().storeAddress(addr(launch)).storeAddress(addr(owner)).endCell();
|
|
17
|
+
export const traderData = (factory: AddressLike, trader: AddressLike): Cell => beginCell().storeAddress(addr(factory)).storeAddress(addr(trader)).endCell();
|
|
18
|
+
|
|
19
|
+
export function factoryAddress(codes: Pick<TonCodes, "factory">, authority: AddressLike, salt: bigint): Address {
|
|
20
|
+
if (!codes.factory) throw new RangeError("factory init code not supplied");
|
|
21
|
+
return contractAddress(codes.factory, factoryData(authority, salt));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function launchAddress(chain: Pick<TonChain, "factory" | "codes">, id: bigint): Address {
|
|
25
|
+
if (id < 1n) throw new RangeError("launch ids start at 1");
|
|
26
|
+
if (!chain.codes.launch) throw new RangeError("launch init code not supplied: read the address with getLaunchAddress, or pass codes.launch");
|
|
27
|
+
return contractAddress(chain.codes.launch, launchData(chain.factory, id));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function launchWalletAddress(chain: Pick<TonChain, "codes">, launch: AddressLike, owner: AddressLike, position?: Cell): Address {
|
|
31
|
+
return contractAddress(chain.codes.wallet, walletData(owner, launch, position));
|
|
32
|
+
}
|
|
33
|
+
export function accountAddress(chain: Pick<TonChain, "codes">, launch: AddressLike, owner: AddressLike): Address {
|
|
34
|
+
return contractAddress(chain.codes.account, accountData(launch, owner));
|
|
35
|
+
}
|
|
36
|
+
export function traderAddress(chain: Pick<TonChain, "factory" | "codes">, trader: AddressLike): Address {
|
|
37
|
+
return contractAddress(chain.codes.trader, traderData(chain.factory, trader));
|
|
38
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
|
|
2
|
+
import type { TonBook } from "./book.ts";
|
|
3
|
+
import { getFamily, getTimelockInfo } from "./reader.ts";
|
|
4
|
+
import type { TonRpc } from "./reader.ts";
|
|
5
|
+
|
|
6
|
+
export const TON_FAMILY = { factory: 0, launch: 1, wallet: 2, account: 3, trader: 4 } as const;
|
|
7
|
+
|
|
8
|
+
const hex = (n: bigint): string => n.toString(16).padStart(64, "0");
|
|
9
|
+
|
|
10
|
+
export async function checkTonBookOnChain(rpc: TonRpc, book: TonBook): Promise<string[]> {
|
|
11
|
+
const out: string[] = [];
|
|
12
|
+
for (const name of ["launch", "wallet", "account", "trader"] as const) {
|
|
13
|
+
const f = await getFamily(rpc, book.factory, TON_FAMILY[name]);
|
|
14
|
+
if (f.initHash === 0n) {
|
|
15
|
+
out.push(`family ${name}: not set on chain`);
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
if (hex(f.codeHash) !== book.codeHashes[name]) out.push(`family ${name}: chain code ${hex(f.codeHash)} is not the book's ${book.codeHashes[name]}`);
|
|
19
|
+
if (hex(f.initHash) !== book.initHashes[name]) out.push(`family ${name}: chain init ${hex(f.initHash)} is not the book's ${book.initHashes[name]}`);
|
|
20
|
+
}
|
|
21
|
+
const t = await getTimelockInfo(rpc, book.timelock);
|
|
22
|
+
if (!t.factory || !t.factory.equals(book.factory)) out.push(`timelock: factory ${t.factory ? t.factory.toRaw() : "none"} is not the book's ${book.factory.toRaw()}`);
|
|
23
|
+
return out;
|
|
24
|
+
}
|
package/src/ton/book.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { Address, bytesOfBase64, fromBoc, hexOf } from "./cell.ts";
|
|
4
|
+
import type { Cell } from "./cell.ts";
|
|
5
|
+
import type { TonChain } from "./types.ts";
|
|
6
|
+
import { TON_MAINNET, TON_INIT_CODES_BASE64, TON_PINNED_CODE_HASHES as GEN_HASHES } from "./consts.generated.ts";
|
|
7
|
+
|
|
8
|
+
export interface TonBook {
|
|
9
|
+
network: "mainnet" | "testnet";
|
|
10
|
+
globalId: -239 | -3;
|
|
11
|
+
factory: Address;
|
|
12
|
+
timelock: Address;
|
|
13
|
+
deploySeqno?: number;
|
|
14
|
+
|
|
15
|
+
codeHashes: Readonly<Record<"factory" | "timelock" | "launch" | "wallet" | "account" | "trader", string>>;
|
|
16
|
+
|
|
17
|
+
initHashes: Readonly<Record<"launch" | "wallet" | "account" | "trader", string>>;
|
|
18
|
+
codes: Readonly<{ wallet: Cell; account: Cell; trader: Cell }>;
|
|
19
|
+
|
|
20
|
+
readAt: string;
|
|
21
|
+
}
|
|
22
|
+
export type TonCodePins = Readonly<{ wallet: string; account: string; trader: string }>;
|
|
23
|
+
|
|
24
|
+
export { TON_PINNED_CODE_HASHES } from "./consts.generated.ts";
|
|
25
|
+
|
|
26
|
+
export class TonBookError extends Error {
|
|
27
|
+
readonly field: string;
|
|
28
|
+
readonly file: string;
|
|
29
|
+
constructor(file: string, field: string, what: string) {
|
|
30
|
+
super(`ton book ${file}: ${field}: ${what}`);
|
|
31
|
+
this.name = "TonBookError";
|
|
32
|
+
this.field = field;
|
|
33
|
+
this.file = file;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const HEX64 = /^[0-9a-fA-F]{64}$/;
|
|
38
|
+
const isObj = (x: unknown): x is Record<string, unknown> => typeof x === "object" && x !== null && !Array.isArray(x);
|
|
39
|
+
const HASH_NAMES = ["factory", "timelock", "launch", "wallet", "account", "trader"] as const;
|
|
40
|
+
const INIT_NAMES = ["launch", "account", "trader"] as const;
|
|
41
|
+
const CODE_NAMES = ["wallet", "account", "trader"] as const;
|
|
42
|
+
|
|
43
|
+
export function parseTonBook(json: unknown, opts: { file?: string; pins?: TonCodePins } = {}): TonBook {
|
|
44
|
+
const file = opts.file ?? "<memory>";
|
|
45
|
+
const pins: TonCodePins = opts.pins ?? GEN_HASHES;
|
|
46
|
+
const fail = (field: string, what: string): never => {
|
|
47
|
+
throw new TonBookError(file, field, what);
|
|
48
|
+
};
|
|
49
|
+
if (!isObj(json)) return fail("(root)", "not a JSON object");
|
|
50
|
+
if (json.chain !== "ton") fail("chain", 'must be "ton"');
|
|
51
|
+
const network = json.network;
|
|
52
|
+
if (network !== "mainnet" && network !== "testnet") fail("network", `must be "mainnet" or "testnet", got "${String(network)}"`);
|
|
53
|
+
const wantId = network === "mainnet" ? -239 : -3;
|
|
54
|
+
if (json.globalId !== wantId) fail("globalId", `${wantId} expected for ${String(network)}, got ${String(json.globalId)}`);
|
|
55
|
+
const rawAddr = (field: "factory" | "timelock"): Address => {
|
|
56
|
+
const v = json[field];
|
|
57
|
+
if (typeof v !== "string" || !/^0:[0-9a-fA-F]{64}$/.test(v)) return fail(field, 'not a workchain-0 raw address "0:<64 hex>"');
|
|
58
|
+
return Address.parse(v);
|
|
59
|
+
};
|
|
60
|
+
const factory = rawAddr("factory");
|
|
61
|
+
const timelock = rawAddr("timelock");
|
|
62
|
+
let deploySeqno: number | undefined;
|
|
63
|
+
if (json.deploySeqno !== undefined) {
|
|
64
|
+
const d = json.deploySeqno;
|
|
65
|
+
if (typeof d !== "number" || !Number.isInteger(d) || d < 1) fail("deploySeqno", "must be an integer of at least 1");
|
|
66
|
+
deploySeqno = d as number;
|
|
67
|
+
}
|
|
68
|
+
const ra = json.readAt;
|
|
69
|
+
const readAt = typeof ra === "string" ? ra : isObj(ra) && typeof ra.time === "string" ? ra.time : undefined;
|
|
70
|
+
if (readAt === undefined || !Number.isFinite(Date.parse(readAt))) fail("readAt", 'not an ISO timestamp (a string, or an object with "time")');
|
|
71
|
+
const ch = json.codeHashes;
|
|
72
|
+
const codeHashes: Record<string, string> = {};
|
|
73
|
+
for (const n of HASH_NAMES) {
|
|
74
|
+
const v = isObj(ch) ? ch[n] : undefined;
|
|
75
|
+
if (typeof v !== "string" || !HEX64.test(v)) fail(`codeHashes.${n}`, "not 64 hex");
|
|
76
|
+
codeHashes[n] = (v as string).toLowerCase();
|
|
77
|
+
}
|
|
78
|
+
const init: Record<string, string> = {};
|
|
79
|
+
const fi = json.familyInit;
|
|
80
|
+
if (fi !== undefined) {
|
|
81
|
+
for (const n of INIT_NAMES) {
|
|
82
|
+
const v = isObj(fi) ? fi[n] : undefined;
|
|
83
|
+
if (typeof v !== "string" || !HEX64.test(v)) fail(`familyInit.${n}`, "not 64 hex");
|
|
84
|
+
init[n] = (v as string).toLowerCase();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const initHashes = {
|
|
88
|
+
launch: init.launch ?? codeHashes.launch!,
|
|
89
|
+
wallet: codeHashes.wallet!,
|
|
90
|
+
account: init.account ?? codeHashes.account!,
|
|
91
|
+
trader: init.trader ?? codeHashes.trader!,
|
|
92
|
+
};
|
|
93
|
+
const rawCodes = json.codes;
|
|
94
|
+
if (rawCodes === undefined || rawCodes === null) fail("codes", "missing (a read-back book carries hashes only: add the three init code cells)");
|
|
95
|
+
const cells: Record<string, Cell> = {};
|
|
96
|
+
for (const n of CODE_NAMES) {
|
|
97
|
+
const v = isObj(rawCodes) ? rawCodes[n] : undefined;
|
|
98
|
+
let c: Cell | undefined;
|
|
99
|
+
if (typeof v === "string") {
|
|
100
|
+
try {
|
|
101
|
+
c = fromBoc(bytesOfBase64(v));
|
|
102
|
+
} catch {
|
|
103
|
+
c = undefined;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (!c) return fail(`codes.${n}`, "not a base64 BoC with one root");
|
|
107
|
+
cells[n] = c;
|
|
108
|
+
}
|
|
109
|
+
for (const n of CODE_NAMES) {
|
|
110
|
+
const got = hexOf(cells[n]!.hash());
|
|
111
|
+
if (got !== initHashes[n]) fail(`codes.${n}`, `cell hash ${got} is not the book's ${n} init hash ${initHashes[n]}`);
|
|
112
|
+
}
|
|
113
|
+
for (const n of CODE_NAMES) {
|
|
114
|
+
const got = hexOf(cells[n]!.hash());
|
|
115
|
+
if (got !== pins[n].toLowerCase()) fail(`codes.${n}`, `${got} is not the pinned ${n} init code hash ${pins[n]} (a child address would move)`);
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
network: network as "mainnet" | "testnet",
|
|
119
|
+
globalId: wantId as -239 | -3,
|
|
120
|
+
factory,
|
|
121
|
+
timelock,
|
|
122
|
+
...(deploySeqno !== undefined ? { deploySeqno } : {}),
|
|
123
|
+
codeHashes: codeHashes as TonBook["codeHashes"],
|
|
124
|
+
initHashes,
|
|
125
|
+
codes: { wallet: cells.wallet!, account: cells.account!, trader: cells.trader! },
|
|
126
|
+
readAt: readAt as string,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function tonChainOf(book?: TonBook): TonChain {
|
|
131
|
+
if (book) return { family: "ton", factory: book.factory, codes: { wallet: book.codes.wallet, account: book.codes.account, trader: book.codes.trader }, workchain: 0 };
|
|
132
|
+
const m = TON_MAINNET;
|
|
133
|
+
if (m.factory === null || m.timelock === null) throw new TonBookError("<built-in>", "factory", "TON mainnet factory is a placeholder (no deploy yet): pass a book, or regenerate the constants from the deploy output");
|
|
134
|
+
const code = (n: "wallet" | "account" | "trader"): Cell => fromBoc(bytesOfBase64(TON_INIT_CODES_BASE64[n]));
|
|
135
|
+
return { family: "ton", factory: Address.parse(m.factory), codes: { wallet: code("wallet"), account: code("account"), trader: code("trader") }, workchain: 0 };
|
|
136
|
+
}
|