If a function is using the gasleft()
function and is called by another smart contract, does it return the total gas left for the transaction or just the gas left passed to the function via the message call?
2 Answers
It's the remaining gas given to the sub smart contract. For instance, setting a high gas limit on the main contract such as 100000 and then executing othercontract.functionToCall{gas: 3000}()), if the first instruction is gasleft() in othercontract.functionToCall(), it will show a very low value of less than 3000. You can test it yourself, the comment by @Ismael is wrong.
If an external contract calls the function of another contract without specifying a gas value, the gasLeft() is the remaining gas. However, if the external contract call specifies the gas to use (e.g. contract.functionToCall{gas: 3000}()
) then the gas is only the amount (here 3000) passed to the function call
-
@kOnIdk Welcome to the Ethereum Stack Exchange! Your answer was auto-flagged as low-quality. I will clean it up and it will help you too since you were downvoted. Feel free to check ethereum.stackexchange.com/help/how-to-answer– eth ♦Commented Sep 24, 2021 at 1:14