-
Notifications
You must be signed in to change notification settings - Fork 0
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gas Optimizations #31
Comments
Less clean, not added |
Valid, will add |
Less clean, won't add |
Acknowledge 1 and 3 |
[S]: Suggested optimation, save a decent amount of gas without compromising readability;
[M]: Minor optimation, the amount of gas saved is minor, change when you see fit;
[N]: Non-preferred, the amount of gas saved is at cost of readability, only apply when gas saving is a top priority.
[S]
ERC20.sol#transferFrom()
Do not reduce approval on transferFrom if current allowance is type(uint256).maxThe Wrapped Ether (WETH) ERC-20 contract has a gas optimization that does not update the allowance if it is the max uint.
The latest version of OpenZeppelin's ERC20 token contract also adopted this optimization.
https://github.com/maple-labs/erc20/blob/10ccf4aa0b2d6914e3c2d32e454e4d106a99a4fd/contracts/ERC20.sol#L109-L113
See:
Recommendation
Change to:
[S] Use immutable variables can save gas
https://github.com/maple-labs/erc20/blob/10ccf4aa0b2d6914e3c2d32e454e4d106a99a4fd/contracts/ERC20.sol#L25-L26
https://github.com/maple-labs/erc20/blob/10ccf4aa0b2d6914e3c2d32e454e4d106a99a4fd/contracts/ERC20.sol#L50-L54
In
ERC20.sol
,name
andsymbol
will never change, use immutable variable instead of storage variable can save gas.[M] Validation can be done earlier to save gas
https://github.com/maple-labs/erc20/blob/10ccf4aa0b2d6914e3c2d32e454e4d106a99a4fd/contracts/ERC20.sol#L75-L102
Check if
owner_ != address(0)
earlier can avoid unnecessary computing when this check failed.Recommendation
Change to:
The text was updated successfully, but these errors were encountered: