9

When we call a contract method, what will be the gas limit?

Is there any other gas limit besides the block gas limit? If there are any others, can we manually set the gas limit when we call a method?

1 Answer 1

7

When an externally owned account (EOA) signs a transaction, their client will usually estimate the gas requirement and provide enough to make it work. This doesn't always work out as expected.

Clients can specify the amount of gas to send, and this amount will be maximum burned. In a Web3 sendTransaction they would add {gas: 50000}, for example.

Whatever gas arrives is the most a contract function will ever have available, and it gets burned as the function executes, step by step. When the contract calls another contract, things get more interesting.

The default is to send most (63/64) available gas so the invoked contract function can consume what it needs and then the caller will carry on with whatever remains.

This works reasonably well in most cases, but the developer might want to limit how much gas to send. This can be done with a Solidity syntax that's a little more raw, as described here: How to specify gas/value when making a call using abstract contracts?

Also, following various "hostile" contracts up to mischief with re-entrance attacks, it was decided that the routine transfer (and older send syntax) method should provide only a stipend of 2,300 gas instead of most of the available gas. This, to ensure receiving fallback functions only have enough gas to perform a little accounting and possibly emit an event, but no more.

Hope it helps.

4
  • " This doesn't always work out as expected" , you can make a Call() to the contract and get back Consumed Gas which may be used as Gas Limit in a real transaction with a 99% probability being successful. This way you can decide in advance if the amount of gas spent is too high and reject issuing the transaction without spending anything.
    – Nulik
    Jul 14, 2018 at 17:48
  • The above isn't totally correct. When a contract function is called. The gas sent is 63 / 64 of the remaining gas. Not all the remaining gas. Nov 4, 2019 at 18:26
  • Good catch. I corrected the answer so it isn't misleading about EIP150. "Most" of the gas. Nov 5, 2019 at 4:31
  • @Nulik in which environment woudl you make the Call() to the contract? Javascript? Using ether.js? Some sort of HTTP RPC by Infura? Or you're talking about doing it within solidity? How can we estimate the amount of gas that will be spent by a transaction, without actually submitting it first? You said you can do this "in advance"... how? Apr 1, 2022 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.