0

I am getting and memory allocation failed error when I am passing in a dynamic seed for my account initialization.

This is how i use it:

pub fn chop_tree(ctx: Context<ChopTree>, counter: u16, levelSeed: String) -> Result<()> {
    chop_tree::chop_tree(ctx, counter)
}

and this is the anchor part:

#[derive(Accounts, Session)]
#[instruction(levelSeed: String)]
pub struct ChopTree<'info> {
    
    #[account(
        init_if_needed,
        payer = signer,
        space = 1000,
        seeds = [levelSeed.as_ref()],
        bump,
    )]
    pub game_data: Account<'info, GameData>,

    #[account(mut)]
    pub signer: Signer<'info>,
    pub system_program: Program<'info, System>,
}

On chain I see this error: enter image description here

2 Answers 2

1

You need to match arguments in your #[instruction(....)] macro above the context to what is being provided in the function arguments in the same order.

I'm guessing that this would work:

#[derive(Accounts, Session)]
#[instruction(_counter: u16, levelSeed: String)]
pub struct ChopTree<'info> {
    
    #[account(
        init_if_needed,
        payer = signer,
        space = 1000,
        seeds = [levelSeed.as_ref()],
        bump,
    )]
    pub game_data: Account<'info, GameData>,

    #[account(mut)]
    pub signer: Signer<'info>,
    pub system_program: Program<'info, System>,
}

With:

pub fn chop_tree(ctx: Context<ChopTree>, counter: u16, levelSeed: String) -> Result<()> {
    chop_tree::chop_tree(ctx, counter)
}
0

The solution here is to have the string seed as first parameter like so:

pub fn chop_tree(ctx: Context<ChopTree>, levelSeed: String, counter: u16) -> Result<()> {
        chop_tree::chop_tree(ctx, counter)
    }

If someone can explain me how that results into an out of memory error would be interesting. Maybe a problem in anchor somewhere.

1
  • 1
    That's really bizarre... maybe you can try passing by reference to avoid moving the whole string into the function? Ie declare levelSeed: &str instead of levelSeed: String
    – Jon C
    Commented Nov 6, 2023 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.