0

I'm using anchor, so I define the space attribute in the Accounts struct, is there a way to make it depend on the instruction arg ?

It's for a PDA storing a string, and I don't know string length before the instruction is called. I can define a max length and allocate space for the max length but it's not very clean

1 Answer 1

2

Found out you can use the #[instruction] macro, to use the args you're waiting for. Also the anchor docs says that the space needed for a String is 4 + length of string in bytes. So i'd do something like that

#[derive(Accounts)]
#[instruction(content: String)]
pub struct InstructionExample {
    #[account(init, space: 8 + usize::from(4 + content.len())]
    pub pda_account: Account

}

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.