Unpredictable_gas_limit

hi guy, i have code

contract TestToken2 is Initializable, ERC20Upgradeable, UUPSUpgradeable {

    /// @custom:oz-upgrades-unsafe-allow constructor

    constructor() initializer {}

    // PANCAKESWAP INTERFACES (For swaps)

    address private constant _pancakeSwapRouterAddress = 0x10ED43C718714eb63d5aA57B78B54704E256024E;

    IPancakeRouter02 private _pancakeswapV2Router;

    address private _pancakeswapV2Pair;

    function initialize() initializer public {

        __ERC20_init("TestToken", "TTK");

        //__ERC20Burnable_init();

        //__Pausable_init();

        //__Ownable_init();

        //__ERC20FlashMint_init();

        //__UUPSUpgradeable_init();

        _mint(msg.sender, 100000 * 10 ** decimals());

        setPancakeSwapRouter();

    }

    function _authorizeUpgrade(address newImplementation)

        internal

       // onlyOwner

        override

    {}

    function setPancakeSwapRouter() public {
        _pancakeswapV2Router = IPancakeRouter02(_pancakeSwapRouterAddress);
        _pancakeswapV2Pair = IPancakeFactory(_pancakeswapV2Router.factory()).createPair(address(this), _pancakeswapV2Router.WETH());

    }

}

when i deploy, i get a error
reason: 'cannot estimate gas; transaction may fail or may require manual gas limit', code: 'UNPREDICTABLE_GAS_LIMIT',

and i know error from function setPancakeSwapRouter() where i set address = 0x...
when i comment code for function setPancakeSwapRouter() , deploy Its ok! plesase help me!! thanks

Hello @khiem_tran_van .

My guess is this fails:

It can be the case if:

  • _pancakeswapV2Router isn't the right address (doesn't implement factory() or WETH())
  • createPair reverts

To what network are you trying to deploy ?

Thank for help, This is full code

pragma solidity ^0.8.2;

import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";

import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20BurnableUpgradeable.sol";

import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";

import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20FlashMintUpgradeable.sol";

import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";

contract TestToken2 is Initializable, ERC20Upgradeable, ERC20BurnableUpgradeable, PausableUpgradeable, OwnableUpgradeable, ERC20FlashMintUpgradeable, UUPSUpgradeable {

    /// @custom:oz-upgrades-unsafe-allow constructor

    constructor() initializer {}

    // PANCAKESWAP INTERFACES (For swaps)

    address private constant _pancakeSwapRouterAddress = 0x10ED43C718714eb63d5aA57B78B54704E256024E; // Pancake Router Address, should be 0x10ed43c718714eb63d5aa57b78b54704e256024e

    IPancakeRouter02 private _pancakeswapV2Router;

    address private _pancakeswapV2Pair;

    function initialize() initializer public {

        __ERC20_init("TestToken2", "TTK");

        __ERC20Burnable_init();

        __Pausable_init();

        __Ownable_init();

        __ERC20FlashMint_init();

        __UUPSUpgradeable_init();

        _mint(msg.sender, 50000000 * 10 ** decimals());

        setPancakeSwapRouter(); // error when i open this segment, when i comment this code, it worked

    }

    function pause() public onlyOwner {

        _pause();

    }

    function unpause() public onlyOwner {

        _unpause();

    }

    function mint(address to, uint256 amount) public onlyOwner {

        _mint(to, amount);

    }

    function _beforeTokenTransfer(address from, address to, uint256 amount)

        internal

        whenNotPaused

        override

    {

        //require(amount < 5000 * 10 ** decimals(), "Amount tranfer < 5000 token");

        super._beforeTokenTransfer(from, to, amount);

    }

    function _authorizeUpgrade(address newImplementation)

        internal

        onlyOwner

        override

    {}

    function setPancakeSwapRouter() public onlyOwner {

        //_pancakeSwapRouterAddress = ;

        _pancakeswapV2Router = IPancakeRouter02(_pancakeSwapRouterAddress);

        _pancakeswapV2Pair = IPancakeFactory(_pancakeswapV2Router.factory()).createPair(address(this), _pancakeswapV2Router.WETH());

    }

    function getAddressIPancakeRouter() public onlyOwner view returns (address) {

        return _pancakeSwapRouterAddress;

    }

}

