@@ -5,34 +5,79 @@ describe("transfer-sol", () => {
55
66 const provider = anchor . AnchorProvider . env ( ) ;
77 anchor . setProvider ( provider ) ;
8- const wallet = provider . wallet as anchor . Wallet ;
8+ const payer = provider . wallet as anchor . Wallet ;
99 const program = anchor . workspace . TransferSol as anchor . Program < TransferSol > ;
1010
11- it ( "Transfer some SOL" , async ( ) => {
11+ const transferAmount = 1 * anchor . web3 . LAMPORTS_PER_SOL ;
12+ const test1Recipient = anchor . web3 . Keypair . generate ( ) ;
13+ const test2Recipient1 = anchor . web3 . Keypair . generate ( ) ;
14+ const test2Recipient2 = anchor . web3 . Keypair . generate ( ) ;
1215
13- async function getBalances ( payerPubkey : anchor . web3 . PublicKey , recipientPubkey : anchor . web3 . PublicKey , timeframe : string ) {
14- let payerBalance = await provider . connection . getBalance ( payerPubkey ) ;
15- let recipientBalance = await provider . connection . getBalance ( recipientPubkey ) ;
16- console . log ( `${ timeframe } balances:` ) ;
17- console . log ( ` Payer: ${ payerBalance } ` ) ;
18- console . log ( ` Recipient: ${ recipientBalance } ` ) ;
16+ it ( "Transfer between accounts using the system program" , async ( ) => {
17+
18+ await getBalances ( payer . publicKey , test1Recipient . publicKey , "Beginning" ) ;
19+
20+ await program . methods . transferSolWithCpi ( new anchor . BN ( transferAmount ) )
21+ . accounts ( {
22+ payer : payer . publicKey ,
23+ recipient : test1Recipient . publicKey ,
24+ systemProgram : anchor . web3 . SystemProgram . programId ,
25+ } )
26+ . signers ( [ payer . payer ] )
27+ . rpc ( ) ;
28+
29+ await getBalances ( payer . publicKey , test1Recipient . publicKey , "Resulting" ) ;
30+
31+ } ) ;
32+
33+ it ( "Create two accounts for the following test" , async ( ) => {
34+
35+ const ix = ( pubkey : anchor . web3 . PublicKey ) => {
36+ return anchor . web3 . SystemProgram . createAccount ( {
37+ fromPubkey : payer . publicKey ,
38+ newAccountPubkey : pubkey ,
39+ space : 0 ,
40+ lamports : 2 * anchor . web3 . LAMPORTS_PER_SOL ,
41+ programId : program . programId ,
42+ } )
1943 } ;
2044
21- const recipientKeypair = anchor . web3 . Keypair . generate ( ) ;
22- const transferAmount = 1 * anchor . web3 . LAMPORTS_PER_SOL ;
45+ await anchor . web3 . sendAndConfirmTransaction (
46+ provider . connection ,
47+ new anchor . web3 . Transaction ( )
48+ . add ( ix ( test2Recipient1 . publicKey ) )
49+ . add ( ix ( test2Recipient2 . publicKey ) )
50+ ,
51+ [ payer . payer , test2Recipient1 , test2Recipient2 ]
52+ ) ;
53+ } ) ;
54+
55+ it ( "Transfer between accounts using our program" , async ( ) => {
2356
24- await getBalances ( wallet . publicKey , recipientKeypair . publicKey , "Beginning" ) ;
57+ await getBalances ( test2Recipient1 . publicKey , test2Recipient2 . publicKey , "Beginning" ) ;
2558
26- await program . methods . transferSol ( new anchor . BN ( transferAmount ) )
27- . accounts ( {
28- payer : wallet . publicKey ,
29- recipient : recipientKeypair . publicKey ,
30- systemProgram : anchor . web3 . SystemProgram . programId ,
31- } )
32- . signers ( [ wallet . payer ] )
33- . rpc ( ) ;
59+ await program . methods . transferSolWithProgram ( new anchor . BN ( transferAmount ) )
60+ . accounts ( {
61+ payer : test2Recipient1 . publicKey ,
62+ recipient : test2Recipient2 . publicKey ,
63+ systemProgram : anchor . web3 . SystemProgram . programId ,
64+ } )
65+ . rpc ( ) ;
3466
35- await getBalances ( wallet . publicKey , recipientKeypair . publicKey , "Resulting" ) ;
67+ await getBalances ( test2Recipient1 . publicKey , test2Recipient2 . publicKey , "Resulting" ) ;
3668
3769 } ) ;
70+
71+ async function getBalances (
72+ payerPubkey : anchor . web3 . PublicKey ,
73+ recipientPubkey : anchor . web3 . PublicKey ,
74+ timeframe : string
75+ ) {
76+
77+ let payerBalance = await provider . connection . getBalance ( payerPubkey ) ;
78+ let recipientBalance = await provider . connection . getBalance ( recipientPubkey ) ;
79+ console . log ( `${ timeframe } balances:` ) ;
80+ console . log ( ` Payer: ${ payerBalance } ` ) ;
81+ console . log ( ` Recipient: ${ recipientBalance } ` ) ;
82+ } ;
3883} ) ;
0 commit comments