question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Potential bug when using "FixedNumber.fromValue" with large number of decimals

See original GitHub issue

While working with FixedNumber, I got this error after setting the number of decimals to 77:

fractional component exceeds decimals (fault=“underflow”, operation=“parseFixed”, code=NUMERIC_FAULT, version=bignumber/5.4.2)

Here’s my code:

const foo = "115792089237316195423570985008687907853269984665640564039457584007913129639935";
const decimals = 77;
const result = FixedNumber.fromValue(foo, decimals);
console.log(result.toString());

I then peaked into the guts of the FixedNumber implementation, and noticed that the number of decimals gets overridden to 18 in this function:

https://github.com/ethers-io/ethers.js/blob/4166b2753d0d6b230a3a22b5a2b6c4e1906a7af7/packages/bignumber/src.ts/fixednumber.ts#L342-L364

That is, even if it’s 77 when it enters the fromValue function, it gets set to 18 by the time it is used in fromString.

My request is to document the maximum number of decimals that can be set when working with FixedNumber, plus any other constraints that there might be.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ricmoocommented, Feb 3, 2022

I think there is some confusion as to what the decimals and format mean.

The decimals indicates the number of decimals to assume the input value is operating under. For example, "10" with 0 decimals is 10, while "10" with 2 decimals is 0.1. It only impacts the interpretation of the value.

The format parameter indicates the internal representation used during fixed-point mathematical operations.

So, the default decimals of 0 and the default format of fixed128x18 indicates that whatever value is passed in should be taken literally and that 18 decimal places are used during maths.

So, the value "123" is represented internally as 123.000_000_000_000_000_000 (underscores added for readability).

Using a value "123" with a decimals of 2 and the default format is represented internally as 1.230_000_000_000_000_000.

Does that make sense?

0reactions
ricmoocommented, Feb 4, 2022

Just discussing this with my partner, and this example came up, which might help explain where the logic and names stem from.

const balance = await erc20Contract.getBalance();
const decimals = await erc20Contract.decimals();

const value = FixedNumber.fromValue(balance, decimals);

Hopefully that helps future peeps who happen across this issue too. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: fractional component exceeds decimals in eithersJS
I think it's because your floating point number has too many decimal places to be represented by BigNumber thus, you need to truncate...
Read more >
decimal — Decimal fixed point and floating point arithmetic ...
The decimal module incorporates a notion of significant places so that 1.30 + 1.20 is 2.50 . The trailing zero is kept to...
Read more >
FixedNumber - ethers
A FixedNumber is a fixed-width (in bits) number with an internal base-10 divisor, which allows it to represent a decimal fractional component.
Read more >
Documentation - Ethers.js
A Signer is a class which (usually) in some way directly or indirectly has access to a private key, which can sign messages...
Read more >
Number.prototype.toFixed() - JavaScript - MDN Web Docs
The toFixed() method returns a string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found