current location:Home >> Blockchain knowledge >> What is rent on Solana and how to calculate rent

What is rent on Solana and how to calculate rent

admin Blockchain knowledge 1401

Overview  

What is rent on Solana and how to calculate rent

Rent is a mechanism on the Solana blockchain designed to ensure efficient resource usage. It requires accounts to maintain a minimum balance proportional to the amount of data they store on the network. Accounts failing to meet this balance may be purged from the ledger to free up storage. Think of it like a bank account: many banks charge fees or even close accounts if the balance falls below a required minimum. Similarly, Solana’s rent acts as a minimum balance requirement, ensuring accounts hold enough lamports (1 lamport = 1/1,000,000,000 SOL) to cover storage costs.  


If an account’s balance drops below the rent-exempt threshold, it risks removal from the network. Rent is refundable: when an account is closed (purged), its on-chain data is deleted, and the rent is returned to the owner (or a designated account).  


Note: 

Currently, rent is not dynamically deducted, meaning accounts cannot fall below the rent-exempt threshold. All accounts must maintain the minimum rent-exempt amount upfront.  


Why Rent Exists  

Solana charges rent to:  

1. Encourage efficient storage usage by removing inactive accounts.  

2. Compensate validators for the resources used to store account data.  


Since rent is mandatory for creating new accounts, you must know the rent-exempt threshold beforehand to fund the account adequately.  


What You’ll Do  

In this guide, you’ll learn three methods to calculate the rent-exempt threshold for an account:  

1. Solana CLI 

2. Solana Web3.js Library 

3. Anchor’s `space` Constraint


Prerequisites  

Before proceeding, ensure you have:  

- Basic knowledge of Solana fundamentals.  

- The latest Solana CLI installed.  

- Node.js (v16.15 or higher).  

- Foundational JavaScript experience.  

- A modern browser (e.g., Google Chrome).  

- (Optional) Anchor experience for the third method.  


How to Calculate the Rent-Exempt Threshold

The threshold is calculated based on an account’s byte size. In these examples, we’ll assume a simple 16-byte account, but you can substitute any size.  


Method 1: Solana CLI 

The simplest way is using the `solana rent` command:  

bash

solana rent 16

Output:

Rent-exempt minimum: 0.00100224 SOL

 

This means a 16-byte account must hold at least 0.00100224 SOL to avoid purging. Replace `16` with your desired account size.  


Method 2: Solana Web3.js Library

Use the `getMinimumBalanceForRentExemption` method to fetch the threshold in lamports.  


Step 1: Set Up a Node.js Project  

```bash

mkdir solana-rent

cd solana-rent

npm init -y

npm install @solana/web3.js@1

echo > index.js


Step 2: Add the Script

javascript

const { Connection, clusterApiUrl, LAMPORTS_PER_SOL } = require('@solana/web3.js');

const connection = new Connection(clusterApiUrl('mainnet-beta'), 'confirmed');


(async () => {

    const dataSize = 16; // Replace with your account size

    const minBalance = await connection.getMinimumBalanceForRentExemption(dataSize);

    console.log(`Rent-exempt minimum: ${minBalance / LAMPORTS_PER_SOL} SOL`);

})();


Step 3: Run the Script

bash

node index.js 

Output:

Rent-exempt minimum: 0.00100224 SOL



Method 3: Anchor’s `space` Constraint  

Anchor automatically calculates rent when you specify an account’s size using the `space` constraint.  


Example: Hello World Program 

Here’s how `space` works in an Anchor program:  

```rust

[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

}


Key Points:

- space = 8 + 8:  

  - 8 bytes for the account discriminator (Anchor’s internal identifier).  

  - 8 bytes for the `u64` field (`data`).  

- Anchor ensures the `new_account` is funded with enough lamports to meet the 16-byte rent-exempt threshold.  

- The signer pays the rent during account creation.  


Summary

| Method                  | Use Case                          | Command/Code Example                  |

|-------------------------|-----------------------------------|---------------------------------------|

| Solana CLI          | Quick calculations                | solana rent <bytes>                |

| Web3.js             | Programmatic queries              | getMinimumBalanceForRentExemption() |

| Anchor space      | Automated in Anchor programs      | [account(..., space = x)]         |  


Choose based on your needs:  

- CLI for quick checks.  

- Web3.js for apps.  

- Anchor for Solana programs.

If you have any questions or uncertainties, please join the official Telegram group: https://t.me/GToken_EN

GTokenTool

GTokenTool is the most comprehensive one click coin issuance tool, supporting multiple public chains such as TON, SOL, BSC, etc. Function: Create tokensmarket value managementbatch airdropstoken pre-sales IDO、 Lockpledge mining, etc. Provide a visual interface that allows users to quickly create, deploy, and manage their own cryptocurrencies without writing code.

Similar recommendations