How to Convert Ethereum Units (Wei, Gwei, Ether) in JavaScript

Ethereum transactions don’t deal in whole numbers. If you’ve ever tried sending Ether, you’ve probably seen a ridiculously long decimal or a number with way too many zeroes. That’s because Ethereum operates on Wei, its smallest unit, instead of whole Ether. And here’s something most beginners don’t realize—Ethereum gas fees aren’t paid in Ether either. They’re paid in Gwei.

If that sounds confusing, don’t worry. Once you understand how these units work and how to convert them in JavaScript, everything clicks. This guide will show you exactly how to do that with clean, simple code.

Understanding Ethereum Units Before Jumping Into Code

Ethereum’s native unit is Wei. Ether is just a large amount of Wei. Instead of floating-point numbers (which can cause precision errors in computing), Ethereum uses integers, making unit conversion necessary.

Here’s how the units relate:

  • 1 Ether (ETH) = 1,000,000,000 Gwei
  • 1 Gwei = 1,000,000,000 Wei
  • 1 Ether = 1,000,000,000,000,000,000 Wei

Most blockchain applications work with Wei under the hood. But humans don’t think in numbers that long, so conversions are a must.

Converting Ethereum Units in JavaScript

Ethereum has an official JavaScript library called ethers.js, which makes unit conversion incredibly easy. You could write your own conversion functions, but unless you love unnecessary math, stick with ethers.js. It saves time and avoids precision errors.

Installing ethers.js

Before running any conversions, install the ethers.js library if you haven’t already.

npm install ethers

Now, let’s go through conversions one by one.

Converting Ether to Wei

To convert Ether into Wei, use parseUnits:

const { ethers } = require("ethers");

const etherToWei = ethers.parseUnits("1", "ether");
console.log(etherToWei.toString()); // Outputs: 1000000000000000000

This takes "1" Ether and converts it to Wei. The .toString() is added because ethers.js returns a BigInt, which JavaScript doesn’t handle well in console logs.

Converting Wei to Ether

Reversing the process is just as simple with formatUnits:

const weiToEther = ethers.formatUnits("1000000000000000000", "ether");
console.log(weiToEther); // Outputs: 1.0

No need for .toString() here since formatUnits returns a string.

Converting Ether to Gwei

For gas fees, Gwei is the standard. Converting Ether to Gwei is almost the same as converting to Wei—just change the unit.

const etherToGwei = ethers.parseUnits("1", "gwei");
console.log(etherToGwei.toString()); // Outputs: 1000000000

Converting Gwei to Ether

If you need to go from Gwei back to Ether:

const gweiToEther = ethers.formatUnits("1000000000", "gwei");
console.log(gweiToEther); // Outputs: 1.0

These functions keep your calculations precise and your code clean.

Using JavaScript Without Libraries

If you can’t (or don’t want to) use ethers.js, you can still do these conversions manually using BigInt. But be warned—this can get messy fast.

const weiToEtherManual = (wei) => (BigInt(wei) / BigInt(1e18)).toString();
console.log(weiToEtherManual("1000000000000000000")); // Outputs: 1

And for Ether to Wei:

const etherToWeiManual = (ether) => (BigInt(ether) * BigInt(1e18)).toString();
console.log(etherToWeiManual("1")); // Outputs: 1000000000000000000

This works, but ethers.js handles BigInt and precision more smoothly. Unless you’re coding for extreme performance, stick to the library.

Handling Gas Fees in Ethereum Transactions

Gas fees are always calculated in Gwei. If you’re working with transaction fees, you’ll frequently see values in Gwei, but your wallet or contract might require Wei.

Here’s how to convert gas prices:

Convert Gwei to Wei for a Transaction

If you need to send a transaction, you’ll need to specify gas fees in Wei.

const gasPriceInWei = ethers.parseUnits("30", "gwei");
console.log(gasPriceInWei.toString()); // Outputs: 30000000000

This means 30 Gwei in Wei format.

Convert Gas Fee from Wei Back to Gwei

Let’s say a transaction returned a gas cost in Wei, but you need it in Gwei.

const gasPriceInGwei = ethers.formatUnits("30000000000", "gwei");
console.log(gasPriceInGwei); // Outputs: 30.0

This is useful when displaying transaction costs in a readable format.

Working With Smart Contracts

Smart contracts typically expect values in Wei. If you’re interacting with a contract function that takes Ether as a parameter, convert it to Wei before passing it.

Example:

const contractFunction = async (contract, amountInEther) => {
  const amountInWei = ethers.parseUnits(amountInEther, "ether");
  await contract.someFunction(amountInWei);
};

This ensures the contract receives the correct value.

Avoiding Common Mistakes

Even with simple conversions, developers often run into pitfalls. Here’s what to watch out for:

  • Forgetting BigInt in manual conversions: JavaScript number type can’t handle big numbers. Always use BigInt when not using a library.
  • Not converting gas fees: Some wallets expect gas fees in Gwei, others in Wei. Always check the format before sending transactions.
  • Missing .toString(): ethers.js functions often return BigInt, which can break logs and JSON responses. Convert it to a string when needed.
  • Confusing Ether and Gwei: Gas fees use Gwei, not Ether. Sending values in the wrong unit can result in overpaying or failed transactions.

Final Thoughts

Ethereum unit conversions can seem complicated at first, but once you understand Wei, Gwei, and Ether, it’s all about using the right functions. The ethers.js library makes it easy, and even manual conversions are manageable with BigInt.

The key takeaway? Always check the unit before sending a transaction. Mixing up Wei and Gwei can lead to costly mistakes. Use these conversions in your JavaScript projects, and you’ll never have to second-guess your Ethereum calculations again.

Leave a Reply

Your email address will not be published. Required fields are marked *