Using both named returns and a return statement isn’t necessary
Removing unused named returns variables can reduce gas usage (MSTOREs/MLOADs) and improve code clarity. To save gas and improve code quality: consider using only one of those.
This can be found in the following;
NFTEscrow.sol, LPFarming.sol, yVaultLPFarming.sol, CryptoPunksHelper.sol , EtherRocksHelper.sol, StrategyPUSDConvex.sol, Controller.sol, YVault.sol, FungibleAssetVaultForDAO.sol, NFTVault.sol
Strings are broken into 32 byte chunks for operations. Error strings in reverts more than 32 bytes consume more gas. Therefore, error messages less than 32 bytes shold be selected. If possible, custom error messages consume much more less and supported by Solidity ^0.8.0.
E.g.
function mint(address to, uint256 amount) external {
require(
hasRole(MINTER_ROLE, _msgSender()),
"JPEG: must have minter role to mint"
);
_mint(to, amount);
}
Also the variable names more than 32 bytes is recommended to reduce to save more gas. Below is the list
At Controller.sol : inCaseStrategyTokensGetStuck()
At NFTVault.sol : setInsuranceLiquidationPenaltyRate(),_validateCreditLimitAndLiquidationRate(),
By minimising the number of SLOADs which are expensive (100 gas) compared to MLOADs/MSTOREs (3 gas), SLOADs calls can be cached to memory variables to turn the proces into MLOADs. And prior to writing to storage again, those can also be cached back. Following contract sections are having this optimization;
At LPFarming.sol, pendingReward method, cache the pool and the user to memory variables after initiliazing to save 6 SLOADs
At LPFarming.sol, deposit method, cache the pool and the user to memory variables after initiliazing to save 3 SLOADs
At LPFarming.sol, withdraw method, cache the pool and the user to memory variables after initiliazing to save 3 SLOADs
At LPFarming.sol, _updatePool method, cache the pool to a memory variable after initiliazing to save 8 SLOADs
At NFTVault.sol, borrow method, cache the position to a memory variable after initiliazing to save 9 SLOADs
Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack.
Caching the array length in the stack saves around 3 gas per iteration. The optimization can be applied to following contract sections:
At LPFarming.sol, claimAll method, caching poolInfo.length
At StrategyPUSDConvex.sol, harvest method, caching rewardTokens.length
Also using unchecked {++i} for all the iterations in the contracts will bring more gas saving (for >= 0.8).
GAS OPTIMIZATIONS
Using both named returns and a return statement isn’t necessary Removing unused named returns variables can reduce gas usage (MSTOREs/MLOADs) and improve code clarity. To save gas and improve code quality: consider using only one of those. This can be found in the following;
NFTEscrow.sol
,LPFarming.sol
,yVaultLPFarming.sol
,CryptoPunksHelper.sol
,EtherRocksHelper.sol
,StrategyPUSDConvex.sol
,Controller.sol
,YVault.sol
,FungibleAssetVaultForDAO.sol
,NFTVault.sol
Strings are broken into 32 byte chunks for operations. Error strings in reverts more than 32 bytes consume more gas. Therefore, error messages less than 32 bytes shold be selected. If possible, custom error messages consume much more less and supported by Solidity ^0.8.0. E.g.
Also the variable names more than 32 bytes is recommended to reduce to save more gas. Below is the list
At Controller.sol :
inCaseStrategyTokensGetStuck()
At NFTVault.sol :setInsuranceLiquidationPenaltyRate()
,_validateCreditLimitAndLiquidationRate()
,By minimising the number of SLOADs which are expensive (100 gas) compared to MLOADs/MSTOREs (3 gas), SLOADs calls can be cached to memory variables to turn the proces into MLOADs. And prior to writing to storage again, those can also be cached back. Following contract sections are having this optimization;
LPFarming.sol
,pendingReward
method, cache thepool
and theuser
to memory variables after initiliazing to save 6 SLOADsLPFarming.sol
,deposit
method, cache thepool
and theuser
to memory variables after initiliazing to save 3 SLOADsLPFarming.sol
,withdraw
method, cache thepool
and theuser
to memory variables after initiliazing to save 3 SLOADsLPFarming.sol
,_updatePool
method, cache thepool
to a memory variable after initiliazing to save 8 SLOADsNFTVault.sol
,borrow
method, cache theposition
to a memory variable after initiliazing to save 9 SLOADsReading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack. Caching the array length in the stack saves around 3 gas per iteration. The optimization can be applied to following contract sections:
LPFarming.sol
,claimAll
method, cachingpoolInfo.length
StrategyPUSDConvex.sol
,harvest
method, cachingrewardTokens.length