Introduction
Searching for Ethereum account balances programmatically is a common task for developers and blockchain enthusiasts. This guide covers various methods to retrieve ETH and token balances from the Ethereum blockchain, including command-line tools, JavaScript APIs, and smart contract interactions.
Checking Balances via Web Interfaces (Non-Programmatic)
For quick balance checks without coding:
- Visit Etherchain or Etherscan
- Enter the wallet address to view its ETH and token holdings
Using Command-Line Tools
JavaScript API Method
Access balance data through Ethereum console tools (Geth/eth/pyeth):
web3.fromWei(eth.getBalance(eth.coinbase));Key Components:
web3: Ethereum JavaScript library (complete web3.js 1.0 manual)eth: Shortcut forweb3.ethin console environmentsFull syntax:
web3.fromWei(web3.eth.getBalance(web3.eth.coinbase));
Advanced Usage Examples
Check multiple accounts:
web3.fromWei(web3.eth.getBalance(web3.eth.accounts[0])); web3.fromWei(web3.eth.getBalance(web3.eth.accounts[1]));Direct address lookup:
web3.fromWei(web3.eth.getBalance('0x2910543af39aba0cd09dbb2d50200b3e800a63d2'));
Balance Check Script
This convenient script displays all account balances:
function checkAllBalances() {
var i = 0;
eth.accounts.forEach(function(e){
console.log("eth.accounts["+i+"]: " + e + " \tbalance: " +
web3.fromWei(eth.getBalance(e), "ether") + " ether");
i++;
})
};
checkAllBalances();Checking Balances Within Smart Contracts
Solidity provides native balance checking capabilities:
Key Features
- Every address has a
.balanceproperty - Returns value in Wei (1 ETH = 10^18 Wei)
Example Contract
contract OwnerBalanceReturner {
address owner;
function OwnerBalanceReturner() public {
owner = msg.sender;
}
function getOwnerBalance() constant returns (uint) {
return owner.balance;
}
}๐ Master Ethereum development with these advanced techniques
FAQ Section
Q1: What's the difference between checking balances via web interfaces vs programmatically?
Web interfaces provide quick, user-friendly access while programmatic methods enable automation and integration with dApps.
Q2: Why does web3.fromWei() convert balance values?
Ethereum stores values in Wei (smallest denomination). fromWei() converts to human-readable Ether units.
Q3: Can I check token balances using these methods?
The basic methods shown check ETH balances. For tokens, you'll need to interact with token contracts using their ABI.
Q4: Is balance information public on Ethereum?
Yes, all ETH balances are publicly visible on the blockchain, though account ownership may be pseudonymous.
Q5: What's the gas cost for balance checks?
Balance queries are read-only operations that don't modify blockchain state, so they're free (require no gas).
๐ Explore more blockchain developer resources
Conclusion
Programmatic balance checking opens doors for:
- Portfolio tracking apps
- Automated payment systems
- Smart contract conditional logic
- Blockchain analytics tools
Remember that while ETH balances are directly accessible, token balances require interaction with specific token contracts. For production applications, consider implementing error handling and connection management for robust performance.