@floorlaunchpad/sdk 0.4.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/CHANGELOG.md +93 -0
- package/dist/abi/evm.gen.js +5 -0
- package/dist/abi/functions.gen.js +1 -1
- package/dist/client/solana/index.js +13 -8
- package/dist/config/generated.js +78 -1
- package/dist/config/registry.gen.js +1 -1
- package/dist/evm/chains.js +56 -18
- package/dist/evm/squid.js +10 -10
- package/dist/evm/zap.js +148 -35
- package/dist/index.js +1 -0
- package/dist/indexer/bodies.js +9 -0
- package/dist/indexer/client.js +2 -0
- package/dist/indexer/contract.js +48 -2
- package/dist/indexer/paths.js +26 -0
- package/dist/indexer/read.js +3 -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/devbuy.js +6 -4
- package/dist/v2/evm.js +3 -1
- package/dist/v2/layouts.js +5 -1
- package/dist/v2/presetRowEvm.js +21 -2
- package/dist/v2/router.js +2 -2
- package/dist/v2/solanaSettle.js +2 -0
- package/docs/ton.md +107 -0
- package/examples/floor-line.html +1 -1
- package/package.json +6 -1
- package/src/abi/evm.gen.ts +5 -0
- package/src/abi/functions.gen.ts +1 -1
- package/src/client/solana/index.ts +13 -8
- package/src/config/generated.ts +78 -1
- package/src/config/registry.gen.ts +1 -1
- package/src/evm/chains.ts +63 -17
- package/src/evm/squid.ts +10 -10
- package/src/evm/zap.ts +185 -36
- package/src/index.ts +1 -0
- package/src/indexer/bodies.ts +11 -0
- package/src/indexer/client.ts +5 -1
- package/src/indexer/contract.ts +48 -2
- package/src/indexer/paths.ts +47 -0
- package/src/indexer/read.ts +5 -2
- 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/devbuy.ts +6 -4
- package/src/v2/evm.ts +5 -1
- package/src/v2/layouts.ts +5 -1
- package/src/v2/presetRowEvm.ts +21 -2
- 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/book.ts
CHANGED
|
@@ -66,6 +66,12 @@ export interface Book {
|
|
|
66
66
|
ckAtF: bigint;
|
|
67
67
|
|
|
68
68
|
ckFloorTokens: bigint;
|
|
69
|
+
|
|
70
|
+
kpot: boolean;
|
|
71
|
+
|
|
72
|
+
kAtF: bigint;
|
|
73
|
+
|
|
74
|
+
kFloorTokens: bigint;
|
|
69
75
|
feePpm: number[];
|
|
70
76
|
|
|
71
77
|
feeStartPpm: number[];
|
|
@@ -125,7 +131,7 @@ export function emptyBook(sqrtP: Sqrt, feePpm: readonly number[]): Book {
|
|
|
125
131
|
return {
|
|
126
132
|
sqrtP, sqrtF: 0n, sqrtLMax: 0n, floor: 0n, floorTokens: 0n, nBands: 0, nC: 0,
|
|
127
133
|
bandEdge: new Array(BIDS_MAX + 1).fill(0n), bandMoney: new Array(BIDS_MAX).fill(0n), bandTokens: new Array(BIDS_MAX).fill(0n),
|
|
128
|
-
bandF: new Array(BIDS_MAX).fill(0n), bandFClaim: new Array(BIDS_MAX).fill(0n), ckAtF: 0n, ckFloorTokens: 0n, feePpm: [...feePpm], feeStartPpm: [0, 0, 0, 0, 0], sqrtFeeA: 0n, sqrtFeeB: 0n, graduated: false, nRanges: 0, ranges: Array.from({ length: RANGES_MAX }, emptyRange),
|
|
134
|
+
bandF: new Array(BIDS_MAX).fill(0n), bandFClaim: new Array(BIDS_MAX).fill(0n), ckAtF: 0n, ckFloorTokens: 0n, kpot: true, kAtF: 0n, kFloorTokens: 0n, feePpm: [...feePpm], feeStartPpm: [0, 0, 0, 0, 0], sqrtFeeA: 0n, sqrtFeeB: 0n, graduated: false, nRanges: 0, ranges: Array.from({ length: RANGES_MAX }, emptyRange),
|
|
129
135
|
};
|
|
130
136
|
}
|
|
131
137
|
|
|
@@ -249,6 +255,21 @@ export const ckMoney = (b: Book): bigint => sum(b.bandMoney, b.nBands) - sum(b.b
|
|
|
249
255
|
|
|
250
256
|
export const ckHeld = (b: Book): bigint => sum(b.bandTokens, b.nBands) + b.ckFloorTokens;
|
|
251
257
|
|
|
258
|
+
export const kSeg = (b: Book): number | null => (b.kpot && b.nBands === b.nC + 1 ? b.nC : null);
|
|
259
|
+
|
|
260
|
+
export const cMoney = (b: Book): bigint => sum(b.bandMoney, b.nC) - sum(b.bandF, b.nC) + (b.ckAtF - b.kAtF);
|
|
261
|
+
|
|
262
|
+
export const kMoney = (b: Book): bigint => ckMoney(b) - cMoney(b);
|
|
263
|
+
|
|
264
|
+
export const cHeld = (b: Book): bigint => sum(b.bandTokens, b.nC) + (b.ckFloorTokens - b.kFloorTokens);
|
|
265
|
+
|
|
266
|
+
export const kHeld = (b: Book): bigint => ckHeld(b) - cHeld(b);
|
|
267
|
+
|
|
268
|
+
export function kHolds(b: Book): boolean {
|
|
269
|
+
const k = kSeg(b);
|
|
270
|
+
return k !== null && (b.bandTokens[k] > 0n || b.bandFClaim[k] > 0n);
|
|
271
|
+
}
|
|
272
|
+
|
|
252
273
|
const bidMoneySum = (b: Book): bigint => b.floor + sum(b.bandMoney, b.nBands);
|
|
253
274
|
|
|
254
275
|
export const placeMoney = (b: Book): bigint => {
|
|
@@ -312,6 +333,8 @@ export function checkBook(b: Book): void {
|
|
|
312
333
|
}
|
|
313
334
|
}
|
|
314
335
|
if (b.ckAtF > b.floor || b.ckFloorTokens > b.floorTokens) fail("Overflow");
|
|
336
|
+
if (b.kAtF > b.ckAtF || b.kFloorTokens > b.ckFloorTokens) fail("Overflow");
|
|
337
|
+
if (b.kpot && b.nBands > b.nC + 1) fail("SqrtRange");
|
|
315
338
|
}
|
|
316
339
|
|
|
317
340
|
function flatUp(b: Book): FlatPart | null {
|
|
@@ -396,6 +419,14 @@ function floorRebuy(b: Book, take: bigint, cost: bigint): void {
|
|
|
396
419
|
if (xe > 0n) {
|
|
397
420
|
const back = (cost * xe) / x0;
|
|
398
421
|
const gone = min((take * xe + x0 - 1n) / x0, xe);
|
|
422
|
+
|
|
423
|
+
const xk = b.kFloorTokens;
|
|
424
|
+
if (xk > 0n) {
|
|
425
|
+
const backK = (back * xk) / xe;
|
|
426
|
+
const goneK = min(min((gone * xk + xe - 1n) / xe, xk), gone);
|
|
427
|
+
b.kAtF = u128(b.kAtF + backK);
|
|
428
|
+
b.kFloorTokens -= goneK;
|
|
429
|
+
}
|
|
399
430
|
b.ckAtF = u128(b.ckAtF + back);
|
|
400
431
|
b.ckFloorTokens -= gone;
|
|
401
432
|
}
|
|
@@ -587,12 +618,14 @@ export function walkDownAcc(b: Book, tokens: bigint, acc: FeeAcc, u: Undo = new
|
|
|
587
618
|
charge(b, acc, q);
|
|
588
619
|
if (fp.t === "floor") {
|
|
589
620
|
|
|
590
|
-
const ce = min(q, b.ckAtF);
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
621
|
+
const ce = min(q, b.ckAtF - b.kAtF);
|
|
622
|
+
const ke = min(q - ce, b.kAtF);
|
|
623
|
+
const xc = ce > 0n ? min(ov(flatTokensForMoney(s, ce, DOWN)), take) : 0n;
|
|
624
|
+
const xk = ke > 0n ? min(ov(flatTokensForMoney(s, ke, DOWN)), take - xc) : 0n;
|
|
625
|
+
b.ckAtF -= ce + ke;
|
|
626
|
+
b.kAtF -= ke;
|
|
627
|
+
b.ckFloorTokens = u64(b.ckFloorTokens + xc + xk);
|
|
628
|
+
b.kFloorTokens = u64(b.kFloorTokens + xk);
|
|
596
629
|
b.floor -= q;
|
|
597
630
|
b.floorTokens = u64(b.floorTokens + take);
|
|
598
631
|
w.fromF += q;
|
|
@@ -638,6 +671,8 @@ export function walkDownAcc(b: Book, tokens: bigint, acc: FeeAcc, u: Undo = new
|
|
|
638
671
|
|
|
639
672
|
b.floor = u128(b.floor + b.bandMoney[j]);
|
|
640
673
|
b.ckAtF = u128(b.ckAtF + b.bandMoney[j] - b.bandF[j]);
|
|
674
|
+
|
|
675
|
+
if (kSeg(b) === j) b.kAtF = u128(b.kAtF + b.bandMoney[j] - b.bandF[j]);
|
|
641
676
|
b.bandMoney[j] = 0n;
|
|
642
677
|
b.bandF[j] = 0n;
|
|
643
678
|
}
|
package/src/math/v2/create.ts
CHANGED
|
@@ -132,7 +132,7 @@ export function teamBuyV2(book: Book, gross: bigint, max: bigint): BookBuy {
|
|
|
132
132
|
const take = gross < max ? gross : max;
|
|
133
133
|
if (take === 0n) {
|
|
134
134
|
return { gross, net: 0n, refund: gross, tokens: 0n, fees: [0n, 0n, 0n, 0n, 0n], fromResale: 0n, fromFirst: 0n, steps: 0, budgetHit: false, graduatedNow: false, sqrtBefore: book.sqrtP, sqrtAfter: book.sqrtP,
|
|
135
|
-
split: { dF: 0n, ckAdd: 0n, ckTake: 0n, ms: 0n, booked: [0n, 0n, 0n, 0n], sqrtFNew: 0n }, relay: { nC: 0, nK: 0, laidC: 0n, laidK: 0n, atF: 0n, kept: 0 } };
|
|
135
|
+
split: { dF: 0n, ckAdd: 0n, ckTake: 0n, cAdd: 0n, kAdd: 0n, cTake: 0n, kTake: 0n, ms: 0n, booked: [0n, 0n, 0n, 0n], sqrtFNew: 0n }, relay: { nC: 0, nK: 0, laidC: 0n, laidK: 0n, atF: 0n, kAtF: 0n, kept: 0 } };
|
|
136
136
|
}
|
|
137
137
|
const r = bookBuy(book, take, 0n);
|
|
138
138
|
return { ...r, refund: r.refund + gross - take, gross };
|
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
|
+
}
|