Did I understand the concept of accounts?

Hi
I’m just starting to learn about Solana(I don’t have Ethereum experience either). But the concept of accounts is a little confusing so I’m not sure whether I understand it correctly or not. So I’m going to explain what I understand from the docs and I appreciate it if anyone can correct my misunderstandings:

  1. Even if I only have a Solana wallet(I don’t have any on-chain programs), that means there is an on-chain account which is holding the balance of my wallet.

  2. The account that is holding the balance of my wallet is owned by the system program.

  3. I can write a Solana program in Rust and then create an on-chain account to hold my program. Then I have to make it executable. After that I need to create another account(NOT executable) so that I can hold the state of my program. The non-executable account must be owned by the executable account.

Are these statements correct? If not, what’s wrong with them?
Thanks

1 Like

This post was flagged by the community and is temporarily hidden.

Hi @massivefermion , welcome to the forum!

Your understandings of accounts sounds correct to me. :slight_smile:

There is one thing that may be worth noting in addition to your points above:

To store your program’s state, you can create as many accounts as you want. For instance, this can be useful if you need to store a list of items in your program data that you don’t know the length of before hand. As far as I understand, once an account is created, you can’t change the length in bytes of the data in the account, so creating multiple accounts to store program data in can be a way to store a non-fixed amount of data.

For instance, I created a simple calendar reservation demo app with Solana, and each reservation was represented with a separate account. That account would store info about the reservation, such as what time the reservation was at, and what user scheduled the reservation, etc.

You can also use the Solana RPC API to query for specific accounts owned by your program that has specific data in a different field. For instance, I could get all of the reservations scheduled by a certain user.

So it’s quite flexible.

1 Like

Thanks
Another thing is that the docs explain that the default owner of an account is the system program and only the system program can change the owner of such accounts. But I don’t quite understand that.
What does it mean for the system program to change the ownership of an account? Because anything that happens on the solana network is a transasction and transactions should be initiated by a account holder, a person. So what kind of transaction is it? Who is initiating it? Why do they say it’s the system program that’s doing it?

Every account has to be owned by some program. A scenario will help to explain:

  1. You create a brand new Solana wallet using any wallet software. This can be done offline without connecting to the Solana cluster.
    • At this point you have a keypair, but there is no account on the Solana chain that is tied to your wallet keypair.
  2. You perform some first transaction using your wallet, such as getting some tokens transferred to your wallet from some other wallet.
    • At this point the system program will realize that there is no account associated to your wallet keypair, and it will create a new, empty account. The system program now owns that account.

So the system program owns every normal Solana user’s account.

The person who holds the private key of the account ( for example, you, in the scenario above ), must sign the transaction that will transfer ownership of that account. The transaction to transfer ownership is just like any other Solana transaction, but because you must sign the transaction in order for it to go through, it isn’t possible for anybody to just try to change the ownership of your account. You would have to approve it. This is just like the way it isn’t possible for anybody else to spend your SOL tokens in your account. You would have to approve the transaction to transfer your tokens.

The changing of ownership, the creation of new accounts, and the transferring of tokens are transactions that are implemented by the system program. These are all operations crucial to the function of Solana and the system program acts sort of like a “default smart contract” of the Solana chain, providing all the basic crucial account functionality.