5 min read
Overview
Rent is a mechanism on the Solana blockchain that ensures efficient usage of the blockchain's resources. It requires accounts to maintain a minimum balance proportional to the amount of data they store on the network. A transaction that would leave an account below this minimum fails. You can think of it as similar to a bank account. Many bank accounts require a minimum balance. If a withdrawal would take your balance below the required minimum, the bank refuses it.
Solana rent is like the minimum balance requirement, ensuring that accounts on the network have enough lamports (a lamport is one one-billionth of a SOL) to cover network storage costs. An account can only drop below the rent-exempt threshold by being closed to zero lamports, which removes it from the network.
Rent is refundable. If an account is closed (removed from the network), the data is deleted from the chain and the rent is refunded to the account owner (or other defined account).
The Solana protocol requires this minimum balance to:
- Encourage efficient use of storage space, since storing more data locks up more SOL.
- Make account owners bear the cost of the state that validators keep for them.
Because rent is necessary to create new accounts, you must know the rent-exempt threshold before creating an account. This is so you can seed the account with enough lamports to cover the minimum. In this guide, you will learn three ways to calculate an account's rent-exempt threshold.
What You Will Do
In this guide, you will calculate the rent exempt threshold for an account using three approaches:
- Solana CLI
- Solana Web3.js library
- Anchor's space constraint
What You Will Need
Please make sure you have installed the required dependencies before proceeding:
- Basic knowledge of Solana Fundamentals
- Solana CLI latest version installed
- Node.js (version 24 or higher) installed
- Basic JavaScript experience
- A modern web browser (e.g., Google Chrome)
- Experience with Anchor will be helpful for one section in this guide, but it is not required
How to Calculate Rent-Exempt Threshold
The rent-exempt threshold is calculated using the size of the account in bytes. For these examples, we will assume a simple account with 16 bytes of data, though you can replace this value with whatever account size you would like to scope.
Method 1 - Solana CLI
The easiest way to calculate the rent-exempt threshold is to use the Solana CLI. You can use the solana rent command to calculate the rent-exempt threshold for a given account size. In your terminal, run the following command:
solana rent 16
This will calculate the rent-exempt threshold for an account with 16 bytes of data. The output will look like this:
Rent-exempt minimum: 0.00100224 SOL
This means an account with 16 bytes of data must hold at least 0.00100224 SOL. You can modify this query to calculate the rent-exempt threshold for any account size.
Method 2 - Solana Web3.js Library
To calculate rent using the Solana-Web3.js, use the getMinimumBalanceForRentExemption method. This method takes the account size in bytes as a parameter and returns the rent-exempt threshold in lamports. Let's create a new Node.js project and install the Solana-Web3.js library:
mkdir solana-rent
cd solana-rent
npm init -y # or yarn init -y
npm install @solana/web3.js@3 # or yarn add @solana/web3.js@3
echo > index.js
This will create a new Node.js project and install the Solana-Web3.js library. Next, let's add the following code to index.js:
const { Connection, clusterApiUrl, LAMPORTS_PER_SOL } = require('@solana/web3.js');
const connection = new Connection(clusterApiUrl('mainnet-beta'), 'finalized');
(async () => {
const dataSize = 16; // Replace with the desired account size in bytes
const minBalance = await connection.getMinimumBalanceForRentExemption(dataSize);
console.log(`Rent-exempt minimum: ${Number(minBalance) / LAMPORTS_PER_SOL} SOL`);
})();
We are establishing a connection to the Solana mainnet cluster here, then calling the getMinimumBalanceForRentExemption method. This method takes the account size in bytes as a parameter and returns the rent-exempt threshold in lamports. The result is a bigint in web3.js v3, so we convert it with Number(), then divide by LAMPORTS_PER_SOL to convert the lamports to SOL and log the result to the console.
In your terminal, run the following command to execute the script:
node index.js
The output should look like this:
Rent-exempt minimum: 0.00100224 SOL
Nice job!
Method 3 - Space Constraint in Anchor
Using the Anchor framework, you can use the space constraint to automatically calculate the rent-exempt threshold required for your new account. The space constraint is used to specify the account size in bytes.
The sample Hello World program on Solana Playground is a good example of how to use the space constraint.
The entire program is linked above if you would like to run it, but let's focus on the Initialize struct:
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(init, payer = signer, space = 8 + 8)]
pub new_account: Account<'info, NewAccount>,
#[account(mut)]
pub signer: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[account]
pub struct NewAccount {
data: u64
}
The Initialize struct defines the context of the initialize function. It contains the necessary account and program references required for the function to execute successfully. The space constraint is applied to the new_account field within the Initialize struct to specify the size of the new NewAccount in bytes.
The NewAccount struct represents the state of the new account being created. In this case, it has a single data field of type u64, which occupies 8 bytes of storage. The space constraint in the Initialize struct allocates additional space for the account discriminator (8 bytes), resulting in a total account size of 16 bytes.
When the Initialize context is passed into the initialize function, Anchor automatically ensures that the new_account account is initialized with enough lamports to meet the rent-exempt threshold for an account with 16 bytes of data. This amount is paid by the signer of the transaction.
Wrap Up
Awesome work! Now that you understand how rent works on Solana, you can start building your own programs and dApps! Check out our other guides to keep learning:
- How to Mint an NFT on Solana
- How to Write your First Solana Program in Anchor
- How to Create a Fungible Token on Solana
We would love to hear more about what you are building. Drop us a line in Discord, or give us a follow on Twitter to stay up to date on all the latest information!
