2

In my hardhat test file I am converting ERC20 tokens transactions/balances with ethers.utils.parseEther like...

const ONE_ETHER = ethers.utils.parseEther("1");

So if everything is being converted why is showing in my console BigNumber for all the values? Is this normally how it would display or am I not converting correctly. Here is screenshot...

enter image description here

2 Answers 2

1

Yes, it is normal for the values to be displayed as a BigNumber in the console, even after converting them with ethers.utils.parseEther.

BigNumber is a data type used by the Ethers.js library to represent large numbers with high precision, which is necessary when dealing with cryptocurrency transactions and balances. When you convert a number using ethers.utils.parseEther, it returns a BigNumber object representing the same value.

Logging this BigNumber object to the console will display it as a BigNumber rather than a regular number. This is because BigNumber objects have properties and methods that differ from regular JavaScript numbers.

If you need to perform any mathematical operations with the converted value, you can use the BigNumber methods provided by the ethers.js library. For example, you can add two converted values together like this:

const balance1 = ethers.utils.parseEther("0.5");
const balance2 = ethers.utils.parseEther("1.5");
const totalBalance = balance1.add(balance2);
console.log(totalBalance.toString());

Chaining the toString() method to a bigNumber will output the total balance as a string, which you can then display or use in your code as needed.

1
  • 1
    Thank you for the great explanation. Appreciate the help.
    – Blockpain
    Commented Mar 27, 2023 at 9:55
0

parseEther converts a string to BigNumber. You can use formatEther to format BigNubmers to strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.