DEV Community

Discussion on: Solidity Basics for JavaScript Devs Part 2

Collapse
 
nedgar profile image
Nick Edgar • Edited

Yes, the BigNumber / BigInt thing is a bit of a pain, particularly when dealing with async results, as one often has to with Ethers.js. e.g. const supply = (await contract.totalSupply()).toBigInt(). It's pretty tedious.

I suggest either converting to BigInt early (for more modern JS) in the rest of the code, or to mostly ignore BigInt and just stick with BigNumber, like some dev from 2 years ago lol.

Adapting your example above to first approach:

const result = value1.toBigInt() + value2.toBigInt();
console.log(result);
Enter fullscreen mode Exit fullscreen mode

Fortunately, Ethers.js now considers BigInt to be "BigNumberIsh", so it accepts BigInts as arguments for uint etc.

Collapse
 
nedgar profile image
Nick Edgar • Edited

First snippet there could also be done as:
const supply = await contract.totalSupply().then(BigInt)
or
const supply = BigInt(await contract.totalSupply()).