rihekopo
rihekopo

Reputation: 3350

Creating Uniswap pair fails on JavaScript VM in Remix

I am implementing a taxing feature in my smart contract which would swap tokens to ETH (BNB) during transactions. In order to do this I am initializing IUniswapV2Router02 and IUniswapV2Factory object inside the contract's constructor, then try to create a pair address with IUniswapV2Factory's createPair() function. However addPair(_factory.createPair(address(this), _router.WETH())); creates an error (added under the code).

I tried to deploy other published contracts which has the same code and function and I got exactly the same error. After several hours of searching the fix I am assuming the reason of the error is that I try to deploy in on the JavaScript VM. Will the createPair function work in this test environment (and my implementation is wrong), or is the code correct and I need to test it on the real blockchain?

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);
    }


}

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.

Upvotes: 0

Views: 1112

Answers (1)

rihekopo
rihekopo

Reputation: 3350

Will the createPair function work in this test environment (and my implementation is wrong), or is the code correct and I need to test it on the real blockchain?

After I found the solution I can answer the question. The solution was:

  • I added the logic of addPair() directly into the constructor
  • I deployed it directly to the BSC main net. It didn't work in Remix's test environment.

Upvotes: 1

Related Questions