-
Notifications
You must be signed in to change notification settings - Fork 68
Get Balance
A simple script that defines an address, gets the balance of it and then converts it to Ether before showing the result in the console.
For this example we're going to use the script located here.
var Web3 = require('web3');
web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/YOUR-API-TOKEN-HERE'));
Please read the Basic Required Setup page for this first bit, you'll see it a lot.
console.log('Getting Ethereum address info.....');
var addr = ('ETHEREUM-ADDRESS-HERE');
console.log('Address:', addr);
Now the first console.log
isn't really necessary, but it looks nice in the console. Following that we define a variable addr
as the Ethereum address. Now you don't need to do this, but should it makes the code later on a little easier to read.
Plus I was told it was good practice to write easily editable code, and while this isn't exactly a trading engine, it makes life easier for noobs like myself. Following that we just write the address in the console with the text "Address" beside it. Again not required, but it makes it look pretty.
web3.eth.getBalance(addr, function (error, result) {
if (!error)
console.log('Ether:', web3.utils.fromWei(result,'ether'));
else
console.log('Huston we have a promblem: ', error);
});
Now the fun begins... We tell web3 to get the balance via getBalance
of the address and then return either an error or a result. The result by default will be in Wei, a 19 digit number. This is the lowest and most precise way to measure Ethereums token of value. To convert it into the more common denomination everyone likes (aka Ether), we can use fromWei
and then specify what we want back, in this case its 'ether'
. Once that's all said and done we write it to the console and call it a day.
And that's it. If you run the script you should get something like this:
Address: YOUR-ADDRESS-HERE
Ether: X.xxxxxxxxxxxxxxxxxx
That long decimal isn't exactly pretty, try shortening it! Or if you're feeling really adventurous you can try to use another API to get the current fiat rate of Ether and multiply the balance and that will give you the dollar value of the address!