1. Cache secondaryReserveBalance in _chargeFee() function can save gas
In NibbleVault._chargeFee() function, secondaryReserveBalance is loaded from storage 2 times and stored in storage 1 time. We can save gas by loading it to memory to do calculation and only save to storage after all. For example
2. Should use _feeAdmin in check before transfer ETH to factory
In _chargeFee() function, it checks if _adminFeeAmt > 0 before sending ETH to factory. _adminFeeAmt is not sent amount but _feeAdmin and when _amount is small, _feeAdmin can be 0 even though _adminFeeAmt > 0 due to division round down. In that case, transfer 0 wei is unnecessary and cost gas. So we should check _feeAdmin > 0 instead.
1. Cache
secondaryReserveBalance
in_chargeFee()
function can save gasIn
NibbleVault._chargeFee()
function,secondaryReserveBalance
is loaded from storage 2 times and stored in storage 1 time. We can save gas by loading it to memory to do calculation and only save to storage after all. For exampleAffected Code
https://github.com/code-423n4/2022-06-nibbl/blob/8c3dbd6adf350f35c58b31723d42117765644110/contracts/NibblVault.sol#L222-L226
2. Should use
_feeAdmin
in check before transfer ETH tofactory
In
_chargeFee()
function, it checks if_adminFeeAmt > 0
before sending ETH tofactory
._adminFeeAmt
is not sent amount but_feeAdmin
and when_amount
is small,_feeAdmin
can be 0 even though_adminFeeAmt > 0
due to division round down. In that case, transfer 0 wei is unnecessary and cost gas. So we should check_feeAdmin > 0
instead.Affected Codes
https://github.com/code-423n4/2022-06-nibbl/blob/8c3dbd6adf350f35c58b31723d42117765644110/contracts/NibblVault.sol#L227
https://github.com/code-423n4/2022-06-nibbl/blob/8c3dbd6adf350f35c58b31723d42117765644110/contracts/NibblVault.sol#L243
3. Should use constant for
UPDATE_TIME
to save gasUPDATE_TIME
is not changed anywhere in the codebase, so it should be markedconstant
to save gasAffected Codes
https://github.com/code-423n4/2022-06-nibbl/blob/8c3dbd6adf350f35c58b31723d42117765644110/contracts/Utilities/NibblVaultFactoryData.sol#L6