@floorlaunchpad/sdk 0.4.1 → 0.7.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 +219 -18
- package/README.md +0 -2
- package/dist/abi/evm-event-names.js +7 -0
- package/dist/abi/evm.gen.js +6 -0
- package/dist/abi/functions.gen.js +49 -1
- package/dist/abi/solana.gen.js +5 -0
- package/dist/config/generated.js +77 -0
- package/dist/errors.js +2 -2
- package/dist/events/types.gen.js +1 -1
- package/dist/evm/chains.js +32 -24
- package/dist/evm/floor/calls.js +5 -0
- package/dist/evm/router.js +1 -25
- package/dist/evm/squid.js +3 -3
- package/dist/generated/interface.gen.js +48 -43
- package/dist/index.js +1 -0
- package/dist/indexer/bodies.js +137 -2
- package/dist/indexer/client.js +10 -1
- package/dist/indexer/contract.js +65 -6
- package/dist/indexer/paths.js +34 -7
- package/dist/indexer/read.js +12 -3
- package/dist/indexer/stream.js +2 -2
- package/dist/math/v2/book.js +40 -7
- package/dist/math/v2/create.js +1 -1
- package/dist/math/v2/fees.js +30 -4
- package/dist/math/v2/milestones.js +1 -1
- package/dist/math/v2/receipt.js +3 -3
- package/dist/math/v2/relay.js +201 -1
- package/dist/math/v2/rows.js +17 -0
- package/dist/math/v2/split.js +60 -15
- package/dist/math/v2/team.js +14 -1
- package/dist/math/v2/trade.js +16 -8
- package/dist/solana/floor/accounts.js +30 -3
- package/dist/solana/floor/events.js +6 -0
- package/dist/solana/floor/instructions.js +23 -1
- package/dist/solana/floor/program.js +3 -0
- 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/events.js +2 -0
- package/dist/v2/evm.js +8 -1
- package/dist/v2/layouts.js +7 -1
- package/dist/v2/router.js +2 -2
- package/dist/v2/solana.js +1 -1
- package/dist/v2/solanaSettle.js +2 -0
- package/dist/v2/tx/evm.js +15 -1
- package/docs/aggregator.md +0 -22
- package/docs/bot.md +0 -8
- package/docs/cursor-pages.md +9 -5
- 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/evm-event-names.ts +7 -0
- package/src/abi/evm.gen.ts +6 -0
- package/src/abi/functions.gen.ts +49 -1
- package/src/abi/solana.gen.ts +5 -0
- package/src/config/generated.ts +77 -0
- package/src/errors.ts +2 -2
- package/src/events/types.gen.ts +57 -1
- package/src/evm/chains.ts +34 -28
- package/src/evm/floor/calls.ts +5 -0
- package/src/evm/router.ts +1 -26
- package/src/evm/squid.ts +3 -3
- package/src/generated/interface.gen.ts +48 -43
- package/src/index.ts +1 -0
- package/src/indexer/bodies.ts +144 -2
- package/src/indexer/client.ts +17 -4
- package/src/indexer/contract.ts +66 -7
- package/src/indexer/paths.ts +62 -7
- package/src/indexer/read.ts +14 -5
- package/src/indexer/stream.ts +4 -4
- package/src/math/v2/book.ts +42 -7
- package/src/math/v2/create.ts +1 -1
- package/src/math/v2/fees.ts +30 -4
- package/src/math/v2/milestones.ts +1 -1
- package/src/math/v2/receipt.ts +7 -3
- package/src/math/v2/relay.ts +208 -1
- package/src/math/v2/rows.ts +17 -0
- package/src/math/v2/split.ts +70 -16
- package/src/math/v2/team.ts +14 -1
- package/src/math/v2/trade.ts +16 -8
- package/src/solana/floor/accounts.ts +30 -3
- package/src/solana/floor/events.ts +6 -0
- package/src/solana/floor/instructions.ts +23 -1
- package/src/solana/floor/program.ts +3 -0
- 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/events.ts +3 -1
- package/src/v2/evm.ts +10 -1
- package/src/v2/layouts.ts +7 -1
- package/src/v2/router.ts +2 -2
- package/src/v2/solana.ts +1 -1
- package/src/v2/solanaSettle.ts +2 -0
- package/src/v2/tx/evm.ts +15 -1
- package/src/v2/types.ts +14 -1
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,
|
|
@@ -90,14 +90,34 @@ export const TRADER_REF: Layout = [
|
|
|
90
90
|
["revoked", "u8"],
|
|
91
91
|
|
|
92
92
|
["pad0", "bytes:5"],
|
|
93
|
-
|
|
93
|
+
|
|
94
|
+
["og_until", "u64[3]"],
|
|
94
95
|
];
|
|
95
96
|
|
|
96
97
|
export const TRADER_LINK: Layout = [
|
|
97
98
|
["wallet", "pubkey"],
|
|
98
99
|
["tiers", "pubkey[3]"],
|
|
99
100
|
["bump", "u8"],
|
|
100
|
-
|
|
101
|
+
|
|
102
|
+
["og_until", "bytes:24"],
|
|
103
|
+
];
|
|
104
|
+
|
|
105
|
+
export const OG_USER: Layout = [
|
|
106
|
+
["referrer", "pubkey"],
|
|
107
|
+
["from", "i64"],
|
|
108
|
+
["until", "i64"],
|
|
109
|
+
["bump", "u8"],
|
|
110
|
+
["revoked", "u8"],
|
|
111
|
+
["pad0", "bytes:6"],
|
|
112
|
+
["reserved", "bytes:32"],
|
|
113
|
+
];
|
|
114
|
+
|
|
115
|
+
export const OG_COUNT: Layout = [
|
|
116
|
+
["count", "u16"],
|
|
117
|
+
["bump", "u8"],
|
|
118
|
+
["pad0", "bytes:5"],
|
|
119
|
+
["total", "u64"],
|
|
120
|
+
["reserved", "bytes:16"],
|
|
101
121
|
];
|
|
102
122
|
|
|
103
123
|
export const REF_OWED: Layout = [
|
|
@@ -209,7 +229,7 @@ export const GENESIS_ALLOC_WAGMI_CLAIMED = 8;
|
|
|
209
229
|
|
|
210
230
|
const NESTED: Record<string, Layout> = { SlotMember: SLOT_MEMBER };
|
|
211
231
|
|
|
212
|
-
export const LAYOUTS = { Config: CONFIG, QuoteEntry: QUOTE_ENTRY, TeamSlot: TEAM_SLOT, SlotMember: SLOT_MEMBER, TraderRef: TRADER_REF, TraderLink: TRADER_LINK, RefOwed: REF_OWED, GenesisRaise: GENESIS_RAISE, GenesisAllocation: GENESIS_ALLOCATION } as const;
|
|
232
|
+
export const LAYOUTS = { Config: CONFIG, QuoteEntry: QUOTE_ENTRY, TeamSlot: TEAM_SLOT, SlotMember: SLOT_MEMBER, TraderRef: TRADER_REF, TraderLink: TRADER_LINK, RefOwed: REF_OWED, OgUser: OG_USER, OgCount: OG_COUNT, GenesisRaise: GENESIS_RAISE, GenesisAllocation: GENESIS_ALLOCATION } as const;
|
|
213
233
|
|
|
214
234
|
function scalarSize(t: string): number {
|
|
215
235
|
switch (t) {
|
|
@@ -367,6 +387,13 @@ export const decodeTeamSlot = (d: Uint8Array) => decodeAccount("TeamSlot", d);
|
|
|
367
387
|
export const decodeTraderRef = (d: Uint8Array) => decodeAccount("TraderRef", d);
|
|
368
388
|
export const decodeTraderLink = (d: Uint8Array) => decodeAccount("TraderLink", d);
|
|
369
389
|
export const decodeRefOwed = (d: Uint8Array) => decodeAccount("RefOwed", d);
|
|
390
|
+
export const decodeOgUser = (d: Uint8Array) => decodeAccount("OgUser", d);
|
|
391
|
+
export const decodeOgCount = (d: Uint8Array) => decodeAccount("OgCount", d);
|
|
392
|
+
|
|
393
|
+
export function traderLinkOgUntil(ogUntil: Uint8Array): [bigint, bigint, bigint] {
|
|
394
|
+
const v = new DataView(ogUntil.buffer, ogUntil.byteOffset, ogUntil.byteLength);
|
|
395
|
+
return [v.getBigUint64(0, true), v.getBigUint64(8, true), v.getBigUint64(16, true)];
|
|
396
|
+
}
|
|
370
397
|
export const decodeGenesisRaise = (d: Uint8Array) => decodeAccount("GenesisRaise", d);
|
|
371
398
|
export const decodeGenesisAllocation = (d: Uint8Array) => decodeAccount("GenesisAllocation", d);
|
|
372
399
|
|
|
@@ -31,6 +31,12 @@ export const EVENTS: Record<string, [string, T][]> = {
|
|
|
31
31
|
TraderLinkOpened: S(["trader", "pubkey"], ["tiers", "pubkey[3]"]),
|
|
32
32
|
|
|
33
33
|
TraderRefCleared: S(["trader", "pubkey"], ["records", "u16"]),
|
|
34
|
+
|
|
35
|
+
OgUserSet: S(["referrer", "pubkey"], ["from", "i64"], ["until", "i64"], ["count", "u16"]),
|
|
36
|
+
|
|
37
|
+
OgUserRevoked: S(["referrer", "pubkey"], ["until", "i64"], ["count", "u16"]),
|
|
38
|
+
|
|
39
|
+
OgSynced: S(["referrer", "pubkey"], ["records", "u16"]),
|
|
34
40
|
CreatorBuyUnlocked: S(["launch", "pubkey"], ["creator", "pubkey"], ["tokens", "u64"], ["members", "u64[10]?"], ["paid", "u64?"]),
|
|
35
41
|
RefOwedOpened: S(["referrer", "pubkey"], ["quoteMint", "pubkey"]),
|
|
36
42
|
RefsRolled: S(["quoteMint", "pubkey"], ["traderRefs", "u16"], ["amount", "u64"]),
|
|
@@ -5,7 +5,7 @@ import type { AccountMeta, Instruction } from "../instruction.ts";
|
|
|
5
5
|
import { SYSTEM_PROGRAM_ID, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID, WSOL_MINT, associatedTokenAddress } from "../pda.ts";
|
|
6
6
|
import {
|
|
7
7
|
FLOOR_AMM_PROGRAM_ID, GENESIS_USDC_MINT, INSTRUCTIONS_SYSVAR_ID, METADATA_PROGRAM_ID, configPda, floorEventAuthorityPda, genesisAllocPda, genesisConvertBandPda, genesisMigrationVaultPda, genesisRaisePda, genesisRaiseTokenVault,
|
|
8
|
-
genesisVaultFloor, genesisVaultUsdc, launchPda, metadataPda, presetPda, quoteEntryPda, teamSlotPda, traderLinkPda,
|
|
8
|
+
genesisVaultFloor, genesisVaultUsdc, launchPda, metadataPda, ogCountPda, ogUserPda, presetPda, quoteEntryPda, teamSlotPda, traderLinkPda,
|
|
9
9
|
} from "./program.ts";
|
|
10
10
|
import { Writer } from "./borsh.ts";
|
|
11
11
|
import { CLAIM_BITS, CLAIM_KNOWN_MASKS, CLAIM_RESERVED, CLAIM_CREATOR, CLAIM_CREATOR_REF, CLAIM_TEAM_SLOT, CLAIM_TRADER_REF, CLAIM_RESERVED_HOLDER, CLAIM_EXTRA, CLAIM_TEAM_BUY, CLAIM_SHARE_BOUGHT } from "../../generated/index.ts";
|
|
@@ -224,6 +224,28 @@ export function openTraderLinkInstruction(p: { payer: string; referralAuthority:
|
|
|
224
224
|
);
|
|
225
225
|
}
|
|
226
226
|
|
|
227
|
+
export function setOgUserInstruction(p: { payer: string; referralAuthority: string; config?: string; referrer: string } & Ctx): Instruction {
|
|
228
|
+
return ix(
|
|
229
|
+
"set_og_user",
|
|
230
|
+
[WS(p.payer), RS(p.referralAuthority), R(p.config ?? configPda(p.programId)[0]), W(ogUserPda(p.referrer, p.programId)[0]), W(ogCountPda(p.programId)[0]), R(SYSTEM_PROGRAM_ID)],
|
|
231
|
+
(w) => w.pubkey(p.referrer),
|
|
232
|
+
p,
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function revokeOgUserInstruction(p: { authority: string; config?: string; referrer: string } & Ctx): Instruction {
|
|
237
|
+
return ix(
|
|
238
|
+
"revoke_og_user",
|
|
239
|
+
[RS(p.authority), R(p.config ?? configPda(p.programId)[0]), W(ogUserPda(p.referrer, p.programId)[0]), W(ogCountPda(p.programId)[0])],
|
|
240
|
+
(w) => w.pubkey(p.referrer),
|
|
241
|
+
p,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export function syncOgInstruction(p: { referrer: string; records: readonly string[] } & Ctx): Instruction {
|
|
246
|
+
return withRest(ix("sync_og", [R(ogUserPda(p.referrer, p.programId)[0])], (w) => w.pubkey(p.referrer), p), p.records.map((r) => W(r)));
|
|
247
|
+
}
|
|
248
|
+
|
|
227
249
|
export function openRefOwedInstruction(p: { payer: string; quoteMint: string; refOwed: string; vault: string; quoteTokenProgram: string; referrer: string } & Ctx): Instruction {
|
|
228
250
|
return ix("open_ref_owed", [WS(p.payer), R(p.quoteMint), W(p.refOwed), W(p.vault), R(p.quoteTokenProgram), R(ASSOCIATED_TOKEN_PROGRAM_ID), R(SYSTEM_PROGRAM_ID)], (w) => w.pubkey(p.referrer), p);
|
|
229
251
|
}
|
|
@@ -34,6 +34,9 @@ export const traderLinkPda = (wallet: string, p = FLOOR_AMM_PROGRAM_ID): Pda =>
|
|
|
34
34
|
export const unwrapPda = (wallet: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["unwrap", k(wallet)], p);
|
|
35
35
|
export const refOwedPda = (referrer: string, quoteMint: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["ref_owed", k(referrer), k(quoteMint)], p);
|
|
36
36
|
|
|
37
|
+
export const ogUserPda = (referrer: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["og_user", k(referrer)], p);
|
|
38
|
+
export const ogCountPda = (p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["og_count"], p);
|
|
39
|
+
|
|
37
40
|
export const genesisRaisePda = (p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["genesis_raise"], p);
|
|
38
41
|
export const genesisAllocPda = (wallet: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["genesis_alloc", k(wallet)], p);
|
|
39
42
|
|
|
@@ -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
|
+
}
|