How to multiply ethers.parseEther() with hardhat?

152 views Asked by At

I have a price object that I'm trying to perform multiplication on

const PRICE_ONE = ethers.parseEther("0.05");
const test = PRICE_ONE * 2;

but I can't get the multiply to work when running npx hardhat test

it gives me the error

TypeError: Cannot mix BigInt and other types, use explicit conversions.

Tried searching for a hardhat example of how to multiply, but couldn't find any on google.

So I threw it into chatgpt and it tells me to do

const test = PRICE_ONE.mul(2);

but that gives the error

TypeError: PRICE_ONE.mul is not a function

How do I multiply PRICE_ONE with 2?

1

There are 1 answers

1
Petr Hejda On BEST ANSWER

The parseEther() function (docs) returns JS native type BigInt. BigInt cannot be used together with another JS native type Number (which is the type of your literal 2 in the code).

The most straightforward solution is to multiply the BigInt returned from parseEther() with another BigInt.

You can declare a BigInt using a n suffix after the literal:

const PRICE_ONE = ethers.parseEther("0.05");

// `test` is now of type `BigInt`
const test = PRICE_ONE * 2n;

The .mul() function that GPT recommended you is used for example in BN.js library (link) that Ethers used in previous versions (up to v5) instead of the native BigInt (introduced in Ethers v6).