4

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

2
  • 1
    how is the transactionLock defined? is that set to false? Nov 7, 2018 at 12:48
  • 1
    yes it is initailized to false in constructor
    – Ajit Soman
    Nov 7, 2018 at 12:49

1 Answer 1

1

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

8
  • 1
    I am following this link for this contract. theethereum.wiki/w/index.php/ERC20_Token_Standard
    – Ajit Soman
    Nov 7, 2018 at 12:53
  • 1
    When i am executing the same code with javascriptVM . it gives me this error The constructor should be payable if you send value.
    – Ajit Soman
    Nov 7, 2018 at 12:57
  • 1
    the transferFrom() function I gave you is the one from openZeppelin: github.com/OpenZeppelin/openzeppelin-solidity/blob/master/… plus your added modifications Nov 7, 2018 at 12:57
  • 1
    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 .
    – Ajit Soman
    Nov 7, 2018 at 14:03
  • 1
    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. Nov 7, 2018 at 14:24

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.