UniswapV2Pair – Unable to Create UniswapV2Pair from Constructor on Ethereum Mainnet

constructorcontract-deploymentremixsolidityuniswap

I want to create a Uniswap Pair in a constructor() block of my token smart-contract.

However, when I try to do this, I get "error":"execution reverted".


Perhaps these nuances will help you understand what the problem is:

  • If the line with the initialization of the pair (_pair = IPan...) is commented out – code works properly.
  • This problem does not arise if the contract is deployed to Goerli, which have the same UniswapV2Router address as Mainnet. Issues occur only when i try to deploy it on Ethereum Mainnet.
  • Also, the contract is successfully deployed on the local blockchain. No errors after compilation.

Here is the address of the UniswapV2Router that I used
0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D

Below is the code related directly to the interaction with Uniswap.


interface IPancakeFactory {
    function createPair(address tokenA, address tokenB) external returns (address pair);
}

interface IPancakePair {
    function token0() external view returns (address);
    function token1() external view returns (address);
    function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
}

interface IPancakeRouter01 {
    function factory() external pure returns (address);
    function WETH() external pure returns (address);
}

interface IPancakeRouter02 is IPancakeRouter01 {}

contract MyTokenContract is Context, Ownable, IERC20 {

  IPancakeRouter02 internal _router;
  IPancakePair internal _pair;

  constructor(address _routerAddress) {
          _router = IPancakeRouter02(_routerAddress);
          _pair = IPancakePair(IPancakeFactory(_router.factory()).createPair(address(this), _router.WETH()));
        
          _balances[owner()] = _totalSupply;
          emit Transfer(address(0), owner(), _totalSupply);
    }
}

It turns out that I simply cannot deploy a contract if a new pair is created in the constructor()

UPD: After a more detailed breakdown of the code into simple operations, I came to the conclusion that the error occurs precisely when the createPair() function is called.

Please respond if you have experienced something like this. Or have solution for this?

Best Answer

The problem was solved when I changed the wallet to one with more ETH (at least twice the amount that needs to be paid for fees for the deployment)

Related Topic