[Ethereum] gas required exceeds allowance (8000029) or always failing transaction

erc-20etherropstentokens

When executing this function, I get an error gas required exceeds allowance (8000029) or always failing transaction i know that this error have when use transferFrom function. Where i wrong? help me please. thanks.

edit: walletOfGoods[msg.sender][_nameOfgood].price it is element of struct, type uint.
walletOfGoods it is struct where i save amount of goods. Struct contain is uint amounOfgood and uint price.
i use ropsten test

contract TRC20{
     event Approval(address indexed _owner, address indexed _spender, uint256 value);

     function approve(address _sender, address _spender, uint256 _value) public returns (bool success){
         allowance[_sender][_spender] = _value;
         emit Approval(_sender, _spender, _value);
         return true;

     }
     function transferFrom(address _from, address _to, uint256 _value) public returns(bool success){

         require(_value<= allowance[_from][msg.sender]);
         _transfer(_from, _to, _value);
         return true;
 }
    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;
       emit Transfer(_from, _to, _value);
       assert(balanceOf[_from]+balanceOf[_to]==previousBalances);

}




function saleOfGoods(string memory _nameOfgood, uint _amountOfgood) public{

   trc20 = TRC20(address(0x235F857D7947b9bC5Dc73f489B70Ef870e6263ed));

   walletOfGoods[msg.sender][_nameOfgood].amountOfgood = walletOfGoods[msg.sender][_nameOfgood].amountOfgood.sub(_amountOfgood);

   trc20.approve(address(0xeBa84a3f7d8d70955bEFF633098D8d6A3a6c18e5), msg.sender, walletOfGoods[msg.sender][_nameOfgood].price);

   // Move to tokens to contract address
   trc20.transferFrom(address(0xeBa84a3f7d8d70955bEFF633098D8d6A3a6c18e5), msg.sender, walletOfGoods[msg.sender][_nameOfgood].price);

   }

Best Answer

Let's say there are two contracts TRC20 which is a token and SaleOfGoods which has the salesOfGood function.

The problem is in saleOfGoods function

function saleOfGoods(string memory _nameOfgood, uint _amountOfgood) public {
   // ....

   trc20.approve(address(0xeBa84a3f7d8d70955bEFF633098D8d6A3a6c18e5), msg.sender, walletOfGoods[msg.sender][_nameOfgood].price);

   // Move to tokens to contract address
   trc20.transferFrom(address(0xeBa84a3f7d8d70955bEFF633098D8d6A3a6c18e5), msg.sender, walletOfGoods[msg.sender][_nameOfgood].price);

}
  • The approve is telling to the TRC20 token that the user (msg.sender) can spend tokens from 0xeBa84a3f7d8d70955bEFF633098D8d6A3a6c18e5.

  • The transferFrom is telling the token that if move tokens from 0xeBa84a3f7d8d70955bEFF633098D8d6A3a6c18e5 in behalf of SaleOfGoods contract to the user (msg.sender).

The transferFrom will fail because the SaleOfGoods contract hasn't been authorized.

The problem is that you are missing the semantics of ERC20 specification. Usually approve and transferFrom are not called by the same account. The token owner calls approve and the spender calls transferFrom.

Related Topic