I am trying to run the uniswap fork on hardhat for learning purpose but getting following error:
Should add liquidity:
Error: Transaction reverted: function call to a non-contract account
at UniswapV2Router02.getReserves (contracts/libraries/UniswapV2Library.sol:33)
at UniswapV2Router02._addLiquidity (contracts/UniswapV2Router02.sol:46)
at UniswapV2Router02.addLiquidityETH (contracts/UniswapV2Router02.sol:86)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at runNextTicks (internal/process/task_queues.js:62:3)
at listOnTimeout (internal/timers.js:523:9)
at processTimers (internal/timers.js:497:7)
at HardhatNode._mineBlockWithPendingTxs (node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:1588:23)
at HardhatNode.mineBlock (node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:442:16)
at EthModule._sendTransactionAndReturnHash (node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:1500:18)
I had similar issues and after doing some debugging I found that there is an issue with
UniswapV2Library function. For some reason function pairFor of this library returns different pair address.
I have forked uniswap contracts (router + factory) and during the test I got the exactly same issue some of you got. It seems like the pairFor function doesn't return the correct pair address for 2 given tokens.
The purpose of this function is to get the contract address for 2 tokens without calling any contract (pure function). The problem is the hex value init code hash this hash is just a keccak256() of your compiled contract UniswapV2Pair.sol
How to solve your problem ?
Very easy. Just find your bytecode from compiled contract, hash it and replace on the contract.
For Hardhat users:
Find IUniswapV2Pair.json on the artifacts folder
Find the bytecode on the file
Copy it on Keccak 256 Online (Don't forget to change the input type to "hex" and delete the "0x" at the start)
Hash it and copy the hash in the pairFor function
Sorry if it's bad written it's the first time I help online