I've been working on Solana program that should hold two different tokens for me in a Vault in an ATA. The first token should be deposited during Vault initialisation, the second will be deposited by exchanging for the first, will some fixed ratio. Both ATAs for the PDA of the Vault i wanted to be created during Vault initialisation. The problem is i keep getting error:
Error initializing vault: Error: AnchorError caused by account: heim_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: NativeLoader1111111111111111111111111111111
Program log: Right:
Program log: TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
Which is weird, because this mint is owned by a TokenProgram (https://solscan.io/token/BVqRM5tbqerXFnGBSDZMvGEvdUx3aVNUadBiQBbjAjxB?cluster=devnet)
This is my Anchor Rust code:
#[program]
pub mod heim_token {
use super::*;
pub fn initialize_vault(ctx: Context<InitializeVault>, initial_heim_amount: u64) -> Result<()> {
if ctx.accounts.authority_heim_ata.mint != mints::HEIM_ADDRESS {
return Err(ProgramError::InvalidAccountData.into());
}
let vault = &mut ctx.accounts.vault;
vault.authority = *ctx.accounts.authority.key;
vault.heim_amount = initial_heim_amount;
vault.usdc_amount = 0;
let cpi_accounts = SplTransfer {
from: ctx.accounts.authority_heim_ata.to_account_info().clone(),
to: ctx.accounts.vault_heim_ata.to_account_info().clone(),
authority: ctx.accounts.authority.to_account_info().clone()
};
let cpi_program = ctx.accounts.token_program.to_account_info();
token::transfer(CpiContext::new(cpi_program, cpi_accounts), initial_heim_amount)?;
Ok(())
}
}
#[account]
pub struct Vault {
pub authority: Pubkey,
pub heim_amount: u64,
pub usdc_amount: u64,
pub bump: u8,
}
#[derive(Accounts)]
pub struct InitializeVault<'info> {
#[account(mut, signer)]
pub authority: Signer<'info>,
#[account(
init,
payer = authority,
space = 8 + 64 + 32 + 1,
seeds=[b"heim_vault".as_ref()],
bump
)]
pub vault: Account<'info, Vault>,
#[account(
init,
payer = authority,
associated_token::mint = heim_mint,
associated_token::authority = authority,
)]
pub vault_heim_ata: Account<'info, TokenAccount>,
pub heim_mint: Account<'info, Mint>,
#[account(
init,
payer = authority,
associated_token::mint = usdc_mint,
associated_token::authority = authority,
)]
pub vault_usdc_ata: Account<'info, TokenAccount>,
pub usdc_mint: Account<'info, Mint>,
#[account(mut)]
pub authority_heim_ata: Account<'info, TokenAccount>,
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
pub rent: Sysvar<'info, Rent>, // do i really need it here? google it
pub associated_token_program: Program<'info, AssociatedToken>,
}
And this is JS client code:
const getProvider = () => {
const connection = new Connection(network, opts.preflightCommitment)
return new AnchorProvider(connection, window.solana, opts.preflightCommitment)
}
const initializeVault = async() => {
try {
const provider = getProvider()
const program = new Program(idl, programID, provider)
const heimMint = new PublicKey(
"BVqRM5tbqerXFnGBSDZMvGEvdUx3aVNUadBiQBbjAjxB"
);
const usdcMint = new PublicKey(
"NkHsgx5xBCPp6Gi33KTPgD2weVJJY884NiuVjz76APn"
);
const mintInfo = await provider.connection.getAccountInfo(heimMint)
const data = MintLayout.decode(mintInfo.data);
const senderATA = await getAssociatedTokenAddress(heimMint, provider.wallet.publicKey);
const heim_amount = new BN(1000).mul(new BN(10).pow(new BN(data.decimals)))
const result = await program.methods.initializeVault(heim_amount).accounts(
{
user: provider.wallet.publicKey,
systemProgram: SystemProgram.programId,
authorityHeimAta: senderATA,
tokenProgram: TOKEN_PROGRAM_ID,
heimMint: heimMint,
usdcMint: usdcMint,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
}
).rpc()
console.log("result", result)
getVault();
} catch (error) {
console.error('Error initializing vault:', error)
}
}
Could you please help me with this? Everything seems to be fine, everything i find in the documentation looks like what i did, i tried searching similar cases but i can't see anything that would help me, ChatGPT is saying the code is fine. But something isn't. Thanks in advance.
initialize_vault