0

I am trying to deploy a new contract to BSC testnet and hitting this error message. I do have enough testnet BNB (about 3.9 BNB). I'm pretty sure I'm missing something vital somewhere in the code.

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? Internal JSON-RPC error. { "code": -32000, "message": "execution reverted" }

I'm self-learning Solidity and it's probably a simple fix. I've dug around the web trying to find the fix but no solutions I've come across seem to work. Any help would be appreciated, thanks in advance!

Here's the code:

pragma solidity >=0.7.0 <0.9.0;
// SPDX-License-Identifier: UNLICENSED

interface IERC20 {
    function balanceOf(address account) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
}

interface IUniswapV2Router01 {
    function factory() external pure returns (address);
    function WETH() external pure returns (address);
    function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
}

interface IUniswapV2Router02 is IUniswapV2Router01 {
    function swapExactETHForTokensSupportingFeeOnTransferTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable;
    function swapExactTokensForTokensSupportingFeeOnTransferTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external;
}

interface IWETH {
    function deposit() external payable;
}

contract ReBuy {
    struct ExchangeResult {
        uint256 expected;
        uint256 received;
        uint256 gas;
    }

    function deposit(address token, address exchangeRouterAddress) internal {
        IUniswapV2Router02 exchangeRouter = IUniswapV2Router02(exchangeRouterAddress);
        address[] memory path = new address[](2);

        if (token != exchangeRouter.WETH()) {
            path[0] = exchangeRouter.WETH();
            path[1] = token;
            exchangeRouter.swapExactETHForTokensSupportingFeeOnTransferTokens{value: msg.value}(0, path, address(this), block.timestamp + 20);
        } 
        else {
            IWETH(token).deposit{value: msg.value}();
        }
    }

    function exchangeToken(address fromToken, address toToken, address exchangeRouterAddress, uint256 amount) private returns (uint256, uint256, uint256)
    {
        IUniswapV2Router02 exchangeRouter = IUniswapV2Router02(exchangeRouterAddress);

        uint256 expected;
        uint256 received;
        uint256 gas;

        //uint256[] memory gas = new uint256[](2);
        address[] memory path = new address[](2);

        IERC20 token = IERC20(fromToken);
        path[0] = fromToken;
        path[1] = toToken;

        expected = exchangeRouter.getAmountsOut(amount, path)[1];
        token.approve(exchangeRouterAddress, type(uint256).max);
        gas = gasleft();

        exchangeRouter.swapExactTokensForTokensSupportingFeeOnTransferTokens(amount, 0, path, address(this), block.timestamp + 20);
        gas = gas - gasleft();
        token = IERC20(toToken);
        received = token.balanceOf(address(this));

        return (expected, received, gas);
    }

    function simulate_txn(address baseToken, address token, address exchangeRouterAddress) public payable returns (uint256, uint256, uint256, uint256, uint256, uint256)
    {
        IERC20 _baseToken = IERC20(baseToken);
        uint256 amount;
        ExchangeResult memory tokenRes;
        ExchangeResult memory baseTokenRes;

        deposit(baseToken, exchangeRouterAddress);
        amount = _baseToken.balanceOf(address(this));

        (tokenRes.expected, tokenRes.received, tokenRes.gas) = exchangeToken(baseToken, token, exchangeRouterAddress, amount);

        (baseTokenRes.expected, baseTokenRes.received, baseTokenRes.gas) = exchangeToken(token, baseToken, exchangeRouterAddress, tokenRes.received);

        // Finally return all results    
        return (tokenRes.expected, tokenRes.received, tokenRes.gas, baseTokenRes.expected, baseTokenRes.received, baseTokenRes.gas);
    }
}
2
  • Do you have this problem on deployment? or on the execution of your function? I was able to deploy locally
    – Adam Boudj
    Mar 22, 2022 at 20:40
  • Really? I'm having issues with deploying. Can you pls share a screenshot of the remix interface you used to deploy along with settings so I can replicate it?
    – user96723
    Mar 23, 2022 at 1:35

1 Answer 1

0

Since it's compiling, there is no error on your code, I'm also able to deploy locally. I think the problem is with your network (BSC), try to change your RPC on your metamask https://bscrpc.com/ https://docs.binance.org/smart-chain/developer/rpc.htm

Config

Deploy

2
  • Thank you! I was able to deploy using the settings above. Previously I had 5 under VALUE (trying to pay for gas) and it didn't like that. Changed to 0 and it worked.
    – user96723
    Mar 23, 2022 at 23:03
  • I'm running into an INSUFFICIENT_INPUT_AMOUNT error when I tried to call the simulate_txn function. Since this issue was about deployment and that part is solved, I decided to open a new issue. Can you pls help me look into it further over there? link
    – user96723
    Mar 23, 2022 at 23:40

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.