interface IPancakeFactory {

    event PairCreated(address indexed token0, address indexed token1, address pair, uint);

    function feeTo() external view returns (address);

    function feeToSetter() external view returns (address);

    function getPair(address tokenA, address tokenB) external view returns (address pair);

    function allPairs(uint) external view returns (address pair);

    function allPairsLength() external view returns (uint);

    function createPair(address tokenA, address tokenB) external returns (address pair);

    function setFeeTo(address) external;

    function setFeeToSetter(address) external;

}

interface IPancakeRouter01 {

    function factory() external pure returns (address);

    function WETH() external pure returns (address);

    function addLiquidity(

        address tokenA,

        address tokenB,

        uint amountADesired,

        uint amountBDesired,

        uint amountAMin,

        uint amountBMin,

        address to,

        uint deadline

    ) external returns (uint amountA, uint amountB, uint liquidity);

    function addLiquidityETH(

        address token,

        uint amountTokenDesired,

        uint amountTokenMin,

        uint amountETHMin,

        address to,

        uint deadline

    ) external payable returns (uint amountToken, uint amountETH, uint liquidity);

    function removeLiquidity(

        address tokenA,

        address tokenB,

        uint liquidity,

        uint amountAMin,

        uint amountBMin,

        address to,

        uint deadline

    ) external returns (uint amountA, uint amountB);

    function removeLiquidityETH(

        address token,

        uint liquidity,

        uint amountTokenMin,

        uint amountETHMin,

        address to,

        uint deadline

    ) external returns (uint amountToken, uint amountETH);

    function removeLiquidityWithPermit(

        address tokenA,

        address tokenB,

        uint liquidity,

        uint amountAMin,

        uint amountBMin,

        address to,

        uint deadline,

        bool approveMax, uint8 v, bytes32 r, bytes32 s

    ) external returns (uint amountA, uint amountB);

    function removeLiquidityETHWithPermit(

        address token,

        uint liquidity,

        uint amountTokenMin,

        uint amountETHMin,

        address to,

        uint deadline,

        bool approveMax, uint8 v, bytes32 r, bytes32 s

    ) external returns (uint amountToken, uint amountETH);

   

    function swapExactTokensForTokens(

        uint amountIn,

        uint amountOutMin,

        address[] calldata path,

        address to,

        uint deadline

    ) external returns (uint[] memory amounts);

    function swapTokensForExactTokens(

        uint amountOut,

        uint amountInMax,

        address[] calldata path,

        address to,

        uint deadline

    ) external returns (uint[] memory amounts);

    function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)

    external

    payable

    returns (uint[] memory amounts);

    function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)

    external

    returns (uint[] memory amounts);

    function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)

    external

    returns (uint[] memory amounts);

    function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)

    external

    payable

    returns (uint[] memory amounts);

    function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);

    function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);

    function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);

    function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);

    function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);

}

interface IPancakeRouter02 is IPancakeRouter01 {

    function removeLiquidityETHSupportingFeeOnTransferTokens(

        address token,

        uint liquidity,

        uint amountTokenMin,

        uint amountETHMin,

        address to,

        uint deadline

    ) external returns (uint amountETH);

    function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(

        address token,

        uint liquidity,

        uint amountTokenMin,

        uint amountETHMin,

        address to,

        uint deadline,

        bool approveMax, uint8 v, bytes32 r, bytes32 s

    ) external returns (uint amountETH);

    function swapExactTokensForTokensSupportingFeeOnTransferTokens(

        uint amountIn,

        uint amountOutMin,

        address[] calldata path,

        address to,

        uint deadline

    ) external;

    function swapExactETHForTokensSupportingFeeOnTransferTokens(

        uint amountOutMin,

        address[] calldata path,

        address to,

        uint deadline

    ) external payable;

    function swapExactTokensForETHSupportingFeeOnTransferTokens(

        uint amountIn,

        uint amountOutMin,

        address[] calldata path,

        address to,

        uint deadline

    ) external;

}

I run in hardhat and using BSC testnet, maybe fail when i pass fixed address in code

address private constant _pancakeSwapRouterAddress = 0x10ED43C718714eb63d5aA57B78B54704E256024E;