4

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);

   }
1
  • 1
    Comments are not for extended discussion; this conversation has been moved to chat.
    – eth
    Commented Mar 12, 2020 at 5:43

2 Answers 2

3

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.

1
  • 1
    thanks for answer, I try use tokens of another user with second user, it was wrong. After i transfer tokens through constract and it work Commented Mar 11, 2020 at 10:23
3

The gas required exceeds allowance (8000029) or always failing transaction error consists of two parts, either one of which may have taken place:

  1. The gas required for executing the transaction exceeds the gas-limit for the given block
  2. The transaction has reverted due to require(cond) with cond evaluating to false

From my experience, this error is issued when you run transaction.estimateGas rather than transaction.send, but it is really not that critical in the given context.

In either case (also from my experience), it is always the second part which has taken place, i.e., the transaction has reverted due to require(cond) with cond evaluating to false.


As to the actual reason for your transaction to revert, it is most likely because of some require statement in the code (for example, inside the sub function), following incorrect input passed to function saleOfGoods.

The input is incorrect with respect to the current state of course (i.e., the current values of all related state variables). Find out which input it is, fix it, and the transaction should complete.

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.