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?
The
parseEther()
function (docs) returns JS native typeBigInt
.BigInt
cannot be used together with another JS native typeNumber
(which is the type of your literal2
in the code).The most straightforward solution is to multiply the
BigInt
returned fromparseEther()
with anotherBigInt
.You can declare a
BigInt
using an
suffix after the literal: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 nativeBigInt
(introduced in Ethers v6).