Skip to content

Instantly share code, notes, and snippets.

@duanescarlett
Created March 31, 2022 15:13
Show Gist options
  • Save duanescarlett/6d2e7285efe628724f15ae32df530f40 to your computer and use it in GitHub Desktop.
Save duanescarlett/6d2e7285efe628724f15ae32df530f40 to your computer and use it in GitHub Desktop.
This converts from decimal to wei then from wei to decimal
const Web3 = require('web3')
require('dotenv').config()
const main = async () => {
// Connect to a EVM blockchain
const web3 = new Web3(Web3.givenProvider || process.env.INFURA_URL)
// Convert from a decimal number to a wei number
let amt = web3.utils.toWei('0.1', 'ether')
console.log('Convert from a decimal number to a wei number', amt)
// Convert from a wei number to a decimal number
amt = web3.utils.fromWei(amt, 'ether')
console.log('Convert from a wei number to a decimal number', amt)
}
main()
.then(() => process.exit(0))
.catch((err) => {
console.error(err)
process.exit(1)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment