Skip to main content

What is Rent on Solana and How to Calculate it

Created on
Updated on
Jul 28, 2023

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. Accounts that fail to maintain this minimum balance will be removed from the ledger to free up storage. You can think of it as similar to a bank account. For many accounts, the bank will charge you a fee if you do not maintain a minimum balance. If your balance falls below the required minimum, the bank may charge you a fee or close your account. 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. If an account's balance falls below the rent-exempt threshold, the account may be removed 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 charges rent to account owners to:

  • Encourage efficient use of storage space by removing accounts that are not actively used.
  • Compensate validators for the resources they provide in storing account data.

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:

  1. Solana CLI
  2. Solana Web3.js library
  3. 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 16.15 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 have at least 0.00100224 SOL to avoid the account being removed from the network. 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 # or yarn add @solana/web3.js
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'), 'confirmed');

(async () => {
const dataSize = 16; // Replace with the desired account size in bytes
const minBalance = await connection.getMinimumBalanceForRentExemption(dataSize);
console.log(`Rent-exempt minimum: ${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. We then divide the result 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:

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!

We <3 Feedback!

If you have any feedback on this guide, let us know. We'd love to hear from you.

Share this guide