0

I have tried creating contracts in ethereum wallet but sometimes it shows an error while executing those contracts. The error says It seems this transaction will fail. If you submit it, it may consume all the gas you provide. Can anyone help me with this issue?

My smart contract is like:

pragma solidity ^0.4.18;
contract MyToken {
        function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);
    }

    contract TransferToken{
        function sendTheToken(address _from, address _to, uint256 _value)public returns(string) {
            address tokenContractAddress = <MyTokenaddress>
            MyToken token = MyToken (tokenContractAddress);
            bool sendSuccess = token.transferFrom(_from, _to, _value);
            if(sendSuccess=true) return 'success';
            else return 'Failure';
        }
    }

In the above contract MyToken is a contract that I have created earlier for crypto-currencies.

pragma solidity ^0.4.18;
interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public; }

contract MyToken {
    string public name;
    string public symbol;
    uint8 public decimals = 2;
    uint256 public totalSupply;
    mapping (address => uint256) public balanceOf;
    mapping (address => mapping (address => uint256)) public allowance;
    event Transfer(address indexed from, address indexed to, uint256 value);

    function MyToken(
        uint256 initialSupply,
        string tokenName,
        string tokenSymbol
    ) public {
        totalSupply = initialSupply * 10 ** uint256(decimals);  
        balanceOf[msg.sender] = totalSupply;                
        name = tokenName;                                
        symbol = tokenSymbol;                               
    }


    function _transfer(address _from, address _to, uint _value) internal {
        require(_to != 0x0);
        require(balanceOf[_from] >= _value);
        require(balanceOf[_to] + _value > balanceOf[_to]);
        uint previousBalances = balanceOf[_from] + balanceOf[_to];
        balanceOf[_from] -= _value;
        balanceOf[_to] += _value;
        Transfer(_from, _to, _value);
        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
    }


    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
        require(_value <= allowance[_from][msg.sender]);  
        allowance[_from][msg.sender] -= _value;
        _transfer(_from, _to, _value);
        return true;
    }
}
2
  • This usually occurs when your require checks or your contract throws. Mist provides max gas limit much more than your transaction needs (aI am not sure of the exact figure, you can see this while sending tx) so when the estimated gas of the transaction becomes equal to max gas supplied, Mist gives this error. Commented Jan 30, 2018 at 4:44
  • I think the require checks are not throwing. Can you tell me what to do if the estimated gas of the transaction becomes equal to mas gas supplied. Commented Jan 30, 2018 at 7:18

1 Answer 1

0

There is probably some error in the method you are trying to invoke. This mostly occurs when the require() in the function is not satisfied or some other visible error occurs. If you can share the code or at least the function, I can help you with that.

Edit (after the code was posted):

The transferFrom() has a require() that needs to be satisfied in order to get executed. The requirement is that the calling address should have sufficient allowance in order to proceed with the transfer of funds.

Though the function checks if the value returned is true or false, only when the transferFrom() method satisfies the require() it will return true. Otherwise, the function will just throw and not return false. When the scenario where it will throw, the error "It seems this transaction will fail. If you submit it,it may consume all the gas you provide" occurs.

The recommended way is

contract TransferToken{
    function sendTheToken(address _from, address _to, uint256 _value)public returns(string) {
        address tokenContractAddress = <MyTokenaddress>
        MyToken token = MyToken (tokenContractAddress);
        token.transferFrom(_from, _to, _value);
    }
}

If the requirement is satisfied, then the function call will succeed. If it fails, It will throw and return the remaining gas.

If you just looking to debug the contract and don't want to modify the sendTheToken() function then, you can modify the transferFrom() method this way to the below code.

function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
        if(_value <= allowance[_from][msg.sender]) {
            allowance[_from][msg.sender] -= _value;
            _transfer(_from, _to, _value);
            return true;
        } else {
            return false;
        }
    }

Note: Either way, no need of the bool success in the transferFrom() method. You can directly return the boolean value. Saves some gas.

7
  • I have edited the question and added the code.I am a beginner so I have no idea what is wrong with this. Commented Jan 30, 2018 at 6:33
  • I just saw the code. The code seems to be fine from the overall view. Which function did you try to invoke when you go the error? Commented Jan 30, 2018 at 7:02
  • I got the error when i invoked the sendTheToken function of TransferToken contract. Commented Jan 30, 2018 at 7:05
  • @MonicaMohandas Since the explanation was a bit big. I have updated my answer. Commented Jan 30, 2018 at 7:31
  • I have tried that. Now I don't have the issue of gas instead the tokens are not getting transferred. Do you know why? Commented Jan 30, 2018 at 8:02

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.