Nonzero values returning as 0x00 using both BigNumber and FixedNumber #3681
Replies: 4 comments 4 replies
-
In this line: const PRECISION = 30;
const price = FixedNumber.fromValue(aReserve1.mul(parseUnits('1', PRECISION)).div(aReserve0), PRECISION)
// or
const priceD30 = aReserve1.mul(parseUnits('1', PRECISION)).div(aReserve0); // with this you need to remember to later divide by 1e30 |
Beta Was this translation helpful? Give feedback.
-
Check out the answer by @zemse. I just wanted to add that the security concerns with BigInt does not affect you. Those concerns are regarding the side-channel attacks possible when running cryptographic signing (for example), since the timing required for various numbers can reveal the value. None of your values are private. :) |
Beta Was this translation helpful? Give feedback.
-
I seem to still be getting 0x00 when using FixedNumber.
Results:
|
Beta Was this translation helpful? Give feedback.
-
Looking more into this, I've come to realize that the problem occurs only when attempting to divide 2 hexadecimal BigNumbers of different levels of precision. I noticed that implementing zemse's method didn't change the outcome, as the numbers returning 0x00 == 1575.345, for example, which would round down to a number > 0. Looking at pairs WMATIC/WETH and WETH/DAI, they produce normal results. As does USDT/USDC, whereas WMATIC/USDC produces 0x00 numbers, therefore I can only assume differences in hexidecimal precision are the culprit. Looking into ways to add precision to the smaller numbers, or take precision away from larger numbers, however, BigNumbers.toPrecision() and .toFixed() do nothing at all. Let me know if anyone can think of another solution. In the meantime, I'll be update if I find anything. |
Beta Was this translation helpful? Give feedback.
-
I'm using the below method to discover reserves and derive price on univ2 clones using BigNumbers & FixedNumbers implementation in Ethers, but BigNumbers is returning 0x00 even though prices are positive numbers.
I have also tried using FixedNumber.from(value) but receive the same 0x00 value.
Here is my query:
Here is my returned data:
As you can see I have attempted to implement all available methods, but all have the same failure.
I have also attempted to multiply by 100000, etc but that was also unsuccessful.
However, it should be noted that floats such as 1550.783, for example, have also returned 0x00, which is incredibly confusing.
Suggestions in other threads were to use BigInt, or FixedNumber, but other discussions claim BigInt library has a possible security flaws when used in conjunction with blockchain development, and FixedNumber above doesn't work.
Not really sure what to do from here.
Beta Was this translation helpful? Give feedback.
All reactions