1- use std:: vec;
2-
31use solana_program:: hash:: Hash ;
42
53use solana_program_test:: { processor, BanksClient , ProgramTest } ;
64use solana_sdk:: {
7- config:: program, lamports, native_token:: LAMPORTS_PER_SOL , signature:: Keypair , signer:: Signer ,
8- transaction:: Transaction ,
5+ native_token:: LAMPORTS_PER_SOL , signature:: Keypair , signer:: Signer , transaction:: Transaction ,
96} ;
10- use steel:: * ;
117use transfer_sol_api:: prelude:: * ;
128
139async fn setup ( ) -> ( BanksClient , Keypair , Hash ) {
@@ -21,28 +17,65 @@ async fn setup() -> (BanksClient, Keypair, Hash) {
2117 program_test. start ( ) . await
2218}
2319
24- // let system_program_id = &system_program::ID;
25- // let key = Rc::new(*system_program_id);
26- // let lamports = Rc::new(RefCell::new(0));
27- // let data = Rc::new(RefCell::new(vec![]));
28- // let owner = system_program_id;
29- // let is_signer = false;
30- // let is_writable = false;
31-
32- // AccountInfo {
33- // key: &*key,
34- // is_signer,
35- // is_writable,
36- // lamports,
37- // data,
38- // owner,
39- // executable: true,
40- // rent_epoch: 0,
41- // }
42- // }
20+ #[ tokio:: test]
21+ async fn transfer_with_program_works ( ) {
22+ // Setup test
23+ let ( mut banks, payer, blockhash) = setup ( ) . await ;
24+
25+ // 1 SOL
26+ let amount = 1 * LAMPORTS_PER_SOL ;
27+
28+ // Generate a couple of keypairs to create accounts owned by our program
29+ let acc_1 = Keypair :: new ( ) ;
30+ let acc_2 = Keypair :: new ( ) ;
31+
32+ // Create the program accounts
33+ let create_account_instruction = |pubkey| {
34+ solana_program:: system_instruction:: create_account (
35+ & payer. pubkey ( ) ,
36+ pubkey,
37+ amount,
38+ 0 ,
39+ & transfer_sol_api:: ID ,
40+ )
41+ } ;
42+
43+ let tx_create_accounts = Transaction :: new_signed_with_payer (
44+ & [
45+ create_account_instruction ( & acc_1. pubkey ( ) ) ,
46+ create_account_instruction ( & acc_2. pubkey ( ) ) ,
47+ ] ,
48+ Some ( & payer. pubkey ( ) ) ,
49+ & [ & payer, & acc_1, & acc_2] ,
50+ blockhash,
51+ ) ;
52+
53+ let res = banks. process_transaction ( tx_create_accounts) . await ;
54+ assert ! ( res. is_ok( ) ) ;
55+
56+ // Let's transfer some lamports from acc_1 to acc_2
57+ let latest_blockhash = banks. get_latest_blockhash ( ) . await . unwrap ( ) ;
58+
59+ let ix = with_program ( acc_1. pubkey ( ) , acc_2. pubkey ( ) , amount) ;
60+ let tx = Transaction :: new_signed_with_payer (
61+ & [ ix] ,
62+ Some ( & payer. pubkey ( ) ) ,
63+ & [ & payer] ,
64+ latest_blockhash,
65+ ) ;
66+
67+ let res = banks. process_transaction ( tx) . await ;
68+ assert ! ( res. is_ok( ) ) ;
69+
70+ // Now, let's check the balances
71+ let acc_1_balance = banks. get_balance ( acc_1. pubkey ( ) ) . await . unwrap ( ) ;
72+ assert_eq ! ( acc_1_balance, 0 ) ;
73+ let acc_2_balance = banks. get_balance ( acc_2. pubkey ( ) ) . await . unwrap ( ) ;
74+ assert_eq ! ( acc_2_balance, 2 * amount) ;
75+ }
4376
4477#[ tokio:: test]
45- async fn run_test ( ) {
78+ async fn transfer_with_cpi_works ( ) {
4679 // Setup test
4780 let ( mut banks, payer, blockhash) = setup ( ) . await ;
4881
@@ -58,33 +91,6 @@ async fn run_test() {
5891 let res = banks. process_transaction ( tx) . await ;
5992 assert ! ( res. is_ok( ) ) ;
6093
61- // // Verify counter was initialized.
62- // let counter_address = counter_pda().0;
63- // let counter_account = banks.get_account(counter_address).await.unwrap().unwrap();
64- // let counter = Counter::try_from_bytes(&counter_account.data).unwrap();
65- // assert_eq!(counter_account.owner, transfer_sol_api::ID);
66- // assert_eq!(counter.value, 0);
67-
68- // // Generate a new keypair to create an account owned by our program
69- // let program_owned_account = Keypair::new();
70- // let system_program_account = create_system_program_account_info();
71-
72- // create_account(
73- // &program_owned_account,
74- // &system_program_account,
75- // &payer.pubkey(),
76- // &program::ID,
77- // &[&payer, &system_program_account],
78- // )?;
79-
80- // // Submit transfer with program transaction.
81- // let ix = with_program(payer.pubkey(), recipient.pubkey(), amount);
82- // let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
83- // let res = banks.process_transaction(tx).await;
84- // assert!(res.is_ok());
85-
86- // // Verify counter was incremented.
87- // let counter_account = banks.get_account(counter_address).await.unwrap().unwrap();
88- // let counter = Counter::try_from_bytes(&counter_account.data).unwrap();
89- // assert_eq!(counter.value, 42);
94+ let balance = banks. get_balance ( recipient. pubkey ( ) ) . await . unwrap ( ) ;
95+ assert_eq ! ( balance, amount) ;
9096}
0 commit comments