2

I have a contract which has transferFrom function

    mapping(address => uint) balances;

    mapping(address => mapping(address => uint)) allowed;

    mapping (address => bool) public frozenAccount;

   // Transfer token from spender account to receiver account
    function transferFrom(address from, address to, uint tokens) public returns (bool success) {
        require(!transactionLock);         // Check for transaction lock
        require(!frozenAccount[from]);     // Check if sender is frozen
        require(!frozenAccount[to]);       // Check if recipient is frozen
        balances[from] = balances[from].sub(tokens);
        allowed[from][msg.sender] = allowed[from][msg.sender].sub(tokens);
        balances[to] = balances[to].add(tokens);
        emit Transfer(from, to, tokens);
        return true;
    }

When i execute this function i am getting error :

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?

gas required exceeds allowance or always failing transaction

I have read many answers in StackOverflow . No solution work for me

  • how is the transactionLock defined? is that set to false? – Tudor Constantin Nov 7 '18 at 12:48
  • yes it is initailized to false in constructor – Zinc Nov 7 '18 at 12:49
0

Maybe the account from has not allowed the to account to withdraw funds. I recommend to add some extra verifications:

  function transferFrom(
    address from,
    address to,
    uint256 tokens
  )
    public
    returns (bool)
  {
    require(!transactionLock);         // Check for transaction lock
    require(!frozenAccount[from]);     // Check if sender is frozen
    require(!frozenAccount[to]);       // Check if recipient is frozen

    require(tokens <= _balances[from]);
    require(tokens <= _allowed[from][msg.sender]);
    require(to != address(0));

    _balances[from] = _balances[from].sub(tokens);
    _balances[to] = _balances[to].add(tokens);
    _allowed[from][msg.sender] = _allowed[from][msg.sender].sub(tokens);
    emit Transfer(from, to, tokens);
    return true;
  }

Also, make sure that the from account allows the to account to withdraw the tokens amount and it should work

  • I am following this link for this contract. theethereum.wiki/w/index.php/ERC20_Token_Standard – Zinc Nov 7 '18 at 12:53
  • When i am executing the same code with javascriptVM . it gives me this error The constructor should be payable if you send value. – Zinc Nov 7 '18 at 12:57
  • the transferFrom() function I gave you is the one from openZeppelin: github.com/OpenZeppelin/openzeppelin-solidity/blob/master/… plus your added modifications – Tudor Constantin Nov 7 '18 at 12:57
  • Is there any restriction in choosing these from and to address OR i can use any address . I have sent some token to from address and to address is a new address which has no token. when i make transaction it gives this error . – Zinc Nov 7 '18 at 14:03
  • you can use any address, but before calling the transferFrom(from, to, amount), you also have to call approve(to, amount), from the above from address. – Tudor Constantin Nov 7 '18 at 14:24

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.