6

I want to use the initialize_account function from anchor_spl::token.

The function is given below:

pub fn initialize_account<'a, 'b, 'c, 'info>(
    ctx: CpiContext<'a, 'b, 'c, 'info, InitializeAccount<'info>>
) -> Result<()>

The struct is:

pub struct InitializeAccount<'info> {
    pub account: AccountInfo<'info>,
    pub mint: AccountInfo<'info>,
    pub authority: AccountInfo<'info>,
    pub rent: AccountInfo<'info>,
}

I am confused about implementing this in my anchor smart contract. Particularly, I want to know what is the account in the struct. An example implementation would be really helpful since it is not available online

2 Answers 2

4

The initialize_account function is to create a new Token Account.

The account in the struct is the Token Account that is being initialized in the instruction

Here's a solpg link for reference implementation: https://beta.solpg.io/63170ee1bb7e0b5f4ca6df9c

#[program]
mod hello_anchor {
    use super::*;
    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
        let space = anchor_spl::token::TokenAccount::LEN as u64;
        let lamports = Rent::get()?.minimum_balance(space as usize);
        
        let ix = anchor_lang::solana_program::system_instruction::create_account(
            &ctx.accounts.authority.key(),
            &ctx.accounts.token_account.key(),
            lamports,
            space,
            &anchor_spl::token::ID,
        );

        anchor_lang::solana_program::program::invoke(
            &ix,
            &[ctx.accounts.authority.to_account_info(), ctx.accounts.token_account.to_account_info(), ctx.accounts.system_program.to_account_info()])?;
        
        let cpi_program = ctx.accounts.token_program.to_account_info();
        let cpi_accounts = InitializeAccount {
            account: ctx.accounts.token_account.to_account_info(),
            mint: ctx.accounts.mint.to_account_info(),
            authority: ctx.accounts.authority.to_account_info(),
            rent: ctx.accounts.rent.to_account_info(),
        };
        let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);
        anchor_spl::token::initialize_account(cpi_ctx)?;
        Ok(())
    }
}

#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(mut)]
    pub token_account: Signer<'info>,
    pub mint: Account<'info, Mint>,
    #[account(mut)]
    pub authority: Signer<'info>,
    pub token_program: Program<'info, Token>,
    pub rent: Sysvar<'info, Rent>,
    pub system_program: Program<'info, System>,
}

enter image description here

7
  • Is there any way to create token account with same address as when created by default?
    – fahad
    Commented Sep 6, 2022 at 9:59
  • if I donot input a random pubkey for token_account it displays an error
    – fahad
    Commented Sep 6, 2022 at 9:59
  • by default, do you mean the associated token account?
    – john
    Commented Sep 6, 2022 at 10:30
  • yes the default ATA which is created by doing CLI command
    – fahad
    Commented Sep 6, 2022 at 12:27
3

Here's a Solpg example for how to create an associated token account in Anchor using init_if_needed

https://beta.solpg.io/6317c4d1bb7e0b5f4ca6dfa2

use anchor_lang::prelude::*;
use anchor_spl::associated_token::AssociatedToken;
use anchor_spl::token::{Mint, Token, TokenAccount};

declare_id!("GAvTLLgVwq7iTk7WCA4Nr2nmtoHGK8E6VWAoZHi4k32X");

#[program]
mod hello_anchor {
    use super::*;
    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
        Ok(())
    }
}

#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(
        init_if_needed,
        payer = payer, 
        associated_token::mint = mint, 
        associated_token::authority = payer
    )]
    pub token_account: Account<'info, TokenAccount>,
    pub mint: Account<'info, Mint>,
     #[account(mut)]
    pub payer: Signer<'info>,
    pub system_program: Program<'info, System>,
    pub token_program: Program<'info, Token>,
    pub associated_token_program: Program<'info, AssociatedToken>,
    pub rent: Sysvar<'info, Rent>,
}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.