Search code examples
stackanchorsolana

Stack error caused working with anchor on solana


I am developing on-chain program of solana with anchor framework.
But I have crashed with stack error.

#[derive(Accounts)]
pub struct ClaimNftContext<'info> {
    #[account(mut)]
    pool: Account<'info, Pool>,
    pool_signer: AccountInfo<'info>,
    vault: AccountInfo<'info>, 
    user: Signer<'info>,
    mint: Account<'info, Mint>,
    #[account(mut)]
    nft_from: Account<'info, TokenAccount>,
    #[account(mut)]
    nft_to: Box<Account<'info, TokenAccount>>,
    #[account(mut)]
    token_from: Account<'info, TokenAccount>, 
    #[account(mut)]
    token_to: Account<'info, TokenAccount>, 
    token_program: Program<'info, Token>
}

As you can see, there are 10 accounts in ClaimNftContext but if I remove one, there's no error.
I think stack size is limited in anchor.
How can I do?


Solution

  • Anchor has limited stack size.
    Then is it impossible to get over 9 accounts from the context?
    Luckily, there is a way to reduce stack.
    That's Box.
    We could use like this:

    token_from: Box<AccountInfo<'info>> Then we can get more accounts.