12

I am trying to write a function to get the ethereum balance of a wallet address via web3js. Problem is I am unable to make it work for some reason. Here is my code running in VSCode:

const testnet = 'https://ropsten.etherscan.io/';
const walletAddress = '0x8690F1feff62008A396B31c2C3f380bD0Ca6d8b8';

const web3 = new Web3(new Web3.providers.HttpProvider(testnet));
var balance = web3.eth.getBalance(walletAddress);

The wallet contains 0.98 ether on ropsten but for some reason its not returning when I call it from my code. What am I doing wrong?

1
  • 1
    getBalance is a promise, it needs "await"
    – WtFudgE
    Commented Jun 14, 2021 at 8:20

6 Answers 6

22

The issue with your testnet url, https://ropsten.etherscan.io/ is not running eth client.

Find below code to connect ropsten node.

const testnet = 'https://ropsten.infura.io/';
const walletAddress = '0x8690F1feff62008A396B31c2C3f380bD0Ca6d8b8';

const web3 = new Web3(new Web3.providers.HttpProvider(testnet));
var balance = web3.eth.getBalance(walletAddress); //Will give value in.
balance = web3.toDecimal(balance);

I am getting value. Now balance will give as wei. Check web3 api method's for the same.

https://github.com/ethereum/wiki/wiki/JavaScript-API#web3towei

3
  • sorry about that, yes I tried infura but I was getting the same issue. I uninstalled web3 which was 1.0.0-beta and installed 0.14.0 and everything is working fine. seems to be an issue with the latest web3js.
    – Zaid Amir
    Commented Feb 15, 2018 at 13:03
  • 5
    that code will no longer work. getBalance() now returns a promise (at least in v1.0.0-beta.43) so you need to do getBalance(...).then(bal => { console.log(bal); }) and incidentally, to convert to decimals one now needs to do web3.utils.toDecimal() and if passing it a balance will make it puke "Error: Number can only safely store up to 53 bits" -- hopeless. after, what? 5 years, how long has ethereum been around? and the most basic functionality is still broken
    – ekkis
    Commented Feb 9, 2019 at 19:29
  • TypeError: web3.toDecimal is not a function
    – Anonymous
    Commented Sep 27, 2021 at 8:43
2

balance = await web3.eth.getBalance(wallet_address);

1
  • 3
    You have to use await in a function. This is not valid syntax. Commented Jan 3, 2020 at 17:11
1

You need to connect to an Ethereum node running on the Ropsten network. https://ropsten.etherscan.io is not such a node.

You can try Infura instead. They provide public Ethereum nodes. You should get an API key, but it works (perhaps with lower rate-limiting?) even without that:

const testnet = 'https://ropsten.infura.io';
1
0

You may go by latest form:

var Web3 = require("web3");
var url = 'https://ropsten.infura.io';
var web3 = new Web3(url);
var address = '0x8690F1feff62008A396B31c2C3f380bD0Ca6d8b8';
var balance = web3.eth.getBalance(address);
var wallet = web3.eth.toWei(balance, 'ether');

Here, I tried to show two changes, i.e,

1. no use of 'new Web3.providers.HttpProviders(url)'
2. conversion of balance into wallet through mapping it to ether.
2
  • downvoted for suggesting an edit to the question to reference your answer
    – Zaid Amir
    Commented Jul 26, 2020 at 10:18
  • @ZaidAmir Why? That works well. Commented Jul 26, 2020 at 10:30
0

Also to show the balance in Ether format use like this. I see a lot of the answers are actually outdated.

const getBalance = await web3.eth.getBalance(walletAddress)
const ethBalance = web3.utils.fromWei(getBalance, 'ether')
console.log(ethBalance)
// 0.98
0

web3.toDecimal(web3.fromWei(await web3.eth.getBalance('wallet-address'))) OR web3.toDecimal(web3.fromWei(await web3.eth.getBalance('wallet-address'),'ether'))

1
  • 2
    It is missing await in front of web3.eth.getBalance, it will fail otherwise.
    – Ismael
    Commented Sep 30, 2021 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.