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,20 @@ 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 ( payerAfter < payerBefore - BigInt ( transferAmount ) , "Payer balance should decrease by at least the transfer amount" ) ;
31+ assert . strictEqual ( recipientAfter , recipientBefore + BigInt ( transferAmount ) , "Recipient balance should increase by exactly the transfer amount" ) ;
2832 } ) ;
2933
30- async function getBalances ( payerPubkey : PublicKey , recipientPubkey : PublicKey , timeframe : string ) {
34+ async function getBalances ( payerPubkey : PublicKey , recipientPubkey : PublicKey , timeframe : string ) : Promise < [ bigint , bigint ] > {
3135 const payerBalance = await client . getBalance ( payerPubkey ) ;
3236 const recipientBalance = await client . getBalance ( recipientPubkey ) ;
3337
3438 console . log ( `${ timeframe } balances:` ) ;
3539 console . log ( ` Payer: ${ payerBalance } ` ) ;
3640 console . log ( ` Recipient: ${ recipientBalance } ` ) ;
41+
42+ return [ payerBalance , recipientBalance ] ;
3743 }
3844} ) ;
0 commit comments