Parameters

dataLength
number

The length of the account data in bytes

config
object

Response

result
number

The minimum balance required for rent exemption in lamports

Code Examples

Basic Request

curl https://rpc.orbitflare.com -X POST -H "Content-Type: application/json" -d '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "getMinimumBalanceForRentExemption",
  "params": [1000]
}'

Using web3.js

import { Connection } from '@solana/web3.js';

const connection = new Connection('https://rpc.orbitflare.com');

// Get minimum balance for rent exemption
const dataLength = 1000; // bytes
const minBalance = await connection.getMinimumBalanceForRentExemption(dataLength);
console.log('Minimum balance:', minBalance);

// Calculate account creation cost
async function calculateAccountCost(
  dataLength: number,
  owner: string
) {
  const minBalance = await connection.getMinimumBalanceForRentExemption(dataLength);
  const rentExempt = minBalance;
  
  return {
    dataLength,
    minBalance,
    rentExempt,
    owner
  };
}

// Check if account has sufficient balance
async function hasSufficientBalance(
  account: string,
  dataLength: number
) {
  const [balance, minBalance] = await Promise.all([
    connection.getBalance(account),
    connection.getMinimumBalanceForRentExemption(dataLength)
  ]);
  
  return {
    account,
    currentBalance: balance,
    requiredBalance: minBalance,
    hasSufficientBalance: balance >= minBalance,
    difference: balance - minBalance
  };
}

Notes

  1. Returns the minimum balance required for an account to be rent exempt
  2. The balance is returned in lamports
  3. The required balance depends on the account’s data length
  4. The response is immediate as it reads from the current state
  5. This value can change with network upgrades

Best Practices

  1. Use this method to determine account creation costs
  2. Cache results when appropriate to reduce RPC load
  3. Consider the impact of data length on costs
  4. Monitor for changes in rent exemption requirements
  5. Use in conjunction with other account-related methods

Common Errors

CodeMessageSolution
-32601Method not foundVerify you’re connected to a Solana RPC node
-32602Invalid paramsCheck dataLength parameter
-32007Balance information unavailableNode may be bootstrapping or syncing

Use Cases

  1. Account Cost Analysis

    interface AccountCost {
      dataLength: number;
      minBalance: number;
      rentExempt: number;
      owner: string;
      metadata: {
        timestamp: number;
        commitment: string;
      };
    }
    
    async function analyzeAccountCost(
      dataLength: number,
      owner: string,
      commitment: string = 'confirmed'
    ): Promise<AccountCost> {
      const minBalance = await connection.getMinimumBalanceForRentExemption(
        dataLength,
        { commitment }
      );
      
      return {
        dataLength,
        minBalance,
        rentExempt: minBalance,
        owner,
        metadata: {
          timestamp: Date.now(),
          commitment
        }
      };
    }
    
  2. Balance Monitoring

    interface BalanceAlert {
      type: 'insufficient' | 'warning' | 'sufficient';
      message: string;
      account: string;
      currentBalance: number;
      requiredBalance: number;
      difference: number;
    }
    
    class BalanceMonitor {
      private previousBalances: Map<string, number> = new Map();
      
      async monitorBalance(
        account: string,
        dataLength: number,
        warningThreshold: number = 0.1 // 10% above minimum
      ): Promise<BalanceAlert[]> {
        const [balance, minBalance] = await Promise.all([
          connection.getBalance(account),
          connection.getMinimumBalanceForRentExemption(dataLength)
        ]);
        
        const alerts: BalanceAlert[] = [];
        const difference = balance - minBalance;
        const warningLevel = minBalance * warningThreshold;
        
        if (difference < 0) {
          alerts.push({
            type: 'insufficient',
            message: `Account ${account} has insufficient balance for rent exemption`,
            account,
            currentBalance: balance,
            requiredBalance: minBalance,
            difference
          });
        } else if (difference < warningLevel) {
          alerts.push({
            type: 'warning',
            message: `Account ${account} balance is close to minimum required`,
            account,
            currentBalance: balance,
            requiredBalance: minBalance,
            difference
          });
        } else {
          alerts.push({
            type: 'sufficient',
            message: `Account ${account} has sufficient balance`,
            account,
            currentBalance: balance,
            requiredBalance: minBalance,
            difference
          });
        }
        
        this.previousBalances.set(account, balance);
        return alerts;
      }
    }
    
  3. Account Planning

    interface AccountPlan {
      accounts: Array<{
        purpose: string;
        dataLength: number;
        minBalance: number;
        estimatedUsage: string;
      }>;
      totalCost: number;
      recommendations: string[];
    }
    
    async function planAccounts(
      accountSpecs: Array<{
        purpose: string;
        dataLength: number;
        estimatedUsage: string;
      }>
    ): Promise<AccountPlan> {
      const accounts = await Promise.all(
        accountSpecs.map(async spec => {
          const minBalance = await connection.getMinimumBalanceForRentExemption(
            spec.dataLength
          );
          
          return {
            ...spec,
            minBalance
          };
        })
      );
      
      const totalCost = accounts.reduce(
        (sum, account) => sum + account.minBalance,
        0
      );
      
      const recommendations = [];
      
      // Check for potential optimizations
      const largeAccounts = accounts.filter(
        account => account.dataLength > 10000
      );
      if (largeAccounts.length > 0) {
        recommendations.push(
          'Consider optimizing data storage for large accounts'
        );
      }
      
      const expensiveAccounts = accounts.filter(
        account => account.minBalance > 1e9 // More than 1 SOL
      );
      if (expensiveAccounts.length > 0) {
        recommendations.push(
          'Review high-cost accounts for potential optimizations'
        );
      }
      
      return {
        accounts,
        totalCost,
        recommendations
      };
    }