11

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?

Zaid Amir
  • 312
  • 1
  • 3
  • 15

6 Answers6

20

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

Jitendra Kumar. Balla
  • 2,154
  • 1
  • 8
  • 15
  • 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 Feb 15 '18 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 Feb 09 '19 at 19:29
  • TypeError: web3.toDecimal is not a function – Anonymous Sep 27 '21 at 08:43
2

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

Russo
  • 1,784
  • 1
  • 21
  • 30
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';
user19510
  • 27,999
  • 2
  • 30
  • 48
  • hi I'm trying to connect to Infura services. But it is not working can you please look into https://ethereum.stackexchange.com/questions/57524/cannot-connect-to-any-ethereum-networks-using-api-key – e.k Aug 28 '18 at 09:54
0

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

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
ozgrozer
  • 101
  • 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.
Ank_247shbm
  • 163
  • 1
  • 7