5 min read
The Solana account model is a critical part of the Solana ecosystem and can be one the hardest concepts for developers, especially ones transitioning from other blockchains, to understand. To work on the Solana blockchain efficiently, a good understanding of the account model is important. Let us first define what accounts are and explore the different types of accounts as well as how we can create and interact with them.
What is an Account?
Accounts are any place where data is stored on the Solana blockchain. What makes the Solana blockchain unique compared to other blockchains like Ethereum is how this data is stored and managed. Here are the different account categories:
- Program Account - These accounts store executable code and the equivalent of Ethereum smart contracts.
- Storage Account - These accounts store the data connected to programs.
- Token Account - These accounts track an account balance of tokens and allow for transferring or receiving tokens between accounts.
In the Solana blockchain, there is a separation between the program and the data/state of the program. Both are assigned separate accounts but are connected. Comparing this to Ethereum, a smart contract and the data of that smart contract are located in one location on the blockchain. If you were to make a program that counted the number of token transfers that a program made, you would need to create the program to make the transfers as well as another account to store the count of transfers.
To take an example from the traditional finance world, you can think of a program like a debit card, your bank as a storage account, and your account balance as a token account. While each of these is connected, they are in separate locations. If you were to lose your debit card, you would not lose your bank account (but maybe the funds in it). Your card is also the only card that can alter the account balance by using it to buy things. Similarly, the program associated with a storage account is the only one that can change the state of the data.
Types of Accounts
There are two types of accounts on the Solana blockchain: executable and non-executable. Programs are executable accounts and store the immutable code of a program. The code of a program is first written in Rust or C/C++ and then compiled into byte code via the LLVM compiler infrastructure.
Data storage and token balances are stored in non-executable accounts as their data can be changed. To control who can change this data, non-executable accounts have an owner program address assigned to them. Other programs can read the data of another account, but if they tried to modify that data, the transaction would fail.
Unfortunately, storing all this data into separate accounts is not free and comes with some costs. Fortunately for developers, these costs, called rent, are paid in lamports. Lamports are fractions of Solana's token, SOL, and are used to make micropayments on the Solana blockchain. Rent fees are calculated based on the storage size of the account. The larger amount of data stored, the higher the rent fee.
Rent fees are collected at the end of every epoch of the Solana blockchain. An epoch is the time that the leading validator is still valid to produce blocks of transactions. You can see the data on the current and past epochs in the Solana Explorer here.
At the time of writing, an epoch lasts roughly 2 days. Much like in real life, if an account has a zero balance and is unable to pay the rent fee, it is removed from the blockchain.
An account can become rent-exempted by holding at least 2 years worth of rent in its token balance. An easy way to estimate rent costs is by using the solana rent command via the Solana CLI. By providing the size (in bytes) of your account, you will see the rent per byte, per epoch, and the minimum amount for the account to be rent-exempted:
How to Create an Account
To create an account on Solana, a key pair (public and private) needs to be generated by a client. Then, the client uses the SystemProgram::CreateAccount call to register the public key and allocate the data storage size needed to be set for this account. Currently, this size can not be changed later, and the size limit is 10 MB. If more size is required, a program can copy the data from one account to another account that has a larger capacity.
When creating an account, an owner needs to be assigned to the account. Only the owner of the account can modify the data that is stored within the account. The default owner after an account is made is called the “System Program”. The System Program is a native program on Solana that is responsible for creating accounts, allocating data on accounts, and assigning ownership of accounts to the connected programs. Native programs are programs that are required to be run by all validators on Solana.
The System Program is also responsible for lamport transfers for the accounts it is assigned owner of. If a user creates an account to use to store a token balance, transfers of that token are handled by the System Program. The user would sign the transfer instructions with their private key and the System Program would handle the deduction of tokens from the sender and credit the receiver account.
Interacting with Accounts
Since the program code and data stored by the program are separate accounts, any program can read the data of another account. Any program can also add lamports to an account but only the owner can subtract them. This is useful when building programs that might need to interact with accounts that it does not own.
When reading an account, you will see this data returned:
Here's a breakdown of that data:
- Public Key - the public key assigned to this account
- Balance - the amount of SOL owned by this account
- Owner - The address of the program that has ownership of this account
- Executable - If this is an executable account or not
- rent_epoch - The next epoch in which this account will pay rent
- Length - The size of the account
Creating and interacting with accounts is how anything gets done on Solana. The separation of programs and the data they use is a unique approach to code and state management. Understanding the ownership rules and how programs work within those rules is important to be successful in developing on Solana. To put these concepts into practice, check out some of our other Solana tutorials.
Subscribe to our newsletter for more articles and guides. Feel free to reach out to us via Twitter if you have any feedback. You can always chat with us on our Discord community server, featuring some of the coolest developers you'll ever meet :)