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

Commit c59702d

Browse files
committed
Remove instruction counter
1 parent e297454 commit c59702d

File tree

1 file changed

+140
-31
lines changed

1 file changed

+140
-31
lines changed

program/tests/batch.rs

Lines changed: 140 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,45 @@
22

33
mod setup;
44

5-
use std::{collections::{BTreeMap, HashMap}, println};
5+
use std::{
6+
collections::{BTreeMap, HashMap},
7+
println,
8+
};
69

710
use pinocchio::instruction;
811
use setup::{account, mint, TOKEN_PROGRAM_ID};
912
use solana_program_test::{tokio, ProgramTest};
1013
use solana_sdk::{
11-
instruction::{AccountMeta, Instruction}, program_error::ProgramError, program_pack::Pack, pubkey::Pubkey, signature::{Keypair, Signer}, system_instruction, system_program, transaction::Transaction
14+
instruction::{AccountMeta, Instruction},
15+
program_error::ProgramError,
16+
program_pack::Pack,
17+
pubkey::Pubkey,
18+
signature::{Keypair, Signer},
19+
system_instruction, system_program,
20+
transaction::Transaction,
1221
};
1322

1423
fn batch_instruction(instructions: Vec<Instruction>) -> Result<Instruction, ProgramError> {
1524
// Create a Vector of ordered, AccountMetas
16-
let mut accounts: Vec<AccountMeta> = vec![];
25+
let mut accounts: Vec<AccountMeta> = vec![];
1726
// Start with the batch discriminator and a length byte
1827

19-
let mut data: Vec<u8> = vec![0xff, instructions.len() as u8];
28+
let mut data: Vec<u8> = vec![0xff];
2029
for instruction in instructions {
2130
// Error out on non-token IX
2231
if instruction.program_id.ne(&spl_token::ID) {
23-
return Err(ProgramError::IncorrectProgramId)
32+
return Err(ProgramError::IncorrectProgramId);
2433
}
25-
34+
2635
data.extend_from_slice(&[instruction.accounts.len() as u8]);
2736
data.extend_from_slice(&[instruction.data.len() as u8]);
28-
data.extend_from_slice(&instruction.data);
29-
accounts.extend_from_slice(&instruction.accounts);
37+
data.extend_from_slice(&instruction.data);
38+
accounts.extend_from_slice(&instruction.accounts);
3039
}
3140
Ok(Instruction {
3241
program_id: spl_token::ID,
3342
data,
34-
accounts
43+
accounts,
3544
})
3645
}
3746

