7

I am pretty sure I remember reading somewhere that once the space is allocated for an account, it cannot be changed. Is this true? If so, can you delete the account and re-initialize it with a different amount of space? Are there any other ways of handling this?

As a specific use case: What if a string was set in storage on some program PDA account and someone wanted to update that string with something larger? What would that process look like? Is it possible?

4
  • 1
    Afaik accounts are now resizable (with some limitations) using the realloc feature. Never tried it so won't leave an answer, but here's some reading on it :) dev.to/jacobcreech/how-to-change-account-size-on-solana-55b4
    – nf0x
    Commented Jul 21, 2022 at 10:04
  • How did you find this? I spent a good 10 minutes searching duckduckgo with these terms and nothing came up :) Commented Jul 21, 2022 at 10:08
  • 1
    Hahaha I just knew from somewhere that it was called realloc, so I DDGd solana realloc and it was like the 5th link
    – nf0x
    Commented Jul 21, 2022 at 10:25
  • 1
    I even searched solana reallocate :D but oh well... thanks a ton... it looks like exactly what i was looking for. Commented Jul 21, 2022 at 10:31

1 Answer 1

10

From the anchor lang 25.0 docs, they have implemented the realloc constraint to give you access to the new functionality of resizing accounts. This functionality is particularly relevant for PDAs which can only be inited with 10kb of space and is limited to a 10kb increase in size each block (or maybe it's per transaction?).

  • realloc = sets the new space in bytes
  • realloc::payer = points to an account with Sol to pay for the extra space
  • realloc::zero = indicates whether you want the data in the account to be zeroed out (basically refresh and delete the entire account). In most cases you may not want this.

Anchor encourages using their constraints over the built in AccountInfo method because it's possible you might over allocate space and mess with other accounts, check the docs for more deets.

#[derive(Accounts)]
pub struct Example {
    #[account(mut)]
    pub payer: Signer<'info>,
    #[account(
        mut,
        seeds = [b"example"],
        bump,
        realloc = 8 + std::mem::size_of::() + 100,
        realloc::payer = payer,
        realloc::zero = false,
    )]
    pub acc: Account<'info, MyType>,
    pub system_program: Program<'info, System>,
}
2
  • Very clear and helpful answer! I just wanted to ask what you would put in place of Example if my program account is called BaseAccount? I've already defined this account as pub struct BaseAccount with the macro #[account] so should I write this in a separate #[derive(Accounts)] macro with the same exact struct name as the one under #[account]? Commented Jul 26, 2022 at 15:01
  • This sounds like a different question. You need to understand what the different structs are and what they are for so that you can change existing code to suit your own needs
    – Henry E
    Commented Jul 26, 2022 at 16:33

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.