1+ import assert from "node:assert" ;
12import { describe , test } from "node:test" ;
23import { Keypair , LAMPORTS_PER_SOL , PublicKey , Transaction } from "@solana/web3.js" ;
34import { start } from "solana-bankrun" ;
@@ -13,7 +14,7 @@ describe("transfer-sol (asm)", async () => {
1314 const recipient = Keypair . generate ( ) ;
1415
1516 test ( "Transfer SOL via CPI to the system program" , async ( ) => {
16- await getBalances ( payer . publicKey , recipient . publicKey , "Beginning" ) ;
17+ const [ payerBefore , recipientBefore ] = await getBalances ( payer . publicKey , recipient . publicKey , "Beginning" ) ;
1718
1819 const ix = createTransferInstruction ( payer . publicKey , recipient . publicKey , PROGRAM_ID , transferAmount ) ;
1920
@@ -24,15 +25,31 @@ describe("transfer-sol (asm)", async () => {
2425
2526 await client . processTransaction ( tx ) ;
2627
27- await getBalances ( payer . publicKey , recipient . publicKey , "Resulting" ) ;
28+ const [ payerAfter , recipientAfter ] = await getBalances ( payer . publicKey , recipient . publicKey , "Resulting" ) ;
29+
30+ assert (
31+ payerAfter < payerBefore - BigInt ( transferAmount ) ,
32+ "Payer balance should decrease by at least the transfer amount" ,
33+ ) ;
34+ assert . strictEqual (
35+ recipientAfter ,
36+ recipientBefore + BigInt ( transferAmount ) ,
37+ "Recipient balance should increase by exactly the transfer amount" ,
38+ ) ;
2839 } ) ;
2940
30- async function getBalances ( payerPubkey : PublicKey , recipientPubkey : PublicKey , timeframe : string ) {
41+ async function getBalances (
42+ payerPubkey : PublicKey ,
43+ recipientPubkey : PublicKey ,
44+ timeframe : string ,
45+ ) : Promise < [ bigint , bigint ] > {
3146 const payerBalance = await client . getBalance ( payerPubkey ) ;
3247 const recipientBalance = await client . getBalance ( recipientPubkey ) ;
3348
3449 console . log ( `${ timeframe } balances:` ) ;
3550 console . log ( ` Payer: ${ payerBalance } ` ) ;
3651 console . log ( ` Recipient: ${ recipientBalance } ` ) ;
52+
53+ return [ payerBalance , recipientBalance ] ;
3754 }
3855} ) ;
0 commit comments