2

The solana documentation states that any transaction which will cause an account to go below it's rent exempt balance, should fail (here).

Every time an account's balance is reduced, a check is performed to see if the account is still rent exempt. Transactions that would cause an account's balance to drop below the rent exempt threshold will fail.

However, my program with the following endpoint succeeds, and I am in fact able to transfer the full amount out:

pub fn end_lottery(ctx: Context<EndLottery>, winner_index: u64) -> Result<()> {
    // ... snip ... 

    // fund the winner account with the total lottery account balance
    let prize = ctx.accounts.lottery_state.get_lamports();
    ctx.accounts.lottery_state.sub_lamports(prize)?;
    ctx.accounts.winner.add_lamports(prize)?;

    Ok(())
}

According to the documentation, this transaction should fail, and yet it doesn't. I am calling the above transaction on local validator node using anchor test. Is there some nuance in anchor that would cause this behaviour?

What seems to happen (instead of the transaction failing) is that the account is deleted instead. When I make the following client call

const lotteryState = await program.account.lotteryState.fetch(
  lotteryStateKeyPair.publicKey
);

I get this error that suggests the account has been deleted:

Error: Account does not exist or has no data 4A1GB3hF3nHdukfZZxHY2qkYU8Sg2xCobchUVddP33PT

What is going on here? Why does the documentation not line up with the actual behaviour?

1 Answer 1

4

An account always need the rent exempt balance for validators to keep its state saved. If a tx would result in an account having less balance than is required for that, it would be reverted. The edge case here is that if an account has balance 0, this is basically equivalent to that account being marked as empty and thus closed, which is what's happening in your code, as PDAs don't have a standard way of being closed. More info on this can be found here.

This behavior can also be found in the official docs: "Each validator periodically scans all accounts and collects rent. Any account that drops to zero lamports is purged."

2
  • Is this documented anywhere? Commented Jan 4 at 21:51
  • 1
    Yes, added it to my answer
    – Ahri
    Commented Jan 4 at 22:00

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.