@@ -54,14 +63,40 @@ async fn batch(token_program: Pubkey) {
5463
// Create a mint
5564
let mint_a = Keypair::new();
5665
let mint_authority = Keypair::new();
57-
let create_mint_a = system_instruction::create_account(&context.payer.pubkey(), &mint_a.pubkey(), mint_rent, mint_len as u64, &token_program);
58-
let initialize_mint_ix = spl_token::instruction::initialize_mint(&token_program, &mint_a.pubkey(), &mint_authority.pubkey(), None, 6).unwrap();
59-
66+
let create_mint_a = system_instruction::create_account(
67+
&context.payer.pubkey(),
68+
&mint_a.pubkey(),
69+
mint_rent,
70+
mint_len as u64,
71+
&token_program,
72+
);
73+
let initialize_mint_ix = spl_token::instruction::initialize_mint(
74+
&token_program,
75+
&mint_a.pubkey(),
76+
&mint_authority.pubkey(),
77+
None,
78+
6,
79+
)
80+
.unwrap();
81+
6082
// Create a mint 2 with a freeze authority
6183
let mint_b = Keypair::new();
6284
let freeze_authority = Pubkey::new_unique();
63-
let create_mint_b = system_instruction::create_account(&context.payer.pubkey(), &mint_b.pubkey(), mint_rent, mint_len as u64, &token_program);
64-
let initialize_mint_with_freeze_authority_ix = spl_token::instruction::initialize_mint2(&token_program, &mint_b.pubkey(), &mint_authority.pubkey(), Some(&freeze_authority), 6).unwrap();
85+
let create_mint_b = system_instruction::create_account(
86+
&context.payer.pubkey(),
87+
&mint_b.pubkey(),
88+
mint_rent,
89+
mint_len as u64,
90+
&token_program,
91+
);
92+
let initialize_mint_with_freeze_authority_ix = spl_token::instruction::initialize_mint2(
93+
&token_program,
94+
&mint_b.pubkey(),
95+
&mint_authority.pubkey(),
96+
Some(&freeze_authority),
97+
6,
98+
)
99+
.unwrap();
65100

66101
// Create 2 token accounts for mint A and 1 for mint B
67102
let owner_a = Keypair::new();
@@ -70,20 +105,73 @@ async fn batch(token_program: Pubkey) {
70105
let owner_a_ta_b = Keypair::new();
71106
let owner_b_ta_a = Keypair::new();
72107

73-
let create_owner_a_ta_a = system_instruction::create_account(&context.payer.pubkey(), &owner_a_ta_a.pubkey(), account_rent, account_len as u64, &token_program);
74-
let create_owner_b_ta_a = system_instruction::create_account(&context.payer.pubkey(), &owner_b_ta_a.pubkey(), account_rent, account_len as u64, &token_program);
75-
let intialize_owner_a_ta_a = spl_token::instruction::initialize_account3(&token_program, &owner_a_ta_a.pubkey(), &mint_a.pubkey(), &owner_a.pubkey()).unwrap();
76-
let intialize_owner_a_ta_b = spl_token::instruction::initialize_account3(&token_program, &owner_a_ta_b.pubkey(), &mint_b.pubkey(), &owner_a.pubkey()).unwrap();
77-
let intialize_owner_b_ta_a = spl_token::instruction::initialize_account3(&token_program, &owner_b_ta_a.pubkey(), &mint_a.pubkey(), &owner_b.pubkey()).unwrap();
108+
let create_owner_a_ta_a = system_instruction::create_account(
109+
&context.payer.pubkey(),
110+
&owner_a_ta_a.pubkey(),
111+
account_rent,
112+
account_len as u64,
113+
&token_program,
114+
);
115+
let create_owner_b_ta_a = system_instruction::create_account(
116+
&context.payer.pubkey(),
117+
&owner_b_ta_a.pubkey(),
118+
account_rent,
119+
account_len as u64,
120+
&token_program,
121+
);
122+
let intialize_owner_a_ta_a = spl_token::instruction::initialize_account3(
123+
&token_program,
124+
&owner_a_ta_a.pubkey(),
125+
&mint_a.pubkey(),
126+
&owner_a.pubkey(),
127+
)
128+
.unwrap();
129+
let intialize_owner_a_ta_b = spl_token::instruction::initialize_account3(
130+
&token_program,
131+
&owner_a_ta_b.pubkey(),
132+
&mint_b.pubkey(),
133+
&owner_a.pubkey(),
134+
)
135+
.unwrap();
136+
let intialize_owner_b_ta_a = spl_token::instruction::initialize_account3(
137+
&token_program,
138+
&owner_b_ta_a.pubkey(),
139+
&mint_a.pubkey(),
140+
&owner_b.pubkey(),
141+
)
142+
.unwrap();
78143

79144
// Mint Token A to Owner A
80-
let mint_token_a_to_owner_a = spl_token::instruction::mint_to(&token_program, &mint_a.pubkey(), &owner_a_ta_a.pubkey(), &mint_authority.pubkey(), &[], 1_000_000).unwrap();
145+
let mint_token_a_to_owner_a = spl_token::instruction::mint_to(
146+
&token_program,
147+
&mint_a.pubkey(),
148+
&owner_a_ta_a.pubkey(),
149+
&mint_authority.pubkey(),
150+
&[],
151+
1_000_000,
152+
)
153+
.unwrap();
81154

82155
// Transfer Token A from Owner A to Owner B
83-
let transfer_token_a_to_owner_b = spl_token::instruction::transfer(&token_program, &owner_a_ta_a.pubkey(), &owner_b_ta_a.pubkey(), &owner_a.pubkey(), &[], 1_000_000).unwrap();
156+
let transfer_token_a_to_owner_b = spl_token::instruction::transfer(
157+
&token_program,
158+
&owner_a_ta_a.pubkey(),
159+
&owner_b_ta_a.pubkey(),
160+
&owner_a.pubkey(),
161+
&[],
162+
1_000_000,
163+
)
164+
.unwrap();
84165

85166
// Close Token A
86-
let close_owner_a_ta_a = spl_token::instruction::close_account(&token_program, &owner_a_ta_a.pubkey(), &owner_a.pubkey(), &owner_a.pubkey(), &[]).unwrap();
167+
let close_owner_a_ta_a = spl_token::instruction::close_account(
168+
&token_program,
169+
&owner_a_ta_a.pubkey(),
170+
&owner_a.pubkey(),
171+
&owner_a.pubkey(),
172+
&[],
173+
)
174+
.unwrap();
87175

88176
let batch_ix = batch_instruction(vec![
89177
initialize_mint_ix,
@@ -92,8 +180,9 @@ async fn batch(token_program: Pubkey) {
92180
intialize_owner_b_ta_a,
93181
mint_token_a_to_owner_a,
94182
transfer_token_a_to_owner_b,
95-
close_owner_a_ta_a
96-
]).unwrap();
183+
close_owner_a_ta_a,
184+
])
185+
.unwrap();
97186

98187
println!("{:?}", batch_ix);
99188

@@ -103,27 +192,47 @@ async fn batch(token_program: Pubkey) {
103192
create_mint_b,
104193
create_owner_a_ta_a,
105194
create_owner_b_ta_a,
106-
batch_ix
195+
batch_ix,
107196
],
108197
Some(&context.payer.pubkey()),
109-
&vec![&context.payer, &mint_a, &mint_b, &owner_a_ta_a, &owner_b_ta_a, &mint_authority, &owner_a],
198+
&vec![
199+
&context.payer,
200+
&mint_a,
201+
&mint_b,
202+
&owner_a_ta_a,
203+
&owner_b_ta_a,
204+
&mint_authority,
205+
&owner_a,
206+
],
110207
context.last_blockhash,
111208
);
112209
context.banks_client.process_transaction(tx).await.unwrap();
113210

