2

In an anchor based project, I've seen something like this in an instruction:

**user_pda_account.to_account_info().try_borrow_mut_lamports()? -= amount;
**transfer_to.try_borrow_mut_lamports()? += amount;

My question is that does this really change the balance of the user_pda_account account?

Note that the user_pda_account is actually indicating this field:

   #[account(init, payer = signer, space = 100, seeds = [b"elevens", player.key().as_ref()], bump)]
   pub user_pda: Account<'info, UserPda>,

And this is UserPda:

#[account]
pub struct UserPda{
    user_wallet: Pubkey,
}

1 Answer 1

3

So two different questions you're asking. Does this perform a transaction, no. Does this actually change the account balance, yes!

Account balance is really just data and a number stored in all accounts. Your program is allowed to change the data of any account it owns, so it's allowed to modify the lamport balance of this account.

The only special thing about account balances is that no lamports can be created or destroyed, so any increment performed here needs to be balanced with an equal decrement somewhere else.

4
  • Thanks for your answer, so you mean we can transfer lamports without actually creating a transaction? Commented Jan 14 at 19:18
  • And as you noted that no lamport can be created or destroyed, would you mind explaining that who prevent someone from cheating this rule? Commented Jan 14 at 19:22
  • And by the fact that you said, instructions must be executed atomically atomically, right? Commented Jan 14 at 19:24
  • 2
    yes it updates lamports balance without creating an instruction, it's actually done inside your instruction. but it's not performing a CPI. No lamports can be created or destroyed = the transaction will fail if the sum of balances are not equal pre / post transaction. Meaning if you try to add lamports to an account without removing the same amount of lamports from another account, tx will fail.
    – Effe2
    Commented Jan 16 at 11:10

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.