Search code examples
solanaanchor-solana

Solana Anchor CPI call that requires pre-created account


I am using Anchor for developing my Solana programs, and I'm wondering how should I make CPI calls that require previously created accounts, for instance, I wasn't able to find how to calculate rent-exemption amount. Should I init these accounts like this?

#[derive(Accounts)]
pub struct CreateNft<'info> {
  #[account(init, payer = user, space = 967)]
  pub candy_machine: AccountInfo<'info>,
  #[account(mut)]
  pub user: Signer<'info>,
  pub system_program: Program<'info, System>,
}

Solution

  • You can just pass in the AccountInfo if it already exists (no need for init) but you do want constraints to check and make sure the account(s) passed in are correct and not some malicious account pretending to be the real thing. For some things like token accounts, Anchor has built in primitives for this.