How do I get the balance of an account in Ethereum

0 votes

How can I programmatically discover how much ETH is in a given account on the Ethereum blockchain?

Nov 15, 2018 in Blockchain by Christine
• 15,790 points
16,766 views

1 answer to this question.

0 votes

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;
    }
}


Hope this helps!

Get your Blockchain certification today to learn more about this technology.

Thanks.

answered Nov 15, 2018 by Perry
• 17,100 points

Related Questions In Blockchain

+1 vote
1 answer

How do i change the names of validating peers in IBM Bluemix blockchain?

IBM Bluemix Blockchain service Hyperledger Fabric v0.6 will ...READ MORE

answered Apr 11, 2018 in Blockchain by Perry
747 views
0 votes
1 answer

How do I get the total amount sent to an address?

If you are using web3JS you can ...READ MORE

answered Jul 19, 2018 in Blockchain by Johnathon
• 9,090 points
701 views
+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,705 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,237 views
0 votes
0 answers

Error: Unexpected token o in JSON at position 1

I have been working with Interacting a ...READ MORE

Mar 6, 2019 in Blockchain by saeedi
• 120 points

edited Mar 6, 2019 by Omkar 2,209 views
0 votes
1 answer

How do I send back ethers to the sender of the tokens in a smart contract?

Whenever a smart contract receives ether via ...READ MORE

answered May 30, 2018 in Blockchain by Perry
• 17,100 points
3,370 views
0 votes
1 answer

How do I withdraw the balance from a contract on Ethereum test blockchain?

Your need to improvise your code a ...READ MORE

answered Aug 3, 2018 in Blockchain by Perry
• 17,100 points
2,454 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP