[G-01] Unnecessary variable initialization of default value
When variable is not initialized, it will have its default values.
Example: 0 for uint, false for bool and address(0) for address
I suggest removing default value initialization for following variables.
NotionalTradeModule.sol:48: address internal constant ETH_ADDRESS = address(0);
NotionalTradeModule.sol:238: for(uint256 i = 0; i < modules.length; i++) {
NotionalTradeModule.sol:254: for(uint256 i = 0; i < modules.length; i++) {
NotionalTradeModule.sol:393: for(uint256 i = 0; i < positionsLength; i++) {
NotionalTradeModule.sol:519: uint32 minImpliedRate = 0;
NotionalTradeModule.sol:605: for(uint256 i = 0; i < positionsLength; i++) {
NotionalTradeModule.sol:618: for(uint256 i = 0; i < positionsLength; i++) {
For example these can change to:
address internal constant ETH_ADDRESS;
for (uint256 i; i < modules.length; i++) {
uint32 minImpliedRate;
[G-02] Save Gas in For-Loops by storing array's length as a variable
3 gas per iteration can be saved by storing an array's length as a variable before
the for-loop.
Issue found at:
NotionalTradeModule.sol:238: for(uint256 i = 0; i < modules.length; i++) {
NotionalTradeModule.sol:254: for(uint256 i = 0; i < modules.length; i++) {
For example, I suggest changing it to:
modulesLength = modules.length
for (uint i; i < modulesLength; i++) {
[G-03] ++i costs less gas than i++
It is better to use ++i than i++ when possible since it costs less gas.
Issue found at:
NotionalTradeModule.sol:238: for(uint256 i = 0; i < modules.length; i++) {
NotionalTradeModule.sol:254: for(uint256 i = 0; i < modules.length; i++) {
NotionalTradeModule.sol:393: for(uint256 i = 0; i < positionsLength; i++) {
NotionalTradeModule.sol:605: for(uint256 i = 0; i < positionsLength; i++) {
NotionalTradeModule.sol:610: numFCashPositions++;
NotionalTradeModule.sol:618: for(uint256 i = 0; i < positionsLength; i++) {
NotionalTradeModule.sol:623: j++;
[G-04] Not Defining Variables to Reduce Gas
Certain variables is defined even though they are used only once.
Remove these unnecessary variables to save gas.
For cases where it will reduce the readability, one can use comments to help describe
what the code is doing.
Remove wrappedfCashAddress of _deployWrappedfCash function at NotionalTradeModule.sol
[G-07] Reduce the long revert strings of error messages
By keeping the revert strings within 32 bytes will save you gas since each slot is 32 bytes.
Following are revert strings that are more than 32 bytes.
NotionalTradeModule.sol:169: require(_setToken.isComponent(address(_sendToken)), "Send token must be an index component");
NotionalTradeModule.sol:199: require(_setToken.isComponent(address(wrappedfCash)), "FCash to redeem must be an index component");
NotionalTradeModule.sol:378: require(wrappedfCashAddress.isContract(), "WrappedfCash not deployed for given parameters");
NotionalTradeModule.sol:573: require(_paymentToken == assetToken, "Token is neither asset nor underlying token");
[G-08] Remove unused parameter
There are unused parameters in the contract.
It is possible to save gas by removing these parameters or comment out the uint256 as well.
wfCashBase.sol (remove returns variable "assetToken", "underlyingPrecision" and "tokenType")
137: function getAssetToken() public view override returns (IERC20 assetToken, int256 underlyingPrecision, TokenType tokenType) {
139: return (IERC20(asset.tokenAddress), asset.decimals, asset.tokenType);
[G-10] Use multiple require instead of &&
When there are multiple conditions in require statement, break down the require statement into
multiple require statements instead of using && can save gas.
Issue found at
wfCashLogic.sol
116: require(
117: msg.sender == address(NotionalV2) &&
118: // Only accept the fcash id that corresponds to the listed currency and maturity
119: _id == fCashID &&
120: // Protect against signed value underflows
121: int256(_value) > 0,
122: "Invalid"
123: );
[G-01] Unnecessary variable initialization of default value
When variable is not initialized, it will have its default values. Example: 0 for uint, false for bool and address(0) for address
I suggest removing default value initialization for following variables.
For example these can change to:
[G-02] Save Gas in For-Loops by storing array's length as a variable
3 gas per iteration can be saved by storing an array's length as a variable before the for-loop.
Issue found at:
For example, I suggest changing it to:
[G-03] ++i costs less gas than i++
It is better to use ++i than i++ when possible since it costs less gas.
Issue found at:
[G-04] Not Defining Variables to Reduce Gas
Certain variables is defined even though they are used only once. Remove these unnecessary variables to save gas. For cases where it will reduce the readability, one can use comments to help describe what the code is doing.
Originally
Can be changed to
Originally
Can be changed to
Originally
Can be changed to
[G-05] Use defined variable to save gas at wfCashERC4626.sol
Since totalSupply() is already defined as supply at line 53, use supply variable at line 60 as well.
can be changed to
[G-06] != 0 costs less gass then > 0
!= 0 costs less gas when optimizer is enabled and is used for unsigned integers in "require" statement. I suggest changing > 0 to != 0
Issue found at:
[G-07] Reduce the long revert strings of error messages
By keeping the revert strings within 32 bytes will save you gas since each slot is 32 bytes.
Following are revert strings that are more than 32 bytes.
[G-08] Remove unused parameter
There are unused parameters in the contract. It is possible to save gas by removing these parameters or comment out the uint256 as well.
Issue found at
Change it to
[G-09] Both named returns and return statement are used
Removing unused named returns variable in below code can save gas and improve code readability.
Issue found at
[G-10] Use multiple require instead of &&
When there are multiple conditions in require statement, break down the require statement into multiple require statements instead of using && can save gas.
Issue found at
For example these can be changed to