简体   繁体   中英

Anchor: Solana Account Initialization Issue

I'm using serum anchor framework for solana. I'm tring to initialize new account with #[account] attribute macro.
Each time i run anchor build i get the below error.

Error("the payer specified for an init constraint must be mutable.")

thread 'main' panicked at 'Code not parseable: Error("the payer specified for an init constraint must be mutable.")', lang/syn/src/idl/file.rs:360:58 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Following is the code snippet, where I'm attempting the account initialization

#[derive(Accounts)]
#[instruction(pool_nonce: u8, vault_nonce: u8)]
pub struct InitializePool<'info> {
    /// CHECK: This is not dangerous because we don't read or write from this account
    authority: UncheckedAccount<'info>,

    #[account(
        mut,
        // constraint = lp_token_pool_vault.mint == LP_TOKEN_MINT_PUBKEY.parse::<Pubkey>().unwrap(),
        constraint = lp_token_pool_vault.owner == pool_signer.key(),
    )]
    lp_token_pool_vault: Box<Account<'info, TokenAccount>>,
    #[account(
        mut,
        // constraint = lp_token_depositor.mint == LP_TOKEN_MINT_PUBKEY.parse::<Pubkey>().unwrap()
    )]
    lp_token_depositor: Box<Account<'info, TokenAccount>>,
    lp_token_deposit_authority: Signer<'info>,

    reward_mint: Box<Account<'info, Mint>>,
    #[account(
        constraint = reward_vault.mint == reward_mint.key(),
        constraint = reward_vault.owner == pool_signer.key(),
        constraint = reward_vault.close_authority == COption::None,
    )]
    reward_vault: Box<Account<'info, TokenAccount>>,

    #[account(
        seeds = [
            pool.to_account_info().key.as_ref()
        ],
        bump,
    )]
    /// CHECK: This is not dangerous because we don't read or write from this account
    pool_signer: UncheckedAccount<'info>,

    #[account(
        zero,
    )]
    pool: Box<Account<'info, Pool>>,
    #[account(
        init,
        payer = owner,
        seeds = [
            owner.key.as_ref(), 
            pool.to_account_info().key.as_ref()
        ],
        bump,
        space = 10240,
    )]
    vault: Box<Account<'info, Vault>>,
    owner: Signer<'info>,
    
    token_program: Program<'info, Token>,
    system_program: Program<'info, System>,
}

While initializing new account you need to pay fees and rent and by specifying payer in

#[account(
    init,
    payer = owner,
    seeds = [
        owner.key.as_ref(), 
        pool.to_account_info().key.as_ref()
    ],
    bump,
    space = 10240,
)]
vault: Box<Account<'info, Vault>>,

In order to modify payer account it has to be mutable.
We specify it as

#[account(mut)] // <== add this missing macro
owner: Signer<'info>,
#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(init, payer = owner, space = 9000)]
    pub base_account: Account<'info, BaseAccount>,
    #[account(mut)]
    pub owner: Signer<'info>,
    pub system_program: Program<'info, System>,
}

you should set the payer one of the Signers in account like above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM