3

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

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 all 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 all 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.

  • " 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 '18 at 17:48

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.