1+ import assert from 'node:assert' ;
2+ import { before , describe , it } from 'node:test' ;
13import * as anchor from '@coral-xyz/anchor' ;
2- import { Keypair , PublicKey } from '@solana/web3.js' ;
4+ import { Keypair , LAMPORTS_PER_SOL , PublicKey , SystemProgram , Transaction , TransactionInstruction } from '@solana/web3.js' ;
35import { BankrunProvider } from 'anchor-bankrun' ;
4- import { assert } from 'chai' ;
5- import { startAnchor } from 'solana-bankrun' ;
6+ import { BanksClient , BanksTransactionResultWithMeta , startAnchor } from 'solana-bankrun' ;
67import type { CounterProgram } from '../target/types/counter_program' ;
78
89const IDL = require ( '../target/idl/counter_program.json' ) ;
10+
911const PROGRAM_ID = new PublicKey ( IDL . address ) ;
1012
13+ async function createAndProcessTransaction (
14+ client : BanksClient ,
15+ payer : Keypair ,
16+ instruction : TransactionInstruction ,
17+ additionalSigners : Keypair [ ] = [ ] ,
18+ ) : Promise < BanksTransactionResultWithMeta > {
19+ const tx = new Transaction ( ) ;
20+ // Get the latest blockhash
21+ const [ latestBlockhash ] = await client . getLatestBlockhash ( ) ;
22+ tx . recentBlockhash = latestBlockhash ;
23+ // Add transaction instructions
24+ tx . add ( instruction ) ;
25+ tx . feePayer = payer . publicKey ;
26+ //Add signers
27+ tx . sign ( payer , ...additionalSigners ) ;
28+ // Process transaction
29+ const result = await client . tryProcessTransaction ( tx ) ;
30+ return result ;
31+ }
32+
1133describe ( 'counter_program' , async ( ) => {
1234 // Configure the client to use the anchor-bankrun
1335 const context = await startAnchor ( '' , [ { name : 'counter_program' , programId : PROGRAM_ID } ] , [ ] ) ;
@@ -18,65 +40,108 @@ describe('counter_program', async () => {
1840
1941 const program = new anchor . Program < CounterProgram > ( IDL , provider ) ;
2042
21- const [ counterState , _ ] = anchor . web3 . PublicKey . findProgramAddressSync ( [ anchor . utils . bytes . utf8 . encode ( 'count' ) ] , program . programId ) ;
43+ const counterKeypair = Keypair . generate ( ) ; // Generate a new user keypair
44+
45+ before ( async ( ) => {
46+ //Transfer SOL to the user account to cover rent
47+ const transferInstruction = SystemProgram . transfer ( {
48+ fromPubkey : payer . publicKey ,
49+ toPubkey : counterKeypair . publicKey ,
50+ lamports : 2 * LAMPORTS_PER_SOL ,
51+ } ) ;
52+
53+ await createAndProcessTransaction ( context . banksClient , payer . payer , transferInstruction , [ payer . payer ] ) ;
54+ const userBalance = await context . banksClient . getBalance ( counterKeypair . publicKey ) ;
55+ console . log ( `User balance after funding: ${ userBalance } ` ) ;
56+ } ) ;
57+
58+ const [ counter , _ ] = anchor . web3 . PublicKey . findProgramAddressSync ( [ Buffer . from ( 'count' ) , counterKeypair . publicKey . toBuffer ( ) ] , program . programId ) ;
2259
2360 it ( 'Initialize Counter' , async ( ) => {
2461 await program . methods
2562 . initializeCounter ( )
2663 . accounts ( {
27- payer : payer . publicKey ,
64+ payer : counterKeypair . publicKey ,
2865 } )
66+ . signers ( [ counterKeypair ] )
2967 . rpc ( ) ;
3068
31- const currentCount = await program . account . counter . fetch ( counterState ) ;
69+ const currentCount = await program . account . counter . fetch ( counter ) ;
3270
3371 assert ( currentCount . count . toNumber ( ) === 0 , 'Expected initialized count to be 0' ) ;
3472 } ) ;
3573
3674 it ( 'Increment Counter' , async ( ) => {
37- await program . methods . increment ( ) . accounts ( { } ) . rpc ( ) ;
75+ await program . methods
76+ . increment ( )
77+ . accounts ( {
78+ counter : counter ,
79+ } )
80+ . rpc ( ) ;
3881
39- const currentCount = await program . account . counter . fetch ( counterState ) ;
82+ const currentCount = await program . account . counter . fetch ( counter ) ;
4083
4184 assert ( currentCount . count . toNumber ( ) === 1 , 'Expected count to be 1' ) ;
4285 } ) ;
4386
4487 it ( 'Increment Counter Again' , async ( ) => {
45- await program . methods . increment ( ) . accounts ( { counter : counterState } ) . rpc ( ) ;
88+ await program . methods
89+ . increment ( )
90+ . accounts ( {
91+ counter : counter ,
92+ } )
93+ . rpc ( ) ;
4694
47- const currentCount = await program . account . counter . fetch ( counterState ) ;
95+ const currentCount = await program . account . counter . fetch ( counter ) ;
4896
4997 assert ( currentCount . count . toNumber ( ) === 2 , 'Expected count to be 2' ) ;
5098 } ) ;
99+
51100 it ( 'Decrement counter' , async ( ) => {
52- await program . methods . decrement ( ) . accounts ( { } ) . rpc ( ) ;
101+ await program . methods
102+ . decrement ( )
103+ . accounts ( {
104+ counter : counter ,
105+ } )
106+ . rpc ( ) ;
53107
54- const currentCount = await program . account . counter . fetch ( counterState ) ;
108+ const currentCount = await program . account . counter . fetch ( counter ) ;
55109 assert ( currentCount . count . toNumber ( ) === 1 , 'Expected count to be 1' ) ;
56110 } ) ;
111+
57112 it ( 'Increment and decrement multiple times' , async ( ) => {
58113 // Increment the counter 5 times
59114 for ( let i = 0 ; i < 5 ; i ++ ) {
60- await program . methods . increment ( ) . accounts ( { } ) . rpc ( ) ;
115+ await program . methods
116+ . increment ( )
117+ . accounts ( {
118+ counter : counter ,
119+ } )
120+ . rpc ( ) ;
61121 }
62122
63- let currentCount = await program . account . counter . fetch ( counterState ) ;
123+ let currentCount = await program . account . counter . fetch ( counter ) ;
64124 assert . strictEqual ( currentCount . count . toNumber ( ) , 6 , 'Expected count to be 6 after 5 increments' ) ;
65125
66126 // Decrement the counter 4 times
67127 for ( let i = 0 ; i < 4 ; i ++ ) {
68- await program . methods . decrement ( ) . accounts ( { } ) . rpc ( ) ;
128+ await program . methods
129+ . decrement ( )
130+ . accounts ( {
131+ counter : counter ,
132+ } )
133+ . rpc ( ) ;
69134 }
70135
71- currentCount = await program . account . counter . fetch ( counterState ) ;
136+ currentCount = await program . account . counter . fetch ( counter ) ;
72137 assert . strictEqual ( currentCount . count . toNumber ( ) , 2 , 'Expected count to be 2 after 4 decrements' ) ;
73138 } ) ;
74139
75140 it ( 'Cannot decrement below 0' , async ( ) => {
76141 // Decrement the counter to 0
77- await program . methods . decrement ( ) . accounts ( { } ) . rpc ( ) ;
78- await program . methods . decrement ( ) . accounts ( { } ) . rpc ( ) ;
79- const currentCount = await program . account . counter . fetch ( counterState ) ;
142+ await program . methods . decrement ( ) . accounts ( { counter : counter } ) . rpc ( ) ;
143+ await program . methods . decrement ( ) . accounts ( { counter : counter } ) . rpc ( ) ;
144+ const currentCount = await program . account . counter . fetch ( counter ) ;
80145 assert . strictEqual ( currentCount . count . toNumber ( ) , 0 , 'Expected count to be 0 after multiple decrements' ) ;
81146 } ) ;
82147} ) ;
0 commit comments