pinocchio_token/instructions/
initialize_account.rs

1use solana_account_view::AccountView;
2use solana_instruction_view::{cpi::invoke, InstructionAccount, InstructionView};
3use solana_program_error::ProgramResult;
4
5/// Initialize a new Token Account.
6///
7/// ### Accounts:
8///   0. `[WRITE]`  The account to initialize.
9///   1. `[]` The mint this account will be associated with.
10///   2. `[]` The new account's owner/multi-signature.
11///   3. `[]` Rent sysvar
12pub struct InitializeAccount<'a> {
13    /// New Account.
14    pub account: &'a AccountView,
15    /// Mint Account.
16    pub mint: &'a AccountView,
17    /// Owner of the new Account.
18    pub owner: &'a AccountView,
19    /// Rent Sysvar Account
20    pub rent_sysvar: &'a AccountView,
21}
22
23impl InitializeAccount<'_> {
24    #[inline(always)]
25    pub fn invoke(&self) -> ProgramResult {
26        // Instruction accounts
27        let instruction_accounts: [InstructionAccount; 4] = [
28            InstructionAccount::writable(self.account.address()),
29            InstructionAccount::readonly(self.mint.address()),
30            InstructionAccount::readonly(self.owner.address()),
31            InstructionAccount::readonly(self.rent_sysvar.address()),
32        ];
33
34        let instruction = InstructionView {
35            program_id: &crate::ID,
36            accounts: &instruction_accounts,
37            data: &[1],
38        };
39
40        invoke(
41            &instruction,
42            &[self.account, self.mint, self.owner, self.rent_sysvar],
43        )
44    }
45}