Skip to content

Commit 7853408

Browse files
committed
test: add litesvm ts testcases
1 parent 203b246 commit 7853408

3 files changed

Lines changed: 170 additions & 29 deletions

File tree

basics/cross-program-invocation/native/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
"type": "module",
33
"scripts": {
44
"test": "pnpm ts-mocha -p ./tsconfig.json -t 1000000 ./tests/test.ts",
5-
"build-and-test": "cargo build-sbf --sbf-out-dir=./tests/fixtures && pnpm test",
5+
"build-and-test": "cargo build-sbf --sbf-out-dir=./tests/fixtures --manifest-path=programs/lever/Cargo.toml && cargo build-sbf --sbf-out-dir=./tests/fixtures --manifest-path=programs/hand/Cargo.toml && pnpm test",
66
"build": "cargo build-sbf --sbf-out-dir=./program/target/so",
77
"deploy": "solana program deploy ./program/target/so/program.so"
88
},
99
"dependencies": {
1010
"@solana/web3.js": "^1.98.4",
1111
"borsh": "^2.0.0",
1212
"buffer": "^6.0.3",
13-
"fs": "^0.0.1-security"
13+
"fs": "^0.0.1-security",
14+
"litesvm": "0.8.0"
1415
},
1516
"devDependencies": {
1617
"@types/bn.js": "^5.1.0",

basics/cross-program-invocation/native/pnpm-lock.yaml

Lines changed: 83 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 84 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,127 @@
11
import { Buffer } from "node:buffer";
2+
import { LiteSVM, TransactionMetadata } from "litesvm";
23
import {
3-
Connection,
44
Keypair,
55
SystemProgram,
6-
sendAndConfirmTransaction,
76
Transaction,
87
TransactionInstruction,
98
} from "@solana/web3.js";
109
import * as borsh from "borsh";
10+
import * as path from "path";
11+
import * as fs from "node:fs";
12+
import * as os from "node:os";
13+
import { describe, it, before } from "node:test";
1114

12-
function createKeypairFromFile(path: string): Keypair {
13-
return Keypair.fromSecretKey(Uint8Array.from(JSON.parse(require("node:fs").readFileSync(path, "utf-8"))));
15+
const PowerStatusSchema = { struct: { is_on: "u8" } };
16+
const SetPowerStatusSchema = { struct: { name: "string" } };
17+
18+
function borshSerialize(schema: borsh.Schema, data: object): Buffer {
19+
return Buffer.from(borsh.serialize(schema, data));
1420
}
1521

16-
describe("CPI Example", () => {
17-
const connection = new Connection("http://localhost:8899", "confirmed");
18-
const payer = createKeypairFromFile(`${require("node:os").homedir()}/.config/solana/id.json`);
19-
const hand = createKeypairFromFile("./target/so/hand-keypair.json");
20-
const lever = createKeypairFromFile("./target/so/lever-keypair.json");
22+
describe("Native CPI Example", () => {
23+
let svm: LiteSVM;
24+
let payer: Keypair;
25+
let handProgramId: Keypair;
26+
let leverProgramId: Keypair;
27+
let powerAccount: Keypair;
28+
29+
before(() => {
30+
svm = new LiteSVM();
31+
payer = Keypair.generate();
32+
33+
handProgramId = Keypair.fromSecretKey(
34+
Uint8Array.from(
35+
JSON.parse(fs.readFileSync("./tests/fixtures/cross_program_invocatio_native_hand-keypair.json", "utf-8"))
36+
)
37+
);
38+
leverProgramId = Keypair.fromSecretKey(
39+
Uint8Array.from(
40+
JSON.parse(fs.readFileSync("./tests/fixtures/cross_program_invocatio_native_lever-keypair.json", "utf-8"))
41+
)
42+
);
43+
44+
svm.airdrop(payer.publicKey, BigInt(10 * 1_000_000_000));
2145

22-
const PowerStatusSchema = { struct: { is_on: "u8" } };
23-
const SetPowerStatusSchema = { struct: { name: "string" } };
46+
const native_hand = path.join("./tests/fixtures", "cross_program_invocatio_native_hand.so");
47+
const native_lever = path.join("./tests/fixtures", "cross_program_invocatio_native_lever.so");
2448

25-
function borshSerialize(schema: borsh.Schema, data: object): Buffer {
26-
return Buffer.from(borsh.serialize(schema, data));
27-
}
49+
svm.addProgramFromFile(
50+
handProgramId.publicKey,
51+
native_hand
52+
);
53+
svm.addProgramFromFile(
54+
leverProgramId.publicKey,
55+
native_lever
56+
);
2857

29-
const powerAccount = Keypair.generate();
58+
powerAccount = Keypair.generate();
59+
});
3060

31-
it("Initialize the lever!", async () => {
61+
it("Initialize the lever!", () => {
3262
const ix = new TransactionInstruction({
3363
keys: [
3464
{ pubkey: powerAccount.publicKey, isSigner: true, isWritable: true },
3565
{ pubkey: payer.publicKey, isSigner: true, isWritable: true },
3666
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
3767
],
38-
programId: lever.publicKey,
39-
data: borshSerialize(PowerStatusSchema, { is_on: true }),
68+
programId: leverProgramId.publicKey,
69+
data: borshSerialize(PowerStatusSchema, { is_on: 1 }),
4070
});
4171

42-
await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer, powerAccount]);
72+
const tx = new Transaction();
73+
tx.recentBlockhash = svm.latestBlockhash();
74+
tx.feePayer = payer.publicKey;
75+
tx.add(ix);
76+
tx.sign(payer, powerAccount);
77+
78+
const res = svm.sendTransaction(tx);
79+
if (!(res instanceof TransactionMetadata)) {
80+
throw new Error(`Transaction failed: ${res.meta().toString()}`);
81+
}
4382
});
4483

45-
it("Pull the lever!", async () => {
84+
it("Pull the lever!", () => {
4685
const ix = new TransactionInstruction({
4786
keys: [
4887
{ pubkey: powerAccount.publicKey, isSigner: false, isWritable: true },
49-
{ pubkey: lever.publicKey, isSigner: false, isWritable: false },
88+
{ pubkey: leverProgramId.publicKey, isSigner: false, isWritable: false },
5089
],
51-
programId: hand.publicKey,
90+
programId: handProgramId.publicKey,
5291
data: borshSerialize(SetPowerStatusSchema, { name: "Chris" }),
5392
});
5493

55-
await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer]);
94+
const tx = new Transaction();
95+
tx.recentBlockhash = svm.latestBlockhash();
96+
tx.feePayer = payer.publicKey;
97+
tx.add(ix);
98+
tx.sign(payer);
99+
100+
const res = svm.sendTransaction(tx);
101+
if (!(res instanceof TransactionMetadata)) {
102+
throw new Error(`Transaction failed: ${res.meta().toString()}`);
103+
}
56104
});
57105

58-
it("Pull it again!", async () => {
106+
it("Pull it again!", () => {
59107
const ix = new TransactionInstruction({
60108
keys: [
61109
{ pubkey: powerAccount.publicKey, isSigner: false, isWritable: true },
62-
{ pubkey: lever.publicKey, isSigner: false, isWritable: false },
110+
{ pubkey: leverProgramId.publicKey, isSigner: false, isWritable: false },
63111
],
64-
programId: hand.publicKey,
112+
programId: handProgramId.publicKey,
65113
data: borshSerialize(SetPowerStatusSchema, { name: "Ashley" }),
66114
});
67115

68-
await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer]);
116+
const tx = new Transaction();
117+
tx.recentBlockhash = svm.latestBlockhash();
118+
tx.feePayer = payer.publicKey;
119+
tx.add(ix);
120+
tx.sign(payer);
121+
122+
const res = svm.sendTransaction(tx);
123+
if (!(res instanceof TransactionMetadata)) {
124+
throw new Error(`Transaction failed: ${res.meta().toString()}`);
125+
}
69126
});
70127
});

0 commit comments

Comments
 (0)