I am writing a BEP20 contract with a fixed transaction fee which gets swapped and sent to the team wallet. To make the whole thing working I could successfully initialize a IUniswapV2Router02 and IUniswapV2Factory object inside the contract's constructor but when I deploy the contract (environment: JavaScript VM) in Remix I get a VM error: revert. I could investigate that the code crashes when I try to pass _factory.createPair(address(this), _router.WETH()) to the addPair function. Unfortunately I am not so familiar with this but I think the addPair function is fine and the issue is with the argument that I try to pass. Far as I know it should return an address instead of generating this error. If anyone sees what did I wrong or know a better solution for creating token-eth(bnb) pairs I would love to see the solution.

pragma solidity ^0.8.7;

import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";

interface IBEP20 {

contract BEP20Token is Context, IBEP20, Ownable {
    using SafeMath for uint256;

    mapping (address => uint256) private _balances;
    mapping (address => mapping (address => uint256)) private _allowances;
    mapping(address => bool) private _pair;
    string private _symbol;
    string private _name;
    uint256 private _totalSupply;
    uint8   private _decimals;
    uint256 private _developmentTax = 3;

    address private constant _factoryAddress = 0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73;
    address private constant _routerAddress = 0x10ED43C718714eb63d5aA57B78B54704E256024E;
    address private constant _deadAddress = 0x000000000000000000000000000000000000dEaD;
    address private _pairAddress;

    IUniswapV2Factory private _factory;
    IUniswapV2Router02 private _router;

  constructor()  {
    _name = "My Token";
    _symbol = "TKN";
    _decimals = 18;
    _totalSupply = 1000 * 10 ** 18;
    _balances[_msgSender()] = _totalSupply;
    _maxTransferLimit = _totalSupply;

    _router = IUniswapV2Router02(_routerAddress);
    _factory = IUniswapV2Factory(_factoryAddress);   
    // it crashes in this line:
    addPair(_factory.createPair(address(this), _router.WETH()));
    emit Transfer(address(0), msg.sender, _totalSupply);


    function addPair(address pairAddress) public onlyOwner {
        _pair[pairAddress] = true;
        _pairAddress = pairAddress;
        emit AddPair(pairAddress);


And here is the error message

creation of BEP20Token errored: VM error: revert.

revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.

2 Answers 2


You're trying to call a function from within the constructor, it shouldn't even compile (ie "addPair() is not (or not yet) visible at this point.").

Either copy the logic of your function into the constructor or call this function after deployment.

  • I am wondering that why my other function calls (that not in this code) working inside the constructor and this is not. Is there a specific reason why this function can't be called from the constructor?
    – rihekopo
    Commented Nov 16, 2021 at 22:45

I believe this link will address why it is not working.

To summarize: "You cannot call external functions on a contract that is being constructed (e.g. this.foo(); inside the constructor function will revert the contract creation transaction)"

Applying that to your code: _factory.createPair() & _router.WETH() I believe are calling external functions.

Take my answer with a grain of salt. I am still learning myself and could very well be wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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