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.