How to Get the Balance of an Ethereum Wallet Address in Code

Jared Stauffer

See How to Connect to Ethereum in Code to understand the first part of this how to that creates the connection to the Ethereum blockchain.

  1. Create the connection to the Ethereum Blockchain
  2. Call the web3.eth.getBalance function

Why wrap this in a separate function (getEthBalance)? We do this because the web3 function returns a Javascript promise, and we want to wait for it to execute before we print out the results.

The getBalance function returns the balance in WEI (the smallest measure of ETH). So we use web3.utils to convert the returned balance from WEI to ETH.

NOTE: This code can be used with any of the available Ethereum networks (mainnet, test — Kovan, Rinkeby, etc). To do this just replace the Infura URL with the correct Etheruem network’s URL.

To continue the fun, see the Web3js docs, and extend the code to do more!

If this was helpful, give me some claps and share!

Jared Stauffer

Written by

CTO @ Radpay, Innovator, Blockchain Geek

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade