1
address routerAddress = 0x7a250d5630b4cf539739df2c5dacb4c659f2488d;
address _WETH = address(0);

function setWETHOnChain(address routerAddress) external {
        IUniswapV2Router02 router = IUniswapV2Router02(routerAddress);
        _WETH = router.WETH();
    }

This line of code no matter what will revert upon execution, to my surprise. Remix debugger said _WETH = router.WETH(); is the last executed statement before revert.

I also have problems with @uniswap/v2-periphery/contracts/libraries/UniswapV2Library.sol, specifically getReserve().

function getReserves(address factory, address tokenA, address tokenB) internal view returns (uint reserveA, uint reserveB) {
    (address token0,) = sortTokens(tokenA, tokenB);
    (uint reserve0, uint reserve1,) = IUniswapV2Pair(pairFor(factory, tokenA, tokenB)).getReserves();
    (reserveA, reserveB) = tokenA == token0 ? (reserve0, reserve1) : (reserve1, reserve0);
}

I would always revert on this line. (uint reserve0, uint reserve1,) = IUniswapV2Pair(pairFor(factory, tokenA, tokenB)).getReserves();, directly after pairFor() has returned successfully.

I tried the first code on Solidity 0.6.12, 0.7.6, and 0.8.7 without any success.

The weirdest thing is, querying such values using a block explorer or an EOA using Ethers.js works just fine.

2
  • Which network are you running this on? Sep 5, 2021 at 18:52
  • @MadhavanMalolan Hardhat Network forking Binance Smart Chain Testnet. I added my own answer, I can accept it tomorrow.
    – rion
    Sep 5, 2021 at 21:15

3 Answers 3

2

For the first one, I think it's because my local Hardhat Network is not forking mainnet correctly because of a configuration error, putting the network block outside of module.exports, leaving the contract in its own blockchain without any other contract. Once I confirmed it was forking mainnet by the warning that says Hardhat Network would be in decreased performance if you fork a very recent block, router.WETH() worked again.

For the second one, Hardhat actually tells me it's calling a non-existent account with no contract bytecode data. pairFor() calculates the pair address without looking up Uniswap V2 factory contract, and ends up producing invalid pair addresses on Binance Smart Chain Testnet with PancakeSwap pairs. I alleviated it by replacing pairFor() with

import '@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol';

IUniswapV2Factory factory = IUniswapV2Factory(factoryAddress); 
factory.getPair(tokenA, tokenB);
1

For the second problem, I think your issue is because you have not updated the INIT_CODE_HASH in your pairFor function.

Please compute the INIT_CODE_HASH locally using:

const { bytecode } = require('@uniswap/v2-core/build/UniswapV2Pair.json');
const { keccak256 } = require ('@ethersproject/solidity');

const INIT_CODE_HASH = keccak256(['bytes'], [`0x${bytecode}`]);
console.log(INIT_CODE_HASH);
0

For the first problem, Which testnet are you using?!

For the second problem, you declared a function named getReserves() which takes 3 parameters. No problem here but in the function you are calling getReserves() again and it makes a cycle forever (however you should give it parameters).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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