2

I am relatively new to Solidity, I am trying to fetch the price of a pair with the following contract:

contract Uniswap {


    constructor () public payable {
    }
   
    function getBalance() public view returns (uint) {
        return address(this).balance;    
    }
   
   
   // calculate price based on pair reserves
   function getTokenPrice(address pairAddress, uint amount) public payable 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
   }
   
}

but when I try to run on Remix the getTokenPriceit returns me the following error message:

The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.

Any idea why? I am trying to fetch the price of ETH/USDT so the using this website (https://v2.info.uniswap.org/pair/0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852) and with the pair address at the end of the URL. Thanks for the help!

Viktor.w
  • 1,787
  • 2
  • 20
  • 46
  • @PetrHejda I am on remix currently, connected to Goerli. I will try to deploy on Goerli network and try from there. – Viktor.w May 06 '21 at 14:54
  • Sorry I deleted my comment because at first I though I misunderstood your question. I assumed you're on the local network and deleted the commend afterwards. But it won't work on Goerli either - read the answer, the Uniswap pair does not exist on the same address on Goerli either. – Petr Hejda May 06 '21 at 14:57

2 Answers2

2

When you deploy a contract in Remix, it deploys to a local network. (By default. You can change it and deploy to mainnet as well, if you inject a production Infura provider for example.)

The Uniswap pair contract 0x0d4a11... does not exist on the local network.


The easiest solution is to deploy your contract on the mainnet (where the 0x0d4a11... Uniswap pair contract exists).

Or you can find another Uniswap pair on a public testnet (well-known are Rinkeby, Ropsten, Goerli, and Kovan), and deploy your contract on the same testnet. Don't forget to change the testnet address of the uniswap pair that you pass to your getTokenPrice() function.

Or you can deploy the Uniswap pair contract and all of its dependencies and change the dependencies addreses (which is a lot of work - my guess is that it's dependend on the Uniswap router contract, the actual token contracts of the pair, maybe they are dependend on some other contract, ...) to your local network. From here it's the same - deploy your contract to the local network, and call the getTokenPrice() with the correct pair address.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
0

You may also invoke uniswap's Subgraph using javascript

d4gkl4s
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 08 '22 at 10:47