@typesol/cli 0.28.0 → 0.30.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 +35 -0
- package/README.md +2 -2
- package/dist/cli.js +13 -20
- package/dist/compiler/analyze.d.ts +6 -2
- package/dist/compiler/analyze.js +57 -15
- package/dist/compiler/diagnostics.d.ts +2 -0
- package/dist/compiler/diagnostics.js +3 -2
- package/dist/compiler/emit/alias.d.ts +48 -0
- package/dist/compiler/emit/alias.js +166 -0
- package/dist/compiler/emit/borsh.js +6 -3
- package/dist/compiler/emit/calls.js +21 -4
- package/dist/compiler/emit/cpi-core.d.ts +70 -0
- package/dist/compiler/emit/cpi-core.js +356 -0
- package/dist/compiler/emit/cpi.d.ts +3 -177
- package/dist/compiler/emit/cpi.js +13 -2721
- package/dist/compiler/emit/create.d.ts +39 -0
- package/dist/compiler/emit/create.js +548 -0
- package/dist/compiler/emit/function.d.ts +11 -2
- package/dist/compiler/emit/function.js +42 -7
- package/dist/compiler/emit/metaplex.d.ts +78 -0
- package/dist/compiler/emit/metaplex.js +1345 -0
- package/dist/compiler/emit/program.js +24 -4
- package/dist/compiler/emit/shared.d.ts +7 -1
- package/dist/compiler/emit/shared.js +11 -2
- package/dist/compiler/emit/statements.d.ts +0 -9
- package/dist/compiler/emit/statements.js +2 -53
- package/dist/compiler/emit/storage.d.ts +5 -20
- package/dist/compiler/emit/storage.js +40 -106
- package/dist/compiler/emit/token.d.ts +8 -0
- package/dist/compiler/emit/token.js +493 -0
- package/dist/compiler/index.js +1 -1
- package/dist/compiler/model.d.ts +10 -2
- package/dist/compiler/model.js +7 -1
- package/dist/tools/portgen.js +3 -0
- package/dist/workspace.d.ts +1 -1
- package/dist/workspace.js +3 -1
- package/docs/architecture.md +30 -2
- package/docs/audit.md +3 -2
- package/docs/case-study-multisig.md +31 -29
- package/docs/cli.md +7 -4
- package/docs/errors.md +3 -2
- package/docs/language.md +4 -3
- package/docs/migrate-from-anchor.md +3 -3
- package/docs/security-model.md +1 -1
- package/lib/typesol.d.ts +17 -6
- package/package.json +1 -1
- package/runtime/typesol_rt.h +85 -1
- package/ts-plugin/index.js +75 -39
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
import { isWritable } from "../model.js";
|
|
2
|
+
import { U64, U8, BOOL, PUBKEY, STRING, cBytes, optC } from "./shared.js";
|
|
3
|
+
import { CreateEmitter } from "./create.js";
|
|
4
|
+
/** SPL Token and Token-2022 instructions (`tokenProgram.transfer(…)` and the rest) and the ATA program's. */
|
|
5
|
+
export class TokenEmitter extends CreateEmitter {
|
|
6
|
+
tokenOp(node, program, method) {
|
|
7
|
+
const a = node.arguments;
|
|
8
|
+
const p = `a_${program.name}`;
|
|
9
|
+
const want = (n, kind, mut, what) => {
|
|
10
|
+
const acct = this.accountParam(n, what);
|
|
11
|
+
if (acct.kind.kind !== kind || (mut && !acct.kind.mut))
|
|
12
|
+
this.fail(n, `${what} must be a ${mut ? "Mut<" : ""}${kind === "token" ? "TokenAccount" : "Mint"}${mut ? ">" : ""}`);
|
|
13
|
+
return acct;
|
|
14
|
+
};
|
|
15
|
+
const arity = (fixed) => {
|
|
16
|
+
if (a.length !== fixed && a.length !== fixed + 1)
|
|
17
|
+
this.fail(node, `${method} takes ${fixed} arguments plus optional signerSeeds`);
|
|
18
|
+
};
|
|
19
|
+
const amount = (n) => this.expect(this.expr(n, U64), U64, n).c;
|
|
20
|
+
switch (method) {
|
|
21
|
+
case "transfer": {
|
|
22
|
+
arity(4);
|
|
23
|
+
const from = want(a[0], "token", true, "the source");
|
|
24
|
+
const to = want(a[1], "token", true, "the destination");
|
|
25
|
+
const auth = this.accountParam(a[2], "the authority");
|
|
26
|
+
const amt = amount(a[3]);
|
|
27
|
+
const s = this.signerSeeds(a[4], auth, a[2]);
|
|
28
|
+
this.line(`TS_TRY(ts_token_transfer(params, ${p}, a_${from.name}, a_${to.name}, a_${auth.name}, ${amt}, ${s.name}, ${s.count}));`);
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
case "transferChecked": {
|
|
32
|
+
arity(5);
|
|
33
|
+
const from = want(a[0], "token", true, "the source");
|
|
34
|
+
const mint = want(a[1], "mint", false, "the mint");
|
|
35
|
+
const to = want(a[2], "token", true, "the destination");
|
|
36
|
+
const auth = this.accountParam(a[3], "the authority");
|
|
37
|
+
const amt = amount(a[4]);
|
|
38
|
+
const s = this.signerSeeds(a[5], auth, a[3]);
|
|
39
|
+
this.line(`TS_TRY(ts_token_transfer_checked(params, ${p}, a_${from.name}, a_${mint.name}, a_${to.name}, a_${auth.name}, ${amt}, ${s.name}, ${s.count}));`);
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
case "mintTo": {
|
|
43
|
+
arity(4);
|
|
44
|
+
// A Mut<Mint>, or a mint created and initialized earlier in this instruction (Mut<Signer> / Mut<AccountInfo>).
|
|
45
|
+
const mint = this.accountParam(a[0], "the mint");
|
|
46
|
+
if (!isWritable(mint.kind) || (mint.kind.kind !== "mint" && mint.kind.kind !== "signer" && mint.kind.kind !== "info"))
|
|
47
|
+
this.fail(a[0], "the mint must be a Mut<Mint>, or a Mut<Signer> / Mut<AccountInfo> mint this instruction created");
|
|
48
|
+
const to = want(a[1], "token", true, "the destination");
|
|
49
|
+
const auth = this.accountParam(a[2], "the mint authority");
|
|
50
|
+
const amt = amount(a[3]);
|
|
51
|
+
const s = this.signerSeeds(a[4], auth, a[2]);
|
|
52
|
+
this.line(`TS_TRY(ts_token_mint_to(params, ${p}, a_${mint.name}, a_${to.name}, a_${auth.name}, ${amt}, ${s.name}, ${s.count}));`);
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
case "burn": {
|
|
56
|
+
arity(4);
|
|
57
|
+
const from = want(a[0], "token", true, "the burned account");
|
|
58
|
+
const mint = want(a[1], "mint", true, "the mint");
|
|
59
|
+
const auth = this.accountParam(a[2], "the authority");
|
|
60
|
+
const amt = amount(a[3]);
|
|
61
|
+
const s = this.signerSeeds(a[4], auth, a[2]);
|
|
62
|
+
this.line(`TS_TRY(ts_token_burn(params, ${p}, a_${from.name}, a_${mint.name}, a_${auth.name}, ${amt}, ${s.name}, ${s.count}));`);
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
case "initializeMetadata": {
|
|
66
|
+
if (program.kind.kind === "tokenProgram" && program.kind.flavor === "token")
|
|
67
|
+
this.fail(node, "metadata lives in Token-2022 mints; use Token2022Program or TokenInterface");
|
|
68
|
+
if (a.length !== 6 && a.length !== 7)
|
|
69
|
+
this.fail(node, "initializeMetadata(mint, updateAuthority, mintAuthority, name, symbol, uri, signerSeeds?)");
|
|
70
|
+
// The mint may have been created and initialized earlier in this instruction (a Mut<Signer> or Mut<AccountInfo>).
|
|
71
|
+
const mint = this.accountParam(a[0], "the mint");
|
|
72
|
+
if (!isWritable(mint.kind))
|
|
73
|
+
this.fail(a[0], "the mint must be writable (Mut<...>)");
|
|
74
|
+
const ua = this.accountParam(a[1], "the update authority");
|
|
75
|
+
const ma = this.accountParam(a[2], "the mint authority");
|
|
76
|
+
const [nm, sym, uri] = [a[3], a[4], a[5]].map((n) => this.expect(this.expr(n, STRING), STRING, n).c);
|
|
77
|
+
const s = this.signerSeeds(a[6], ma, a[2]);
|
|
78
|
+
this.line(`TS_TRY(ts_t22_init_metadata(params, ${p}, a_${mint.name}, a_${ua.name}, a_${ma.name}, ${nm}, ${sym}, ${uri}, ${s.name}, ${s.count}));`);
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
case "closeAccount": {
|
|
82
|
+
arity(3);
|
|
83
|
+
// A token account, or (Token-2022, with a MintCloseAuthority and no supply left) a mint.
|
|
84
|
+
const acct = this.accountParam(a[0], "the closed account");
|
|
85
|
+
if (!((acct.kind.kind === "token" || acct.kind.kind === "mint") && acct.kind.mut))
|
|
86
|
+
this.fail(a[0], "the closed account must be a Mut<TokenAccount> or a Mut<Mint>");
|
|
87
|
+
const dest = this.accountParam(a[1], "the destination");
|
|
88
|
+
if (!isWritable(dest.kind))
|
|
89
|
+
this.fail(a[1], "the destination must be writable (Mut<...>)");
|
|
90
|
+
const auth = this.accountParam(a[2], "the authority");
|
|
91
|
+
const s = this.signerSeeds(a[3], auth, a[2]);
|
|
92
|
+
this.line(`TS_TRY(ts_token_close(params, ${p}, a_${acct.name}, a_${dest.name}, a_${auth.name}, ${s.name}, ${s.count}));`);
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// The remaining SPL Token instructions share one shape: a tag, a few fixed fields, and fixed account metas.
|
|
97
|
+
const anyAcct = (n, what, mut) => {
|
|
98
|
+
const acct = this.accountParam(n, what);
|
|
99
|
+
if (mut && !isWritable(acct.kind))
|
|
100
|
+
this.fail(n, `${what} must be writable (Mut<...>)`);
|
|
101
|
+
return acct;
|
|
102
|
+
};
|
|
103
|
+
const simple = (tag, fields, metas, seeds) => {
|
|
104
|
+
const { buf, len, put, finish } = this.byteBuffer("tok", 128, "TS_ERR_INSTRUCTION_DATA", "token instruction");
|
|
105
|
+
const t = this.tmp("tag");
|
|
106
|
+
this.line(`uint8_t ${t} = ${tag};`);
|
|
107
|
+
put(`&${t}`, "1", 1);
|
|
108
|
+
for (const f of fields)
|
|
109
|
+
f(put);
|
|
110
|
+
finish(node);
|
|
111
|
+
this.cpi(p, metas.map(([acc, w, sg]) => [`a_${acc.name}`, w, sg]), buf, len, seeds);
|
|
112
|
+
};
|
|
113
|
+
const value = (n, t) => (put) => this.serialize(this.expect(this.expr(n, t), t, n), n, put);
|
|
114
|
+
const optKey = { kind: "option", inner: PUBKEY };
|
|
115
|
+
const U16 = { kind: "int", name: "u16" };
|
|
116
|
+
const I16 = { kind: "int", name: "i16" };
|
|
117
|
+
const byte = (k) => (put) => {
|
|
118
|
+
const t = this.tmp("b");
|
|
119
|
+
this.line(`uint8_t ${t} = ${k};`);
|
|
120
|
+
put(`&${t}`, "1", 1);
|
|
121
|
+
};
|
|
122
|
+
/** Token-2022's OptionalNonZeroPubkey: 32 bytes, all zero for none. */
|
|
123
|
+
const nonZeroKey = (n) => (put) => {
|
|
124
|
+
this.model.options.set("pubkey", PUBKEY);
|
|
125
|
+
const v = this.expect(this.expr(n, optKey), optKey, n);
|
|
126
|
+
const t = this.tmp("nzk");
|
|
127
|
+
this.line(`SolPubkey ${t};`);
|
|
128
|
+
this.line(`{ ${optC(PUBKEY)} o = ${v.c}; if (o.some) ${t} = o.v; else ts_memzero(&${t}, 32); }`);
|
|
129
|
+
put(`&${t}`, "32", 32);
|
|
130
|
+
};
|
|
131
|
+
const token2022Only = () => {
|
|
132
|
+
if (program.kind.kind === "tokenProgram" && program.kind.flavor === "token")
|
|
133
|
+
this.fail(node, `${method} is a Token-2022 extension; use Token2022Program or TokenInterface`);
|
|
134
|
+
};
|
|
135
|
+
const mintOnly = (n, usage) => {
|
|
136
|
+
token2022Only();
|
|
137
|
+
if (a.length !== n)
|
|
138
|
+
this.fail(node, usage);
|
|
139
|
+
return anyAcct(a[0], "the mint", true);
|
|
140
|
+
};
|
|
141
|
+
const none = { name: "NULL", count: 0 };
|
|
142
|
+
switch (method) {
|
|
143
|
+
case "initializeTransferFeeConfig": {
|
|
144
|
+
const mint = mintOnly(5, "initializeTransferFeeConfig(mint, configAuthority | null, withdrawAuthority | null, basisPoints, maximumFee)");
|
|
145
|
+
this.model.options.set("pubkey", PUBKEY);
|
|
146
|
+
// Arguments in source order, as JS evaluates them: the basis points are checked when they are read.
|
|
147
|
+
const bps = (put) => {
|
|
148
|
+
const v = this.expect(this.expr(a[3], U16), U16, a[3]);
|
|
149
|
+
this.line(`if (${v.c} > 10000) TS_FAIL(TS_ERR_INSTRUCTION_DATA, "TypeSOL: transfer fee above 10000 basis points");`);
|
|
150
|
+
this.serialize(v, a[3], put);
|
|
151
|
+
};
|
|
152
|
+
simple(26, [byte(0), value(a[1], optKey), value(a[2], optKey), bps, value(a[4], U64)], [[mint, true, false]], none);
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
case "initializeMintCloseAuthority": {
|
|
156
|
+
const mint = mintOnly(2, "initializeMintCloseAuthority(mint, closeAuthority | null)");
|
|
157
|
+
this.model.options.set("pubkey", PUBKEY);
|
|
158
|
+
simple(25, [value(a[1], optKey)], [[mint, true, false]], none);
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
161
|
+
case "initializeNonTransferableMint": {
|
|
162
|
+
const mint = mintOnly(1, "initializeNonTransferableMint(mint)");
|
|
163
|
+
simple(32, [], [[mint, true, false]], none);
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
case "initializePermanentDelegate": {
|
|
167
|
+
const mint = mintOnly(2, "initializePermanentDelegate(mint, delegate)");
|
|
168
|
+
simple(35, [value(a[1], PUBKEY)], [[mint, true, false]], none);
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
171
|
+
case "initializeDefaultAccountState": {
|
|
172
|
+
const mint = mintOnly(2, "initializeDefaultAccountState(mint, frozen)");
|
|
173
|
+
const frozen = this.expect(this.expr(a[1], BOOL), BOOL, a[1]);
|
|
174
|
+
const st = this.tmp("st");
|
|
175
|
+
this.line(`uint8_t ${st} = (${frozen.c}) ? 2 : 1;`); // AccountState::Frozen / Initialized
|
|
176
|
+
simple(28, [byte(0), (put) => put(`&${st}`, "1", 1)], [[mint, true, false]], none);
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
case "initializeMetadataPointer":
|
|
180
|
+
case "initializeGroupPointer":
|
|
181
|
+
case "initializeGroupMemberPointer":
|
|
182
|
+
case "initializeTransferHook": {
|
|
183
|
+
const tag = { initializeMetadataPointer: 39, initializeGroupPointer: 40, initializeGroupMemberPointer: 41, initializeTransferHook: 36 }[method];
|
|
184
|
+
const second = {
|
|
185
|
+
initializeMetadataPointer: "metadataAddress",
|
|
186
|
+
initializeGroupPointer: "groupAddress",
|
|
187
|
+
initializeGroupMemberPointer: "memberAddress",
|
|
188
|
+
initializeTransferHook: "programId",
|
|
189
|
+
}[method];
|
|
190
|
+
const mint = mintOnly(3, `${method}(mint, authority | null, ${second} | null)`);
|
|
191
|
+
simple(tag, [byte(0), nonZeroKey(a[1]), nonZeroKey(a[2])], [[mint, true, false]], none);
|
|
192
|
+
return undefined;
|
|
193
|
+
}
|
|
194
|
+
case "initializeInterestBearingMint": {
|
|
195
|
+
const mint = mintOnly(3, "initializeInterestBearingMint(mint, rateAuthority | null, rate)");
|
|
196
|
+
simple(33, [byte(0), nonZeroKey(a[1]), value(a[2], I16)], [[mint, true, false]], none);
|
|
197
|
+
return undefined;
|
|
198
|
+
}
|
|
199
|
+
case "initializeImmutableOwner": {
|
|
200
|
+
token2022Only();
|
|
201
|
+
if (a.length !== 1)
|
|
202
|
+
this.fail(node, "initializeImmutableOwner(account)");
|
|
203
|
+
const acct = anyAcct(a[0], "the token account", true);
|
|
204
|
+
simple(22, [], [[acct, true, false]], none);
|
|
205
|
+
return undefined;
|
|
206
|
+
}
|
|
207
|
+
case "updateDefaultAccountState": {
|
|
208
|
+
token2022Only();
|
|
209
|
+
if (a.length !== 3 && a.length !== 4)
|
|
210
|
+
this.fail(node, "updateDefaultAccountState(mint, freezeAuthority, frozen, signerSeeds?)");
|
|
211
|
+
const mint = anyAcct(a[0], "the mint", true);
|
|
212
|
+
const auth = this.accountParam(a[1], "the freeze authority");
|
|
213
|
+
const frozen = this.expect(this.expr(a[2], BOOL), BOOL, a[2]);
|
|
214
|
+
const st = this.tmp("st");
|
|
215
|
+
this.line(`uint8_t ${st} = (${frozen.c}) ? 2 : 1;`);
|
|
216
|
+
simple(28, [byte(1), (put) => put(`&${st}`, "1", 1)], [
|
|
217
|
+
[mint, true, false],
|
|
218
|
+
[auth, false, true],
|
|
219
|
+
], this.signerSeeds(a[3], auth, a[1]));
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
case "enableRequiredMemoTransfers":
|
|
223
|
+
case "disableRequiredMemoTransfers": {
|
|
224
|
+
token2022Only();
|
|
225
|
+
if (a.length !== 2 && a.length !== 3)
|
|
226
|
+
this.fail(node, `${method}(account, owner, signerSeeds?)`);
|
|
227
|
+
const acct = anyAcct(a[0], "the token account", true);
|
|
228
|
+
const owner = this.accountParam(a[1], "the owner");
|
|
229
|
+
simple(30, [byte(method === "enableRequiredMemoTransfers" ? 0 : 1)], [
|
|
230
|
+
[acct, true, false],
|
|
231
|
+
[owner, false, true],
|
|
232
|
+
], this.signerSeeds(a[2], owner, a[1]));
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
case "updateTransferHook": {
|
|
236
|
+
token2022Only();
|
|
237
|
+
if (a.length !== 3 && a.length !== 4)
|
|
238
|
+
this.fail(node, "updateTransferHook(mint, authority, programId | null, signerSeeds?)");
|
|
239
|
+
const mint = anyAcct(a[0], "the mint", true);
|
|
240
|
+
const auth = this.accountParam(a[1], "the transfer hook authority");
|
|
241
|
+
simple(36, [byte(1), nonZeroKey(a[2])], [
|
|
242
|
+
[mint, true, false],
|
|
243
|
+
[auth, false, true],
|
|
244
|
+
], this.signerSeeds(a[3], auth, a[1]));
|
|
245
|
+
return undefined;
|
|
246
|
+
}
|
|
247
|
+
case "updateInterestRate": {
|
|
248
|
+
token2022Only();
|
|
249
|
+
if (a.length !== 3 && a.length !== 4)
|
|
250
|
+
this.fail(node, "updateInterestRate(mint, rateAuthority, rate, signerSeeds?)");
|
|
251
|
+
const mint = anyAcct(a[0], "the mint", true);
|
|
252
|
+
const auth = this.accountParam(a[1], "the rate authority");
|
|
253
|
+
simple(33, [byte(1), value(a[2], I16)], [
|
|
254
|
+
[mint, true, false],
|
|
255
|
+
[auth, false, true],
|
|
256
|
+
], this.signerSeeds(a[3], auth, a[1]));
|
|
257
|
+
return undefined;
|
|
258
|
+
}
|
|
259
|
+
case "transferCheckedWithFee": {
|
|
260
|
+
token2022Only();
|
|
261
|
+
if (a.length !== 6 && a.length !== 7)
|
|
262
|
+
this.fail(node, "transferCheckedWithFee(from, mint, to, authority, amount, fee, signerSeeds?)");
|
|
263
|
+
const from = want(a[0], "token", true, "the source");
|
|
264
|
+
const mint = want(a[1], "mint", false, "the mint");
|
|
265
|
+
const to = want(a[2], "token", true, "the destination");
|
|
266
|
+
const auth = this.accountParam(a[3], "the authority");
|
|
267
|
+
// The decimals are the mint's own (checked when the mint was), as transferChecked does.
|
|
268
|
+
const dec = (put) => {
|
|
269
|
+
const t = this.tmp("dec");
|
|
270
|
+
this.line(`uint8_t ${t} = a_${mint.name}->data[44];`);
|
|
271
|
+
put(`&${t}`, "1", 1);
|
|
272
|
+
};
|
|
273
|
+
simple(26, [byte(1), value(a[4], U64), dec, value(a[5], U64)], [
|
|
274
|
+
[from, true, false],
|
|
275
|
+
[mint, false, false],
|
|
276
|
+
[to, true, false],
|
|
277
|
+
[auth, false, true],
|
|
278
|
+
], this.signerSeeds(a[6], auth, a[3]));
|
|
279
|
+
return undefined;
|
|
280
|
+
}
|
|
281
|
+
case "harvestWithheldTokensToMint": {
|
|
282
|
+
token2022Only();
|
|
283
|
+
if (a.length !== 2)
|
|
284
|
+
this.fail(node, "harvestWithheldTokensToMint(mint, sources): sources is remainingAccounts() or rest.slice(a, b)");
|
|
285
|
+
if (!this.ix)
|
|
286
|
+
this.fail(node, "harvestWithheldTokensToMint takes remaining accounts, so only instructions can call it");
|
|
287
|
+
const mint = anyAcct(a[0], "the mint", true);
|
|
288
|
+
const range = this.remainingRange(a[1]) ?? this.fail(a[1], "pass the source token accounts as remainingAccounts() or rest.slice(a, b)");
|
|
289
|
+
const { buf, len, put, finish } = this.byteBuffer("tok", 2, "TS_ERR_INSTRUCTION_DATA", "token instruction");
|
|
290
|
+
byte(26)(put);
|
|
291
|
+
byte(4)(put);
|
|
292
|
+
finish(node);
|
|
293
|
+
// The sources go as they were passed (Token-2022 skips any it cannot harvest from).
|
|
294
|
+
this.cpi(p, [[`a_${mint.name}`, true, false]], buf, len, none, { ...range, asPassed: true });
|
|
295
|
+
return undefined;
|
|
296
|
+
}
|
|
297
|
+
case "withdrawWithheldTokensFromMint": {
|
|
298
|
+
token2022Only();
|
|
299
|
+
if (a.length !== 3 && a.length !== 4)
|
|
300
|
+
this.fail(node, "withdrawWithheldTokensFromMint(mint, destination, authority, signerSeeds?)");
|
|
301
|
+
const mint = anyAcct(a[0], "the mint", true);
|
|
302
|
+
const dest = want(a[1], "token", true, "the destination");
|
|
303
|
+
const auth = this.accountParam(a[2], "the withdraw authority");
|
|
304
|
+
simple(26, [byte(2)], [
|
|
305
|
+
[mint, true, false],
|
|
306
|
+
[dest, true, false],
|
|
307
|
+
[auth, false, true],
|
|
308
|
+
], this.signerSeeds(a[3], auth, a[2]));
|
|
309
|
+
return undefined;
|
|
310
|
+
}
|
|
311
|
+
case "setTransferFee": {
|
|
312
|
+
token2022Only();
|
|
313
|
+
if (a.length !== 4 && a.length !== 5)
|
|
314
|
+
this.fail(node, "setTransferFee(mint, configAuthority, basisPoints, maximumFee, signerSeeds?)");
|
|
315
|
+
const mint = anyAcct(a[0], "the mint", true);
|
|
316
|
+
const auth = this.accountParam(a[1], "the config authority");
|
|
317
|
+
const bps = (put) => {
|
|
318
|
+
const v = this.expect(this.expr(a[2], U16), U16, a[2]);
|
|
319
|
+
this.line(`if (${v.c} > 10000) TS_FAIL(TS_ERR_INSTRUCTION_DATA, "TypeSOL: transfer fee above 10000 basis points");`);
|
|
320
|
+
this.serialize(v, a[2], put);
|
|
321
|
+
};
|
|
322
|
+
simple(26, [byte(5), bps, value(a[3], U64)], [
|
|
323
|
+
[mint, true, false],
|
|
324
|
+
[auth, false, true],
|
|
325
|
+
], this.signerSeeds(a[4], auth, a[1]));
|
|
326
|
+
return undefined;
|
|
327
|
+
}
|
|
328
|
+
case "updateMetadataField":
|
|
329
|
+
case "removeMetadataKey":
|
|
330
|
+
case "updateMetadataAuthority":
|
|
331
|
+
case "emitMetadata": {
|
|
332
|
+
token2022Only();
|
|
333
|
+
// The Token Metadata interface (spl-token-metadata-interface), served by Token-2022 for metadata in the mint.
|
|
334
|
+
const usage = {
|
|
335
|
+
updateMetadataField: "updateMetadataField(mint, updateAuthority, field, value, signerSeeds?)",
|
|
336
|
+
removeMetadataKey: "removeMetadataKey(mint, updateAuthority, key, idempotent, signerSeeds?)",
|
|
337
|
+
updateMetadataAuthority: "updateMetadataAuthority(mint, updateAuthority, newAuthority | null, signerSeeds?)",
|
|
338
|
+
emitMetadata: "emitMetadata(mint)",
|
|
339
|
+
}[method];
|
|
340
|
+
const fixedArgs = { updateMetadataField: 4, removeMetadataKey: 4, updateMetadataAuthority: 3, emitMetadata: 1 }[method];
|
|
341
|
+
if (a.length !== fixedArgs && (method === "emitMetadata" || a.length !== fixedArgs + 1))
|
|
342
|
+
this.fail(node, usage);
|
|
343
|
+
const disc = {
|
|
344
|
+
updateMetadataField: "dde9312db5cadcc8",
|
|
345
|
+
removeMetadataKey: "ea122038598d25b5",
|
|
346
|
+
updateMetadataAuthority: "d7e4a6e45464567b",
|
|
347
|
+
emitMetadata: "faa6b4fa0d0cb846",
|
|
348
|
+
}[method];
|
|
349
|
+
const mint = anyAcct(a[0], "the mint", method !== "emitMetadata");
|
|
350
|
+
const auth = method === "emitMetadata" ? undefined : this.accountParam(a[1], "the update authority");
|
|
351
|
+
const { buf, len, put, finish } = this.byteBuffer("tmd", 1232, "TS_ERR_INSTRUCTION_DATA", "token metadata instruction");
|
|
352
|
+
const d = this.tmp("disc");
|
|
353
|
+
this.line(`static const uint8_t ${d}[8] = ${cBytes([...Buffer.from(disc, "hex")])};`);
|
|
354
|
+
put(d, "8", 8);
|
|
355
|
+
if (method === "updateMetadataField") {
|
|
356
|
+
// Field: Name / Symbol / Uri for those names, otherwise Key(field), as the Token-2022 CLI reads them.
|
|
357
|
+
const f = this.expect(this.expr(a[2], STRING), STRING, a[2]);
|
|
358
|
+
const ft = this.tmp("field");
|
|
359
|
+
const tag = this.tmp("ftag");
|
|
360
|
+
this.line(`TS_Str ${ft} = ${f.c};`);
|
|
361
|
+
this.line(`uint8_t ${tag} = ts_str_eq(${ft}, ts_str((const uint8_t *)"name", 4)) ? 0 : ts_str_eq(${ft}, ts_str((const uint8_t *)"symbol", 6)) ? 1 : ts_str_eq(${ft}, ts_str((const uint8_t *)"uri", 3)) ? 2 : 3;`);
|
|
362
|
+
put(`&${tag}`, "1", 1);
|
|
363
|
+
const kl = this.tmp("klen");
|
|
364
|
+
this.line(`uint32_t ${kl} = (uint32_t)${ft}.len;`);
|
|
365
|
+
put(`&${kl}`, `(${tag} == 3 ? 4 : 0)`);
|
|
366
|
+
put(`${ft}.ptr`, `(${tag} == 3 ? ${ft}.len : 0)`);
|
|
367
|
+
this.serialize(this.expect(this.expr(a[3], STRING), STRING, a[3]), a[3], put);
|
|
368
|
+
}
|
|
369
|
+
else if (method === "removeMetadataKey") {
|
|
370
|
+
const key = this.expect(this.expr(a[2], STRING), STRING, a[2]);
|
|
371
|
+
const idem = this.expect(this.expr(a[3], BOOL), BOOL, a[3]);
|
|
372
|
+
const t = this.tmp("idem");
|
|
373
|
+
this.line(`uint8_t ${t} = (${idem.c}) ? 1 : 0;`);
|
|
374
|
+
put(`&${t}`, "1", 1);
|
|
375
|
+
this.serialize(key, a[2], put);
|
|
376
|
+
}
|
|
377
|
+
else if (method === "updateMetadataAuthority") {
|
|
378
|
+
nonZeroKey(a[2])(put);
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
// start: None, end: None (the whole TokenMetadata as return data).
|
|
382
|
+
byte(0)(put);
|
|
383
|
+
byte(0)(put);
|
|
384
|
+
}
|
|
385
|
+
finish(node);
|
|
386
|
+
const seeds = auth ? this.signerSeeds(a[fixedArgs], auth, a[1]) : none;
|
|
387
|
+
this.cpi(p, auth
|
|
388
|
+
? [
|
|
389
|
+
[`a_${mint.name}`, true, false],
|
|
390
|
+
[`a_${auth.name}`, false, true],
|
|
391
|
+
]
|
|
392
|
+
: [[`a_${mint.name}`, false, false]], buf, len, seeds);
|
|
393
|
+
return undefined;
|
|
394
|
+
}
|
|
395
|
+
case "approve": {
|
|
396
|
+
arity(4);
|
|
397
|
+
const src = want(a[0], "token", true, "the source");
|
|
398
|
+
const delegate = this.accountParam(a[1], "the delegate");
|
|
399
|
+
const owner = this.accountParam(a[2], "the owner");
|
|
400
|
+
const s = this.signerSeeds(a[4], owner, a[2]);
|
|
401
|
+
const amt = value(a[3], U64);
|
|
402
|
+
simple(4, [amt], [
|
|
403
|
+
[src, true, false],
|
|
404
|
+
[delegate, false, false],
|
|
405
|
+
[owner, false, true],
|
|
406
|
+
], s);
|
|
407
|
+
return undefined;
|
|
408
|
+
}
|
|
409
|
+
case "revoke": {
|
|
410
|
+
arity(2);
|
|
411
|
+
const src = want(a[0], "token", true, "the source");
|
|
412
|
+
const owner = this.accountParam(a[1], "the owner");
|
|
413
|
+
simple(5, [], [
|
|
414
|
+
[src, true, false],
|
|
415
|
+
[owner, false, true],
|
|
416
|
+
], this.signerSeeds(a[2], owner, a[1]));
|
|
417
|
+
return undefined;
|
|
418
|
+
}
|
|
419
|
+
case "setAuthority": {
|
|
420
|
+
arity(4);
|
|
421
|
+
const target = anyAcct(a[0], "the mint or token account", true);
|
|
422
|
+
const current = this.accountParam(a[1], "the current authority");
|
|
423
|
+
this.model.options.set("pubkey", PUBKEY);
|
|
424
|
+
const s = this.signerSeeds(a[4], current, a[1]);
|
|
425
|
+
simple(6, [value(a[2], U8), value(a[3], optKey)], [
|
|
426
|
+
[target, true, false],
|
|
427
|
+
[current, false, true],
|
|
428
|
+
], s);
|
|
429
|
+
return undefined;
|
|
430
|
+
}
|
|
431
|
+
case "freezeAccount":
|
|
432
|
+
case "thawAccount": {
|
|
433
|
+
arity(3);
|
|
434
|
+
const acct = want(a[0], "token", true, "the token account");
|
|
435
|
+
const mint = this.accountParam(a[1], "the mint");
|
|
436
|
+
const auth = this.accountParam(a[2], "the freeze authority");
|
|
437
|
+
simple(method === "freezeAccount" ? 10 : 11, [], [
|
|
438
|
+
[acct, true, false],
|
|
439
|
+
[mint, false, false],
|
|
440
|
+
[auth, false, true],
|
|
441
|
+
], this.signerSeeds(a[3], auth, a[2]));
|
|
442
|
+
return undefined;
|
|
443
|
+
}
|
|
444
|
+
case "syncNative": {
|
|
445
|
+
if (a.length !== 1)
|
|
446
|
+
this.fail(node, "syncNative(account) takes the wrapped-SOL token account");
|
|
447
|
+
const acct = anyAcct(a[0], "the token account", true);
|
|
448
|
+
simple(17, [], [[acct, true, false]], { name: "NULL", count: 0 });
|
|
449
|
+
return undefined;
|
|
450
|
+
}
|
|
451
|
+
case "initializeMint2": {
|
|
452
|
+
if (a.length !== 4)
|
|
453
|
+
this.fail(node, "initializeMint2(mint, decimals, mintAuthority, freezeAuthority | null)");
|
|
454
|
+
const mint = anyAcct(a[0], "the mint", true);
|
|
455
|
+
this.model.options.set("pubkey", PUBKEY);
|
|
456
|
+
simple(20, [value(a[1], U8), value(a[2], PUBKEY), value(a[3], optKey)], [[mint, true, false]], { name: "NULL", count: 0 });
|
|
457
|
+
return undefined;
|
|
458
|
+
}
|
|
459
|
+
case "initializeAccount3": {
|
|
460
|
+
if (a.length !== 3)
|
|
461
|
+
this.fail(node, "initializeAccount3(account, mint, owner)");
|
|
462
|
+
const acct = anyAcct(a[0], "the token account", true);
|
|
463
|
+
const mint = this.accountParam(a[1], "the mint");
|
|
464
|
+
simple(18, [value(a[2], PUBKEY)], [
|
|
465
|
+
[acct, true, false],
|
|
466
|
+
[mint, false, false],
|
|
467
|
+
], { name: "NULL", count: 0 });
|
|
468
|
+
return undefined;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
this.fail(node.expression, `token programs support transfer, transferChecked, mintTo, burn, closeAccount, approve, revoke, setAuthority, freezeAccount, thawAccount, syncNative, initializeMint2, initializeAccount3, initializeMetadata, updateMetadataField, removeMetadataKey, updateMetadataAuthority, emitMetadata, the Token-2022 extension instructions (updateDefaultAccountState, updateTransferHook, enableRequiredMemoTransfers, disableRequiredMemoTransfers, updateInterestRate, transferCheckedWithFee, harvestWithheldTokensToMint, withdrawWithheldTokensFromMint, setTransferFee) and initializers (initializeTransferFeeConfig, initializeMintCloseAuthority, initializeNonTransferableMint, initializePermanentDelegate, initializeDefaultAccountState, initializeMetadataPointer, initializeGroupPointer, initializeGroupMemberPointer, initializeTransferHook, initializeInterestBearingMint, initializeImmutableOwner), not ${method}`);
|
|
472
|
+
}
|
|
473
|
+
ataOp(node, program, method) {
|
|
474
|
+
if (method !== "create" && method !== "createIdempotent")
|
|
475
|
+
this.fail(node.expression, "the Associated Token program supports create and createIdempotent");
|
|
476
|
+
const a = node.arguments;
|
|
477
|
+
if (a.length !== 6)
|
|
478
|
+
this.fail(node, `${method}(payer, ata, owner, mint, systemProgram, tokenProgram) takes six accounts`);
|
|
479
|
+
const payer = this.accountParam(a[0], "the payer");
|
|
480
|
+
if (payer.kind.kind !== "signer" || !payer.kind.mut)
|
|
481
|
+
this.fail(a[0], "the payer must be a Mut<Signer>");
|
|
482
|
+
const ata = this.accountParam(a[1], "the token account");
|
|
483
|
+
if (!isWritable(ata.kind))
|
|
484
|
+
this.fail(a[1], "the token account must be Mut<AccountInfo>");
|
|
485
|
+
const [owner, mint, sys, tp] = [a[2], a[3], a[4], a[5]].map((n, i) => this.accountParam(n, ["the owner", "the mint", "the system program", "the token program"][i]));
|
|
486
|
+
if (sys.kind.kind !== "system")
|
|
487
|
+
this.fail(a[4], "pass the SystemProgram parameter");
|
|
488
|
+
if (tp.kind.kind !== "tokenProgram")
|
|
489
|
+
this.fail(a[5], "pass a token program parameter");
|
|
490
|
+
this.line(`TS_TRY(ts_ata_create(params, a_${program.name}, a_${payer.name}, a_${ata.name}, a_${owner.name}, a_${mint.name}, a_${sys.name}, a_${tp.name}, ${method === "createIdempotent"}));`);
|
|
491
|
+
return undefined;
|
|
492
|
+
}
|
|
493
|
+
}
|
package/dist/compiler/index.js
CHANGED
|
@@ -12,7 +12,7 @@ export { CompileError, formatDiagnostic } from "./diagnostics.js";
|
|
|
12
12
|
export { SBPF_VERSIONS } from "./toolchain.js";
|
|
13
13
|
/** Typecheck, enforce the TypeSOL subset, and generate C. Throws CompileError with every diagnostic. */
|
|
14
14
|
export function generate(entry, opts = {}) {
|
|
15
|
-
const analysis = analyze(entry);
|
|
15
|
+
const analysis = analyze(entry, opts.files);
|
|
16
16
|
analysis.reporter.throwIfAny();
|
|
17
17
|
const c = emitProgram(analysis, opts);
|
|
18
18
|
analysis.reporter.throwIfAny();
|
package/dist/compiler/model.d.ts
CHANGED
|
@@ -115,12 +115,16 @@ export type FieldType = ScalarType | {
|
|
|
115
115
|
kind: "dataEnum";
|
|
116
116
|
name: string;
|
|
117
117
|
}
|
|
118
|
-
/**
|
|
119
|
-
|
|
118
|
+
/**
|
|
119
|
+
* `Vec<T>` (stored after the fixed-size fields, grows the account) or, with `cap`, `FixedVec<T, N>`: a u32 length and
|
|
120
|
+
* room for N elements. With `heap`, a `Vec<T>` in a `@layout borsh` struct: Anchor's Vec, held on the heap while the
|
|
121
|
+
* instruction runs (`TSH_*`: length, capacity, elements) and bounded only by the account when it is written back.
|
|
122
|
+
*/
|
|
120
123
|
| {
|
|
121
124
|
kind: "vec";
|
|
122
125
|
elem: FieldType;
|
|
123
126
|
cap?: number;
|
|
127
|
+
heap?: true;
|
|
124
128
|
};
|
|
125
129
|
export interface StructField {
|
|
126
130
|
name: string;
|
|
@@ -487,6 +491,10 @@ export declare function optionKey(t: ScalarType | {
|
|
|
487
491
|
name: string;
|
|
488
492
|
}): string;
|
|
489
493
|
export declare function scalarKey(t: ScalarType): string;
|
|
494
|
+
/** The C type of a heap Vec<T> (in a `@layout borsh` struct): `TSH_<T>`. */
|
|
495
|
+
export declare function heapVecName(t: FieldType & {
|
|
496
|
+
kind: "vec";
|
|
497
|
+
}): string;
|
|
490
498
|
/** The IDL type name of a FixedVec<T, N>: `FixedVec<N>_<T>`. */
|
|
491
499
|
export declare function fixedVecName(t: FieldType & {
|
|
492
500
|
kind: "vec";
|
package/dist/compiler/model.js
CHANGED
|
@@ -95,7 +95,7 @@ export function sameType(a, b) {
|
|
|
95
95
|
case "option":
|
|
96
96
|
return sameType(a.inner, b.inner);
|
|
97
97
|
case "vec":
|
|
98
|
-
return a.cap === b.cap && sameType(a.elem, b.elem);
|
|
98
|
+
return a.cap === b.cap && !a.heap === !b.heap && sameType(a.elem, b.elem);
|
|
99
99
|
case "vecArg":
|
|
100
100
|
return sameType(a.elem, b.elem);
|
|
101
101
|
case "arrayArg":
|
|
@@ -123,6 +123,12 @@ export function optionKey(t) {
|
|
|
123
123
|
export function scalarKey(t) {
|
|
124
124
|
return t.kind === "int" ? t.name : t.kind === "enum" ? `e_${t.name}` : t.kind === "pubkey" && t.hash ? "hash" : t.kind;
|
|
125
125
|
}
|
|
126
|
+
/** The C type of a heap Vec<T> (in a `@layout borsh` struct): `TSH_<T>`. */
|
|
127
|
+
export function heapVecName(t) {
|
|
128
|
+
return `TSH_${typeName(t.elem)
|
|
129
|
+
.replace(/[^A-Za-z0-9]+/g, "_")
|
|
130
|
+
.replace(/_+$/, "")}`;
|
|
131
|
+
}
|
|
126
132
|
/** The IDL type name of a FixedVec<T, N>: `FixedVec<N>_<T>`. */
|
|
127
133
|
export function fixedVecName(t) {
|
|
128
134
|
return `FixedVec${t.cap}_${typeName(t.elem)
|
package/dist/tools/portgen.js
CHANGED
|
@@ -209,6 +209,9 @@ export function generatePort(idl) {
|
|
|
209
209
|
}
|
|
210
210
|
if (t === "bytes")
|
|
211
211
|
return borshField({ vec: "u8" }, at);
|
|
212
|
+
// A Vec of fixed-size elements is Anchor's Vec as is (bounded by the account); of strings or arrays it needs a capacity.
|
|
213
|
+
if (typeof t === "object" && t.vec && t.vec !== "string" && !(typeof t.vec === "object" && t.vec.array) && fixedOk(t.vec))
|
|
214
|
+
return quiet ? "x" : `${use("Vec")}<${tsType(t.vec, "account", at)}>`;
|
|
212
215
|
if (typeof t === "object" && t.vec) {
|
|
213
216
|
const elem = t.vec === "string"
|
|
214
217
|
? `${use("FixedString")}<32> /* TODO: #[max_len] */`
|
package/dist/workspace.d.ts
CHANGED
|
@@ -48,5 +48,5 @@ export declare function deployArgs(soPath: string, keypair: string, opts: {
|
|
|
48
48
|
payer?: string;
|
|
49
49
|
config?: string;
|
|
50
50
|
}): string[];
|
|
51
|
-
/** Create a project: typesol.json, one program from a template, its LiteSVM test, configs. Refuses to overwrite files. */
|
|
51
|
+
/** Create a project: typesol.json, one program from a template, its LiteSVM test, configs (editor settings included). Refuses to overwrite files. */
|
|
52
52
|
export declare function scaffold(dir: string, name: string, template?: TemplateName): string[];
|
package/dist/workspace.js
CHANGED
|
@@ -105,7 +105,7 @@ export function deployArgs(soPath, keypair, opts) {
|
|
|
105
105
|
args.push("--keypair", opts.payer);
|
|
106
106
|
return args;
|
|
107
107
|
}
|
|
108
|
-
/** Create a project: typesol.json, one program from a template, its LiteSVM test, configs. Refuses to overwrite files. */
|
|
108
|
+
/** Create a project: typesol.json, one program from a template, its LiteSVM test, configs (editor settings included). Refuses to overwrite files. */
|
|
109
109
|
export function scaffold(dir, name, template = "counter") {
|
|
110
110
|
const t = TEMPLATES[template];
|
|
111
111
|
if (!/^[a-z][a-z0-9_]*$/.test(name))
|
|
@@ -146,6 +146,8 @@ export function scaffold(dir, name, template = "counter") {
|
|
|
146
146
|
...t.devDependencies,
|
|
147
147
|
},
|
|
148
148
|
}, null, 2) + "\n",
|
|
149
|
+
// VS Code loads tsconfig plugins (typesol/ts-plugin) only with the workspace's TypeScript: offer to switch to it.
|
|
150
|
+
".vscode/settings.json": JSON.stringify({ "typescript.tsdk": "node_modules/typescript/lib", "typescript.enablePromptUseWorkspaceTsdk": true }, null, 2) + "\n",
|
|
149
151
|
// target/deploy/*-keypair.json are the programs' addresses: never commit them, but keep a backup.
|
|
150
152
|
".gitignore": "node_modules/\n# target/deploy/*-keypair.json are the programs' addresses (and upgrade keys): never commit them, but BACK THEM UP.\n# A fresh clone cannot build or deploy a program whose address is recorded in typesol.json without its keypair.\ntarget/\n",
|
|
151
153
|
"tsconfig.json": JSON.stringify({
|
package/docs/architecture.md
CHANGED
|
@@ -60,7 +60,7 @@ program.so + program.build.json (manifest: versions, sha256 of sources and binar
|
|
|
60
60
|
| `src/templates.ts` | The programs and tests `typesol init --template` writes (copies of `examples/`; a test checks they match). |
|
|
61
61
|
| `src/tools/svmrpc.ts` | `svmConnection`: a kit RPC and subscriptions pair answered by LiteSVM, so generated clients run in tests (exported by `typesol/testing`). |
|
|
62
62
|
| `src/keys.ts` | Base58 and program keypair helpers. |
|
|
63
|
-
| `ts-plugin/index.js` | `typesol/ts-plugin` (CommonJS, loaded by tsserver):
|
|
63
|
+
| `ts-plugin/index.js` | `typesol/ts-plugin` (CommonJS, loaded by tsserver): checks the program entries with the editor's texts (`generate(entry, { files })`, in-process once `dist/compiler` has loaded, `typesol check --json --files` before) and adds each diagnostic to the file it is in. |
|
|
64
64
|
| `src/testing.ts` | `typesol/testing`: LiteSVM test helpers for projects (accounts by name from the IDL, plain values). |
|
|
65
65
|
| `src/tools/onchain.ts` | `typesol verify --address`: a deployed program's ELF, authority and deploy slot under loader v2, v3 or v4. |
|
|
66
66
|
| `src/tools/profile.ts` | Turns `--profile` logs into compute units per source line. |
|
|
@@ -77,16 +77,44 @@ call evaluates expressions), the method is declared `abstract` at the end of `ba
|
|
|
77
77
|
| --- | --- | --- |
|
|
78
78
|
| `shared.ts` | | `Value`, `Ref` and `Local`, limits, and the C type helpers (`valueC`, `fieldC`, `storageDecl`, `cString`), growable Vec placement (`vecPlace`, `vecLenCheck`) |
|
|
79
79
|
| `base.ts` | `EmitterBase` | function state; `line()`, `tmp()`, `settle()`, scopes, `fail()`, account lookup |
|
|
80
|
+
| `alias.ts` | `AliasEmitter` | aliasing, in one place: the storage paths an expression names, refusing to replace data a live reference still reads, and which values may resize a growable Vec |
|
|
80
81
|
| `storage.ts` | `StorageEmitter` | references into account data and locals, reads and writes, indexing, aliasing, argument validation |
|
|
81
82
|
| `expressions.ts` | `ExpressionEmitter` | literals, operators and checked arithmetic, property reads, conditionals, text, casts |
|
|
82
83
|
| `calls.ts` | `CallEmitter` | `call()` (the built-in dispatch), Vec methods, helpers, remaining accounts, foreign reads, return data |
|
|
83
|
-
| `cpi.ts` | `
|
|
84
|
+
| `cpi-core.ts` | `CpiCoreEmitter` | seeds and PDAs, signer seeds, remaining-account ranges, byte buffers and Borsh serialization |
|
|
85
|
+
| `create.ts` | `CreateEmitter` | `create` / `createIfNeeded` of program accounts, mints (with Token-2022 extensions), token accounts and ATAs; System transfers |
|
|
86
|
+
| `token.ts` | `TokenEmitter` | SPL Token / Token-2022 instructions and the ATA program |
|
|
87
|
+
| `metaplex.ts` | `MetaplexEmitter` | Token Metadata, Core (plugins, external plugins) and Bubblegum |
|
|
88
|
+
| `cpi.ts` | `CpiEmitter` | `invoke`, `invokeInstruction`, events |
|
|
84
89
|
| `statements.ts` | `StatementEmitter` | variables, assignment, `if`/loops/`switch`, `return` |
|
|
85
90
|
| `function.ts` | `FunctionEmitter` | the instruction prologue (account checks, constraints, argument decoding) and epilogue |
|
|
86
91
|
|
|
87
92
|
`borsh.ts` writes the Borsh pack/unpack codecs of `@layout borsh` structs, and `program.ts` assembles the C file
|
|
88
93
|
(typedefs, codecs, helpers, instructions, the entry point). `index.ts` is the emitter's export.
|
|
89
94
|
|
|
95
|
+
## Why there is no IR (yet)
|
|
96
|
+
|
|
97
|
+
The emitter lowers the checked TypeScript AST straight to C, and clang (-O2) optimizes it. There is no intermediate
|
|
98
|
+
representation in between. We reviewed this on 2026-10-30 and kept it that way for now:
|
|
99
|
+
|
|
100
|
+
- **What an IR would buy:** flow-sensitive analyses instead of syntactic ones (aliasing today follows storage paths
|
|
101
|
+
named in the source, see `alias.ts`), a second backend (SBF without clang), and optimizations clang cannot see
|
|
102
|
+
because they depend on account semantics (dropping a repeated check, packing a `@layout borsh` account only where
|
|
103
|
+
it changed).
|
|
104
|
+
- **What it would cost now:** rewriting most of the compiler's 12,000 lines, and losing the property every release
|
|
105
|
+
relies on: programs that do not use a change compile to the same binaries. Audit scope and differential results
|
|
106
|
+
would start over.
|
|
107
|
+
- **What we have instead:** the layers above, each owning one concern; `Ref` / `Value` as the typed place and value
|
|
108
|
+
model; refusals where the syntactic analysis cannot prove safety (a false refusal is a compile error, never a wrong
|
|
109
|
+
program); clang for the generic optimizations. Refactors are checked with `scripts/codegen-snapshot.ts`: the
|
|
110
|
+
generated C and IDL of every example, fixture and port, compared byte for byte before and after.
|
|
111
|
+
|
|
112
|
+
Revisit it when one of these becomes real:
|
|
113
|
+
|
|
114
|
+
1. a second backend (no clang, or another VM);
|
|
115
|
+
2. an optimization that needs data flow across statements and pays for itself in the benchmarks;
|
|
116
|
+
3. aliasing refusals that users hit in correct programs and that only flow-sensitive analysis can lift.
|
|
117
|
+
|
|
90
118
|
## Rules the emitter keeps
|
|
91
119
|
|
|
92
120
|
These invariants are easy to break. Each one has tests.
|