Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit f825f1d

Browse files
authored
examples: Use txs instead of set_account in token test (#3969)
1 parent 9fd9805 commit f825f1d

File tree

2 files changed

+103
-86
lines changed

2 files changed

+103
-86
lines changed

examples/rust/transfer-tokens/src/processor.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,11 @@ pub fn process_instruction(
2626
let account_info_iter = &mut accounts.iter();
2727

2828
// As part of the program specification the instruction gives:
29-
// 1. source token account
30-
// 2. mint account
31-
// 3. destination token account
32-
// 4. program-derived address that owns 1.
33-
// 5. token program
34-
let source_info = next_account_info(account_info_iter)?;
35-
let mint_info = next_account_info(account_info_iter)?;
36-
let destination_info = next_account_info(account_info_iter)?;
37-
let authority_info = next_account_info(account_info_iter)?;
38-
let token_program_info = next_account_info(account_info_iter)?;
29+
let source_info = next_account_info(account_info_iter)?; // 1.
30+
let mint_info = next_account_info(account_info_iter)?; // 2.
31+
let destination_info = next_account_info(account_info_iter)?; // 3.
32+
let authority_info = next_account_info(account_info_iter)?; // 4.
33+
let token_program_info = next_account_info(account_info_iter)?; // 5.
3934

4035
// In order to transfer from the source account, owned by the program-derived
4136
// address, we must have the correct address and seeds.

examples/rust/transfer-tokens/tests/functional.rs

Lines changed: 98 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,115 +4,137 @@ use {
44
program_pack::Pack,
55
pubkey::Pubkey,
66
rent::Rent,
7+
system_instruction,
78
},
89
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},
1011
spl_example_transfer_tokens::processor::process_instruction,
11-
spl_token::state::{Account as TokenAccount, Mint},
12+
spl_token::state::{Account, Mint},
1213
std::str::FromStr,
1314
};
1415

1516
#[tokio::test]
1617
async fn success() {
1718
// Setup some pubkeys for the accounts
1819
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();
2323
let (authority_pubkey, _) = Pubkey::find_program_address(&[b"authority"], &program_id);
2424

2525
// 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(
2827
"spl_example_transfer_tokens",
2928
program_id,
3029
processor!(process_instruction),
3130
);
3231
let amount = 10_000;
3332
let decimals = 9;
33+
let rent = Rent::default();
3434

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;
5737

5838
// 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,
7860
);
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();
7986

8087
// Setup the destination account, used to receive tokens from the account
8188
// 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,
102109
);
110+
banks_client.process_transaction(transaction).await.unwrap();
103111

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();
106128

107129
// Create an instruction following the account order expected by the program
108130
let transaction = Transaction::new_signed_with_payer(
109131
&[Instruction::new_with_bincode(
110132
program_id,
111133
&(),
112134
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),
116138
AccountMeta::new_readonly(authority_pubkey, false),
117139
AccountMeta::new_readonly(spl_token::id(), false),
118140
],
@@ -127,10 +149,10 @@ async fn success() {
127149

128150
// Check that the destination account now has `amount` tokens
129151
let account = banks_client
130-
.get_account(destination_pubkey)
152+
.get_account(destination.pubkey())
131153
.await
132154
.unwrap()
133155
.unwrap();
134-
let token_account = TokenAccount::unpack(&account.data).unwrap();
156+
let token_account = Account::unpack(&account.data).unwrap();
135157
assert_eq!(token_account.amount, amount);
136158
}

0 commit comments

Comments
 (0)