1+ import { readFileSync } from 'node:fs' ;
12import { describe , test } from 'node:test' ;
23import { 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