[Ethereum] Remix: Gas required exceeds allowance or always failing transaction

contract-debuggingcontract-developmentremixsoliditytransactions

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

Best Answer

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

Related Topic