@typesol/cli 0.46.0 → 0.47.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 +23 -0
- package/README.md +2 -3
- package/dist/idl-coder.d.ts +110 -0
- package/dist/idl-coder.js +529 -0
- package/dist/testing.d.ts +44 -7
- package/dist/testing.js +30 -19
- package/dist/workspace.js +0 -1
- package/docker/deps/0.47.0.lock.json +1206 -0
- package/docs/test-your-program.md +4 -2
- package/llms-full.txt +6 -5
- package/package.json +1 -5
- package/releases/0.46.0.json +817 -0
package/dist/testing.d.ts
CHANGED
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
* integers, address strings for Pubkeys, null for an empty Option. Decoded accounts and return data come back the
|
|
15
15
|
* same way (bigints, address strings).
|
|
16
16
|
*
|
|
17
|
-
* Needs `litesvm
|
|
17
|
+
* Needs `litesvm`, which `typesol init` projects have. Instruction data, accounts and return data are encoded and
|
|
18
|
+
* decoded from the IDL by TypeSOL's own Borsh coder (src/idl-coder.ts).
|
|
18
19
|
*/
|
|
19
|
-
import anchor, { type Idl } from "@anchor-lang/core";
|
|
20
20
|
import { type Address, type Instruction, type KeyPairSigner } from "@solana/kit";
|
|
21
21
|
import { FailedTransactionMetadata, LiteSVM, TransactionMetadata } from "litesvm";
|
|
22
22
|
import { loadClonedAccounts } from "./tools/clone.js";
|
|
@@ -25,8 +25,44 @@ export { type SvmConnection, svmConnection };
|
|
|
25
25
|
export * from "./testing-stake.js";
|
|
26
26
|
/** Load what `typesol clone` wrote (a directory of `<address>.json` accounts and `<address>.so` programs) into a LiteSVM. */
|
|
27
27
|
export { loadClonedAccounts };
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
/** An Anchor IDL (spec 0.1.0), as `typesol build` writes it and Anchor's `Idl` type describes it. */
|
|
29
|
+
export interface TestIdl {
|
|
30
|
+
address: string;
|
|
31
|
+
metadata?: unknown;
|
|
32
|
+
instructions: readonly {
|
|
33
|
+
name: string;
|
|
34
|
+
discriminator: readonly number[];
|
|
35
|
+
accounts: readonly unknown[];
|
|
36
|
+
args: readonly {
|
|
37
|
+
name: string;
|
|
38
|
+
type: unknown;
|
|
39
|
+
}[];
|
|
40
|
+
returns?: unknown;
|
|
41
|
+
}[];
|
|
42
|
+
accounts?: readonly {
|
|
43
|
+
name: string;
|
|
44
|
+
discriminator: readonly number[];
|
|
45
|
+
}[];
|
|
46
|
+
events?: readonly unknown[];
|
|
47
|
+
errors?: readonly {
|
|
48
|
+
code: number;
|
|
49
|
+
name: string;
|
|
50
|
+
msg?: string;
|
|
51
|
+
}[];
|
|
52
|
+
types?: readonly unknown[];
|
|
53
|
+
constants?: readonly unknown[];
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Anchor's `BorshCoder` for the program's IDL (`instruction`, `accounts`, `events`, `types`), which works with BN and
|
|
57
|
+
* PublicKey values. `@anchor-lang/core` is loaded the first time `t.coder` is read: install it to use `t.coder`.
|
|
58
|
+
* The other helpers do not need it.
|
|
59
|
+
*/
|
|
60
|
+
export interface AnchorBorshCoder {
|
|
61
|
+
instruction: any;
|
|
62
|
+
accounts: any;
|
|
63
|
+
events: any;
|
|
64
|
+
types: any;
|
|
65
|
+
}
|
|
30
66
|
export type Result = TransactionMetadata | FailedTransactionMetadata;
|
|
31
67
|
/** An account for an instruction: an address, a keypair (which then signs), or null to leave an optional one out. */
|
|
32
68
|
export type AccountInput = Address | KeyPairSigner | null;
|
|
@@ -38,8 +74,9 @@ export type SeedInput = string | Address | KeyPairSigner | Uint8Array | bigint;
|
|
|
38
74
|
export interface TestProgram {
|
|
39
75
|
svm: LiteSVM;
|
|
40
76
|
programId: Address;
|
|
41
|
-
idl:
|
|
42
|
-
|
|
77
|
+
idl: TestIdl;
|
|
78
|
+
/** Anchor's BorshCoder for this IDL; needs `@anchor-lang/core` installed (loaded on first use). */
|
|
79
|
+
readonly coder: AnchorBorshCoder;
|
|
43
80
|
/** A new keypair funded with `sol` SOL (default 10). */
|
|
44
81
|
wallet(sol?: number): Promise<KeyPairSigner>;
|
|
45
82
|
/** The program's PDA for these seeds. */
|
|
@@ -100,4 +137,4 @@ export interface TestProgram {
|
|
|
100
137
|
/** Build `program` of the project around `from` (a file URL such as import.meta.url, or a directory) and load it. */
|
|
101
138
|
export declare function testProgram(program: string, from: string | URL): Promise<TestProgram>;
|
|
102
139
|
/** The same helpers for a program already loaded into `svm` at `programId` with this IDL. */
|
|
103
|
-
export declare function programHarness(svm: LiteSVM, programId: Address, idl:
|
|
140
|
+
export declare function programHarness(svm: LiteSVM, programId: Address, idl: TestIdl): TestProgram;
|
package/dist/testing.js
CHANGED
|
@@ -14,14 +14,16 @@
|
|
|
14
14
|
* integers, address strings for Pubkeys, null for an empty Option. Decoded accounts and return data come back the
|
|
15
15
|
* same way (bigints, address strings).
|
|
16
16
|
*
|
|
17
|
-
* Needs `litesvm
|
|
17
|
+
* Needs `litesvm`, which `typesol init` projects have. Instruction data, accounts and return data are encoded and
|
|
18
|
+
* decoded from the IDL by TypeSOL's own Borsh coder (src/idl-coder.ts).
|
|
18
19
|
*/
|
|
19
|
-
import anchor from "@anchor-lang/core";
|
|
20
20
|
import { AccountRole, appendTransactionMessageInstructions, createTransactionMessage, generateKeyPairSigner, getAddressDecoder, getAddressEncoder, getProgramDerivedAddress, isAddress, isOffCurveAddress, lamports, pipe, setTransactionMessageFeePayerSigner, signTransactionMessageWithSigners, } from "@solana/kit";
|
|
21
21
|
import { FailedTransactionMetadata, LiteSVM } from "litesvm";
|
|
22
22
|
import { createHash } from "node:crypto";
|
|
23
|
+
import { createRequire } from "node:module";
|
|
23
24
|
import path from "node:path";
|
|
24
25
|
import { fileURLToPath } from "node:url";
|
|
26
|
+
import { IdlCoder } from "./idl-coder.js";
|
|
25
27
|
import { TYPESOL_ERRORS } from "./tools/clientgen.js";
|
|
26
28
|
import { loadClonedAccounts } from "./tools/clone.js";
|
|
27
29
|
import { svmConnection } from "./tools/svmrpc.js";
|
|
@@ -30,8 +32,21 @@ export { svmConnection };
|
|
|
30
32
|
export * from "./testing-stake.js";
|
|
31
33
|
/** Load what `typesol clone` wrote (a directory of `<address>.json` accounts and `<address>.so` programs) into a LiteSVM. */
|
|
32
34
|
export { loadClonedAccounts };
|
|
33
|
-
|
|
34
|
-
|
|
35
|
+
/** `new BorshCoder(idl)` from `@anchor-lang/core`, found next to typesol or in the project being tested. */
|
|
36
|
+
function anchorCoder(idl) {
|
|
37
|
+
for (const from of [import.meta.url, path.join(process.cwd(), "package.json")]) {
|
|
38
|
+
let anchor;
|
|
39
|
+
try {
|
|
40
|
+
// CommonJS: loaded with require, as Node cannot import its classes by name from an ES module.
|
|
41
|
+
anchor = createRequire(from)("@anchor-lang/core");
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
return new anchor.BorshCoder(idl);
|
|
47
|
+
}
|
|
48
|
+
throw new Error("t.coder is Anchor's BorshCoder: install @anchor-lang/core (npm install -D @anchor-lang/core) to use it");
|
|
49
|
+
}
|
|
35
50
|
/**
|
|
36
51
|
* An instruction's accounts with groups flattened as the generated client does: `pool.vault` becomes `pool_vault`
|
|
37
52
|
* (`poolVault` in `t.ix`), and a seed naming a field of the same group names its flattened account.
|
|
@@ -51,7 +66,6 @@ function flattenAccounts(items, prefix = "") {
|
|
|
51
66
|
}
|
|
52
67
|
return out;
|
|
53
68
|
}
|
|
54
|
-
const BIG = new Set(["u64", "i64", "u128", "i128"]);
|
|
55
69
|
const snake = (s) => s.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
56
70
|
const camel = (s) => s.replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
57
71
|
const isSigner = (v) => typeof v === "object" && v !== null && "address" in v && "signTransactions" in v;
|
|
@@ -76,23 +90,22 @@ export async function testProgram(program, from) {
|
|
|
76
90
|
}
|
|
77
91
|
/** The same helpers for a program already loaded into `svm` at `programId` with this IDL. */
|
|
78
92
|
export function programHarness(svm, programId, idl) {
|
|
79
|
-
const coder = new
|
|
93
|
+
const coder = new IdlCoder(idl);
|
|
94
|
+
let anchor;
|
|
80
95
|
const source = idl;
|
|
81
96
|
const pidl = { ...source, instructions: source.instructions.map((ix) => ({ ...ix, accounts: flattenAccounts(ix.accounts) })) };
|
|
82
97
|
const types = new Map((pidl.types ?? []).map((t) => [t.name, t]));
|
|
83
98
|
const enc = getAddressEncoder();
|
|
84
|
-
/** A plain value → what the
|
|
99
|
+
/** A plain value → what the coder expects for `type` (integers of 64 bits and more it parses itself). */
|
|
85
100
|
const toCoder = (v, type, where) => {
|
|
86
101
|
if (typeof type === "string") {
|
|
87
|
-
if (BIG.has(type))
|
|
88
|
-
return new BN(String(v));
|
|
89
102
|
if (/^[ui](8|16|32)$/.test(type))
|
|
90
103
|
return Number(v);
|
|
91
104
|
if (type === "pubkey") {
|
|
92
105
|
const a = isSigner(v) ? v.address : v;
|
|
93
106
|
if (typeof a !== "string" || !isAddress(a))
|
|
94
107
|
throw new Error(`${where}: expected an address`);
|
|
95
|
-
return
|
|
108
|
+
return a;
|
|
96
109
|
}
|
|
97
110
|
return v;
|
|
98
111
|
}
|
|
@@ -136,14 +149,10 @@ export function programHarness(svm, programId, idl) {
|
|
|
136
149
|
}
|
|
137
150
|
return v;
|
|
138
151
|
};
|
|
139
|
-
/** What the coder decodes
|
|
152
|
+
/** What the coder decodes (bigints, address strings) with TypeSOL's fixed-layout Options and strings unwrapped. */
|
|
140
153
|
const plain = (v) => {
|
|
141
154
|
if (v === null || v === undefined)
|
|
142
155
|
return v;
|
|
143
|
-
if (BN.isBN(v))
|
|
144
|
-
return BigInt(v.toString());
|
|
145
|
-
if (typeof v === "object" && typeof v.toBase58 === "function")
|
|
146
|
-
return v.toBase58();
|
|
147
156
|
if (Array.isArray(v))
|
|
148
157
|
return v.map(plain);
|
|
149
158
|
if (v instanceof Uint8Array)
|
|
@@ -228,7 +237,9 @@ export function programHarness(svm, programId, idl) {
|
|
|
228
237
|
svm,
|
|
229
238
|
programId,
|
|
230
239
|
idl,
|
|
231
|
-
coder
|
|
240
|
+
get coder() {
|
|
241
|
+
return (anchor ??= anchorCoder(idl));
|
|
242
|
+
},
|
|
232
243
|
async wallet(sol = 10) {
|
|
233
244
|
const k = await generateKeyPairSigner();
|
|
234
245
|
svm.airdrop(k.address, lamports(BigInt(Math.round(sol * 1e9))));
|
|
@@ -318,7 +329,7 @@ export function programHarness(svm, programId, idl) {
|
|
|
318
329
|
});
|
|
319
330
|
const a = args;
|
|
320
331
|
const encoded = Object.fromEntries(def.args.map((x) => [x.name, toCoder(a[x.name] ?? a[camel(x.name)], x.type, `${def.name}(${camel(x.name)})`)]));
|
|
321
|
-
return { programAddress: programId, accounts: metas, data: new Uint8Array(coder.
|
|
332
|
+
return { programAddress: programId, accounts: metas, data: new Uint8Array(coder.encodeInstruction(def.name, encoded)) };
|
|
322
333
|
},
|
|
323
334
|
async send(payer, ...ixs) {
|
|
324
335
|
const tx = await pipe(createTransactionMessage({ version: 0 }), (t) => setTransactionMessageFeePayerSigner(payer, t), (t) => svm.setTransactionMessageLifetimeUsingLatestBlockhash(t), (t) => appendTransactionMessageInstructions(ixs, t), (t) => signTransactionMessageWithSigners(t));
|
|
@@ -403,7 +414,7 @@ export function programHarness(svm, programId, idl) {
|
|
|
403
414
|
const a = svm.getAccount(address);
|
|
404
415
|
if (!a.exists)
|
|
405
416
|
return null;
|
|
406
|
-
return plain(coder.
|
|
417
|
+
return plain(coder.decodeAccount(type, Buffer.from(a.data)));
|
|
407
418
|
},
|
|
408
419
|
returned(instruction, res) {
|
|
409
420
|
const def = pidl.instructions.find((i) => i.name === instruction || i.name === snake(instruction));
|
|
@@ -446,7 +457,7 @@ export function programHarness(svm, programId, idl) {
|
|
|
446
457
|
return (bytes <= 4 ? Number(n) : n);
|
|
447
458
|
}
|
|
448
459
|
if ("defined" in t)
|
|
449
|
-
return plain(coder.
|
|
460
|
+
return plain(coder.decodeType(t.defined.name, data));
|
|
450
461
|
throw new Error(`decode ${instruction}'s return data yourself (${JSON.stringify(t)})`);
|
|
451
462
|
},
|
|
452
463
|
balance(address) {
|
package/dist/workspace.js
CHANGED
|
@@ -363,7 +363,6 @@ export function scaffold(dir, name, template = "counter", opts = {}) {
|
|
|
363
363
|
typesol: `npm:@typesol/cli@^${COMPILER_VERSION}`,
|
|
364
364
|
"@types/node": "^22.0.0",
|
|
365
365
|
typescript: "^6.0.3",
|
|
366
|
-
"@anchor-lang/core": "^1.2.1",
|
|
367
366
|
"@solana/kit": "^8.5.0",
|
|
368
367
|
litesvm: "^1.6.1",
|
|
369
368
|
vitest: "^5.0.5",
|