1

solc estimates an infinite gas usage from _transfer and I am unsure why. I did find that if I comment out the Transfer(_from, _to, _amount) line, the gas usage becomes finite.

Here is the minimum code to reproduce the issue:

pragma solidity ^0.4.19;

contract Token {

    event Transfer(address indexed _from, address indexed _to, uint _value);

    struct Balance {
        uint amount;
        uint lastUpdated;
    }

    mapping (address => Balance) public balances;


    function _transfer(address _from, address _to, uint _amount) internal returns (bool success) {

            // check if sender has sufficient balance
            require(balances[_from].amount >= _amount);
            //check for uint overflow
            require(balances[_to].amount + _amount > balances[_to].amount);

            balances[_from].amount -= _amount;
            balances[_to].amount += _amount;

            Transfer(_from, _to, _amount);
            return true;
        }
}

Using solc version 0.4.19+commit.c4cbbb05.Darwin.appleclang

  • did you ever find the answer? – Stormsson Jan 11 '18 at 20:57
  • no, but it ended up not being an issue—I was able to publish the contract regardless – rohanp Jan 22 '18 at 21:21
1

I have noticed this problem with Zeppelin BasicToken contract. The answer I have found is that SAfeMath operations cannot be analyzed statically so safe math causes this error.

 function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;

}

/** * @dev Adds two numbers, throws on overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256 c) { c = a + b; assert(c >= a); return c; }

Might be that you are getting it due to this check

    require(balances[_to].amount + _amount > balances[_to].amount);

However, I don't know how this problem can be solved without removing all overflow checks. Execution is not a problem, just static analysis and .estimageGas() calls in Web3.

  • so it's not possible to estimate gas for transactions that involve a transfer? – Andy Hin Jun 8 '18 at 3:36
  • it is possible to estimate gas in most cases dynamically, but it might be impossible to do that statically at the time of compilation - this is what is all about. So you get warnings about potential infinite gas consumption at the time of compilation – randaman Jun 11 '18 at 8:23

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.