Parameters
The length of the account data in bytes
Commitment level (processed, confirmed, finalized)
Response
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
- Returns the minimum balance required for an account to be rent exempt
- The balance is returned in lamports
- The required balance depends on the account’s data length
- The response is immediate as it reads from the current state
- This value can change with network upgrades
Best Practices
- Use this method to determine account creation costs
- Cache results when appropriate to reduce RPC load
- Consider the impact of data length on costs
- Monitor for changes in rent exemption requirements
- Use in conjunction with other account-related methods
Common Errors
Code | Message | Solution |
---|
-32601 | Method not found | Verify you’re connected to a Solana RPC node |
-32602 | Invalid params | Check dataLength parameter |
-32007 | Balance information unavailable | Node may be bootstrapping or syncing |
Use Cases
-
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
}
};
}
-
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;
}
}
-
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
};
}