How can I programmatically discover how much ETH is in a given account on the Ethereum blockchain?
Just below your address, you will see your total ETH balance, USD value equivalent, and the total number of different types of tokens you hold. In the first tab, 'Transaction History', you will find a list of all your recent transactions. In the 'ERC20 Tokens' tab, you can find your token balances.
An Ethereum account is an entity with an ether (ETH) balance that can send transactions on Ethereum. Accounts can be user-controlled or deployed as smart contracts.
On the Web:
(Not programmatic, but for completeness...) If you just want to get the balance of an account or contract, you can visit http://etherchain.org or http://etherscan.io.
From the geth, eth, pyeth consoles:
Using the Javascript API, (which is what the geth, eth and pyeth consoles use), you can get the balance of an account with the following:
web3.fromWei(eth.getBalance(eth.coinbase));
"web3" is the Ethereum-compatible Javascript library web3.js.
"eth" is actually a shorthand for "web3.eth" (automatically available in geth). So, really, the above should be written:
web3.fromWei(web3.eth.getBalance(web3.eth.coinbase));
"web3.eth.coinbase" is the default account for your console session. You can plug in other values for it, if you like. All account balances are open in Ethereum. Ex, if you have multiple accounts:
web3.fromWei(web3.eth.getBalance(web3.eth.accounts[0])); web3.fromWei(web3.eth.getBalance(web3.eth.accounts[1])); web3.fromWei(web3.eth.getBalance(web3.eth.accounts[2]));
or
web3.fromWei(web3.eth.getBalance('0x2910543af39aba0cd09dbb2d50200b3e800a63d2'));
EDIT: Here's a handy script for listing the balances of all of your accounts:
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();
Inside Contracts:
Inside contracts, Solidity provides a simple way to get balances. Every address has a .balance property, which returns the value in wei. Sample contract:
contract ownerbalancereturner { address owner; function ownerbalancereturner() public { owner = msg.sender; } function getOwnerBalance() constant returns (uint) { return owner.balance; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With