Gas Estimation Error – Fixing Gas Estimation Error in Solidity with Remix

remixsolidity

So I am trying to implement a simple deposit contract deployed to the testnet of Binance smart chain. The code looks simple as following:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

interface IBEP20 {
    function totalSupply() external view returns (uint256);
    function decimals() external view returns (uint8);
    function symbol() external view returns (string memory);
    function name() external view returns (string memory);
    function getOwner() external view returns (address);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function allowance(address _owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool); // This is a function we gonna use
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool); // This is a function we gonna use
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

contract SimpleDeposit {
    function simpledeposit(address _tokenIn, uint256 _amountsIn) external {
        IBEP20(_tokenIn).approve(address(this), _amountsIn);
        IBEP20(_tokenIn).transferFrom(msg.sender, address(this), _amountsIn);
    }
}

I have some test BUSD in metamask wallet, so here msg.sender really means my wallet address. After deploying this contract, when I try to execute this function, the remix shows me the following errors:

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": 3, "message": "execution reverted: BEP20: transfer amount exceeds allowance", "data": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002842455032303a207472616e7366657220616d6f756e74206578636565647320616c6c6f77616e6365000000000000000000000000000000000000000000000000" }

My question is how to properly fund a contract from my metamask wallet in this case? And also that the tranfer amount definitely not exceed the amounts of test BUSD in my test wallet.

Best Answer

You need to call the approve function directly from your wallet. You cant have your contract do that. So you'd need 2 transactions, one to the token contract that would be something like approve(yourContractAddress, amount) and one to your SimpleDeposit contract calling the simpleDeposit function with the parameters you want. The way you tried to do it by coding it wont work because the ERC20/BEP20 standard approve function simply doesnt work that way