0

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 above positive integers.

I have also tried using FixedNumber.from(value) but receive the same 0x00 value.

Here is my query:

                    aReserve0 = aReserves[0] 
                    aReserve1 = aReserves[1] 
                    let aReserve0Formatted = Number(ethers.utils.formatUnits(aReserve0, token0dec))
                    let aReserve1Formatted = Number(ethers.utils.formatUnits(aReserve1, token1dec))
                    const aPrice0 = (aReserve0Formatted / aReserve1Formatted)//.toFixed(token1dec) //1 / 2100
                    const aPrice1 = (aReserve1Formatted / aReserve0Formatted)//.toFixed(token0dec) //2100 / 1
                    const aPrice0BN = aReserve0.div(aReserve1)
                    const aPrice1BN = aReserve1.div(aReserve0)
                    const aPrice0FN = FixedNumber.fromValue(aReserve0.div(aReserve1), token0dec)
                    const aPrice1FN = FixedNumber.fromValue(aReserve1.div(aReserve0), token1dec)


                    let aPriceMap = {
                        exchange: exchangeA,
                        asset: token0symbol + "/" + token1symbol,
                        aPrice0: aPrice0,
                        aPrice1: aPrice1,
                        Reserves0: aReserve0Formatted,
                        Reserves1: aReserve1Formatted,
                        Reserves0BN: aReserve0,
                        Reserves1BN: aReserve1,
                        aPrice0BN: aPrice0BN,
                        aPrice1BN: aPrice1BN,
                        aPrice0FN: aPrice0FN,
                        aPrice1FN: aPrice1FN
                    }

Here is my returned data:

{
  exchangeA: {
    exchange: 'QUICK',
    asset: 'WMATIC/USDT',
    aPrice0: 1.0447422301973823,
    aPrice1: 0.9571739048119753, //non-zero
    Reserves0: 794970.8491943,
    Reserves1: 760925.351935,
    Reserves0BN: BigNumber { _hex: '0xa85774c22e243002313d', _isBigNumber: true },
    Reserves1BN: BigNumber { _hex: '0xb12aaf6fff', _isBigNumber: true },
    aPrice0BN: BigNumber { _hex: '0xf33f7d50b5', _isBigNumber: true },
    aPrice1BN: BigNumber { _hex: '0x00', _isBigNumber: true },//zero
    aPrice0FN: FixedNumber {
      format: [FixedFormat],
      _hex: '0xf33f7d50b5',
      _value: '0.000001044742230197',
      _isFixedNumber: true
    },
    aPrice1FN: FixedNumber {
      format: [FixedFormat],
      _hex: '0x00',//zero
      _value: '0.0',
      _isFixedNumber: true
    }
  },

As you can see I have attempted to implement both methods, but both have the same failure.

  • 1
    It appears that division of the two BigNumbers is causing integer division, which results in a rounded down number equal to zero. (credit to Zemse: https://github.com/ethers-io/ethers.js/discussions/3681#discussioncomment-4773328) I forgot that ETH (and therefore BigNumber) rounds down. I'll try to understand the answer and then update this when that's completed. – the_prophecy Jan 25 '23 at 12:26

0 Answers0