I'm confused about how to manage/calculate with WEI in a web3 application.
So far I found:
BigInt
: This is JS native and it works with large quantities (e.g.10000000000000000000000
). But it doesn't have out of the box utilities like e.g. converting to hex.BigNumber
: This is provided by ethers:
Many operations in Ethereum operation on numbers which are outside the range of safe values to use in JavaScript. A BigNumber is an object which safely allows mathematic operations on numbers of any magnitude.
Given this description, and that it's dedicated to Ethereum, I thought this must be it, but it actually doesn't work with large numbers:
// Numbers outside the safe range fail:
BigNumber.from(Number.MAX_SAFE_INTEGER);
// Error: overflow (fault="overflow", operation="BigNumber.from", value=9007199254740991, code=NUMERIC_FAULT, version=bignumber/5.0.8)
Aside of that, for WEI I probably need integers, not floating point.
So what's the recommended way to deal with WEI?
In the ideal case I'd like something that not only represent big numbers but actual WEIs and has built in functionality to convert safely to and calculate with GWEI or ETH out of the box, but this is not a must.
Using Typescript, but it probably isn't relevant.
Edit: I found another library: BigNumber.js. This seems to do what I expect. It can be converted to string to interact with web3.js.