Skip to content

Commit 48500a2

Browse files
authored
Feat: use litesvm in create account native ts test (#511)
1 parent 4e5f04b commit 48500a2

File tree

3 files changed

+158
-86
lines changed

3 files changed

+158
-86
lines changed

basics/create-account/native/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"@types/chai": "^4.3.1",
1515
"@types/mocha": "^9.1.1",
1616
"chai": "^4.3.4",
17+
"litesvm": "^0.3.3",
1718
"mocha": "^9.0.3",
18-
"solana-bankrun": "^0.3.0",
1919
"ts-mocha": "^10.0.0",
2020
"typescript": "^4.3.5"
2121
}

basics/create-account/native/pnpm-lock.yaml

Lines changed: 112 additions & 67 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
1+
import { readFileSync } from 'node:fs';
12
import { describe, test } from 'node:test';
23
import { Keypair, LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js';
3-
import { start } from 'solana-bankrun';
4+
import { LiteSVM } from 'litesvm';
45

5-
describe('Create a system account', async () => {
6-
const PROGRAM_ID = PublicKey.unique();
7-
const context = await start([{ name: 'create_account_program', programId: PROGRAM_ID }], []);
8-
const client = context.banksClient;
9-
const payer = context.payer;
6+
describe('Create a system account', () => {
7+
// Load the program keypair
8+
const programKeypairPath = new URL(
9+
'./fixtures/create_account_program-keypair.json',
10+
// @ts-ignore
11+
import.meta.url,
12+
).pathname;
13+
const programKeypairData = JSON.parse(readFileSync(programKeypairPath, 'utf-8'));
14+
const programKeypair = Keypair.fromSecretKey(new Uint8Array(programKeypairData));
15+
const PROGRAM_ID = programKeypair.publicKey;
1016

11-
test('Create the account via a cross program invocation', async () => {
17+
const litesvm = new LiteSVM();
18+
const payer = Keypair.generate();
19+
20+
// Load the program
21+
const programPath = new URL(
22+
'./fixtures/create_account_program.so',
23+
// @ts-ignore
24+
import.meta.url,
25+
).pathname;
26+
litesvm.addProgramFromFile(PROGRAM_ID, programPath);
27+
28+
// Fund the payer account
29+
litesvm.airdrop(payer.publicKey, BigInt(100 * LAMPORTS_PER_SOL));
30+
31+
test('Create the account via a cross program invocation', () => {
1232
const newKeypair = Keypair.generate();
13-
const blockhash = context.lastBlockhash;
1433

1534
const ix = new TransactionInstruction({
1635
keys: [
@@ -22,16 +41,20 @@ describe('Create a system account', async () => {
2241
data: Buffer.alloc(0),
2342
});
2443

25-
const tx = new Transaction();
26-
tx.recentBlockhash = blockhash;
27-
tx.add(ix).sign(payer, newKeypair);
44+
const tx = new Transaction().add(ix);
45+
tx.feePayer = payer.publicKey;
46+
tx.recentBlockhash = litesvm.latestBlockhash();
47+
tx.sign(payer, newKeypair);
2848

29-
await client.processTransaction(tx);
49+
litesvm.sendTransaction(tx);
50+
51+
// Verify the account was created
52+
const accountInfo = litesvm.getAccount(newKeypair.publicKey);
53+
console.log(`Account with public key ${newKeypair.publicKey} successfully created via CPI`);
3054
});
3155

32-
test('Create the account via direct call to system program', async () => {
56+
test('Create the account via direct call to system program', () => {
3357
const newKeypair = Keypair.generate();
34-
const blockhash = context.lastBlockhash;
3558

3659
const ix = SystemProgram.createAccount({
3760
fromPubkey: payer.publicKey,
@@ -41,11 +64,15 @@ describe('Create a system account', async () => {
4164
programId: SystemProgram.programId,
4265
});
4366

44-
const tx = new Transaction();
45-
tx.recentBlockhash = blockhash;
46-
tx.add(ix).sign(payer, newKeypair);
67+
const tx = new Transaction().add(ix);
68+
tx.feePayer = payer.publicKey;
69+
tx.recentBlockhash = litesvm.latestBlockhash();
70+
tx.sign(payer, newKeypair);
71+
72+
litesvm.sendTransaction(tx);
4773

48-
await client.processTransaction(tx);
74+
// Verify the account was created
75+
const accountInfo = litesvm.getAccount(newKeypair.publicKey);
4976
console.log(`Account with public key ${newKeypair.publicKey} successfully created`);
5077
});
5178
});

0 commit comments

Comments
 (0)