1

In a transaction, I’m trying to close an ATA and a PDA. The ATA is closed by calling close_account from the SPL Token 2022 crate / On Chain Program, and the rent exemption can be sent without any issue to the signing & transaction fee payer account.

For the PDA (called record below) though, I haven’t managed to do it. I can close the account, but only if I send the rent exemption lamports to an ad-hoc account that I add to the transaction just for that purpose. If I try to send the rent exemption to the signing & fee transaction payer account, I get an error: sum of account balances before and after instruction do not match. The accounts are all writable in this context.

Basically, the code is:

for account in accounts {
    instruction_whatever(account)?;
    let lamports = account.lamports();
    **account.try_borrow_mut_lamports()? -= lamports;
    **payer.try_borrow_mut_lamports()? += lamports;
}
another_instruction()?;

It works just fine if I replace payer by a 'random' account, but otherwise it doesn’t work and it’s a bit annoying to have to use another account just for that.

I’m guessing it has to do with the balance of the paying account being changed to pay for the transaction and messing things up, but it feels weird. Is there a trick that I’ve missed somewhere?

1 Answer 1

3

So, I found what my problem is.

If I get the sum of the rents and apply it at the very end instead of updating the payer account at each step of the loop it works. So it looks something like this:

let rents = 0;
for account in accounts {
    instruction_whatever(account)?;
    let lamports = account.lamports();
    **account.try_borrow_mut_lamports()? = 0;
    rents += lamports;
}
another_instruction()?;
**payer.try_borrow_mut_lamports()? += rents;
4
  • Could you add the code snippet to your original question so the answer makes a little more sense? Otherwise this answer both includes the question and answer
    – Jacob Creech
    Jan 5 at 3:07
  • Sure, it’s done. Jan 5 at 13:04
  • 1
    This sounds like its due to this runtime bug that prevents you from directly manipulating account lamports before any CPIs that involve those accounts: github.com/solana-labs/solana/issues/9711
    – billy
    Jan 6 at 10:26
  • That seems to be it yes. Jan 6 at 10:52

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.