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?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityWhen 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.
" 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.