Getting toWei function using window.ethereum

I am simply trying to access a toWei utils function on a front end website in javascript.

"Since we removed our window.web3, MetaMask no longer automatically reloads the page on chain/network changes."

So I am using window.ethereum which has been fine so far. But I can’t find, even with considerable googling, a direct example of using window.ethereum to access a .toWei(..) function.

For example, I’d want something like:

var weiVal = window.ethereum.toWei(String(eth_float), 'ether');

But this particular function call doesn’t exist. Can someone quickly show me how to do this? Thanks!

>Solution :

The toWei() function was a part of the removed web3 module, and is not part of the MetaMask browser extension anymore.

Currently 0 results in code:

However, you can import the web3 JS package to your frontend and use the function from here.

<script src=""></script>
    const web3 = new Web3();
    const weiAmount = web3.utils.toWei("1", "ether");

Source of the minified package: the official web3.js repo –

Leave a Reply