Categories
Cryptocurrency Ethereum Mastering Development

How to safely manage WEI units?

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.

BigNumer: 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.

Leave a Reply

Your email address will not be published. Required fields are marked *