@@ -4,115 +4,137 @@ use {
4
4
program_pack:: Pack ,
5
5
pubkey:: Pubkey ,
6
6
rent:: Rent ,
7
+ system_instruction,
7
8
} ,
8
9
solana_program_test:: { processor, tokio, ProgramTest } ,
9
- solana_sdk:: { account :: Account , signature :: Signer , transaction:: Transaction } ,
10
+ solana_sdk:: { signature :: Signer , signer :: keypair :: Keypair , transaction:: Transaction } ,
10
11
spl_example_transfer_tokens:: processor:: process_instruction,
11
- spl_token:: state:: { Account as TokenAccount , Mint } ,
12
+ spl_token:: state:: { Account , Mint } ,
12
13
std:: str:: FromStr ,
13
14
} ;
14
15
15
16
#[ tokio:: test]
16
17
async fn success ( ) {
17
18
// Setup some pubkeys for the accounts
18
19
let program_id = Pubkey :: from_str ( "TransferTokens11111111111111111111111111111" ) . unwrap ( ) ;
19
- let source_pubkey = Pubkey :: new_unique ( ) ;
20
- let mint_pubkey = Pubkey :: new_unique ( ) ;
21
- let destination_pubkey = Pubkey :: new_unique ( ) ;
22
- let destination_owner_pubkey = Pubkey :: new_unique ( ) ;
20
+ let source = Keypair :: new ( ) ;
21
+ let mint = Keypair :: new ( ) ;
22
+ let destination = Keypair :: new ( ) ;
23
23
let ( authority_pubkey, _) = Pubkey :: find_program_address ( & [ b"authority" ] , & program_id) ;
24
24
25
25
// Add the program to the test framework
26
- let rent = Rent :: default ( ) ;
27
- let mut program_test = ProgramTest :: new (
26
+ let program_test = ProgramTest :: new (
28
27
"spl_example_transfer_tokens" ,
29
28
program_id,
30
29
processor ! ( process_instruction) ,
31
30
) ;
32
31
let amount = 10_000 ;
33
32
let decimals = 9 ;
33
+ let rent = Rent :: default ( ) ;
34
34
35
- // Setup the source account, owned by the program-derived address
36
- let mut data = vec ! [ 0 ; TokenAccount :: LEN ] ;
37
- TokenAccount :: pack (
38
- TokenAccount {
39
- mint : mint_pubkey,
40
- owner : authority_pubkey,
41
- amount,
42
- state : spl_token:: state:: AccountState :: Initialized ,
43
- ..TokenAccount :: default ( )
44
- } ,
45
- & mut data,
46
- )
47
- . unwrap ( ) ;
48
- program_test. add_account (
49
- source_pubkey,
50
- Account {
51
- lamports : rent. minimum_balance ( TokenAccount :: LEN ) ,
52
- owner : spl_token:: id ( ) ,
53
- data,
54
- ..Account :: default ( )
55
- } ,
56
- ) ;
35
+ // Start the program test
36
+ let ( mut banks_client, payer, recent_blockhash) = program_test. start ( ) . await ;
57
37
58
38
// Setup the mint, used in `spl_token::instruction::transfer_checked`
59
- let mut data = vec ! [ 0 ; Mint :: LEN ] ;
60
- Mint :: pack (
61
- Mint {
62
- supply : amount,
63
- decimals,
64
- is_initialized : true ,
65
- ..Mint :: default ( )
66
- } ,
67
- & mut data,
68
- )
69
- . unwrap ( ) ;
70
- program_test. add_account (
71
- mint_pubkey,
72
- Account {
73
- lamports : rent. minimum_balance ( Mint :: LEN ) ,
74
- owner : spl_token:: id ( ) ,
75
- data,
76
- ..Account :: default ( )
77
- } ,
39
+ let transaction = Transaction :: new_signed_with_payer (
40
+ & [
41
+ system_instruction:: create_account (
42
+ & payer. pubkey ( ) ,
43
+ & mint. pubkey ( ) ,
44
+ rent. minimum_balance ( Mint :: LEN ) ,
45
+ Mint :: LEN as u64 ,
46
+ & spl_token:: id ( ) ,
47
+ ) ,
48
+ spl_token:: instruction:: initialize_mint (
49
+ & spl_token:: id ( ) ,
50
+ & mint. pubkey ( ) ,
51
+ & payer. pubkey ( ) ,
52
+ None ,
53
+ decimals,
54
+ )
55
+ . unwrap ( ) ,
56
+ ] ,
57
+ Some ( & payer. pubkey ( ) ) ,
58
+ & [ & payer, & mint] ,
59
+ recent_blockhash,
78
60
) ;
61
+ banks_client. process_transaction ( transaction) . await . unwrap ( ) ;
62
+
63
+ // Setup the source account, owned by the program-derived address
64
+ let transaction = Transaction :: new_signed_with_payer (
65
+ & [
66
+ system_instruction:: create_account (
67
+ & payer. pubkey ( ) ,
68
+ & source. pubkey ( ) ,
69
+ rent. minimum_balance ( Account :: LEN ) ,
70
+ Account :: LEN as u64 ,
71
+ & spl_token:: id ( ) ,
72
+ ) ,
73
+ spl_token:: instruction:: initialize_account (
74
+ & spl_token:: id ( ) ,
75
+ & source. pubkey ( ) ,
76
+ & mint. pubkey ( ) ,
77
+ & authority_pubkey,
78
+ )
79
+ . unwrap ( ) ,
80
+ ] ,
81
+ Some ( & payer. pubkey ( ) ) ,
82
+ & [ & payer, & source] ,
83
+ recent_blockhash,
84
+ ) ;
85
+ banks_client. process_transaction ( transaction) . await . unwrap ( ) ;
79
86
80
87
// Setup the destination account, used to receive tokens from the account
81
88
// owned by the program-derived address
82
- let mut data = vec ! [ 0 ; TokenAccount :: LEN ] ;
83
- TokenAccount :: pack (
84
- TokenAccount {
85
- mint : mint_pubkey ,
86
- owner : destination_owner_pubkey ,
87
- amount : 0 ,
88
- state : spl_token :: state :: AccountState :: Initialized ,
89
- .. TokenAccount :: default ( )
90
- } ,
91
- & mut data ,
92
- )
93
- . unwrap ( ) ;
94
- program_test . add_account (
95
- destination_pubkey ,
96
- Account {
97
- lamports : rent . minimum_balance ( TokenAccount :: LEN ) ,
98
- owner : spl_token :: id ( ) ,
99
- data ,
100
- .. Account :: default ( )
101
- } ,
89
+ let transaction = Transaction :: new_signed_with_payer (
90
+ & [
91
+ system_instruction :: create_account (
92
+ & payer . pubkey ( ) ,
93
+ & destination . pubkey ( ) ,
94
+ rent . minimum_balance ( Account :: LEN ) ,
95
+ Account :: LEN as u64 ,
96
+ & spl_token :: id ( ) ,
97
+ ) ,
98
+ spl_token :: instruction :: initialize_account (
99
+ & spl_token :: id ( ) ,
100
+ & destination . pubkey ( ) ,
101
+ & mint . pubkey ( ) ,
102
+ & payer . pubkey ( ) ,
103
+ )
104
+ . unwrap ( ) ,
105
+ ] ,
106
+ Some ( & payer . pubkey ( ) ) ,
107
+ & [ & payer , & destination ] ,
108
+ recent_blockhash ,
102
109
) ;
110
+ banks_client. process_transaction ( transaction) . await . unwrap ( ) ;
103
111
104
- // Start the program test
105
- let ( mut banks_client, payer, recent_blockhash) = program_test. start ( ) . await ;
112
+ // Mint some tokens to the PDA account
113
+ let transaction = Transaction :: new_signed_with_payer (
114
+ & [ spl_token:: instruction:: mint_to (
115
+ & spl_token:: id ( ) ,
116
+ & mint. pubkey ( ) ,
117
+ & source. pubkey ( ) ,
118
+ & payer. pubkey ( ) ,
119
+ & [ ] ,
120
+ amount,
121
+ )
122
+ . unwrap ( ) ] ,
123
+ Some ( & payer. pubkey ( ) ) ,
124
+ & [ & payer] ,
125
+ recent_blockhash,
126
+ ) ;
127
+ banks_client. process_transaction ( transaction) . await . unwrap ( ) ;
106
128
107
129
// Create an instruction following the account order expected by the program
108
130
let transaction = Transaction :: new_signed_with_payer (
109
131
& [ Instruction :: new_with_bincode (
110
132
program_id,
111
133
& ( ) ,
112
134
vec ! [
113
- AccountMeta :: new( source_pubkey , false ) ,
114
- AccountMeta :: new_readonly( mint_pubkey , false ) ,
115
- AccountMeta :: new( destination_pubkey , false ) ,
135
+ AccountMeta :: new( source . pubkey ( ) , false ) ,
136
+ AccountMeta :: new_readonly( mint . pubkey ( ) , false ) ,
137
+ AccountMeta :: new( destination . pubkey ( ) , false ) ,
116
138
AccountMeta :: new_readonly( authority_pubkey, false ) ,
117
139
AccountMeta :: new_readonly( spl_token:: id( ) , false ) ,
118
140
] ,
@@ -127,10 +149,10 @@ async fn success() {
127
149
128
150
// Check that the destination account now has `amount` tokens
129
151
let account = banks_client
130
- . get_account ( destination_pubkey )
152
+ . get_account ( destination . pubkey ( ) )
131
153
. await
132
154
. unwrap ( )
133
155
. unwrap ( ) ;
134
- let token_account = TokenAccount :: unpack ( & account. data ) . unwrap ( ) ;
156
+ let token_account = Account :: unpack ( & account. data ) . unwrap ( ) ;
135
157
assert_eq ! ( token_account. amount, amount) ;
136
158
}
0 commit comments