114-
let mint_a_account = context.banks_client.get_account(mint_a.pubkey()).await.unwrap();
211+
let mint_a_account = context
212+
.banks_client
213+
.get_account(mint_a.pubkey())
214+
.await
215+
.unwrap();
115216
assert!(mint_a_account.is_some());
116217
let mint_a_account = spl_token::state::Mint::unpack(&mint_a_account.unwrap().data).unwrap();
117218
assert_eq!(mint_a_account.supply, 1000000);
118219

119-
let mint_b_account = context.banks_client.get_account(mint_b.pubkey()).await.unwrap();
220+
let mint_b_account = context
221+
.banks_client
222+
.get_account(mint_b.pubkey())
223+
.await
224+
.unwrap();
120225
assert!(mint_b_account.is_some());
121226
let mint_b_account = spl_token::state::Mint::unpack(&mint_b_account.unwrap().data).unwrap();
122227
assert_eq!(mint_b_account.supply, 0);
123228

124-
let owner_b_ta_a_account = context.banks_client.get_account(owner_b_ta_a.pubkey()).await.unwrap();
229+
let owner_b_ta_a_account = context
230+
.banks_client
231+
.get_account(owner_b_ta_a.pubkey())
232+
.await
233+
.unwrap();
125234
assert!(owner_b_ta_a_account.is_some());
126-
let owner_b_ta_a_account = spl_token::state::Account::unpack(&owner_b_ta_a_account.unwrap().data).unwrap();
235+
let owner_b_ta_a_account =
236+
spl_token::state::Account::unpack(&owner_b_ta_a_account.unwrap().data).unwrap();
127237
assert_eq!(owner_b_ta_a_account.amount, 1000000);
128-
129238
}

0 commit comments

Comments
 (0)