@@ -52,12 +52,18 @@ impl Processor {
5252 Ok ( ( ) )
5353 }
5454
55- /// Processes an [InitializeAccount](enum.TokenInstruction.html) instruction.
56- pub fn process_initialize_account ( accounts : & [ AccountInfo ] ) -> ProgramResult {
55+ fn _process_initialize_account (
56+ accounts : & [ AccountInfo ] ,
57+ owner : Option < & Pubkey > ,
58+ ) -> ProgramResult {
5759 let account_info_iter = & mut accounts. iter ( ) ;
5860 let new_account_info = next_account_info ( account_info_iter) ?;
5961 let mint_info = next_account_info ( account_info_iter) ?;
60- let owner_info = next_account_info ( account_info_iter) ?;
62+ let owner = if let Some ( owner) = owner {
63+ owner
64+ } else {
65+ next_account_info ( account_info_iter) ?. key
66+ } ;
6167 let new_account_info_data_len = new_account_info. data_len ( ) ;
6268 let rent = & Rent :: from_account_info ( next_account_info ( account_info_iter) ?) ?;
6369
@@ -76,7 +82,7 @@ impl Processor {
7682 }
7783
7884 account. mint = * mint_info. key ;
79- account. owner = * owner_info . key ;
85+ account. owner = * owner ;
8086 account. delegate = COption :: None ;
8187 account. delegated_amount = 0 ;
8288 account. state = AccountState :: Initialized ;
@@ -97,6 +103,16 @@ impl Processor {
97103 Ok ( ( ) )
98104 }
99105
106+ /// Processes an [InitializeAccount](enum.TokenInstruction.html) instruction.
107+ pub fn process_initialize_account ( accounts : & [ AccountInfo ] ) -> ProgramResult {
108+ Self :: _process_initialize_account ( accounts, None )
109+ }
110+
111+ /// Processes an [InitializeAccount2](enum.TokenInstruction.html) instruction.
112+ pub fn process_initialize_account2 ( accounts : & [ AccountInfo ] , owner : Pubkey ) -> ProgramResult {
113+ Self :: _process_initialize_account ( accounts, Some ( & owner) )
114+ }
115+
100116 /// Processes a [InitializeMultisig](enum.TokenInstruction.html) instruction.
101117 pub fn process_initialize_multisig ( accounts : & [ AccountInfo ] , m : u8 ) -> ProgramResult {
102118 let account_info_iter = & mut accounts. iter ( ) ;
@@ -641,6 +657,10 @@ impl Processor {
641657 msg ! ( "Instruction: InitializeAccount" ) ;
642658 Self :: process_initialize_account ( accounts)
643659 }
660+ TokenInstruction :: InitializeAccount2 { owner } => {
661+ msg ! ( "Instruction: InitializeAccount2" ) ;
662+ Self :: process_initialize_account2 ( accounts, owner)
663+ }
644664 TokenInstruction :: InitializeMultisig { m } => {
645665 msg ! ( "Instruction: InitializeMultisig" ) ;
646666 Self :: process_initialize_multisig ( accounts, m)
@@ -5660,4 +5680,52 @@ mod tests {
56605680 let account = Account :: unpack_unchecked ( & account_account. data ) . unwrap ( ) ;
56615681 assert_eq ! ( account. state, AccountState :: Initialized ) ;
56625682 }
5683+
5684+ #[ test]
5685+ fn test_initialize_account2 ( ) {
5686+ let program_id = Pubkey :: new_unique ( ) ;
5687+ let account_key = Pubkey :: new_unique ( ) ;
5688+ let mut account_account = SolanaAccount :: new (
5689+ account_minimum_balance ( ) ,
5690+ Account :: get_packed_len ( ) ,
5691+ & program_id,
5692+ ) ;
5693+ let mut account2_account = SolanaAccount :: new (
5694+ account_minimum_balance ( ) ,
5695+ Account :: get_packed_len ( ) ,
5696+ & program_id,
5697+ ) ;
5698+ let owner_key = Pubkey :: new_unique ( ) ;
5699+ let mut owner_account = SolanaAccount :: default ( ) ;
5700+ let mint_key = Pubkey :: new_unique ( ) ;
5701+ let mut mint_account =
5702+ SolanaAccount :: new ( mint_minimum_balance ( ) , Mint :: get_packed_len ( ) , & program_id) ;
5703+ let mut rent_sysvar = rent_sysvar ( ) ;
5704+
5705+ // create mint
5706+ do_process_instruction (
5707+ initialize_mint ( & program_id, & mint_key, & owner_key, None , 2 ) . unwrap ( ) ,
5708+ vec ! [ & mut mint_account, & mut rent_sysvar] ,
5709+ )
5710+ . unwrap ( ) ;
5711+
5712+ do_process_instruction (
5713+ initialize_account ( & program_id, & account_key, & mint_key, & owner_key) . unwrap ( ) ,
5714+ vec ! [
5715+ & mut account_account,
5716+ & mut mint_account,
5717+ & mut owner_account,
5718+ & mut rent_sysvar,
5719+ ] ,
5720+ )
5721+ . unwrap ( ) ;
5722+
5723+ do_process_instruction (
5724+ initialize_account2 ( & program_id, & account_key, & mint_key, & owner_key) . unwrap ( ) ,
5725+ vec ! [ & mut account2_account, & mut mint_account, & mut rent_sysvar] ,
5726+ )
5727+ . unwrap ( ) ;
5728+
5729+ assert_eq ! ( account_account, account2_account) ;
5730+ }
56635731}
0 commit comments