@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
|
@@ -2,7 +2,7 @@ import ts from "typescript";
|
|
|
2
2
|
import { type Analysis, type TypeArg, type TypeScope } from "../analyze.js";
|
|
3
3
|
import type { Reporter } from "../diagnostics.js";
|
|
4
4
|
import { type AccountParam, type GenericHelper, type Helper, type IdlSeed, type Instruction, type ProgramModel, type ValueType } from "../model.js";
|
|
5
|
-
import { type Value, type Ref, type Local, type Closure, type EmitOptions } from "./shared.js";
|
|
5
|
+
import { type Value, type Ref, type Local, type Closure, type FunctionChoice, type EmitOptions } from "./shared.js";
|
|
6
6
|
/**
|
|
7
7
|
* State of one function being lowered, and the primitives every layer uses: emitting lines, temporaries,
|
|
8
8
|
* scopes, evaluation-order snapshots and account lookups.
|
|
@@ -49,6 +49,8 @@ export declare abstract class EmitterBase {
|
|
|
49
49
|
protected readonly inlining: Set<ts.Node>;
|
|
50
50
|
/** Enclosing loops, innermost last; each has a `continue` label. */
|
|
51
51
|
protected readonly loops: string[];
|
|
52
|
+
/** loadAccountMut calls whose claim a loop drops at each iteration's end: the C variable that records it. */
|
|
53
|
+
protected readonly claimSlots: Map<ts.Node, string>;
|
|
52
54
|
protected readonly ix: Instruction | undefined;
|
|
53
55
|
protected readonly helper: Helper | undefined;
|
|
54
56
|
/** Type names in effect (a generic helper expanded where it is called binds its type parameters here). */
|
|
@@ -119,6 +121,7 @@ export declare abstract class EmitterBase {
|
|
|
119
121
|
protected abstract ref(node: ts.Expression): Ref | undefined;
|
|
120
122
|
protected abstract call(node: ts.CallExpression, topLevel: boolean): Value | undefined;
|
|
121
123
|
protected abstract isRemaining(node: ts.Expression): boolean;
|
|
124
|
+
protected abstract remainingBase(node: ts.Expression): string;
|
|
122
125
|
protected abstract handle(node: ts.Expression): string | undefined;
|
|
123
126
|
protected abstract instructionsParam(node: ts.Expression): AccountParam | undefined;
|
|
124
127
|
protected abstract isTxIx(node: ts.Expression): boolean;
|
|
@@ -133,6 +136,7 @@ export declare abstract class EmitterBase {
|
|
|
133
136
|
protected abstract createToken(node: ts.CallExpression, target: AccountParam, topLevel: boolean): Value;
|
|
134
137
|
protected abstract systemTransfer(node: ts.CallExpression): undefined;
|
|
135
138
|
protected abstract createAccount(node: ts.CallExpression, system: AccountParam): undefined;
|
|
139
|
+
protected abstract createPda(node: ts.CallExpression): Value;
|
|
136
140
|
protected abstract allocateOrAssign(node: ts.CallExpression, system: AccountParam, method: "allocate" | "assign"): undefined;
|
|
137
141
|
protected abstract tokenOp(node: ts.CallExpression, program: AccountParam, method: string): undefined;
|
|
138
142
|
protected abstract ataOp(node: ts.CallExpression, program: AccountParam, method: string): undefined;
|
|
@@ -149,6 +153,7 @@ export declare abstract class EmitterBase {
|
|
|
149
153
|
}): undefined;
|
|
150
154
|
protected abstract epilogue(): string[];
|
|
151
155
|
protected abstract expandClosure(c: Closure, args: readonly ts.Expression[], node: ts.Node): Value | undefined;
|
|
156
|
+
protected abstract callChoice(choice: FunctionChoice, args: readonly ts.Expression[], node: ts.Node): Value | undefined;
|
|
152
157
|
protected abstract listMethod(node: ts.CallExpression, method: string): Value | undefined;
|
|
153
158
|
protected abstract isPipeline(e: ts.Expression): e is ts.CallExpression;
|
|
154
159
|
protected abstract closureOf(arg: ts.Expression): Closure;
|
|
@@ -162,5 +167,6 @@ export declare abstract class EmitterBase {
|
|
|
162
167
|
ci: string;
|
|
163
168
|
next?: string;
|
|
164
169
|
alwaysNext?: boolean;
|
|
170
|
+
atNext?: () => void;
|
|
165
171
|
}, body: (element: ts.Expression, index: ts.Expression) => void): void;
|
|
166
172
|
}
|
|
@@ -34,6 +34,8 @@ export class EmitterBase {
|
|
|
34
34
|
inlining = new Set();
|
|
35
35
|
/** Enclosing loops, innermost last; each has a `continue` label. */
|
|
36
36
|
loops = [];
|
|
37
|
+
/** loadAccountMut calls whose claim a loop drops at each iteration's end: the C variable that records it. */
|
|
38
|
+
claimSlots = new Map();
|
|
37
39
|
ix;
|
|
38
40
|
helper;
|
|
39
41
|
/** Type names in effect (a generic helper expanded where it is called binds its type parameters here). */
|
|
@@ -49,18 +49,27 @@ export function borshMinSize(s, model) {
|
|
|
49
49
|
n += 4;
|
|
50
50
|
else if (t.kind === "option" || t.kind === "dataEnum")
|
|
51
51
|
n += 1;
|
|
52
|
+
else if (t.kind === "struct" && !borshIdentical(t, model))
|
|
53
|
+
n += borshMinSize(model.structs.get(t.name), model);
|
|
52
54
|
else
|
|
53
55
|
n += fixedBytes(t, model);
|
|
54
56
|
}
|
|
55
57
|
return n;
|
|
56
58
|
}
|
|
59
|
+
/** A struct (not itself @layout borsh) whose Borsh form is variable: it holds an Option, at any depth. */
|
|
60
|
+
function variableStruct(t, model) {
|
|
61
|
+
if (t.kind !== "struct" || borshIdentical(t, model))
|
|
62
|
+
return undefined;
|
|
63
|
+
const s = model.structs.get(t.name);
|
|
64
|
+
return s && s.layout !== "borsh" ? s : undefined;
|
|
65
|
+
}
|
|
57
66
|
const DESER = `TS_FAIL(TS_ERR_ACCOUNT_TOO_SMALL, "TypeSOL: bytes are not a valid Borsh value")`;
|
|
58
67
|
const SER = `TS_FAIL(TS_ERR_DID_NOT_SERIALIZE, "TypeSOL: account data too small for the value")`;
|
|
59
68
|
/** C for `ts_unpack_<S>` and `ts_pack_<S>` of every `@layout borsh` struct. */
|
|
60
69
|
export function emitBorshCodecs(model) {
|
|
61
70
|
const out = [];
|
|
62
71
|
for (const s of model.structs.values()) {
|
|
63
|
-
if (s.layout !== "borsh" || s.size === undefined)
|
|
72
|
+
if ((s.layout !== "borsh" && s.wire !== "borsh") || s.size === undefined)
|
|
64
73
|
continue;
|
|
65
74
|
let k = 0;
|
|
66
75
|
const tmp = (p) => `${p}${k++}`;
|
|
@@ -84,7 +93,13 @@ export function emitBorshCodecs(model) {
|
|
|
84
93
|
};
|
|
85
94
|
// Bounds: the caller checked `len >= MIN` (the smallest Borsh form). Fixed-size pieces then always fit; only a
|
|
86
95
|
// String / Vec / Option payload is checked, against what is left after the fields that follow it still need.
|
|
87
|
-
const minOf = (t) => t.kind === "fstring" || t.kind === "vec" || t.kind === "string"
|
|
96
|
+
const minOf = (t) => t.kind === "fstring" || t.kind === "vec" || t.kind === "string"
|
|
97
|
+
? 4
|
|
98
|
+
: t.kind === "option" || t.kind === "dataEnum"
|
|
99
|
+
? 1
|
|
100
|
+
: variableStruct(t, model)
|
|
101
|
+
? borshMinSize(variableStruct(t, model), model)
|
|
102
|
+
: fixedBytes(t, model);
|
|
88
103
|
const minAfter = s.fields.map((_, i) => s.fields.slice(i + 1).reduce((n, f) => n + minOf(f.type), 0));
|
|
89
104
|
const need = (n, rest, pad) => `${pad}if (${n} > len - at - ${rest}) ${DESER};`;
|
|
90
105
|
const room = (n, rest, pad) => `${pad}if (${n} > cap - at - ${rest}) ${SER};`;
|
|
@@ -137,8 +152,35 @@ export function emitBorshCodecs(model) {
|
|
|
137
152
|
`${pad}}`,
|
|
138
153
|
];
|
|
139
154
|
}
|
|
155
|
+
case "struct": {
|
|
156
|
+
// A struct holding Options, nested: its fields one by one, each checked against what is left.
|
|
157
|
+
const sub = variableStruct(t, model);
|
|
158
|
+
const after = (i) => sub.fields.slice(i + 1).reduce((n, f) => n + minOf(f.type), rest);
|
|
159
|
+
return sub.fields.flatMap((f, i) => {
|
|
160
|
+
if (borshIdentical(f.type, model)) {
|
|
161
|
+
const n = fixedBytes(f.type, model);
|
|
162
|
+
return [
|
|
163
|
+
need(String(n), after(i), pad),
|
|
164
|
+
`${pad}ts_memcpy(&${lv}.${f.name}, src + at, ${n});`,
|
|
165
|
+
`${pad}at += ${n};`,
|
|
166
|
+
...validate(f.type, `${lv}.${f.name}`, pad),
|
|
167
|
+
];
|
|
168
|
+
}
|
|
169
|
+
return unpackVar(f.type, `${lv}.${f.name}`, after(i), pad);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
140
172
|
case "option": {
|
|
141
173
|
const tag = tmp("tag");
|
|
174
|
+
if (variableStruct(t.inner, model))
|
|
175
|
+
return [
|
|
176
|
+
need("1", rest, pad),
|
|
177
|
+
`${pad}uint8_t ${tag} = src[at++];`,
|
|
178
|
+
`${pad}if (${tag} > 1) ${DESER};`,
|
|
179
|
+
`${pad}${lv}.some = ${tag};`,
|
|
180
|
+
`${pad}if (${tag}) {`,
|
|
181
|
+
...unpackVar(t.inner, `${lv}.v`, rest, `${pad} `),
|
|
182
|
+
`${pad}}`,
|
|
183
|
+
];
|
|
142
184
|
const n = fixedBytes(t.inner, model);
|
|
143
185
|
return [
|
|
144
186
|
`${pad}uint8_t ${tag} = src[at++];`,
|
|
@@ -228,7 +270,26 @@ export function emitBorshCodecs(model) {
|
|
|
228
270
|
`${pad}}`,
|
|
229
271
|
];
|
|
230
272
|
}
|
|
273
|
+
case "struct": {
|
|
274
|
+
const sub = variableStruct(t, model);
|
|
275
|
+
const after = (i) => sub.fields.slice(i + 1).reduce((n, f) => n + minOf(f.type), rest);
|
|
276
|
+
return sub.fields.flatMap((f, i) => {
|
|
277
|
+
if (borshIdentical(f.type, model)) {
|
|
278
|
+
const n = fixedBytes(f.type, model);
|
|
279
|
+
return [room(String(n), after(i), pad), `${pad}ts_memcpy(dst + at, &${lv}.${f.name}, ${n});`, `${pad}at += ${n};`];
|
|
280
|
+
}
|
|
281
|
+
return packVar(f.type, `${lv}.${f.name}`, after(i), pad);
|
|
282
|
+
});
|
|
283
|
+
}
|
|
231
284
|
case "option": {
|
|
285
|
+
if (variableStruct(t.inner, model))
|
|
286
|
+
return [
|
|
287
|
+
room("1", rest, pad),
|
|
288
|
+
`${pad}dst[at++] = ${lv}.some ? 1 : 0;`,
|
|
289
|
+
`${pad}if (${lv}.some) {`,
|
|
290
|
+
...packVar(t.inner, `${lv}.v`, rest, `${pad} `),
|
|
291
|
+
`${pad}}`,
|
|
292
|
+
];
|
|
232
293
|
const n = fixedBytes(t.inner, model);
|
|
233
294
|
return [
|
|
234
295
|
room(`(uint64_t)(${lv}.some ? ${1 + n} : 1)`, rest, pad),
|
|
@@ -19,6 +19,8 @@ export declare abstract class CallEmitter extends ExpressionEmitter {
|
|
|
19
19
|
name: string;
|
|
20
20
|
count: number;
|
|
21
21
|
};
|
|
22
|
+
/** The index of the first remaining account among the instruction's accounts, as C: a constant, or a helper's parameter. */
|
|
23
|
+
protected remainingBase(node: ts.Expression): string;
|
|
22
24
|
/** `remainingAccounts()` or a const bound to it. */
|
|
23
25
|
protected isRemaining(node: ts.Expression): boolean;
|
|
24
26
|
/** C pointer (`TsAccount *`) for an expression naming a remaining account, or undefined. */
|
|
@@ -36,8 +38,15 @@ export declare abstract class CallEmitter extends ExpressionEmitter {
|
|
|
36
38
|
protected anyAccount(node: ts.Expression, what: string): string;
|
|
37
39
|
/** `readData<T>(account, offset)`: a bounds-checked little-endian read of any account's data. */
|
|
38
40
|
protected readData(node: ts.CallExpression): Value;
|
|
41
|
+
/** `writeData<T>(account, offset, value)`: a little-endian store into a writable AccountInfo's data. */
|
|
42
|
+
protected writeData(node: ts.CallExpression): undefined;
|
|
39
43
|
/** `loadForeign<T>(account, owner)`: read-only view of another program's Anchor account. */
|
|
40
44
|
protected loadForeign(node: ts.CallExpression): Value;
|
|
45
|
+
/**
|
|
46
|
+
* `loadRaw<T>(acct)` / `loadRawMut<T>(acct)`: a native program's account (no discriminator) as a fixed-layout struct
|
|
47
|
+
* from byte 0, checked as a `#[repr(C)]` type's load_checked does: this program's (3007), large enough (3003).
|
|
48
|
+
*/
|
|
49
|
+
protected loadRaw(node: ts.CallExpression, mut: boolean): Value;
|
|
41
50
|
/** `loadAccount<T>(acct)` / `loadAccountMut<T>(acct)`: checked view of a program account. */
|
|
42
51
|
protected loadAccount(node: ts.CallExpression, mut: boolean): Value;
|
|
43
52
|
/**
|
|
@@ -72,12 +81,16 @@ export declare abstract class CallEmitter extends ExpressionEmitter {
|
|
|
72
81
|
* A struct/array/data-enum helper argument. Helpers that take accounts may change account data, so they get a
|
|
73
82
|
* copy taken at the call (JS passes the object as it was); other helpers cannot write, so a pointer is enough.
|
|
74
83
|
*/
|
|
75
|
-
protected snapshotPlace(lv: string, type: FieldType, h: Helper): string;
|
|
84
|
+
protected snapshotPlace(lv: string, type: FieldType, h: Helper, node?: ts.Expression): string;
|
|
85
|
+
/** Whether `node` is (part of) a struct value this function owns: `h`, `h.inner`, `h.list.get(i)`. */
|
|
86
|
+
protected ownedRoot(node: ts.Expression): boolean;
|
|
76
87
|
/** `signer([...seeds])` (one PDA signature), or a SignerSeeds parameter passed on. */
|
|
77
88
|
protected signerSeedsArg(n: ts.Expression): {
|
|
78
89
|
name: string;
|
|
79
90
|
count: number | string;
|
|
80
91
|
};
|
|
92
|
+
/** Whether `require`'s error is named in the source (an error enum member, AnchorError.X or a message). */
|
|
93
|
+
protected namesError(node: ts.Expression): boolean;
|
|
81
94
|
protected errorRef(node: ts.Expression): ErrorDef;
|
|
82
95
|
/** The struct of `returnData<Quote>(…)`, when T is one; refused when its bytes are not its Borsh form. */
|
|
83
96
|
protected returnedStruct(node: ts.CallExpression): StructDef | undefined;
|