3

I may have misunderstood how accounts work but it is my understanding that if a program is marked as the owner of an account then that program can modify the data in the account.

By default, all new accounts are assigned the system program as the owner. This means that the system program has the authority to write the data of the account and credit/debit lamports from that account.

But suppose that I assign the ownership of an account I have control of to be a different program from the system program.

Does that not mean that the system program no longer has authority to credit/debit lamports from my account? Or is the system program a special case where it can write and change data of an account without being the assigned owner?

2 Answers 2

3

When your PDA (program derived address) is not owned by the system program you can not use the system program transfer CPI anymore. But you can still change the data of the account so also the lamports amount. Like this:

        **ctx
            .accounts
            .chest_vault
            .to_account_info()
            .try_borrow_mut_lamports()? -= 100000;
        **ctx
            .accounts
            .player
            .to_account_info()
            .try_borrow_mut_lamports()? += 100000;

Here is an example in solana playground with video walkthrough: https://beta.solpg.io/tutorials/tiny-adventure-two

3
  • So given arbitrary account inputs, I should directly change the lamport data of the account and NOT use the system program transfer CPI (because that might fail due to the account owner not being the system program). Is there a place I can read more about modifying lamports directly? I assume that there are some rules associated with doing this. Jan 2 at 13:06
  • 1
    Rules are: You can only change the lamports when the program is the owner of the account. If its the system program you need to use the CPI. Second rule, all lamports of all accounts need to sum up the the lamports before the transaction, so you dont delete or create new lamports. Here you can read some about it: solanacookbook.com/gaming/…
    – Jonas H.
    Jan 2 at 13:17
  • Ah the lamport summation invariant is what I was trying to ask about but couldn’t find the words - that makes sense. Jan 3 at 1:09
3

You are correct in that only the owning program of an account can write data and that also applies to the lamports field.

However, there is one special case for lamports that has not been mentioned yet: any program can credit lamports to an account but only the owning program can debit lamports from an account

That means that if your program owns the account, you must use the field directly (as shown in Jonas' answer) because the SystemProgram cannot do the transfer anymore (as it doesn't own the account).

I hope that makes more sense now :)

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.