Solidity Unit Testing – Testing UniswapV2Pair Function Call to a Non-Contract Account

hardhatsolidityuniswapunittesting

I'm trying to create unit tests for a token using Hardhat and run them locally.
The token uses Uniswap to swap for ETH.

Before each unit test, I…

  1. Deploy WETH, UniswapV2Factory, and UniswapV2Router02.
  2. Deploy my token
    • The token's constructor calls uniswapFactory.createPair(this, weth)

This all works properly and I'm able to run tests with my token.

However, one of my unit tests calls a function that ends up calling:

***Stack Trace***
UniswapV2Router02._swapSupportingFeeOnTransferTokens
UniswapV2Router02.swapExactTokensForEthSupportingFeeOnTransferTokens

https://github.com/Uniswap/v2-periphery/blob/master/contracts/UniswapV2Router02.sol#L321

When it gets to line 329, it errors out with Transaction reverted: function call to a non-contract account

Reference:
Create Pair code:
https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Factory.sol#L23


I assume this means that the pair's address is not a contract that deployed locally.

What else do I need to do in order to get my pair to be a working contract?

I thought createPair() would be enough.

Best Answer

Factory.createPair() will effectively deploy your pair contract, while when using UniswapLibrary.pairFor(), you will get the theoretical address of any potential pair, based on the uniswapV2Pair bytecode keccak256 hash (the "INIT_CODE" in get_pair()). If this hash differs than the one based on your actual deployment (ie an "old" value stayed hard-coded), createPair and pairFor will return different values.

See https://ethereum.stackexchange.com/a/112709/68134 for similar concept in V3

Related Topic