Skip to content

Commit 4c08c57

Browse files
committed
feat: add litesvm test for transfer-sol anchor program
* Updated package.json to include litesvm dependency. * Modified tsconfig.json to use 'nodenext' module. * Added litesvm test cases for transferring SOL using both CPI and program calls.
1 parent 020ebe7 commit 4c08c57

File tree

4 files changed

+192
-9
lines changed

4 files changed

+192
-9
lines changed

basics/transfer-sol/anchor/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"type": "module",
33
"dependencies": {
44
"@coral-xyz/anchor": "0.32.1",
5-
"@solana/web3.js": "^1.95.2"
5+
"@solana/web3.js": "^1.95.2",
6+
"litesvm": "^0.4.0"
67
},
78
"devDependencies": {
89
"@types/bn.js": "^5.1.0",

basics/transfer-sol/anchor/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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import anchor from "@coral-xyz/anchor";
2+
import {
3+
Keypair,
4+
LAMPORTS_PER_SOL,
5+
PublicKey,
6+
SystemProgram,
7+
Transaction,
8+
TransactionInstruction,
9+
} from "@solana/web3.js";
10+
import { assert } from "chai";
11+
import { LiteSVM } from "litesvm";
12+
import Idl from "../target/idl/transfer_sol.json" with { type: "json" };
13+
14+
describe("LiteSVM: Transfer SOL", () => {
15+
const svm = new LiteSVM();
16+
const programId = new PublicKey(Idl.address);
17+
const coder = new anchor.BorshCoder(Idl as anchor.Idl);
18+
const payer = Keypair.generate();
19+
svm.airdrop(payer.publicKey, BigInt(5 * LAMPORTS_PER_SOL));
20+
21+
const programFilePath = new URL(
22+
"../target/deploy/transfer_sol.so",
23+
import.meta.url,
24+
).pathname;
25+
svm.addProgramFromFile(programId, programFilePath);
26+
27+
it("Transfer SOL with CPI", () => {
28+
const recipient = Keypair.generate();
29+
30+
const ixArgs = {
31+
amount: new anchor.BN(LAMPORTS_PER_SOL),
32+
};
33+
const data = coder.instruction.encode("transfer_sol_with_cpi", ixArgs);
34+
const ix = new TransactionInstruction({
35+
keys: [
36+
{ pubkey: payer.publicKey, isSigner: true, isWritable: true },
37+
{ pubkey: recipient.publicKey, isSigner: false, isWritable: true },
38+
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
39+
],
40+
programId,
41+
data,
42+
});
43+
44+
const tx = new Transaction().add(ix);
45+
tx.feePayer = payer.publicKey;
46+
tx.recentBlockhash = svm.latestBlockhash();
47+
tx.sign(payer);
48+
svm.sendTransaction(tx);
49+
50+
const recipientAcc = svm.getAccount(recipient.publicKey);
51+
assert.equal(recipientAcc.lamports, LAMPORTS_PER_SOL);
52+
});
53+
54+
it("Transfer SOL with Program", () => {
55+
const payerAccount = Keypair.generate();
56+
const ixPayer = SystemProgram.createAccount({
57+
fromPubkey: payer.publicKey,
58+
newAccountPubkey: payerAccount.publicKey,
59+
lamports: LAMPORTS_PER_SOL,
60+
space: 0,
61+
programId,
62+
});
63+
const txPayer = new Transaction().add(ixPayer);
64+
txPayer.feePayer = payer.publicKey;
65+
txPayer.recentBlockhash = svm.latestBlockhash();
66+
txPayer.sign(payer, payerAccount);
67+
svm.sendTransaction(txPayer);
68+
svm.expireBlockhash();
69+
70+
const recipientAccount = Keypair.generate();
71+
72+
const ixArgs = {
73+
amount: new anchor.BN(LAMPORTS_PER_SOL),
74+
};
75+
const data = coder.instruction.encode("transfer_sol_with_program", ixArgs);
76+
const ix = new TransactionInstruction({
77+
keys: [
78+
{ pubkey: payerAccount.publicKey, isSigner: true, isWritable: true },
79+
{
80+
pubkey: recipientAccount.publicKey,
81+
isSigner: false,
82+
isWritable: true,
83+
},
84+
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
85+
],
86+
programId,
87+
data,
88+
});
89+
90+
const tx = new Transaction().add(ix);
91+
tx.feePayer = payer.publicKey;
92+
tx.recentBlockhash = svm.latestBlockhash();
93+
tx.sign(payer, payerAccount);
94+
svm.sendTransaction(tx);
95+
96+
const recipientAcc = svm.getAccount(recipientAccount.publicKey);
97+
assert.equal(recipientAcc.lamports, LAMPORTS_PER_SOL);
98+
});
99+
});
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"compilerOptions": {
3-
"types": ["mocha", "chai"],
4-
"typeRoots": ["./node_modules/@types"],
5-
"lib": ["es2015"],
6-
"module": "commonjs",
7-
"target": "es6",
8-
"esModuleInterop": true
9-
}
2+
"compilerOptions": {
3+
"types": ["mocha", "chai"],
4+
"typeRoots": ["./node_modules/@types"],
5+
"lib": ["es2015"],
6+
"module": "nodenext",
7+
"target": "es6",
8+
"esModuleInterop": true
9+
}
1010
}

0 commit comments

Comments
 (0)