0

addLiquidity() function reverts without any reason on local development instance of ganache, any ideas why this might be happening?

Some background:

  • I have forked the UniswapV2Factory and UniswapV2Router02 and deployed on ganache.
  • I have created 2 ERC20 tokens, and created a pair (via factory.createPair()) for them.
  • Minted both ERC20 tokens to my address.
  • I have approved token amounts to the router contract address (via erc20.approve() function)

This is how I am calling the addLiquidity Function

const blockNumber = await web3.eth.getBlockNumber();
const block = await web3.eth.getBlock(blockNumber);
const timestamp = block.timestamp + 300;

await router.addLiquidity(
    ERC20_TOKEN_1_ADDRESS,
    ERC20_TOKEN_2_ADDRESS,
    web3.utils.toWei('1', 'ether'),
    web3.utils.toWei('1', 'ether'),
    web3.utils.toWei('0.001', 'ether'),
    web3.utils.toWei('0.001', 'ether'),
    OWNER_ADDRESS,
    timestamp,
    { gas: 4000000 }
)
TylerH
  • 20,799
  • 66
  • 75
  • 101
Suraj Kohli
  • 547
  • 2
  • 5
  • 11
  • 1
    i had the same error when forked uniswap. in my case, the problem was in the `UniswapV2Library` at `pairFor` method. they use some fancy tricks here, and for some reasons in fact it returned not correct address for deployed pair in fork (copy-past the contracts to another repo, maybe it was a compiler issue). with wrong address, it failed on `getReserves` call. what i did was copying not contracts, but `.json` files from `build` directory and use these files in tests and deployment. suddenly, it worked.. hope it helps you – invisiblecat Feb 24 '22 at 16:12
  • @invisiblecat I was actually able to fix this. The function `pairFor` uses the creation code hash of the UniswapV2Pair contract. Since I had deployed it on my own private network, I needed to update the hash in the library, before deploying the library. – Suraj Kohli Mar 02 '22 at 07:12
  • from where did you get the new code hash? – invisiblecat Mar 02 '22 at 08:43
  • 1
    @invisiblecat you can either hash the source code of `UniswapV2Pair` contract or use the `factory.pairCodeHash()` function which does the same. – Suraj Kohli Mar 02 '22 at 10:49
  • wow thank you! i didn't know that.. funny that you was one asked a question but at the end me, attempted answerer, getting new knowlegde dx – invisiblecat Mar 02 '22 at 12:52

0 Answers0