title: Solidity Uniswap: get token price
body: <p>I am trying to get the price of a pair on UniswapV2:
  This is my code:</p>
  <pre><code>pragma solidity ^0.5.1;
  
  import '@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol';
  import './IERC20.sol';
  
  
  contract Uniswap {
  
     // calculate price based on pair reserves
     function getTokenPrice(address pairAddress, uint amount) public view returns(uint)
     {
      IUniswapV2Pair pair = IUniswapV2Pair(pairAddress);
      IERC20 token1 = IERC20(pair.token1);
      (uint Res0, uint Res1,) = pair.getReserves();
  
      // decimals
      uint res0 = Res0*(10**token1.decimals());
      return((amount*res0)/Res1); // return amount of token0 needed to buy token1
     }
      
  }
  </code></pre>
  <p>I manually imported the openzeppelin IERC20 interface and put as compiler version 0.5.1, because the current version of uniswap v2 periphery is 0.5.1</p>
  <p>But I have the following error for the line <code>IERC20 token1 = IERC20(pair.token1);</code>:</p>
  <pre><code>Explicit type conversion not allowed from &quot;function () view external returns (address)&quot; to &quot;contract IERC20&quot;. IERC20 token1 = IERC20(pair.token1); ^-----------------^
  </code></pre>
  <p>Any idea on how to solve that? thanks!</p>