0

This error seems to get fixed by resolving an obscure exception in one's code. I'm trying to invoke Owner.transfer in Remix, but being thrown the alert: Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? The execution failed due to an exception. Reverted.

The Owner contract just approves the sender for the specified amount, and invokes the Depository to transfer that amount to itself before updating balances.

    pragma solidity ^0.5.11;

    import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.0/contracts/token/ERC20/ERC20.sol";

    contract Token is ERC20 {}

    contract Depository {
        mapping(address => uint256) public balances;

        function deposit(uint256 amount) external {
            require(amount > 0);

            Token token = Token(0xFab46E002BbF0b4509813474841E0716E6730136);
            require(token.transferFrom(msg.sender, address(this), amount), "The deposit failed");
            balances[msg.sender] += amount;
        }
    }

    contract Owner {
        function transfer(uint256 amount) external {
            require(amount > 0);

            Token token = Token(0xFab46E002BbF0b4509813474841E0716E6730136);
            // 0xEa917... is the deployed Depository contract address
            token.approve(0xEa917F373afa9F7F4AdB31a01D4c91E763De6502, amount);

            Depository depository = Depository(0xEa917F373afa9F7F4AdB31a01D4c91E763De6502);
            depository.deposit(amount);

        }
    }

Is there a code issue? Can't quite figure out where I'm going wrong.

7
  • Please explain the opening statement in your question - "This error seems to get fixed by resolving an obscure exception in one's code". – goodvibration May 14 '20 at 10:52
  • Correct me if I'm wrong, but it seems this error is usually caused by some gas-related bug specific to one's code. – slider May 14 '20 at 10:54
  • 1
    This error is usually caused because one (Remix) in this case, executes transaction.estimateGas before executing transaction.send, in order to determine how much gas is required for the latter to complete successfully. However, if the actual function to be execute should revert for some reason, then it would revert in both cases (i.e., both in estimateGas and in send). In that sense, calling estimateGas serves also as a preliminary warning that your function will revert when you execute it (and the gas used prior to reverting will be lost). – goodvibration May 14 '20 at 11:12
  • 1
    You can understand that even from the error-message itself: The transaction execution will likely fail - likely, when you run it "for real" (i.e., not just for gas-estimation). – goodvibration May 14 '20 at 11:12
  • 1
    And BTW, approve/transferFrom flows typically fail for either one of two reasons: 1. Insufficient amount of tokens approved. 2. Insufficient amount of tokens available for transfer. In both cases, the revert occurs at a "safeSub" function. So I suggest that you start by looking into each one of these two reasons. – goodvibration May 14 '20 at 11:15
1

I copied and deployed your code in remix. I got it to work fine.

are you sure that you have tokens in the token contract, and in the right place? I had to add a mint function in the Token constructor so I actually had some tokens. Then, I needed to transfer some to the Owner contract - as this is where they actually need to be to subsequently be added to the Depository.

After that Owner.transfer worked and appeared to do the correct thing.

2
  • As I said in the (currently) last comment to the question - "2. Insufficient amount of tokens available for transfer". – goodvibration May 14 '20 at 11:44
  • 1
    Thanks a bunch @zakalwe and @goodvibration. That error only gets thrown when there aren't enough tokens in the Owner contract. Simple fix! – slider May 14 '20 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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