2

I'd like to use getMinimumBalanceForRentExemption. With that said, it expects a parameter of the size of the data account. I'd like to know if there's a way to get the estimate size of an account or whether I have to calculate it by hand.

In my rust code, I use use std::mem::size_of to get the size of a struct. Anything similar in the client side which is the one setting the rent budget or maybe something similar in the IDL?

1 Answer 1

2

Yes there is:

const program = anchor.workspace.MyProgram as Program<MyProgram>;
const myAccountSize = program.account.myAccount.size;
console.log('Size', myAccountSize);

Note that size also includes the anchor discriminator, so it will be 8 bytes bigger than you might expect.

2
  • If you are allocating space for the rent-free exception, you do have to include those 8 bytes, so it would be appropriate to use directly for OP's purposes.
    – Whiteseal
    Commented Mar 20 at 5:57
  • Absolutely - I included this just as an FYI, as otherwise it might come as a surprise that an account holding e.g. a u32 would return 12 with .size instead of the more intuitive 4
    – McBain
    Commented Mar 20 at 6: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.