[Ethereum] Remix: Gas estimation errored…The transaction execution will likely fail

remixsolidity

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.

Best Answer

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.