I attempt to create a new account with the anchor #[account(init)]
macro, then assign ownership of the new account to the token program
, and then attempt to send the initialize_mint
instruction to the token program
via a CPI call.
But I seem to always get the following error:
Error: AnchorError caused by account: mint. Error Code: AccountOwnedByWrongProgram. Error Number: 3007. Error Message: The given account is owned by a different program than expected.
Program log: Left:
Program log: CgduZwGapBDVX3bJLRG8RR3HUYf2rQ8xjvtiBWE9bbbN
Program log: Right:
Program log: TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
My code:
#[program]
pub mod pump {
use super::*;
pub fn create(ctx: Context<Create>) -> Result<()> {
// assign ownership of the mint account to the token program
ctx.accounts.mint.to_account_info().assign(&token::ID);
// Create a new token mint
let cpi_accounts = InitializeMint {
mint: ctx.accounts.mint.to_account_info(),
rent: ctx.accounts.rent.to_account_info(),
};
let decimals = 9;
let cpi_program = ctx.accounts.token_program.clone().to_account_info();
let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);
token::initialize_mint(cpi_ctx, decimals, &ctx.accounts.mint_authority.key(), None)?;
Ok(())
}
}
#[derive(Accounts)]
pub struct Create<'info> {
#[account(init, payer = user, space = Mint::LEN)]
pub mint: Account<'info, Mint>,
#[account(mut)]
pub user: Signer<'info>,
#[account(seeds = [b"mint-authority"], bump)]
/// CHECK: The mint authority is the program derived address.
pub mint_authority: UncheckedAccount<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
}
This error goes away if I change the Create
struct to use an UncheckedAccount
:
#[derive(Accounts)]
pub struct Create<'info> {
#[account(init, payer = user, space = Mint::LEN)]
/// CHECK: foobar
pub mint: UncheckedAccount<'info>, // by changing this to be UncheckedAccount
// ... snip ...
}
What is going on here? Why does it work when I change from Account<'info, Mint>
to UncheckedAccount<'info>
? Is it safe to do this? Is there a better way of fixing this error?