4

Is this always necessary?

If for example you are increasing the supply from an external contract, and the supply is already at max, the function will fail and hence revert. There's no need for a return bool to return false as the tx will fail either way.

Why do we include the return bool? Isn't it just wasting gas?

1
  • Could you provide links to the functions you are talking about? Commented Apr 15, 2018 at 15:39

1 Answer 1

2

There are a couple reasons.

  1. If the function is used internally (e.g. without a message call), meaning a revert would undo the entire call, so you would just return if it failed or succeeded instead of failing the call.

  2. Prior to the Byzantium hardfork, the only way to fail a call was by using an invalid opcode (the opcode 0xfe, INVALID, is the official byte for this) which ate all the gas sent to the call. If you returned true/false to indicate failure/success, you could instead control how much gas it ate. Now there is the REVERT opcode which undoes the message call but doesn't eat all the unused gas.

If the function is external only, e.g. #1 above doesn't apply to you, then you don't need to do this true/false anymore and you can just use the REVERT opcode (the "require" function in Solidity).

Realistically #1 shouldn't be an issue either because it's a good pattern to not have functions callable externally and internally, e.g. you should have external function withdraw which calls the internal function _withdraw, and other functions within the contract can call _withdraw as well.

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.