1- use core:: mem:: size_of;
21use pinocchio:: {
3- account_info:: AccountInfo , program_error:: ProgramError , pubkey :: Pubkey , ProgramResult ,
2+ account_info:: AccountInfo , program_error:: ProgramError , ProgramResult
43} ;
54
6- use crate :: processor:: {
7- process_close_account, process_initialize_account3, process_initialize_mint,
8- process_initialize_mint2, process_mint_to, process_transfer,
9- } ;
10-
11- macro_rules! map_accounts {
12- // For 1 account
13- ( $accounts: expr, $instruction_data: expr, 1 ) => { {
14- let ( account_idx, rest) = $instruction_data
15- . split_first( )
16- . ok_or( ProgramError :: InvalidInstructionData ) ?;
17- * $instruction_data = rest;
18- let batch_accounts = [ $accounts[ * account_idx as usize ] . clone( ) ] ;
19- batch_accounts
20- } } ;
21-
22- // For 2 accounts
23- ( $accounts: expr, $instruction_data: expr, 2 ) => { {
24- let ( account_indices, rest) = $instruction_data. split_at( 2 ) ;
25- * $instruction_data = rest;
26- let batch_accounts = [
27- $accounts[ account_indices[ 0 ] as usize ] . clone( ) ,
28- $accounts[ account_indices[ 1 ] as usize ] . clone( ) ,
29- ] ;
30- batch_accounts
31- } } ;
32-
33- // For 3 accounts
34- ( $accounts: expr, $instruction_data: expr, 3 ) => { {
35- let ( account_indices, rest) = $instruction_data. split_at( 3 ) ;
36- * $instruction_data = rest;
37- let batch_accounts = [
38- $accounts[ account_indices[ 0 ] as usize ] . clone( ) ,
39- $accounts[ account_indices[ 1 ] as usize ] . clone( ) ,
40- $accounts[ account_indices[ 2 ] as usize ] . clone( ) ,
41- ] ;
42- batch_accounts
43- } } ;
44- }
5+ use crate :: entrypoint:: process_instruction;
456
467#[ inline( always) ]
478pub fn process_batch ( accounts : & [ AccountInfo ] , instruction_data : & [ u8 ] ) -> ProgramResult {
@@ -50,77 +11,18 @@ pub fn process_batch(accounts: &[AccountInfo], instruction_data: &[u8]) -> Progr
5011 . split_first ( )
5112 . ok_or ( ProgramError :: InvalidInstructionData ) ?;
5213
53- let mut discriminator;
14+ let mut lengths: & [ u8 ] ;
15+ let mut accounts = accounts;
16+ let mut current_accounts: & [ AccountInfo ] ;
17+ let mut current_instruction_data: & [ u8 ] ;
18+
5419 for _ in 0 ..* counter {
55- ( discriminator , instruction_data) = instruction_data
56- . split_first ( )
20+ ( lengths , instruction_data) = instruction_data
21+ . split_at_checked ( 2 )
5722 . ok_or ( ProgramError :: InvalidInstructionData ) ?;
58- match discriminator {
59- // 0 - InitializeMint
60- 0 => {
61- #[ cfg( feature = "logging" ) ]
62- pinocchio:: msg!( "Batch Instruction: InitializeMint" ) ;
63-
64- let batch_accounts = map_accounts ! ( accounts, & mut instruction_data, 2 ) ;
65- process_initialize_mint ( & batch_accounts, instruction_data, true ) ?;
66- if instruction_data[ size_of :: < ( u8 , Pubkey ) > ( ) ] == 0 {
67- instruction_data = & instruction_data[ size_of :: < ( u8 , Pubkey , u8 ) > ( ) ..] ;
68- } else {
69- instruction_data = & instruction_data[ size_of :: < ( u8 , Pubkey , u8 , Pubkey ) > ( ) ..] ;
70- }
71- }
72- // 3 - Transfer
73- 3 => {
74- #[ cfg( feature = "logging" ) ]
75- pinocchio:: msg!( "Batch Instruction: Transfer" ) ;
76-
77- let batch_accounts = map_accounts ! ( accounts, & mut instruction_data, 3 ) ;
78- process_transfer ( & batch_accounts, instruction_data) ?;
79- instruction_data = & instruction_data[ size_of :: < u64 > ( ) ..] ;
80- }
81- // 7 - MintTo
82- 7 => {
83- #[ cfg( feature = "logging" ) ]
84- pinocchio:: msg!( "Batch Instruction: MintTo" ) ;
85-
86- let batch_accounts = map_accounts ! ( accounts, & mut instruction_data, 3 ) ;
87- process_mint_to ( & batch_accounts, instruction_data) ?;
88- instruction_data = & instruction_data[ size_of :: < u64 > ( ) ..] ;
89- }
90- // 9 - CloseAccount
91- 9 => {
92- #[ cfg( feature = "logging" ) ]
93- pinocchio:: msg!( "Batch Instruction: CloseAccount" ) ;
94-
95- let batch_accounts = map_accounts ! ( accounts, & mut instruction_data, 2 ) ;
96- process_close_account ( & batch_accounts) ?;
97- }
98- // 18 - InitializeAccount3
99- 18 => {
100- #[ cfg( feature = "logging" ) ]
101- pinocchio:: msg!( "Batch Instruction: InitializeAccount3" ) ;
102-
103- let batch_accounts = map_accounts ! ( accounts, & mut instruction_data, 3 ) ;
104- process_initialize_account3 ( & batch_accounts, instruction_data) ?;
105- instruction_data = & instruction_data[ size_of :: < Pubkey > ( ) ..] ;
106- }
107- // 20 - InitializeMint2
108- 20 => {
109- #[ cfg( feature = "logging" ) ]
110- pinocchio:: msg!( "Instruction: InitializeMint2" ) ;
111-
112- let batch_accounts = map_accounts ! ( accounts, & mut instruction_data, 1 ) ;
113- process_initialize_mint2 ( & batch_accounts, instruction_data) ?;
114- if instruction_data[ size_of :: < ( u8 , Pubkey ) > ( ) ] == 0 {
115- instruction_data = & instruction_data[ size_of :: < ( u8 , Pubkey , u8 ) > ( ) ..] ;
116- } else {
117- instruction_data = & instruction_data[ size_of :: < ( u8 , Pubkey , u8 , Pubkey ) > ( ) ..] ;
118- }
119- }
120- _ => {
121- return Err ( ProgramError :: InvalidInstructionData ) ;
122- }
123- }
23+ ( current_accounts, accounts) = accounts. split_at_checked ( lengths[ 0 ] . into ( ) ) . ok_or ( ProgramError :: InvalidInstructionData ) ?;
24+ ( current_instruction_data, instruction_data) = instruction_data. split_at_checked ( lengths[ 1 ] . into ( ) ) . ok_or ( ProgramError :: InvalidInstructionData ) ?;
25+ process_instruction ( & token_interface:: program:: ID , current_accounts, current_instruction_data) ?;
12426 }
12527 Ok ( ( ) )
126- }
28+ }
0 commit comments