0

I have a contract which tries to calculate the token price based on the reserves in the liquidity pools. When I call the contract function getTokenPriceAB() and input the pair address of WBTC-USDC as a parameter, it successfully returns the amount.

But when I change it to another one like WBTC-WETH, it returns 0.

   function getTokenPriceAB(address pairAddress) public override view returns(uint) {

        IUniswapV2Pair pair = IUniswapV2Pair(pairAddress);

        IERC20 token1 = IERC20(pair.token1());
        (uint Res0, uint Res1,) = pair.getReserves();

        require(Res1 != 0, "Division by zero");

        // decimals
        uint res0 = Res0.mul(10**token1.decimals());

        // return amount of _tokenA needed to buy _tokenB
        // to improve precision, multiply before dividing
        return((1000000000000000000*res0)/Res1); 
    }

I am expecting any other result than 0 independent from the pair address.

I also made sure that I input a valid address in there and checked that this pool has actually liquidity in it on etherscan.

2 Answers 2

1

Stop using getReserves(), use kLast()

   function getTokenPriceAB(address pairAddress) public view returns(uint256) {

        IUniswapV2Pair pair = IUniswapV2Pair(pairAddress);
        uint256 price = pair.kLast();
        return price;

    }

Using the kLast() function instead of getReserves() makes sense in this context because it returns the last trade price of the token pair. Rather than the current reserves of the token pair, it returns the last trade price of a token pair, which is calculated as the ratio of the token0 price to token1 price.

5
  • I was digging in more and edited your answer with some explanations after I finally got it. Thanks, that helped!
    – Abi
    Jan 14 at 16:16
  • Its me again.. It seems like this also returns always 0. Any ideas?
    – Abi
    Jan 14 at 16:20
  • Then it has no reserves. Post here the LP address for me to verify please. Jan 14 at 22:30
  • I don't think that's the case. The USDC-ETH pair has liquidity in it with the following address: 0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc Here are some other examples which all should return any other value than 0. WBTC-USDC pair address: 0x004375dff511095cc5a197a54140a24efef3a416 WBTC-ETH pair address: 0xBb2b8038a1640196FbE3e38816F3e67Cba72D940 Thank you for you help so far @rodrigo-burgos
    – Abi
    Jan 16 at 8:36
  • yes. kLast() returns 0.
    – sanjay dev
    Mar 22 at 16:12
0

I tried using kLast() on the WETH-USDC pair on etherscan but even the read contract function returns always 0 for kLast(). So I am at least correctly calling the function. Why is the value always 0 ? The liquidity pool has clearly funds in it so it should be able to calculate the kLast price.

Also checking the getReserves() function, it successfully returns both values.

This doesn't make sense at all and I can't find any information in the documentation.

1
  • Have u find any solution?
    – sanjay dev
    Mar 22 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.