@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
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { Address, beginCell, bytesOfBase64, bytesOfHex, Cell, fromBoc, hexOf, snakeCell } from "./cell.ts";
|
|
4
|
+
import type { Builder } from "./cell.ts";
|
|
5
|
+
import type { Slice } from "./cell.ts";
|
|
6
|
+
import { addr } from "./addresses.ts";
|
|
7
|
+
import { op as opcodeOf } from "./ops.ts";
|
|
8
|
+
import { decodeRowV2 } from "../math/v2/preset.ts";
|
|
9
|
+
import type { RowV2 } from "../math/v2/preset.ts";
|
|
10
|
+
import type { AddressLike } from "./types.ts";
|
|
11
|
+
|
|
12
|
+
export class TonGovDecodeError extends Error {
|
|
13
|
+
constructor(m: string) {
|
|
14
|
+
super(m);
|
|
15
|
+
this.name = "TonGovDecodeError";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type Kind =
|
|
20
|
+
| "u8" | "u16" | "u32" | "u64" | "u256" | "hash" | "pubkey" | "bool"
|
|
21
|
+
| "addrInt" | "addr" | "gram" | "quoteCoins"
|
|
22
|
+
| "snake" | "presetRow" | "quoteEntry" | "launchIds" | "addrList" | "code" | "maybeCell" | "action";
|
|
23
|
+
|
|
24
|
+
export interface TonGovQuoteEntry {
|
|
25
|
+
kind: 0 | 1; master: Address | null; decimals: number; walletKind: number; walletCodeHash: bigint; walletCode: Cell | null;
|
|
26
|
+
createFee: bigint; issuerCanFreeze: boolean; createsEnabled: boolean; cap: bigint;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface KindValue {
|
|
30
|
+
u8: number; u16: number; u32: number; u64: bigint; u256: bigint; hash: bigint; pubkey: bigint; bool: boolean;
|
|
31
|
+
addrInt: Address; addr: Address | null; gram: bigint; quoteCoins: bigint;
|
|
32
|
+
snake: string; presetRow: Uint8Array; quoteEntry: TonGovQuoteEntry; launchIds: bigint[]; addrList: Address[];
|
|
33
|
+
code: Cell; maybeCell: Cell | null; action: Cell;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type FieldSpec = readonly [name: string, tlb: string, kind: Kind];
|
|
37
|
+
interface OpSpec { readonly to: string; readonly from: string; readonly fields: readonly FieldSpec[] }
|
|
38
|
+
|
|
39
|
+
const AUTH = "authority";
|
|
40
|
+
const TL = "timelock";
|
|
41
|
+
const GOV = "timelock / governance";
|
|
42
|
+
|
|
43
|
+
export const TON_GOV_SPEC = {
|
|
44
|
+
transfer_authority: { to: "FloorFactory", from: AUTH, fields: [["new_authority", "MsgAddressInt", "addrInt"]] },
|
|
45
|
+
accept_authority: { to: "FloorFactory", from: "pending authority", fields: [] },
|
|
46
|
+
set_timelock: { to: "FloorFactory", from: AUTH, fields: [["timelock", "MsgAddressInt", "addrInt"]] },
|
|
47
|
+
set_create_paused: { to: "FloorFactory", from: "governance multisig (short delay)", fields: [["paused", "Bool", "bool"]] },
|
|
48
|
+
set_treasury: { to: "FloorFactory", from: TL, fields: [["treasury", "MsgAddressInt", "addrInt"]] },
|
|
49
|
+
set_native_create_fee: { to: "FloorFactory", from: AUTH, fields: [["fee", "Coins", "gram"]] },
|
|
50
|
+
set_metadata_base: { to: "FloorFactory", from: AUTH, fields: [["base", "^SnakeString", "snake"]] },
|
|
51
|
+
set_referral_authority: { to: "FloorFactory", from: AUTH, fields: [["pubkey", "uint256", "pubkey"]] },
|
|
52
|
+
set_holder_dest: { to: "FloorFactory", from: AUTH, fields: [["dest", "MsgAddressInt", "addrInt"]] },
|
|
53
|
+
add_preset: { to: "FloorFactory", from: AUTH, fields: [["id", "uint32", "u32"], ["row", "^PresetRow", "presetRow"]] },
|
|
54
|
+
update_preset: { to: "FloorFactory", from: AUTH, fields: [["id", "uint32", "u32"], ["row", "^PresetRow", "presetRow"]] },
|
|
55
|
+
set_preset_enabled: { to: "FloorFactory", from: AUTH, fields: [["id", "uint32", "u32"], ["enabled", "Bool", "bool"]] },
|
|
56
|
+
delete_preset: { to: "FloorFactory", from: AUTH, fields: [["id", "uint32", "u32"]] },
|
|
57
|
+
add_quote: { to: "FloorFactory", from: AUTH, fields: [["row", "^QuoteEntry", "quoteEntry"]] },
|
|
58
|
+
set_quote_cap: { to: "FloorFactory", from: AUTH, fields: [["quote", "MsgAddress", "addr"], ["cap", "Coins", "quoteCoins"]] },
|
|
59
|
+
update_quote: { to: "FloorFactory", from: AUTH, fields: [["row", "^QuoteEntry", "quoteEntry"]] },
|
|
60
|
+
set_quote_creates: {
|
|
61
|
+
to: "FloorFactory", from: "governance multisig (short delay)", fields: [["quote", "MsgAddress", "addr"], ["enabled", "Bool", "bool"]],
|
|
62
|
+
},
|
|
63
|
+
set_price_updater: { to: "FloorFactory", from: AUTH, fields: [["pubkey", "uint256", "pubkey"]] },
|
|
64
|
+
withdraw_platform: { to: "FloorFactory", from: GOV, fields: [["launch_ids", "^Cell", "launchIds"]] },
|
|
65
|
+
withdraw_holder: { to: "FloorFactory", from: GOV, fields: [["launch_ids", "^Cell", "launchIds"]] },
|
|
66
|
+
pull_bucket: {
|
|
67
|
+
to: "FloorLaunch", from: "FloorFactory only", fields: [["bucket", "uint8", "u8"], ["to", "MsgAddressInt", "addrInt"]],
|
|
68
|
+
},
|
|
69
|
+
push_upgrade: { to: "FloorFactory", from: "any", fields: [["from_id", "uint64", "u64"], ["count", "uint16", "u16"]] },
|
|
70
|
+
upgrade_code: {
|
|
71
|
+
to: "FloorLaunch | FloorAccount | FloorTrader",
|
|
72
|
+
from: "FloorLaunch, FloorTrader: FloorFactory only; FloorAccount: its launch only (relay_code); only the scheduled code (code_hash = cell_hash(code), else NotAuthority)",
|
|
73
|
+
fields: [["version", "uint32", "u32"], ["code_hash", "uint256", "hash"], ["code", "^Cell", "code"], ["migrate", "(Maybe ^Cell)", "maybeCell"]],
|
|
74
|
+
},
|
|
75
|
+
schedule: { to: "FloorTimelock", from: "the multisig v2 admin", fields: [["action", "^Cell", "action"], ["eta", "uint32", "u32"]] },
|
|
76
|
+
execute: { to: "FloorTimelock", from: "any after eta", fields: [["action_hash", "uint256", "hash"]] },
|
|
77
|
+
cancel: { to: "FloorTimelock", from: "the multisig v2 admin", fields: [["action_hash", "uint256", "hash"]] },
|
|
78
|
+
set_code: {
|
|
79
|
+
to: "FloorFactory", from: TL, fields: [["family", "uint8", "u8"], ["version", "uint32", "u32"], ["code", "^Cell", "code"]],
|
|
80
|
+
},
|
|
81
|
+
update_delay: { to: "FloorTimelock", from: "the timelock itself (a scheduled self action)", fields: [["delay", "uint32", "u32"]] },
|
|
82
|
+
set_admin: { to: "FloorTimelock", from: "the timelock itself (a scheduled self action)", fields: [["admin", "MsgAddressInt", "addrInt"]] },
|
|
83
|
+
accept_admin: { to: "FloorTimelock", from: "the pending admin (set by a scheduled set_admin)", fields: [] },
|
|
84
|
+
push_child_code: {
|
|
85
|
+
to: "FloorFactory", from: "any",
|
|
86
|
+
fields: [["family", "uint8", "u8"], ["launch_id", "uint64", "u64"], ["owners", "^AddrList", "addrList"]],
|
|
87
|
+
},
|
|
88
|
+
relay_code: {
|
|
89
|
+
to: "FloorLaunch", from: "FloorFactory only",
|
|
90
|
+
fields: [["version", "uint32", "u32"], ["code_hash", "uint256", "hash"], ["code", "^Cell", "code"], ["owners", "^AddrList", "addrList"]],
|
|
91
|
+
},
|
|
92
|
+
} as const satisfies Record<string, OpSpec>;
|
|
93
|
+
|
|
94
|
+
export type TonGovOp = keyof typeof TON_GOV_SPEC;
|
|
95
|
+
|
|
96
|
+
export const TON_GOV_OPS = Object.keys(TON_GOV_SPEC) as TonGovOp[];
|
|
97
|
+
|
|
98
|
+
type FieldsOfSpec<F extends readonly FieldSpec[]> = { -readonly [X in F[number] as X[0]]: KindValue[X[2]] };
|
|
99
|
+
|
|
100
|
+
export type TonGovFields<N extends TonGovOp> = FieldsOfSpec<(typeof TON_GOV_SPEC)[N]["fields"]>;
|
|
101
|
+
|
|
102
|
+
export interface TonGovAction<N extends TonGovOp = TonGovOp> {
|
|
103
|
+
op: N;
|
|
104
|
+
opcode: number;
|
|
105
|
+
|
|
106
|
+
target: string;
|
|
107
|
+
|
|
108
|
+
from: string;
|
|
109
|
+
queryId: bigint;
|
|
110
|
+
fields: TonGovFields<N>;
|
|
111
|
+
|
|
112
|
+
derived: Record<string, string>;
|
|
113
|
+
|
|
114
|
+
inner?: TonGovAction;
|
|
115
|
+
|
|
116
|
+
lines: string[];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export const TON_GRAM_DECIMALS = 9;
|
|
120
|
+
const FAMILY = ["factory", "launch", "launch-token wallet", "account", "trader"] as const;
|
|
121
|
+
const BUCKET = ["platform → treasury", "holder → holder_dest"] as const;
|
|
122
|
+
|
|
123
|
+
export function tonFormatUnits(v: bigint, decimals: number): string {
|
|
124
|
+
if (decimals === 0) return v.toString();
|
|
125
|
+
const s = v.toString().padStart(decimals + 1, "0");
|
|
126
|
+
const int = s.slice(0, -decimals);
|
|
127
|
+
const frac = s.slice(-decimals).replace(/0+$/, "");
|
|
128
|
+
return frac ? `${int}.${frac}` : int;
|
|
129
|
+
}
|
|
130
|
+
const hex64 = (v: bigint): string => v.toString(16).padStart(64, "0");
|
|
131
|
+
const cellHash = (c: Cell): string => hexOf(c.hash());
|
|
132
|
+
const addrText = (a: Address | null): string => (a ? a.toString({ bounceable: true }) : "addr_none");
|
|
133
|
+
|
|
134
|
+
export interface TonGovDecodeOptions {
|
|
135
|
+
|
|
136
|
+
quoteDecimals?: (master: Address | null) => number | undefined;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const MAX = { u8: 0xffn, u16: 0xffffn, u32: 0xffffffffn, u64: (1n << 64n) - 1n, u256: (1n << 256n) - 1n } as const;
|
|
140
|
+
function uint(v: bigint | number, bits: 8 | 16 | 32 | 64 | 256, what: string): bigint {
|
|
141
|
+
const b = BigInt(v);
|
|
142
|
+
const max = { 8: MAX.u8, 16: MAX.u16, 32: MAX.u32, 64: MAX.u64, 256: MAX.u256 }[bits];
|
|
143
|
+
if (b < 0n || b > max) throw new RangeError(`${what} out of uint${bits} range`);
|
|
144
|
+
return b;
|
|
145
|
+
}
|
|
146
|
+
function coins(v: bigint, what: string): bigint {
|
|
147
|
+
if (v < 0n || v >= 1n << 120n) throw new RangeError(`${what} out of Coins range`);
|
|
148
|
+
return v;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function tonPresetRowCell(bytes: Uint8Array): Cell {
|
|
152
|
+
let next: Cell | null = null;
|
|
153
|
+
for (let off = Math.floor(Math.max(bytes.length - 1, 0) / 127) * 127; off >= 0; off -= 127) {
|
|
154
|
+
const b = beginCell().storeBytes(bytes.subarray(off, off + 127));
|
|
155
|
+
if (next) b.storeRef(next);
|
|
156
|
+
next = b.endCell();
|
|
157
|
+
}
|
|
158
|
+
return next!;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function tonQuoteEntryCell(e: TonGovQuoteEntry): Cell {
|
|
162
|
+
if (e.kind !== 0 && e.kind !== 1) throw new RangeError("quote kind is 0 (native) or 1 (jetton)");
|
|
163
|
+
return beginCell()
|
|
164
|
+
.storeUint(e.kind, 1)
|
|
165
|
+
.storeAddress(e.master)
|
|
166
|
+
.storeUint(uint(e.decimals, 8, "decimals"), 8)
|
|
167
|
+
.storeUint(uint(e.walletKind, 8, "wallet_kind"), 8)
|
|
168
|
+
.storeUint(uint(e.walletCodeHash, 256, "wallet_code_hash"), 256)
|
|
169
|
+
.storeMaybeRef(e.walletCode)
|
|
170
|
+
.storeCoins(coins(e.createFee, "create_fee"))
|
|
171
|
+
.storeBit(e.issuerCanFreeze)
|
|
172
|
+
.storeBit(e.createsEnabled)
|
|
173
|
+
.storeCoins(coins(e.cap, "cap"))
|
|
174
|
+
.endCell();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function tonLaunchIdsCell(ids: readonly bigint[]): Cell {
|
|
178
|
+
if (ids.length === 0) throw new RangeError("launch_ids is empty");
|
|
179
|
+
if (ids.length > 250) throw new RangeError("launch_ids holds at most 250 ids (PUSH_BATCH_MAX)");
|
|
180
|
+
let next: Cell | null = null;
|
|
181
|
+
for (let off = Math.floor((ids.length - 1) / 15) * 15; off >= 0; off -= 15) {
|
|
182
|
+
const b = beginCell();
|
|
183
|
+
for (const id of ids.slice(off, off + 15)) b.storeUint(uint(id, 64, "launch id"), 64);
|
|
184
|
+
if (next) b.storeRef(next);
|
|
185
|
+
next = b.endCell();
|
|
186
|
+
}
|
|
187
|
+
return next!;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function tonAddrListCell(list: readonly AddressLike[]): Cell {
|
|
191
|
+
if (list.length === 0) throw new RangeError("owners is empty");
|
|
192
|
+
if (list.length > 32) throw new RangeError("owners holds at most 32 addresses");
|
|
193
|
+
let next: Cell | null = null;
|
|
194
|
+
for (let off = Math.floor((list.length - 1) / 3) * 3; off >= 0; off -= 3) {
|
|
195
|
+
const part = list.slice(off, off + 3).map(addr);
|
|
196
|
+
const b = beginCell().storeUint(part.length, 2).storeAddress(part[0]!);
|
|
197
|
+
b.storeMaybeAddress(part[1] ?? null).storeMaybeAddress(part[2] ?? null);
|
|
198
|
+
b.storeMaybeRef(next);
|
|
199
|
+
next = b.endCell();
|
|
200
|
+
}
|
|
201
|
+
return next!;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function storeField(b: Builder, k: Kind, v: unknown, name: string): void {
|
|
205
|
+
switch (k) {
|
|
206
|
+
case "u8": b.storeUint(uint(v as number, 8, name), 8); return;
|
|
207
|
+
case "u16": b.storeUint(uint(v as number, 16, name), 16); return;
|
|
208
|
+
case "u32": b.storeUint(uint(v as number, 32, name), 32); return;
|
|
209
|
+
case "u64": b.storeUint(uint(v as bigint, 64, name), 64); return;
|
|
210
|
+
case "u256": case "hash": case "pubkey": b.storeUint(uint(v as bigint, 256, name), 256); return;
|
|
211
|
+
case "bool": b.storeBit(v as boolean); return;
|
|
212
|
+
case "addrInt": b.storeAddress(addr(v as AddressLike)); return;
|
|
213
|
+
case "addr": b.storeAddress(v == null ? null : addr(v as AddressLike)); return;
|
|
214
|
+
case "gram": case "quoteCoins": b.storeCoins(coins(v as bigint, name)); return;
|
|
215
|
+
case "snake": b.storeRef(snakeCell(v as string)); return;
|
|
216
|
+
case "presetRow": decodeRowV2(v as Uint8Array); b.storeRef(tonPresetRowCell(v as Uint8Array)); return;
|
|
217
|
+
case "quoteEntry": b.storeRef(tonQuoteEntryCell(v as TonGovQuoteEntry)); return;
|
|
218
|
+
case "launchIds": b.storeRef(tonLaunchIdsCell(v as bigint[])); return;
|
|
219
|
+
case "addrList": b.storeRef(tonAddrListCell(v as AddressLike[])); return;
|
|
220
|
+
case "code": case "action": b.storeRef(v as Cell); return;
|
|
221
|
+
case "maybeCell": b.storeMaybeRef(v as Cell | null); return;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function buildTonGovBody<N extends TonGovOp>(name: N, fields: TonGovFields<N>, queryId: bigint = 0n): Cell {
|
|
226
|
+
const spec: OpSpec | undefined = (TON_GOV_SPEC as Record<string, OpSpec>)[name];
|
|
227
|
+
if (!spec) throw new RangeError(`${name} is not a governance op`);
|
|
228
|
+
const b = beginCell().storeUint(opcodeOf(name), 32).storeUint(uint(queryId, 64, "query_id"), 64);
|
|
229
|
+
const f = fields as Record<string, unknown>;
|
|
230
|
+
for (const [n, , k] of spec.fields) {
|
|
231
|
+
if (!(n in f)) throw new RangeError(`${name}: field ${n} missing`);
|
|
232
|
+
if (name === "withdraw_holder" && k === "launchIds" && (f[n] as bigint[]).some((x) => x === 0n)) {
|
|
233
|
+
throw new RangeError("withdraw_holder: launch id 0 is the factory's own platform bucket; the factory refuses it for the holder bucket");
|
|
234
|
+
}
|
|
235
|
+
storeField(b, k, f[n], `${name}.${n}`);
|
|
236
|
+
}
|
|
237
|
+
for (const n of Object.keys(f)) if (!spec.fields.some((x) => x[0] === n)) throw new RangeError(`${name}: unknown field ${n}`);
|
|
238
|
+
return b.endCell();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function buildTonGovSchedule<N extends TonGovOp>(inner: N, fields: TonGovFields<N>, eta: number, queryId: bigint = 0n, innerQueryId = 0n): Cell {
|
|
242
|
+
const action = buildTonGovBody(inner, fields, innerQueryId);
|
|
243
|
+
decodeTonGovAction(action, {}, true);
|
|
244
|
+
return buildTonGovBody("schedule", { action, eta } as TonGovFields<"schedule">, queryId);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const BY_OPCODE = new Map<number, TonGovOp>();
|
|
248
|
+
function byOpcode(): Map<number, TonGovOp> {
|
|
249
|
+
if (BY_OPCODE.size === 0) for (const n of TON_GOV_OPS) BY_OPCODE.set(opcodeOf(n), n);
|
|
250
|
+
return BY_OPCODE;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function guard<T>(what: string, f: () => T): T {
|
|
254
|
+
try {
|
|
255
|
+
return f();
|
|
256
|
+
} catch (e) {
|
|
257
|
+
if (e instanceof TonGovDecodeError) throw e;
|
|
258
|
+
throw new TonGovDecodeError(`${what}: ${(e as Error).message}`);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
function done(s: Slice, what: string): void {
|
|
262
|
+
if (s.remainingBits !== 0) throw new TonGovDecodeError(`${what}: ${s.remainingBits} bits left over`);
|
|
263
|
+
if (s.remainingRefs !== 0) throw new TonGovDecodeError(`${what}: ${s.remainingRefs} refs left over`);
|
|
264
|
+
}
|
|
265
|
+
function addrInt(s: Slice, what: string): Address {
|
|
266
|
+
const a = guard(what, () => s.loadAddress());
|
|
267
|
+
if (!a) throw new TonGovDecodeError(`${what}: addr_none where MsgAddressInt is required`);
|
|
268
|
+
return a;
|
|
269
|
+
}
|
|
270
|
+
function maybeAddrInt(s: Slice, what: string): Address | null {
|
|
271
|
+
return guard(what, () => s.loadBit()) ? addrInt(s, what) : null;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function readPresetRow(c: Cell, what: string): Uint8Array {
|
|
275
|
+
const parts: Uint8Array[] = [];
|
|
276
|
+
for (let cur: Cell | null = c, i = 0; cur; i++) {
|
|
277
|
+
const s = cur.beginParse();
|
|
278
|
+
if (s.remainingBits % 8 !== 0) throw new TonGovDecodeError(`${what}: cell ${i} is not whole bytes`);
|
|
279
|
+
if (s.remainingRefs > 1) throw new TonGovDecodeError(`${what}: cell ${i} has ${s.remainingRefs} refs`);
|
|
280
|
+
parts.push(guard(what, () => s.loadBytes(s.remainingBits / 8)));
|
|
281
|
+
cur = s.remainingRefs ? s.loadRef() : null;
|
|
282
|
+
}
|
|
283
|
+
const all = new Uint8Array(parts.reduce((a, p) => a + p.length, 0));
|
|
284
|
+
let o = 0;
|
|
285
|
+
for (const p of parts) (all.set(p, o), (o += p.length));
|
|
286
|
+
guard(what, () => decodeRowV2(all));
|
|
287
|
+
return all;
|
|
288
|
+
}
|
|
289
|
+
function readSnakeStrict(c: Cell, what: string): string {
|
|
290
|
+
const parts: Uint8Array[] = [];
|
|
291
|
+
for (let cur: Cell | undefined = c, i = 0; cur; cur = cur.refs[0], i++) {
|
|
292
|
+
if (cur.len % 8 !== 0) throw new TonGovDecodeError(`${what}: cell ${i} is not whole bytes`);
|
|
293
|
+
if (cur.refs.length > 1) throw new TonGovDecodeError(`${what}: cell ${i} has ${cur.refs.length} refs`);
|
|
294
|
+
parts.push(cur.bits.subarray(0, cur.len >> 3));
|
|
295
|
+
}
|
|
296
|
+
const all = new Uint8Array(parts.reduce((a, p) => a + p.length, 0));
|
|
297
|
+
let o = 0;
|
|
298
|
+
for (const p of parts) (all.set(p, o), (o += p.length));
|
|
299
|
+
return guard(what, () => new TextDecoder("utf-8", { fatal: true }).decode(all));
|
|
300
|
+
}
|
|
301
|
+
function readQuoteEntry(c: Cell, what: string): TonGovQuoteEntry {
|
|
302
|
+
const s = c.beginParse();
|
|
303
|
+
const e = guard(what, () => {
|
|
304
|
+
const kind = Number(s.loadUint(1)) as 0 | 1;
|
|
305
|
+
const master = s.loadAddress();
|
|
306
|
+
const decimals = Number(s.loadUint(8));
|
|
307
|
+
const walletKind = Number(s.loadUint(8));
|
|
308
|
+
const walletCodeHash = s.loadUint(256);
|
|
309
|
+
const walletCode = s.loadMaybeRef();
|
|
310
|
+
const createFee = s.loadCoins();
|
|
311
|
+
const issuerCanFreeze = s.loadBit();
|
|
312
|
+
const createsEnabled = s.loadBit();
|
|
313
|
+
const cap = s.loadCoins();
|
|
314
|
+
return { kind, master, decimals, walletKind, walletCodeHash, walletCode, createFee, issuerCanFreeze, createsEnabled, cap };
|
|
315
|
+
});
|
|
316
|
+
done(s, what);
|
|
317
|
+
return e;
|
|
318
|
+
}
|
|
319
|
+
function readLaunchIds(c: Cell, what: string): bigint[] {
|
|
320
|
+
const ids: bigint[] = [];
|
|
321
|
+
for (let cur: Cell | null = c, i = 0; cur; i++) {
|
|
322
|
+
const s = cur.beginParse();
|
|
323
|
+
if (s.remainingBits % 64 !== 0 || s.remainingBits === 0) throw new TonGovDecodeError(`${what}: cell ${i} is not whole uint64 ids`);
|
|
324
|
+
if (s.remainingRefs > 1) throw new TonGovDecodeError(`${what}: cell ${i} has ${s.remainingRefs} refs`);
|
|
325
|
+
while (s.remainingBits >= 64) ids.push(s.loadUint(64));
|
|
326
|
+
cur = s.remainingRefs ? s.loadRef() : null;
|
|
327
|
+
}
|
|
328
|
+
if (ids.length > 250) throw new TonGovDecodeError(`${what}: ${ids.length} ids, at most 250`);
|
|
329
|
+
return ids;
|
|
330
|
+
}
|
|
331
|
+
function readAddrList(c: Cell, what: string): Address[] {
|
|
332
|
+
const out: Address[] = [];
|
|
333
|
+
for (let cur: Cell | null = c, i = 0; cur; i++) {
|
|
334
|
+
const s = cur.beginParse();
|
|
335
|
+
const w = `${what} cell ${i}`;
|
|
336
|
+
const n = Number(guard(w, () => s.loadUint(2)));
|
|
337
|
+
const got = [addrInt(s, w), maybeAddrInt(s, w), maybeAddrInt(s, w)].filter((a): a is Address => a !== null);
|
|
338
|
+
if (n !== got.length || n === 0) throw new TonGovDecodeError(`${w}: n = ${n} but ${got.length} addresses`);
|
|
339
|
+
out.push(...got);
|
|
340
|
+
cur = guard(w, () => s.loadMaybeRef());
|
|
341
|
+
done(s, w);
|
|
342
|
+
}
|
|
343
|
+
if (out.length > 32) throw new TonGovDecodeError(`${what}: ${out.length} addresses, at most 32`);
|
|
344
|
+
return out;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function readField(s: Slice, k: Kind, what: string, opts: TonGovDecodeOptions): unknown {
|
|
348
|
+
void opts;
|
|
349
|
+
switch (k) {
|
|
350
|
+
case "u8": return Number(guard(what, () => s.loadUint(8)));
|
|
351
|
+
case "u16": return Number(guard(what, () => s.loadUint(16)));
|
|
352
|
+
case "u32": return Number(guard(what, () => s.loadUint(32)));
|
|
353
|
+
case "u64": return guard(what, () => s.loadUint(64));
|
|
354
|
+
case "u256": case "hash": case "pubkey": return guard(what, () => s.loadUint(256));
|
|
355
|
+
case "bool": return guard(what, () => s.loadBit());
|
|
356
|
+
case "addrInt": return addrInt(s, what);
|
|
357
|
+
case "addr": return guard(what, () => s.loadAddress());
|
|
358
|
+
case "gram": case "quoteCoins": return guard(what, () => s.loadCoins());
|
|
359
|
+
case "snake": return readSnakeStrict(guard(what, () => s.loadRef()), what);
|
|
360
|
+
case "presetRow": return readPresetRow(guard(what, () => s.loadRef()), what);
|
|
361
|
+
case "quoteEntry": return readQuoteEntry(guard(what, () => s.loadRef()), what);
|
|
362
|
+
case "launchIds": return readLaunchIds(guard(what, () => s.loadRef()), what);
|
|
363
|
+
case "addrList": return readAddrList(guard(what, () => s.loadRef()), what);
|
|
364
|
+
case "code": case "action": return guard(what, () => s.loadRef());
|
|
365
|
+
case "maybeCell": return guard(what, () => s.loadMaybeRef());
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function fieldText(name: string, k: Kind, v: unknown, opts: TonGovDecodeOptions, quote: Address | null | undefined, op = ""): string {
|
|
370
|
+
void name;
|
|
371
|
+
switch (k) {
|
|
372
|
+
case "u8": case "u16": case "u32": case "u64": case "bool": return String(v);
|
|
373
|
+
case "hash": return `0x${hex64(v as bigint)}`;
|
|
374
|
+
case "pubkey": return `ed25519 0x${hex64(v as bigint)}`;
|
|
375
|
+
case "u256": return `0x${hex64(v as bigint)}`;
|
|
376
|
+
case "addrInt": case "addr": return addrText(v as Address | null);
|
|
377
|
+
case "gram": return `${tonFormatUnits(v as bigint, TON_GRAM_DECIMALS)} GRAM (${v} nanogram)`;
|
|
378
|
+
case "quoteCoins": {
|
|
379
|
+
const d = quote === null ? TON_GRAM_DECIMALS : quote ? opts.quoteDecimals?.(quote) : undefined;
|
|
380
|
+
return d === undefined ? `${v} raw units (the quote's decimals unknown)` : `${tonFormatUnits(v as bigint, d)} (${v} units, ${d} decimals)`;
|
|
381
|
+
}
|
|
382
|
+
case "snake": return JSON.stringify(v);
|
|
383
|
+
case "presetRow": return `${(v as Uint8Array).length} bytes, canonical RowV2`;
|
|
384
|
+
case "quoteEntry": {
|
|
385
|
+
const e = v as TonGovQuoteEntry;
|
|
386
|
+
return [
|
|
387
|
+
`kind ${e.kind === 0 ? "native (GRAM)" : "jetton"}`, `master ${addrText(e.master)}`, `decimals ${e.decimals}`,
|
|
388
|
+
`wallet_kind ${e.walletKind}`, `wallet_code_hash 0x${hex64(e.walletCodeHash)}`,
|
|
389
|
+
`wallet_code ${e.walletCode ? `0x${cellHash(e.walletCode)}` : "none"}`,
|
|
390
|
+
`create_fee ${tonFormatUnits(e.createFee, e.decimals)}`, `issuer_can_freeze ${e.issuerCanFreeze}`,
|
|
391
|
+
`creates_enabled ${e.createsEnabled}`, `cap ${tonFormatUnits(e.cap, e.decimals)}`,
|
|
392
|
+
].join(", ");
|
|
393
|
+
}
|
|
394
|
+
case "launchIds": return (v as bigint[]).map((x) => (x === 0n ? (op === "withdraw_holder" ? "0 (REFUSED: the factory's own bucket is platform only)" : "0 (the factory's own platform bucket)") : String(x))).join(", ");
|
|
395
|
+
case "addrList": return (v as Address[]).map(addrText).join(", ");
|
|
396
|
+
case "code": case "action": return `cell 0x${cellHash(v as Cell)}`;
|
|
397
|
+
case "maybeCell": return v ? `cell 0x${cellHash(v as Cell)}` : "none";
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function toCell(body: Cell | Uint8Array | string): Cell {
|
|
402
|
+
if (body instanceof Cell) return body;
|
|
403
|
+
return guard("BoC", () => {
|
|
404
|
+
if (body instanceof Uint8Array) return fromBoc(body);
|
|
405
|
+
const t = body.trim();
|
|
406
|
+
return fromBoc(/^(0x)?[0-9a-fA-F]+$/.test(t) ? bytesOfHex(t.replace(/^0x/, "")) : bytesOfBase64(t));
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
export function decodeTonGovAction(body: Cell | Uint8Array | string, opts: TonGovDecodeOptions = {}, asAction = false): TonGovAction {
|
|
411
|
+
const c = toCell(body);
|
|
412
|
+
const s = c.beginParse();
|
|
413
|
+
const opcode = Number(guard("op", () => s.loadUint(32)));
|
|
414
|
+
const name = byOpcode().get(opcode);
|
|
415
|
+
if (!name) throw new TonGovDecodeError(`unknown op 0x${opcode.toString(16).padStart(8, "0")}: not a governance op, refused`);
|
|
416
|
+
const spec: OpSpec = TON_GOV_SPEC[name];
|
|
417
|
+
if (asAction && !(spec.to === "FloorFactory" || name === "update_delay" || name === "set_admin")) {
|
|
418
|
+
throw new TonGovDecodeError(`${name} cannot be a timelock action (the timelock sends its actions to the factory)`);
|
|
419
|
+
}
|
|
420
|
+
const queryId = guard(`${name}.query_id`, () => s.loadUint(64));
|
|
421
|
+
const fields: Record<string, unknown> = {};
|
|
422
|
+
for (const [n, , k] of spec.fields) fields[n] = readField(s, k, `${name}.${n}`, opts);
|
|
423
|
+
done(s, name);
|
|
424
|
+
|
|
425
|
+
const derived: Record<string, string> = {};
|
|
426
|
+
let inner: TonGovAction | undefined;
|
|
427
|
+
for (const [n, , k] of spec.fields) {
|
|
428
|
+
const v = fields[n];
|
|
429
|
+
if (k === "code") derived[`${n}_hash`] = `0x${cellHash(v as Cell)}`;
|
|
430
|
+
if (k === "presetRow") derived[`${n}_hash`] = `0x${cellHash(tonPresetRowCell(v as Uint8Array))}`;
|
|
431
|
+
if (k === "action") {
|
|
432
|
+
inner = decodeTonGovAction(v as Cell, opts, true);
|
|
433
|
+
derived.action_hash = `0x${cellHash(v as Cell)}`;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
if ("code_hash" in fields && "code" in fields) {
|
|
437
|
+
const ok = hex64(fields.code_hash as bigint) === cellHash(fields.code as Cell);
|
|
438
|
+
derived.code_hash_matches = ok ? "yes" : "NO: the contract refuses this code (code_hash ≠ cell_hash(code))";
|
|
439
|
+
}
|
|
440
|
+
if (name === "set_code" || name === "push_child_code") derived.family_name = FAMILY[fields.family as number] ?? "unknown family (refused)";
|
|
441
|
+
if (name === "pull_bucket") derived.bucket_name = BUCKET[fields.bucket as number] ?? "unknown bucket (refused)";
|
|
442
|
+
if (name === "set_create_paused") derived.scope = "new creates only; buys and sells can never be paused";
|
|
443
|
+
|
|
444
|
+
const quote = "quote" in fields ? (fields.quote as Address | null) : undefined;
|
|
445
|
+
const lines = [`${name} → ${spec.to} (query_id ${queryId})`];
|
|
446
|
+
for (const [n, , k] of spec.fields) lines.push(` ${n}: ${fieldText(n, k, fields[n], opts, quote, name)}`);
|
|
447
|
+
for (const [k, v] of Object.entries(derived)) lines.push(` [${k}] ${v}`);
|
|
448
|
+
if (inner) lines.push(" action decodes as:", ...inner.lines.map((l) => ` ${l}`));
|
|
449
|
+
return { op: name, opcode, target: spec.to, from: spec.from, queryId, fields: fields as TonGovFields<TonGovOp>, derived, inner, lines };
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
export const tonGovPresetRow = (bytes: Uint8Array): RowV2 => decodeRowV2(bytes);
|
package/src/ton/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
export * from "./types.ts";
|
|
3
|
+
export * from "./cell.ts";
|
|
4
|
+
export * from "./ops.ts";
|
|
5
|
+
export * from "./addresses.ts";
|
|
6
|
+
export * from "./messages.ts";
|
|
7
|
+
export * from "./reader.ts";
|
|
8
|
+
export * from "./quote.ts";
|
|
9
|
+
export * from "./governance.ts";
|
|
10
|
+
export * from "./quote-entry.ts";
|
|
11
|
+
export * from "./price.ts";
|
|
12
|
+
|
|
13
|
+
export { defaultClaimValue, pushChildMin, pushEach, childEach } from "./gas.ts";
|
|
14
|
+
export * from "./book.ts";
|
|
15
|
+
export * from "./book-check.ts";
|
|
16
|
+
export * from "./wallet.ts";
|
|
17
|
+
export { TON_MAINNET, TON_INIT_CODES_BASE64 } from "./consts.generated.ts";
|
|
18
|
+
export * from "./content.ts";
|