Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve gas efficiency of SafeERC20.forceApprove when the token returns a bool on approve #4925

Open
fedgiac opened this issue Feb 28, 2024 · 0 comments

Comments

@fedgiac
Copy link

fedgiac commented Feb 28, 2024

馃 Motivation

More gas-efficient implementation of SafeERC20.forceApprove in the case where the token returns a bool on approve.
Expected savings are a little more than a call to EXTCODESIZE, that is 2600 gas in the worst case of no access lists or 100 gas otherwise.
The required code changes are very small.

馃摑 Details

forceApprove is the only function calling _callOptionalReturnBool (code).
This function is supposed to behave like _callOptionalReturn except that if the inner call reverts then it returns false instead of reverting.
The output is currently computed as:

success && (returnsEmptyOutput || returnsBoolTrue) && targetHasCode

My proposal it to change this expression to:

success && ((returnsEmptyOutput && targetHasCode) || returnsBoolTrue)

With the proposed change, if returnsEmptyOutput is false then the evalutation of the second && short-circuits and the code is assumed to have code.
This is the same assumption made in _callOptionalReturn through functionCall.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant