1

I have a smart contract:

pragma solidity 0.5.3;

import "./ERC20.sol";

contract Transfer {

  ERC20 private erc20iface;
  address private _owner;

  modifier validOwner() {
    require(msg.sender == _owner);
    _;
  }

  constructor() public {
    _owner = msg.sender;
  }

  function () external payable {}

  function transferTo(address[] memory receivers, uint[] memory amounts, address token) {
    //Some code uses ERC20 methods/or simple transfers with .call()
    }
 }

When I try to deploy the contract in Remix it shows the transaction cost = 424652 gas. However when I try to estimate gas limit with web3 .estimateGas()

const contractData = {
      data: '0x123',
      arguments: []
    };

    const contract = new web3.eth.Contract(CONTRACT_ABI).deploy(
      contractData
    );
    const gasLimit = await contract.estimateGas();

I get gasLimit = 409652 And when I try to deploy, I receive an error: Out of gas

But if I use the amount of gas from Remix (424652) it works fine.

What can be the reason of such differences (15000 gas, like if .estimateGas is missing ERC20 import)? And how to estimate the gasLimit with .estimateGas() correctly?

1
  • Different compiler perhaps? Commented Jun 27, 2019 at 15:48

1 Answer 1

0

Since you aren't including the creator address in the estimateGas() version, it defaults to the 0x0 address. So in the constructor, it sets _owner = 0x0, which only costs 5k. The Remix version uses a non-zero creator though, which causes a 20k cost. That accounts for the 15k savings in estimateGas()

5
  • 1
    I think I'd do some clarification work on "causing the 15k savings over the Remix version which uses a non-zero creator address" (the contextual grammar here is rather unclear IMO). Commented Jun 27, 2019 at 15:50
  • haha i'd noticed I worded it poorly, was editing it while you commented
    – natewelch_
    Commented Jun 27, 2019 at 15:50
  • I actually copied it after the change, and it still isn't very clear (though a little clearer than the previous version). Why would a 5k cost lead to a 15k savings? You mean, setting to 0 costs 5k while setting to non-zero costs 20k? The math would be a whole lot more obvious if you phrased it this way. Commented Jun 27, 2019 at 15:52
  • does the current version make more sense? feel free to edit if you think you could make it clearer
    – natewelch_
    Commented Jun 27, 2019 at 15:53
  • Yes, though probably also worth mentioning that by "creator" you mean "the deployer account", and also, you could add {from: "0x0"} to the estimateGas call, so that this user will understand where he or she should have specified that "creator". Commented Jun 27, 2019 at 15:55

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.