eth_getBalance
Returns the ETH balance of a given address on Optimism Layer 2.
When to Use This Method
eth_getBalance
is essential for:
- Wallet Applications - Display user balances on Optimism
- Transaction Validation - Check if account has sufficient funds for L2 operations
- DeFi Applications - Monitor collateral and liquidity on Optimism
- Account Monitoring - Track balance changes over time
Parameters
-
Address -
DATA
, 20 bytes- The address to check balance for
- Format:
0x
prefixed, 40 hex characters
-
Block Parameter -
QUANTITY|TAG
"latest"
- Most recent block (default)"earliest"
- Genesis block"pending"
- Pending state"safe"
- Latest safe block"finalized"
- Latest finalized block- Block number in hex format
{
"jsonrpc": "2.0",
"method": "eth_getBalance",
"params": [
"0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
"latest"
],
"id": 1
}
Returns
QUANTITY
- Integer of the current balance in wei.
- Type: Hexadecimal string
- Unit: Wei (1 ETH = 10^18 wei)
- Format:
0x
prefixed
Implementation Examples
- cURL
- JavaScript
curl -X POST https://api-optimism-mainnet-archive.n.dwellir.com/YOUR_API_KEY \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_getBalance",
"params": [
"0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
"latest"
],
"id": 1
}'
// Using ethers.js
import { JsonRpcProvider, formatEther } from 'ethers';
const provider = new JsonRpcProvider('https://api-optimism-mainnet-archive.n.dwellir.com/YOUR_API_KEY');
async function getBalance(address) {
// Get balance in wei
const balanceWei = await provider.getBalance(address);
// Convert to ETH
const balanceEth = formatEther(balanceWei);
console.log(`Optimism Balance: ${balanceEth} ETH`);
return balanceEth;
}
// Check multiple balances on Optimism
async function getMultipleBalances(addresses) {
const balances = await Promise.all(
addresses.map(async (addr) => {
const balance = await provider.getBalance(addr);
return {
address: addr,
balance: formatEther(balance)
};
})
);
return balances;
}
// Get balance with L2 cost consideration
async function getBalanceWithL2Context(address) {
const balance = await provider.getBalance(address);
const gasPrice = await provider.getFeeData();
return {
balance: formatEther(balance),
canAffordTx: balance > gasPrice.gasPrice * 21000n, // Basic tx cost
formatted: `${formatEther(balance)} ETH on Optimism`
};
}
Response Example
Successful Response
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x1a055690d9db80000"
}
This represents 0x1a055690d9db80000
wei = 1.85 ETH on Optimism L2.
Error Response
{
"jsonrpc": "2.0",
"id": 1,
"error": {
"code": -32602,
"message": "Invalid params: invalid address"
}
}
Optimism L2 Considerations
Gas Cost Advantages
On Optimism, balance checks are significantly cheaper than Ethereum mainnet:
async function compareGasCosts() {
// Optimism balance check
const optimismGas = await provider.estimateGas({
to: "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
data: "0x" // Simple balance check call
});
console.log('Optimism gas cost: ~1/10th of Ethereum mainnet');
console.log('Perfect for frequent balance monitoring');
}
L2-Specific Balance Monitoring
class OptimismBalanceMonitor {
constructor(provider, address) {
this.provider = provider;
this.address = address;
this.lastBalance = null;
}
async start(callback, interval = 1000) { // Faster polling on L2
setInterval(async () => {
const currentBalance = await this.provider.getBalance(this.address);
if (this.lastBalance && currentBalance !== this.lastBalance) {
const change = currentBalance - this.lastBalance;
callback({
address: this.address,
network: 'Optimism',
chainId: 10,
previousBalance: this.lastBalance,
currentBalance,
change,
changeFormatted: formatEther(change)
});
}
this.lastBalance = currentBalance;
}, interval);
}
}
Common Use Cases on Optimism
1. DeFi Balance Tracking
async function trackDeFiBalances(userAddress) {
const provider = new JsonRpcProvider('https://api-optimism-mainnet-archive.n.dwellir.com/YOUR_API_KEY');
// Get ETH balance
const ethBalance = await provider.getBalance(userAddress);
// Get gas price for transaction cost estimation
const feeData = await provider.getFeeData();
return {
address: userAddress,
network: 'Optimism',
ethBalance: formatEther(ethBalance),
estimatedTxCost: formatEther(feeData.gasPrice * 21000n),
canAffordBasicTx: ethBalance > feeData.gasPrice * 21000n
};
}
2. L2 Bridge Balance Verification
async function verifyBridgeBalance(l2Address) {
const balance = await provider.getBalance(l2Address);
const blockNumber = await provider.getBlockNumber();
return {
address: l2Address,
balance: formatEther(balance),
blockNumber,
timestamp: Date.now(),
network: 'Optimism L2',
verified: true
};
}
Need help? Contact our support team or check the Optimism documentation.