michielpost / MetaMask.Blazor

Use MetaMask with Blazor WebAssembly
MIT License
44 stars 21 forks source link

`GetBalance` is limited to long. #15

Closed Stitch-Lunar closed 2 years ago

Stitch-Lunar commented 2 years ago

We return the type long from the method GetBalance.

This means that we can't return a balance larger than 9223372036854775807 Wei which is equivalent to just above 9 Ether.

We should consider using a type that can hold larger numbers. There are some different options.

ulong

ulong can hold equivalent to just above 18 Ether which is better, but still just pushes the problem a tiny bit.

decimal

decimal can hold whole numbers equivalent to around 79228000000 Ether which is much better. The drawback is that decimal can also hold numbers that are not whole which might confuse people who use the project. decimal was created specifically for work with currencies, but the high precision in cryptocurrencies might not be suitable for this.

BigInteger

BigInteger can hold arbitrarily big whole numbers. The problem with BigInteger is that it does not play as nice with other types like decimal, float, etc. as it doesn't do implicit conversions when used in common operations like multiply, divide, add, subtract, etc.

Conclusion

I think the best option is BigInteger. Other libraries that work with cryptocurrencies also use this.

If you would like this change, then I can make a PR.

michielpost commented 2 years ago

Good find and yes, I agree with your conclusion. BigInteger seems like the best option. A PR is welcome of course, thanks!