Loading...
Hello Folks,
So in this session we will be discussing about How to convert one ether unit to other using web3 from nodejs.
To do that we need to have web3 Node module installed on our system.
We can do this by running the following code on the cmd
npm install web3
Then let's run our node server. Just run the following code on cmd.
node
Then we need web3 module in our server so run the following code
var Web3 = require('web3')
we will be needing a Blockchain network to create an account so get an rpc url from here
Just click on the link -> Login to your account ->Click on ethereum on left side bar ->create project ->settings->keys->endpoints
and choose your respective testnet rpc url
rpc url it will look like this - https://mainnet.infura.io/v3/your_api_key
var url = 'your RPC url here'
Then run this one
var web3 = new Web3(url)
For testing purpose we have chosen an address from ethereum mainnet and we look at it's balance to convert it's units to other ether units
let's use this to get mainnet transactions of an address
and finally run this code
var address = '0x00000000219ab540356cBB839Cbe05303d7705Fa' //put an address here
Let's find the balance of this address
web3.eth.getBalance(address,(err,bal)=>{balance=bal})
Output will be like this
Promise { <pending> }
Then type the variable "balance" which stores the balance in callback function to see the balance
balance
output will be in wei and will look like this
'6266626000069000000000069'
let's convert it from wei to ether
web3.utils.fromWei(balance,'ether')
output will be in ether and will look like this
'6266626.000069000000000069'
let's convert it from wei to gwei
web3.utils.fromWei(balance,'gwei')
output will be in ether and will look like this
'6266626000069000.000000069'
and so on......
For test ethers and link tokens. Click Here
For many more exciting tutorials keep following my username @BlockTalks_Raj
!!!!!HAPPY LEARNING!!!!