@typesol/cli 0.38.0 → 0.40.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 +373 -2
- package/README.md +43 -26
- package/SECURITY.md +1 -1
- package/dist/cli.js +37 -3
- package/dist/compiler/analyze.d.ts +6 -11
- package/dist/compiler/analyze.js +331 -34
- package/dist/compiler/emit/alias.js +3 -2
- package/dist/compiler/emit/base.d.ts +7 -1
- package/dist/compiler/emit/base.js +2 -0
- package/dist/compiler/emit/borsh.js +63 -2
- package/dist/compiler/emit/calls.d.ts +14 -1
- package/dist/compiler/emit/calls.js +311 -21
- package/dist/compiler/emit/cpi-core.js +17 -5
- package/dist/compiler/emit/cpi.js +81 -27
- package/dist/compiler/emit/create.d.ts +7 -0
- package/dist/compiler/emit/create.js +27 -0
- package/dist/compiler/emit/expressions.d.ts +6 -0
- package/dist/compiler/emit/expressions.js +102 -6
- package/dist/compiler/emit/function.d.ts +7 -0
- package/dist/compiler/emit/function.js +164 -29
- package/dist/compiler/emit/program.js +27 -2
- package/dist/compiler/emit/shared.d.ts +15 -5
- package/dist/compiler/emit/shared.js +5 -0
- package/dist/compiler/emit/statements.d.ts +28 -1
- package/dist/compiler/emit/statements.js +270 -13
- package/dist/compiler/emit/storage.d.ts +10 -1
- package/dist/compiler/emit/storage.js +50 -11
- package/dist/compiler/hints.d.ts +5 -0
- package/dist/compiler/hints.js +88 -0
- package/dist/compiler/idl.d.ts +1 -1
- package/dist/compiler/idl.js +42 -11
- package/dist/compiler/index.d.ts +3 -0
- package/dist/compiler/index.js +6 -1
- package/dist/compiler/model.d.ts +47 -10
- package/dist/compiler/model.js +14 -4
- package/dist/testing.js +8 -0
- package/dist/tools/clientgen.js +10 -1
- package/dist/tools/portgen.d.ts +6 -0
- package/dist/tools/portgen.js +68 -17
- package/docs/architecture.md +5 -4
- package/docs/audit.md +31 -19
- package/docs/case-study-escrow.md +5 -5
- package/docs/case-study-jito-mev.md +9 -5
- package/docs/case-study-marginfi.md +797 -0
- package/docs/case-study-multisig.md +3 -3
- package/docs/case-study-raydium-amm.md +80 -0
- package/docs/case-study-raydium-clmm.md +44 -33
- package/docs/case-study-raydium-cp-swap.md +3 -3
- package/docs/cli.md +5 -5
- package/docs/client.md +1 -1
- package/docs/errors.md +13 -5
- package/docs/language.md +65 -32
- package/docs/migrate-from-anchor.md +4 -1
- package/docs/runtime.md +1 -1
- package/docs/security-model.md +2 -1
- package/docs/testing.md +34 -1
- package/docs/troubleshooting.md +6 -0
- package/lib/typesol.d.ts +118 -10
- package/package.json +9 -7
- package/runtime/typesol_rt.h +158 -17
- package/ts-plugin/index.js +24 -1
package/dist/compiler/analyze.js
CHANGED
|
@@ -5,17 +5,18 @@ import { fileURLToPath } from "node:url";
|
|
|
5
5
|
import { CompileError, Reporter } from "./diagnostics.js";
|
|
6
6
|
import { decodeAddress } from "../keys.js";
|
|
7
7
|
import { borshIdentical } from "./emit/borsh.js";
|
|
8
|
+
import { hintForTypeScript, unsupportedSyntax } from "./hints.js";
|
|
8
9
|
import { optionKey, INT_INFO, INT_NAMES, scalarKey, fixedVecName, isScalar, scalarSize, typeName, } from "./model.js";
|
|
9
10
|
export const LIB_PATH = fileURLToPath(new URL("../../lib/typesol.d.ts", import.meta.url));
|
|
10
11
|
/** Names a struct field may not use because account parameters already expose them. */
|
|
11
12
|
const RESERVED_FIELDS = new Set(["create", "equals", "get", "set", "length"]);
|
|
12
13
|
/** Members every account parameter has (`acct.key`, `acct.isSigner`, …): a struct held in an account cannot use them. */
|
|
13
|
-
const ACCOUNT_MEMBERS = new Set(["key", "lamports", "dataLen", "isSigner", "isWritable", "pdaBump"]);
|
|
14
|
+
export const ACCOUNT_MEMBERS = new Set(["key", "lamports", "dataLen", "isSigner", "isWritable", "pdaBump"]);
|
|
14
15
|
/** A struct used as account data: none of its fields may shadow an account member. */
|
|
15
16
|
export function checkAccountMembers(s, at, reporter) {
|
|
16
17
|
const clash = s.fields.find((f) => ACCOUNT_MEMBERS.has(f.name));
|
|
17
18
|
if (clash)
|
|
18
|
-
reporter.fail(at, `${s.name}.${clash.name}: "${clash.name}" is an account member (acct.${clash.name}); rename the field to hold ${s.name} in an account`);
|
|
19
|
+
reporter.fail(at, `${s.name}.${clash.name}: "${clash.name}" is an account member (acct.${clash.name}); rename the field to hold ${s.name} in an account (${clash.name}_ keeps the name "${snakeCase(clash.name)}" in the IDL)`);
|
|
19
20
|
}
|
|
20
21
|
/**
|
|
21
22
|
* Struct fields and enum variants become C member names as written. C keywords and the object-like
|
|
@@ -61,6 +62,7 @@ function checkSnakeDuplicates(names, what, reporter) {
|
|
|
61
62
|
/** Exports of the `typesol` module that are types (everything else is a function). */
|
|
62
63
|
const TYPE_EXPORTS = new Set([
|
|
63
64
|
...INT_NAMES,
|
|
65
|
+
"f64",
|
|
64
66
|
"Pubkey",
|
|
65
67
|
"AccountMeta",
|
|
66
68
|
"Signer",
|
|
@@ -146,6 +148,42 @@ export function snakeCase(name) {
|
|
|
146
148
|
* argument of one name (`authority`); TypeScript cannot, so the argument is written with a trailing underscore
|
|
147
149
|
* (`authority_`) and recorded without it.
|
|
148
150
|
*/
|
|
151
|
+
/** `/** @repr c *\/` (packed, align N) / `@repr rust` and `/** @serialization bytemuck *\/` on a type: see IdlMeta. */
|
|
152
|
+
function idlMetaOf(decl, reporter) {
|
|
153
|
+
const text = (name) => {
|
|
154
|
+
const tag = ts.getJSDocTags(decl).find((t) => t.tagName.text === name);
|
|
155
|
+
return tag ? { tag, words: (typeof tag.comment === "string" ? tag.comment : "").trim().split(/\s+/).filter(Boolean) } : undefined;
|
|
156
|
+
};
|
|
157
|
+
const repr = text("repr");
|
|
158
|
+
const serialization = text("serialization");
|
|
159
|
+
if (!repr && !serialization)
|
|
160
|
+
return undefined;
|
|
161
|
+
const meta = {};
|
|
162
|
+
if (repr) {
|
|
163
|
+
const [kind, ...rest] = repr.words;
|
|
164
|
+
if (kind !== "c" && kind !== "rust")
|
|
165
|
+
reporter.fail(repr.tag, "@repr is c or rust, then optionally packed and align N: @repr c align 8");
|
|
166
|
+
meta.repr = { kind };
|
|
167
|
+
for (let i = 0; i < rest.length; i++) {
|
|
168
|
+
if (rest[i] === "packed")
|
|
169
|
+
meta.repr.packed = true;
|
|
170
|
+
else if (rest[i] === "align" && /^\d+$/.test(rest[i + 1] ?? ""))
|
|
171
|
+
meta.repr.align = Number(rest[++i]);
|
|
172
|
+
else
|
|
173
|
+
reporter.fail(repr.tag, `@repr: unknown "${rest[i]}" (packed, align N)`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (serialization) {
|
|
177
|
+
if (serialization.words.length !== 1)
|
|
178
|
+
reporter.fail(serialization.tag, "@serialization takes one word: bytemuck, bytemuckunsafe, …");
|
|
179
|
+
meta.serialization = serialization.words[0];
|
|
180
|
+
}
|
|
181
|
+
return meta;
|
|
182
|
+
}
|
|
183
|
+
/** A struct field's IDL name: snake_case, and an account member's name plus "_" (`key_`) without the "_". */
|
|
184
|
+
export function fieldIdlName(name) {
|
|
185
|
+
return name.endsWith("_") && ACCOUNT_MEMBERS.has(name.slice(0, -1)) ? snakeCase(name.slice(0, -1)) : snakeCase(name);
|
|
186
|
+
}
|
|
149
187
|
export function argIdlName(name, accountNames) {
|
|
150
188
|
const base = snakeCase(name);
|
|
151
189
|
if (base.length > 1 && base.endsWith("_") && !base.endsWith("__") && accountNames.some((a) => snakeCase(a) === base.slice(0, -1)))
|
|
@@ -155,18 +193,19 @@ export function argIdlName(name, accountNames) {
|
|
|
155
193
|
/**
|
|
156
194
|
* An account struct's discriminator: Anchor's sha256("account:<Name>")[..8], or the one a `/** @discriminator … *\/` tag
|
|
157
195
|
* gives (a sha256 preimage such as `account:Counter`, or 0x and 8 bytes), e.g. for an old version of a struct kept under
|
|
158
|
-
* a new name for Migrate<From, To
|
|
196
|
+
* a new name for Migrate<From, To>; 0x and 1 to 7 bytes for another program's account (`Owner<Account<T>, "…">`).
|
|
159
197
|
*/
|
|
160
198
|
function accountDiscriminator(decl, name, reporter) {
|
|
161
199
|
const tag = ts.getJSDocTags(decl).find((t) => t.tagName.text === "discriminator");
|
|
162
200
|
if (!tag)
|
|
163
201
|
return discriminator(`account:${name}`);
|
|
164
202
|
const text = (typeof tag.comment === "string" ? tag.comment : (ts.getTextOfJSDocComment(tag.comment) ?? "")).trim();
|
|
165
|
-
|
|
203
|
+
// 8 bytes; another program's account may have a shorter one (Solend's reserve: its version byte).
|
|
204
|
+
if (/^0x([0-9a-fA-F]{2}){1,8}$/.test(text))
|
|
166
205
|
return [...Buffer.from(text.slice(2), "hex")];
|
|
167
206
|
if (text && !text.startsWith("0x"))
|
|
168
207
|
return discriminator(text);
|
|
169
|
-
reporter.fail(decl, "@discriminator on an account struct needs a sha256 preimage (account:<Name>) or 0x and 8 bytes");
|
|
208
|
+
reporter.fail(decl, "@discriminator on an account struct needs a sha256 preimage (account:<Name>) or 0x and 8 bytes (1 to 8 for another program's account)");
|
|
170
209
|
}
|
|
171
210
|
export function discriminator(preimage) {
|
|
172
211
|
return [...createHash("sha256").update(preimage).digest().subarray(0, 8)];
|
|
@@ -221,7 +260,9 @@ function typecheck(entry, files) {
|
|
|
221
260
|
const tsDiagnostics = userFiles.flatMap((f) => ts.getPreEmitDiagnostics(program, f));
|
|
222
261
|
if (tsDiagnostics.length > 0) {
|
|
223
262
|
const diagnostics = tsDiagnostics.map((d) => {
|
|
224
|
-
const
|
|
263
|
+
const flat = ts.flattenDiagnosticMessageText(d.messageText, "\n");
|
|
264
|
+
const hint = hintForTypeScript(d, flat, LIB_PATH);
|
|
265
|
+
const message = hint ? `${flat}\nhint: ${hint}` : flat;
|
|
225
266
|
if (!d.file || d.start === undefined)
|
|
226
267
|
return { file: entry, line: 1, column: 1, message };
|
|
227
268
|
const { line, character } = d.file.getLineAndCharacterOfPosition(d.start);
|
|
@@ -376,7 +417,19 @@ export function analyze(entry, files) {
|
|
|
376
417
|
for (const m of stmt.members) {
|
|
377
418
|
if (!m.initializer || !ts.isStringLiteral(m.initializer))
|
|
378
419
|
reporter.fail(m, 'error enum members need a string message: Name = "message"');
|
|
379
|
-
|
|
420
|
+
// Codes run on from the last one; `/** @code 6200 */` sets a member's (Anchor's `Variant = 200`), and the
|
|
421
|
+
// members after it continue from there.
|
|
422
|
+
let code = errors.length ? errors[errors.length - 1].code + 1 : 6000;
|
|
423
|
+
const tag = ts.getJSDocTags(m).find((t) => t.tagName.text === "code");
|
|
424
|
+
if (tag) {
|
|
425
|
+
const text = typeof tag.comment === "string" ? tag.comment.trim() : "";
|
|
426
|
+
// The first code may be below 6000: a native program's `ProgramError::Custom(n)` errors start at 0.
|
|
427
|
+
const least = errors.length ? code : 0;
|
|
428
|
+
if (!/^\d+$/.test(text) || Number(text) < least || Number(text) > 4_294_967_295)
|
|
429
|
+
reporter.fail(m, `@code must be a number from ${least} up (codes only go up; untagged ones start at 6000)`);
|
|
430
|
+
code = Number(text);
|
|
431
|
+
}
|
|
432
|
+
const def = { code, name: m.name.getText(file), msg: m.initializer.text };
|
|
380
433
|
// Names identify errors in the IDL: they must be unique across the program.
|
|
381
434
|
if (errors.some((e) => e.name === def.name))
|
|
382
435
|
reporter.fail(m, `error "${def.name}" is already declared; error names are program-wide`);
|
|
@@ -397,7 +450,8 @@ export function analyze(entry, files) {
|
|
|
397
450
|
});
|
|
398
451
|
if (variants.length === 0 || variants.length > 256)
|
|
399
452
|
reporter.fail(stmt, "enums need 1..256 variants");
|
|
400
|
-
|
|
453
|
+
const idlMeta = idlMetaOf(stmt, reporter);
|
|
454
|
+
enums.set(stmt.name.text, { name: stmt.name.text, variants, ...(idlMeta ? { idlMeta } : {}) });
|
|
401
455
|
}
|
|
402
456
|
}
|
|
403
457
|
else if (ts.isFunctionDeclaration(stmt)) {
|
|
@@ -459,7 +513,7 @@ export function analyze(entry, files) {
|
|
|
459
513
|
}
|
|
460
514
|
}
|
|
461
515
|
else {
|
|
462
|
-
reporter.fail(stmt,
|
|
516
|
+
reporter.fail(stmt, unsupportedSyntax(stmt.kind, "top level"));
|
|
463
517
|
}
|
|
464
518
|
});
|
|
465
519
|
}
|
|
@@ -672,6 +726,8 @@ export function analyze(entry, files) {
|
|
|
672
726
|
};
|
|
673
727
|
reporter.guard(() => checkRecursion([...helperNodes, ...locals.filter((l) => !inline.has(l.decl))], helperDecls, checker, reporter));
|
|
674
728
|
const accountStructs = new Set();
|
|
729
|
+
const foreignStructs = new Set();
|
|
730
|
+
const ownStructs = new Set();
|
|
675
731
|
const argStructs = new Set();
|
|
676
732
|
const instructions = [];
|
|
677
733
|
for (const fn of functions) {
|
|
@@ -716,6 +772,14 @@ export function analyze(entry, files) {
|
|
|
716
772
|
}
|
|
717
773
|
if (a.kind.kind === "account" || a.kind.kind === "init" || a.kind.kind === "zeroed" || a.kind.kind === "initIfNeeded") {
|
|
718
774
|
accountStructs.add(a.kind.struct.name);
|
|
775
|
+
// Owner<Account<T>, "…">: another program's account, which Anchor's IDL lists among the types only.
|
|
776
|
+
if (a.kind.kind === "account" && a.constraints.owner)
|
|
777
|
+
foreignStructs.add(a.kind.struct.name);
|
|
778
|
+
else {
|
|
779
|
+
ownStructs.add(a.kind.struct.name);
|
|
780
|
+
if (a.kind.struct.accountDiscriminator.length !== 8)
|
|
781
|
+
reporter.error(a.node, `${a.kind.struct.name}'s discriminator is shorter than 8 bytes: it is another program's account, Owner<Account<${a.kind.struct.name}>, "…">`);
|
|
782
|
+
}
|
|
719
783
|
if (a.kind.struct.viewsOnly)
|
|
720
784
|
reporter.error(a.node, `${a.kind.struct.name} holds a string (or an enum carrying one), so it is an argument, return value or event; account data uses FixedString<N>`);
|
|
721
785
|
}
|
|
@@ -725,6 +789,18 @@ export function analyze(entry, files) {
|
|
|
725
789
|
// nowhere else (not account data, not nested, not returned) is read as Anchor sends it: @layout borsh, implied.
|
|
726
790
|
// Anywhere else its FixedVec keeps the padded layout of account data, which an argument cannot have.
|
|
727
791
|
const hasFixedVec = (n) => structs.get(n).fields.some((f) => f.type.kind === "vec" && f.type.cap !== undefined);
|
|
792
|
+
// An Option anywhere in it (Anchor's BankConfigOpt): Borsh in arguments, one byte when empty.
|
|
793
|
+
const hasOption = (n, seen = new Set()) => !seen.has(n) &&
|
|
794
|
+
structs.get(n).fields.some((f) => {
|
|
795
|
+
const inner = f.type.kind === "option"
|
|
796
|
+
? "option"
|
|
797
|
+
: f.type.kind === "struct"
|
|
798
|
+
? f.type.name
|
|
799
|
+
: f.type.kind === "array" && f.type.elem.kind === "struct"
|
|
800
|
+
? f.type.elem.name
|
|
801
|
+
: undefined;
|
|
802
|
+
return inner === "option" || (inner !== undefined && hasOption(inner, new Set([...seen, n])));
|
|
803
|
+
});
|
|
728
804
|
const holds = (f, n) => {
|
|
729
805
|
const inner = f.type.kind === "array" || f.type.kind === "vec" ? f.type.elem : f.type.kind === "option" ? f.type.inner : f.type;
|
|
730
806
|
return inner.kind === "struct" && inner.name === n;
|
|
@@ -751,9 +827,53 @@ export function analyze(entry, files) {
|
|
|
751
827
|
reporter.guard(() => checkBorshLayout(sd, structs, dataEnums, reporter));
|
|
752
828
|
continue;
|
|
753
829
|
}
|
|
754
|
-
reporter.error(ix.node.name ?? ix.node, `argument ${a.name}: ${n} holds a FixedVec and is also account data, nested or returned
|
|
830
|
+
reporter.error(ix.node.name ?? ix.node, `argument ${a.name}: ${n} holds a FixedVec and is also account data, nested or returned: its bytes there are a fixed layout, in instruction data Borsh, and the IDL describes a type one way; declare a separate struct for the argument, or pass the list as its own FixedVec<T, N> argument`);
|
|
755
831
|
}
|
|
756
832
|
}
|
|
833
|
+
// Structs with Options (at any depth) that travel only in instruction data and events (Anchor's BankConfigOpt):
|
|
834
|
+
// Borsh on the wire, an empty Option one byte, while their C layout stays fixed. Not when they are also account
|
|
835
|
+
// data, nested in a fixed-layout struct that is not one of these, or returned.
|
|
836
|
+
{
|
|
837
|
+
const argStructs = new Set();
|
|
838
|
+
for (const ix of instructions)
|
|
839
|
+
for (const a of ix.args)
|
|
840
|
+
collectStructs(a.type, structs, dataEnums, argStructs);
|
|
841
|
+
// Also what @layout borsh structs (events, accounts) hold: their bytes there are Borsh at every depth.
|
|
842
|
+
const inBorsh = new Set();
|
|
843
|
+
const collectHeld = (n) => {
|
|
844
|
+
for (const f of structs.get(n)?.fields ?? []) {
|
|
845
|
+
const t = f.type.kind === "option" ? f.type.inner : f.type.kind === "array" ? f.type.elem : f.type;
|
|
846
|
+
if (t.kind === "struct" && !inBorsh.has(t.name)) {
|
|
847
|
+
inBorsh.add(t.name);
|
|
848
|
+
collectHeld(t.name);
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
};
|
|
852
|
+
for (const sd of structs.values())
|
|
853
|
+
if (sd.layout === "borsh")
|
|
854
|
+
collectHeld(sd.name);
|
|
855
|
+
const wire = new Set([...argStructs, ...inBorsh].filter((n) => structs.get(n).layout !== "borsh" && hasOption(n)));
|
|
856
|
+
const returned = (n) => instructions.some((i) => i.returns?.kind === "struct" && i.returns.name === n);
|
|
857
|
+
for (let changed = true; changed;) {
|
|
858
|
+
changed = false;
|
|
859
|
+
for (const n of [...wire]) {
|
|
860
|
+
const heldBy = [...structs.values()].filter((o) => o.fields.some((f) => holds(f, n)));
|
|
861
|
+
if (accountStructs.has(n) || returned(n) || heldBy.some((o) => o.layout !== "borsh" && !wire.has(o.name))) {
|
|
862
|
+
wire.delete(n);
|
|
863
|
+
changed = true;
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
for (const n of wire)
|
|
868
|
+
structs.get(n).wire = "borsh";
|
|
869
|
+
}
|
|
870
|
+
// An Option<Vec<T>> field is Borsh, of no fixed size: a field of an event, not of account data, an argument, a
|
|
871
|
+
// returned struct or a struct nested in another.
|
|
872
|
+
for (const sd of structs.values()) {
|
|
873
|
+
const f = sd.fields.find((x) => x.type.kind === "vec" && x.type.optional);
|
|
874
|
+
if (f && (accountStructs.has(sd.name) || argStructs.has(sd.name) || nestedSomewhere(sd.name) || sd.layout === "borsh"))
|
|
875
|
+
reporter.error(sd.node, `${sd.name}.${f.name}: Option<Vec<T>> fields are for events (emit<${sd.name}>), not account data, arguments or nested structs`);
|
|
876
|
+
}
|
|
757
877
|
checkSnakeDuplicates(instructions.map((ix) => ({ name: ix.node.name.text, node: ix.node.name })), "instructions", reporter);
|
|
758
878
|
// emitCpi's event instruction (Anchor's EVENT_IX_TAG) is dispatched before every other instruction.
|
|
759
879
|
if (instructions.some((ix) => ix.accounts.some((a) => a.kind.kind === "eventAuthority"))) {
|
|
@@ -795,6 +915,7 @@ export function analyze(entry, files) {
|
|
|
795
915
|
constants,
|
|
796
916
|
events: collectEvents([...functions, ...helperNodes], structs),
|
|
797
917
|
accountStructs,
|
|
918
|
+
foreignAccounts: new Set([...foreignStructs].filter((n) => !ownStructs.has(n))),
|
|
798
919
|
argStructs,
|
|
799
920
|
fstrings,
|
|
800
921
|
fixedVecs,
|
|
@@ -859,8 +980,26 @@ export function resolveArgAggregate(node, scope, structs) {
|
|
|
859
980
|
const top = fixed(node);
|
|
860
981
|
if (top?.kind === "struct")
|
|
861
982
|
return { kind: "structRef", struct: top.name, writable: false };
|
|
983
|
+
// Option<S> / S | null of a fixed-size struct whose Borsh bytes are its layout (no Options or Vecs inside).
|
|
984
|
+
{
|
|
985
|
+
let inner;
|
|
986
|
+
if (typesolName(node, scope.imports) === "Option")
|
|
987
|
+
inner = node.typeArguments?.[0];
|
|
988
|
+
else if (ts.isUnionTypeNode(node) && node.types.length === 2) {
|
|
989
|
+
const isNull = (t) => ts.isLiteralTypeNode(t) && t.literal.kind === ts.SyntaxKind.NullKeyword;
|
|
990
|
+
inner = isNull(node.types[1]) ? node.types[0] : isNull(node.types[0]) ? node.types[1] : undefined;
|
|
991
|
+
}
|
|
992
|
+
const s = inner && ts.isTypeReferenceNode(inner) && ts.isIdentifier(inner.typeName) && !inner.typeArguments?.length
|
|
993
|
+
? structs.get(structNameOf(inner, scope))
|
|
994
|
+
: undefined;
|
|
995
|
+
if (s && s.size !== undefined && !s.vec && s.layout !== "borsh" && s.wire !== "borsh")
|
|
996
|
+
return { kind: "optionArg", option: { kind: "option", inner: { kind: "struct", name: s.name } } };
|
|
997
|
+
}
|
|
862
998
|
if (top?.kind === "array")
|
|
863
999
|
return { kind: "arrayArg", array: top };
|
|
1000
|
+
const optionalList = optionalVecArg(node, scope, (n) => resolveArgAggregate(n, scope, structs));
|
|
1001
|
+
if (optionalList)
|
|
1002
|
+
return optionalList;
|
|
864
1003
|
const list = typesolName(node, scope.imports);
|
|
865
1004
|
if (list === "Vec" || list === "FixedVec") {
|
|
866
1005
|
const [e, n] = node.typeArguments ?? [];
|
|
@@ -895,22 +1034,41 @@ export function resolveScalarType(node, scope) {
|
|
|
895
1034
|
return { kind: "pubkey" };
|
|
896
1035
|
if (n === "Hash")
|
|
897
1036
|
return { kind: "pubkey", hash: true };
|
|
1037
|
+
// An f64 is held as its IEEE bits, a u64 (Borsh writes it as those 8 bytes); the IDL calls it f64.
|
|
1038
|
+
if (n === "f64")
|
|
1039
|
+
return { kind: "int", name: "u64", f64: true };
|
|
898
1040
|
if (ts.isTypeReferenceNode(node) && ts.isIdentifier(node.typeName) && scope.enums.has(node.typeName.text))
|
|
899
1041
|
return { kind: "enum", name: node.typeName.text };
|
|
900
1042
|
return undefined;
|
|
901
1043
|
}
|
|
902
1044
|
/** `Option<T>` or `T | null` with a scalar T. Records the use so a C typedef / IDL type is emitted. */
|
|
903
|
-
|
|
904
|
-
|
|
1045
|
+
/** The T of `Option<T>` or `T | null`. */
|
|
1046
|
+
function optionInner(node, scope) {
|
|
905
1047
|
if (typesolName(node, scope.imports) === "Option")
|
|
906
|
-
|
|
907
|
-
|
|
1048
|
+
return node.typeArguments?.[0];
|
|
1049
|
+
if (ts.isUnionTypeNode(node) && node.types.length === 2) {
|
|
908
1050
|
const isNull = (t) => ts.isLiteralTypeNode(t) && t.literal.kind === ts.SyntaxKind.NullKeyword;
|
|
909
1051
|
if (isNull(node.types[1]))
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
1052
|
+
return node.types[0];
|
|
1053
|
+
if (isNull(node.types[0]))
|
|
1054
|
+
return node.types[1];
|
|
913
1055
|
}
|
|
1056
|
+
return undefined;
|
|
1057
|
+
}
|
|
1058
|
+
/**
|
|
1059
|
+
* `Option<Vec<T>>` (or `Vec<T> | null`, or a FixedVec) as an instruction argument: Anchor's `Option<Vec<u8>>`. A tag
|
|
1060
|
+
* byte, then the Borsh Vec when Some; a None reads as an empty list, and `x !== null` tells them apart.
|
|
1061
|
+
*/
|
|
1062
|
+
function optionalVecArg(node, scope, inner) {
|
|
1063
|
+
const n = optionInner(node, scope);
|
|
1064
|
+
const list = n && typesolName(n, scope.imports);
|
|
1065
|
+
if (!n || (list !== "Vec" && list !== "FixedVec"))
|
|
1066
|
+
return undefined;
|
|
1067
|
+
const v = inner(n);
|
|
1068
|
+
return v?.kind === "vecArg" && v.elem.kind !== "string" ? { ...v, optional: true } : undefined;
|
|
1069
|
+
}
|
|
1070
|
+
export function resolveOptionType(node, scope) {
|
|
1071
|
+
const innerNode = optionInner(node, scope);
|
|
914
1072
|
if (!innerNode)
|
|
915
1073
|
return undefined;
|
|
916
1074
|
const inner = resolveScalarType(innerNode, scope);
|
|
@@ -926,6 +1084,11 @@ export function resolveValueType(node, scope, allowVec = false) {
|
|
|
926
1084
|
const opt = resolveOptionType(node, scope);
|
|
927
1085
|
if (opt)
|
|
928
1086
|
return opt;
|
|
1087
|
+
if (allowVec) {
|
|
1088
|
+
const v = optionalVecArg(node, scope, (n) => resolveValueType(n, scope, true));
|
|
1089
|
+
if (v)
|
|
1090
|
+
return v;
|
|
1091
|
+
}
|
|
929
1092
|
if (ts.isTypeReferenceNode(node) && ts.isIdentifier(node.typeName) && scope.dataEnums?.has(node.typeName.text))
|
|
930
1093
|
return { kind: "dataEnumArg", name: node.typeName.text };
|
|
931
1094
|
if (allowVec && typesolName(node, scope.imports) === "Vec") {
|
|
@@ -962,6 +1125,7 @@ function analyzeStructs(items, aliases, dataEnums, reporter, scopeFor, fstrings,
|
|
|
962
1125
|
const structs = new Map();
|
|
963
1126
|
let scope = scopeFor(new Map());
|
|
964
1127
|
let imports = scope.imports;
|
|
1128
|
+
const isScalarField = (t) => t.kind === "int" || t.kind === "bool" || t.kind === "pubkey" || t.kind === "enum";
|
|
965
1129
|
const fieldType = (node) => {
|
|
966
1130
|
if (node.kind === ts.SyntaxKind.StringKeyword)
|
|
967
1131
|
return { kind: "string" };
|
|
@@ -971,6 +1135,15 @@ function analyzeStructs(items, aliases, dataEnums, reporter, scopeFor, fstrings,
|
|
|
971
1135
|
const opt = resolveOptionType(node, scope);
|
|
972
1136
|
if (opt)
|
|
973
1137
|
return opt;
|
|
1138
|
+
// Option<Vec<T>> (events only: Anchor's Option<Vec<Pubkey>>): a tag byte, then the Vec when Some.
|
|
1139
|
+
{
|
|
1140
|
+
const inner = optionInner(node, scope);
|
|
1141
|
+
if (inner && typesolName(inner, imports) === "Vec") {
|
|
1142
|
+
const v = fieldType(inner);
|
|
1143
|
+
if (v.kind === "vec")
|
|
1144
|
+
return { ...v, optional: true };
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
974
1147
|
// Option of a struct (account data only): Option<Point> or Point | null
|
|
975
1148
|
{
|
|
976
1149
|
let inner;
|
|
@@ -985,6 +1158,15 @@ function analyzeStructs(items, aliases, dataEnums, reporter, scopeFor, fstrings,
|
|
|
985
1158
|
scope.options.set(optionKey(t), t);
|
|
986
1159
|
return { kind: "option", inner: t };
|
|
987
1160
|
}
|
|
1161
|
+
// Option<FixedArray<T, N>> of fixed-size elements (Anchor's Option<[u16; 3]>).
|
|
1162
|
+
if (inner && typesolName(inner, imports) === "FixedArray") {
|
|
1163
|
+
const arr = fieldType(inner);
|
|
1164
|
+
if (arr.kind === "array" && (isScalarField(arr.elem) || arr.elem.kind === "struct")) {
|
|
1165
|
+
const t = { kind: "array", elem: arr.elem, len: arr.len };
|
|
1166
|
+
scope.options.set(optionKey(t), t);
|
|
1167
|
+
return { kind: "option", inner: t };
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
988
1170
|
}
|
|
989
1171
|
if (ts.isTypeReferenceNode(node) && ts.isIdentifier(node.typeName) && enumNames.has(node.typeName.text))
|
|
990
1172
|
return { kind: "dataEnum", name: node.typeName.text };
|
|
@@ -1055,7 +1237,18 @@ function analyzeStructs(items, aliases, dataEnums, reporter, scopeFor, fstrings,
|
|
|
1055
1237
|
if (problem)
|
|
1056
1238
|
reporter.fail(m.name, problem);
|
|
1057
1239
|
const type = fieldType(m.type);
|
|
1058
|
-
|
|
1240
|
+
// `/** @bytes */`: an integer the IDL describes as its bytes (Marginfi's WrappedI80F48 { value: [u8; 16] }), or a
|
|
1241
|
+
// FixedArray of them, each its bytes (Marginfi's HealthCache.prices: [[u8; 8]; 16], f64 bits).
|
|
1242
|
+
const bytesTag = ts.getJSDocTags(m).find((t) => t.tagName.text === "bytes");
|
|
1243
|
+
const bytesOf = type.kind === "int" ? type : type.kind === "array" && type.elem.kind === "int" ? type.elem : undefined;
|
|
1244
|
+
if (bytesTag && !bytesOf)
|
|
1245
|
+
reporter.fail(bytesTag, "@bytes is for integer fields (or FixedArrays of integers): the IDL then shows the bytes, [u8; N]");
|
|
1246
|
+
const idlBytes = bytesTag && bytesOf ? Number(/\d+/.exec(bytesOf.name)[0]) / 8 : undefined;
|
|
1247
|
+
const nameTag = ts.getJSDocTags(m).find((t) => t.tagName.text === "idlName");
|
|
1248
|
+
const idlName = typeof nameTag?.comment === "string" ? nameTag.comment.trim() : undefined;
|
|
1249
|
+
if (nameTag && !/^[A-Za-z_][A-Za-z0-9_]*$/.test(idlName ?? ""))
|
|
1250
|
+
reporter.fail(nameTag, "@idlName takes the field's name in the IDL: @idlName _pad0");
|
|
1251
|
+
fields.push({ name, type, node: m, ...(idlBytes ? { idlBytes } : {}), ...(idlName ? { idlName } : {}) });
|
|
1059
1252
|
}
|
|
1060
1253
|
checkSnakeDuplicates(decl.members.map((m) => ({ name: m.name.text, node: m })), "fields", reporter);
|
|
1061
1254
|
const name = decl.name.text;
|
|
@@ -1076,7 +1269,13 @@ function analyzeStructs(items, aliases, dataEnums, reporter, scopeFor, fstrings,
|
|
|
1076
1269
|
const stored = layoutText === "borsh" ? fields : [...fields.filter((f) => !growable(f)), ...fields.filter(growable)];
|
|
1077
1270
|
for (const f of stored)
|
|
1078
1271
|
delete f.node;
|
|
1272
|
+
const idlMeta = idlMetaOf(decl, reporter);
|
|
1273
|
+
if (idlMeta?.repr && layoutText === "borsh")
|
|
1274
|
+
reporter.fail(decl.name, "@repr describes a zero-copy (fixed layout) struct, not a @layout borsh one");
|
|
1275
|
+
const declared = fields.map((f) => f.name);
|
|
1079
1276
|
structs.set(name, {
|
|
1277
|
+
...(idlMeta ? { idlMeta } : {}),
|
|
1278
|
+
...(stored.some((f, i) => f.name !== declared[i]) ? { declared } : {}),
|
|
1080
1279
|
name,
|
|
1081
1280
|
fields: stored,
|
|
1082
1281
|
size: undefined,
|
|
@@ -1130,7 +1329,8 @@ function analyzeStructs(items, aliases, dataEnums, reporter, scopeFor, fstrings,
|
|
|
1130
1329
|
}
|
|
1131
1330
|
if (variants.length < 1 || variants.length > 256)
|
|
1132
1331
|
reporter.fail(decl, "data enums need 1..256 variants");
|
|
1133
|
-
|
|
1332
|
+
const idlMeta = idlMetaOf(decl, reporter);
|
|
1333
|
+
dataEnums.set(decl.name.text, { name: decl.name.text, variants, payload: 0, node: decl, ...(idlMeta ? { idlMeta } : {}) });
|
|
1134
1334
|
});
|
|
1135
1335
|
}
|
|
1136
1336
|
// Sizes, with cycle detection. `string` fields leave the size undefined (event-only struct).
|
|
@@ -1143,7 +1343,7 @@ function analyzeStructs(items, aliases, dataEnums, reporter, scopeFor, fstrings,
|
|
|
1143
1343
|
case "enum":
|
|
1144
1344
|
return scalarSize(t);
|
|
1145
1345
|
case "option":
|
|
1146
|
-
return 1 + (t.inner.kind === "struct" ? sizeOf(t.inner, at) : scalarSize(t.inner));
|
|
1346
|
+
return 1 + (t.inner.kind === "struct" || t.inner.kind === "array" ? sizeOf(t.inner, at) : scalarSize(t.inner));
|
|
1147
1347
|
case "dataEnum": {
|
|
1148
1348
|
const e = dataEnums.get(t.name);
|
|
1149
1349
|
if (!e)
|
|
@@ -1212,6 +1412,43 @@ function analyzeStructs(items, aliases, dataEnums, reporter, scopeFor, fstrings,
|
|
|
1212
1412
|
}
|
|
1213
1413
|
}
|
|
1214
1414
|
};
|
|
1415
|
+
// A `@repr c` struct that is not packed is Rust's repr(C): each field at a multiple of its alignment and the size a
|
|
1416
|
+
// multiple of the struct's. TypeSOL stores fields back to back, so that holds only when C would add no padding.
|
|
1417
|
+
const alignOf = (t, at) => {
|
|
1418
|
+
switch (t.kind) {
|
|
1419
|
+
case "int": {
|
|
1420
|
+
const bytes = Number(/\d+/.exec(t.name)[0]) / 8;
|
|
1421
|
+
if (bytes > 8)
|
|
1422
|
+
reporter.fail(at, `a ${t.name} field in a repr(C) zero-copy struct: its alignment differs between targets; hold it as /** @bytes */ in a struct of its own (Marginfi's WrappedI80F48) or mark the struct packed`);
|
|
1423
|
+
return bytes;
|
|
1424
|
+
}
|
|
1425
|
+
case "array":
|
|
1426
|
+
return alignOf(t.elem, at);
|
|
1427
|
+
case "struct": {
|
|
1428
|
+
const inner = structs.get(t.name);
|
|
1429
|
+
return inner ? Math.max(inner.idlMeta?.repr?.align ?? 1, ...inner.fields.map((f) => (f.idlBytes ? 1 : alignOf(f.type, at)))) : 1;
|
|
1430
|
+
}
|
|
1431
|
+
case "bool":
|
|
1432
|
+
case "pubkey":
|
|
1433
|
+
case "enum":
|
|
1434
|
+
return 1;
|
|
1435
|
+
default:
|
|
1436
|
+
return reporter.fail(at, "a zero-copy struct holds integers, booleans, Pubkeys, unit enums, fixed arrays and other such structs");
|
|
1437
|
+
}
|
|
1438
|
+
};
|
|
1439
|
+
const zeroCopyOffsets = (s, align) => {
|
|
1440
|
+
let offset = 0;
|
|
1441
|
+
let max = align ?? 1;
|
|
1442
|
+
for (const f of s.fields) {
|
|
1443
|
+
const align = f.idlBytes ? 1 : alignOf(f.type, s.node);
|
|
1444
|
+
max = Math.max(max, align);
|
|
1445
|
+
if (offset % align !== 0)
|
|
1446
|
+
reporter.fail(s.node.name, `${s.name}.${f.name} is at byte ${offset}, which C would pad to a multiple of ${align}: add the padding field repr(C) has there, or mark it packed`);
|
|
1447
|
+
offset += sizeOf(f.type, s.node);
|
|
1448
|
+
}
|
|
1449
|
+
if (offset % max !== 0)
|
|
1450
|
+
reporter.fail(s.node.name, `${s.name} is ${offset} bytes, which C would pad to a multiple of ${max}: add the trailing padding field repr(C) has`);
|
|
1451
|
+
};
|
|
1215
1452
|
const visit = (s) => {
|
|
1216
1453
|
if (state.get(s.name) === "done")
|
|
1217
1454
|
return;
|
|
@@ -1247,6 +1484,30 @@ function analyzeStructs(items, aliases, dataEnums, reporter, scopeFor, fstrings,
|
|
|
1247
1484
|
};
|
|
1248
1485
|
for (const s of structs.values())
|
|
1249
1486
|
reporter.guard(() => visit(s));
|
|
1487
|
+
// The repr(C) offsets matter where the bytes are the struct's memory (bytemuck zero-copy accounts and what they
|
|
1488
|
+
// hold), not for a repr(C) struct that only travels as Borsh (an argument).
|
|
1489
|
+
{
|
|
1490
|
+
const inMemory = new Set();
|
|
1491
|
+
const hold = (n) => {
|
|
1492
|
+
if (inMemory.has(n))
|
|
1493
|
+
return;
|
|
1494
|
+
inMemory.add(n);
|
|
1495
|
+
const sd = structs.get(n);
|
|
1496
|
+
for (const f of sd?.fields ?? []) {
|
|
1497
|
+
const t = f.type.kind === "array" ? f.type.elem : f.type.kind === "option" ? f.type.inner : f.type;
|
|
1498
|
+
if (t.kind === "struct")
|
|
1499
|
+
hold(t.name);
|
|
1500
|
+
}
|
|
1501
|
+
};
|
|
1502
|
+
for (const sd of structs.values())
|
|
1503
|
+
if (sd.idlMeta?.serialization?.startsWith("bytemuck"))
|
|
1504
|
+
hold(sd.name);
|
|
1505
|
+
for (const n of inMemory) {
|
|
1506
|
+
const sd = structs.get(n);
|
|
1507
|
+
if (sd.idlMeta?.repr?.kind === "c" && !sd.idlMeta.repr.packed && sd.size !== undefined)
|
|
1508
|
+
reporter.guard(() => zeroCopyOffsets(sd, sd.idlMeta.repr.align));
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1250
1511
|
for (const s of structs.values())
|
|
1251
1512
|
if (s.layout === "borsh")
|
|
1252
1513
|
reporter.guard(() => checkBorshLayout(s, structs, dataEnums, reporter));
|
|
@@ -1364,6 +1625,17 @@ export function heapVecOf(s) {
|
|
|
1364
1625
|
function checkBorshLayout(s, structs, dataEnums, reporter) {
|
|
1365
1626
|
const model = { structs };
|
|
1366
1627
|
const fixed = (t) => borshIdentical(t, model);
|
|
1628
|
+
// A nested struct with Options in it (at any depth) and otherwise fixed-size values: Borsh, field by field.
|
|
1629
|
+
const nestedOptions = (t) => {
|
|
1630
|
+
if (t.kind !== "struct")
|
|
1631
|
+
return false;
|
|
1632
|
+
const n = structs.get(t.name);
|
|
1633
|
+
return (!!n &&
|
|
1634
|
+
n.layout !== "borsh" &&
|
|
1635
|
+
n.fields.every((f) => fixed(f.type) ||
|
|
1636
|
+
nestedOptions(f.type) ||
|
|
1637
|
+
(f.type.kind === "option" && (fixed(f.type.inner) || nestedOptions(f.type.inner)))));
|
|
1638
|
+
};
|
|
1367
1639
|
for (const f of s.fields) {
|
|
1368
1640
|
const t = f.type;
|
|
1369
1641
|
const at = s.node.members.find((m) => m.name && ts.isIdentifier(m.name) && m.name.text === f.name) ?? s.node;
|
|
@@ -1385,11 +1657,13 @@ function checkBorshLayout(s, structs, dataEnums, reporter) {
|
|
|
1385
1657
|
}
|
|
1386
1658
|
if (t.kind === "vec" && (fixed(t.elem) || t.elem.kind === "fstring"))
|
|
1387
1659
|
continue;
|
|
1388
|
-
if (t.kind === "option" && fixed(t.inner))
|
|
1660
|
+
if (t.kind === "option" && (fixed(t.inner) || nestedOptions(t.inner)))
|
|
1661
|
+
continue;
|
|
1662
|
+
if (nestedOptions(t))
|
|
1389
1663
|
continue;
|
|
1390
1664
|
if (t.kind === "struct" && structs.get(t.name)?.layout === "borsh")
|
|
1391
1665
|
reporter.fail(at, `${t.name} is @layout borsh itself: only account structs take a layout; nest a struct without one`);
|
|
1392
|
-
reporter.fail(at, `${typeName(t)} in a @layout borsh struct: its fields may be fixed-size values, FixedString<N>, FixedVec<T, N> (of those or of FixedString) and Option<T>; nested structs and lists
|
|
1666
|
+
reporter.fail(at, `${typeName(t)} in a @layout borsh struct: its fields may be fixed-size values, FixedString<N>, FixedVec<T, N> (of those or of FixedString) and Option<T>; nested structs hold fixed-size values and Options, lists fixed-size values only`);
|
|
1393
1667
|
}
|
|
1394
1668
|
for (const other of structs.values())
|
|
1395
1669
|
for (const f of other.fields) {
|
|
@@ -1429,10 +1703,21 @@ function analyzeHelper(fn, name, display, reporter, scope, structs, skip = new S
|
|
|
1429
1703
|
if (p.name.text.includes("__") || p.name.text.includes("$"))
|
|
1430
1704
|
reporter.error(p.name, `"${p.name.text}": "__" and "$" are reserved in names`);
|
|
1431
1705
|
const type = resolveValueType(p.type, scope, true) ?? resolveArgAggregate(p.type, scope, structs);
|
|
1706
|
+
if (type?.kind === "optionArg")
|
|
1707
|
+
reporter.fail(p.type, "helpers take the struct itself: check the Option argument is not null and pass it (or its fields)");
|
|
1708
|
+
if (type?.kind === "vecArg" && type.optional)
|
|
1709
|
+
reporter.fail(p.type, "helpers take the list itself: check the Option argument is not null and pass it");
|
|
1432
1710
|
if (type)
|
|
1433
1711
|
return { name: p.name.text, type, node: p };
|
|
1434
1712
|
if (typesolName(p.type, scope.imports) === "SignerSeeds")
|
|
1435
1713
|
return { name: p.name.text, type: { kind: "signerSeeds" }, node: p };
|
|
1714
|
+
// One of the remaining accounts (remainingAccounts().get(i)), as the instruction holds it: read with readData or
|
|
1715
|
+
// loadAccount, its key, owner, lamports and data length.
|
|
1716
|
+
if (typesolName(p.type, scope.imports) === "RemainingAccount")
|
|
1717
|
+
return { name: p.name.text, type: { kind: "account" }, node: p };
|
|
1718
|
+
// The remaining accounts as a list (`remainingAccounts()` at the call): `.length` and `.get(i)` in the helper.
|
|
1719
|
+
if (typesolName(p.type, scope.imports) === "RemainingAccounts")
|
|
1720
|
+
return { name: p.name.text, type: { kind: "remaining" }, node: p };
|
|
1436
1721
|
// Accounts the caller already checked: Signer, AccountInfo, Account<T>/Mut<T>, token accounts, programs.
|
|
1437
1722
|
const wrapped = typesolName(p.type, scope.imports);
|
|
1438
1723
|
if (wrapped === "HasOne" ||
|
|
@@ -1605,15 +1890,17 @@ function analyzeInstruction(fn, reporter, scope, structs, groups = new Map()) {
|
|
|
1605
1890
|
if (value) {
|
|
1606
1891
|
args.push({ name: p.name.text, type: value, node: p });
|
|
1607
1892
|
minArgsSize +=
|
|
1608
|
-
value.kind === "
|
|
1609
|
-
?
|
|
1610
|
-
: value.kind === "
|
|
1611
|
-
?
|
|
1612
|
-
: value.kind === "
|
|
1613
|
-
?
|
|
1614
|
-
:
|
|
1615
|
-
?
|
|
1616
|
-
:
|
|
1893
|
+
value.kind === "vecArg" && value.optional
|
|
1894
|
+
? 1
|
|
1895
|
+
: value.kind === "string" || value.kind === "vecArg"
|
|
1896
|
+
? 4
|
|
1897
|
+
: value.kind === "option" || value.kind === "optionArg" || value.kind === "dataEnumArg"
|
|
1898
|
+
? 1
|
|
1899
|
+
: value.kind === "structRef"
|
|
1900
|
+
? structs.get(value.struct).size
|
|
1901
|
+
: isScalar(value)
|
|
1902
|
+
? scalarSize(value)
|
|
1903
|
+
: 0;
|
|
1617
1904
|
return;
|
|
1618
1905
|
}
|
|
1619
1906
|
// An account group: its accounts are this instruction's, in their order, named `<param>__<field>`.
|
|
@@ -1685,7 +1972,7 @@ function analyzeInstruction(fn, reporter, scope, structs, groups = new Map()) {
|
|
|
1685
1972
|
if (c.closeTo)
|
|
1686
1973
|
c.closeTo = sibling(c.closeTo);
|
|
1687
1974
|
if (c.ata)
|
|
1688
|
-
c.ata = { owner: sibling(c.ata.owner), mint: sibling(c.ata.mint) };
|
|
1975
|
+
c.ata = { owner: c.ata.owner.startsWith("@") ? at(c.ata.owner) : sibling(c.ata.owner), mint: sibling(c.ata.mint) };
|
|
1689
1976
|
}
|
|
1690
1977
|
return all;
|
|
1691
1978
|
}
|
|
@@ -1803,7 +2090,10 @@ function analyzeInstruction(fn, reporter, scope, structs, groups = new Map()) {
|
|
|
1803
2090
|
if (c.ata) {
|
|
1804
2091
|
if (a.kind.kind !== "token" && a.kind.kind !== "info")
|
|
1805
2092
|
reporter.fail(a.node, "Ata<…> applies to a TokenAccount (or an AccountInfo about to be created)");
|
|
1806
|
-
|
|
2093
|
+
// The owner may be a Pubkey field of a struct account parameter, "@acct.field" (Anchor's
|
|
2094
|
+
// `associated_token::authority = acct.load()?.field`).
|
|
2095
|
+
const owner = c.ata.owner.startsWith("@") ? c.ata.owner.slice(1).split(".")[0] : c.ata.owner;
|
|
2096
|
+
for (const n of [owner, c.ata.mint])
|
|
1807
2097
|
if (!accounts.some((o) => o.name === n))
|
|
1808
2098
|
reporter.fail(a.node, `Ata<…>: add an account parameter named "${n}"`);
|
|
1809
2099
|
}
|
|
@@ -1827,7 +2117,11 @@ function analyzeInstruction(fn, reporter, scope, structs, groups = new Map()) {
|
|
|
1827
2117
|
...(c.seeds?.bump?.startsWith("@") ? [c.seeds.bump.slice(1).split(".")[0]] : []),
|
|
1828
2118
|
...(c.closeTo ? [c.closeTo] : []),
|
|
1829
2119
|
...(c.ata
|
|
1830
|
-
? [
|
|
2120
|
+
? [
|
|
2121
|
+
c.ata.owner.startsWith("@") ? c.ata.owner.slice(1).split(".")[0] : c.ata.owner,
|
|
2122
|
+
c.ata.mint,
|
|
2123
|
+
...(a.kind.kind === "token" ? [] : accounts.filter((o) => o.kind.kind === "tokenProgram").map((o) => o.name)),
|
|
2124
|
+
]
|
|
1831
2125
|
: []),
|
|
1832
2126
|
];
|
|
1833
2127
|
for (const r of refs) {
|
|
@@ -1864,6 +2158,9 @@ function analyzeInstruction(fn, reporter, scope, structs, groups = new Map()) {
|
|
|
1864
2158
|
a.constraints.owner ||
|
|
1865
2159
|
a.constraints.dup))
|
|
1866
2160
|
reporter.error(a.node, "Migrate<From, To> takes no constraints yet: check the old fields with require(…, …) on acct.from");
|
|
2161
|
+
for (const a of accounts)
|
|
2162
|
+
if (a.kind.kind === "instructionsSysvar" && a.constraints.address && a.constraints.address.base58 !== "Sysvar1nstructions1111111111111111111111111")
|
|
2163
|
+
reporter.error(a.node, "Address<InstructionsSysvar, …> must name the Instructions sysvar (Sysvar1nstructions1111111111111111111111111)");
|
|
1867
2164
|
const name = snakeCase(fn.name.text);
|
|
1868
2165
|
// `/** @discriminator spl-transfer-hook-interface:execute */` (sha256 preimage) or `@discriminator 0x...` (raw bytes)
|
|
1869
2166
|
// lets an instruction implement another program's interface.
|
|
@@ -134,7 +134,8 @@ export class AliasEmitter extends EmitterBase {
|
|
|
134
134
|
}
|
|
135
135
|
helperTakesMutAccounts(node) {
|
|
136
136
|
const h = this.helperOf(node);
|
|
137
|
-
|
|
137
|
+
// A RemainingAccount(s) parameter may be loaded with loadAccountMut inside.
|
|
138
|
+
return !!h?.params.some((p) => (p.account ? isWritable(p.account) : p.type.kind === "account" || p.type.kind === "remaining"));
|
|
138
139
|
}
|
|
139
140
|
/**
|
|
140
141
|
* Whether evaluating `node` may resize a growable Vec (and so move the Vecs after it): a push / pop / clear, a
|
|
@@ -156,7 +157,7 @@ export class AliasEmitter extends EmitterBase {
|
|
|
156
157
|
else if (ts.isIdentifier(callee)) {
|
|
157
158
|
if (this.lookup(callee.text)?.closure)
|
|
158
159
|
found = true;
|
|
159
|
-
else if (["realloc", "close"].includes(this.exportName(callee) ?? ""))
|
|
160
|
+
else if (["realloc", "resizeAccount", "close"].includes(this.exportName(callee) ?? ""))
|
|
160
161
|
found = true;
|
|
161
162
|
else if (this.helperOf(n) && (this.helperTakesMutAccounts(n) || n.arguments.some(isClosure)))
|
|
162
163
|
found = true;
|