code-423n4 / 2022-03-biconomy-findings

0 stars 0 forks source link

Gas Optimizations #176

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

LiquidityPool.sol

LiquidityPool.sol::140 - uint256 liquidityPoolBalance = liquidityProviders.getCurrentLiquidity(tokenAddress);
(this extra MSTORE is unneeded. Since the variable is only used once just read directly from storage and save an MSTORE/MLOAD)
LiquidityPool.sol::525 -  if (transferFeePerc > tokenManager.getTokensInfo(tokenAddress).equilibriumFee) 
(multiple reads to tokenManager.getTokensInfo(tokenAddress).equilibriumFee, save as a local variable to save gas)

ExecutorManager.sol -

ExecutorManager.sol::l20 - require(executorStatus[msg.sender], "You are not allowed to perform this operation");
(Shorten revert string to <32 bytes)
ExecutorManager.sol::36 and ExecutorManager.sol::52 - for (uint256 i = 0; i < executorArray.length; ++i) {
(The increment in for loop post condition can be made unchecked. Array length is read each iteration - uses more gas than caching len outside of loop.)

LiquidityFarming.sol -

 LiquidityFarming.sol::32 -  can pack address and bool together to save 1 storage slot
 LiquidityFarming.sol::190 - (bool success, ) = payable(_to).call{value: _amount}("");
 (no need to recast address payable _to as payable again)
 LiquidityFarming.sol::224 -  totalSharesStaked[baseToken] += amount;
 (can be done in an unchecked block to save gas, no risk of overflow)
 LiquidityFarming.sol::286 - --i;
 (can be done in an unchecked block to save gas, no risk of underflow)

 LiquidityFarming.sol::135 - if (amount > 0) {
 LiquidityFarming.sol::321 - if (totalSharesStaked[_baseToken] > 0) {
 (This is disputed. != is a cheaper than > for unit comparision.)
 3 optims:
 LiquidityFarming.sol::236 - for (index = 0; index < nftsStakedLength; ++index) {
 (The increment in for loop post condition can be made unchecked. Array length is read each iteration - uses more gas than caching len outside of loop. != is a cheaper than > for unit comparision. (Compiler optimizes this, might remove.))

LiquidityProviders.sol -

  LiquidityProviders.sol::69 - modifier onlyValidLpToken(uint256 _tokenId, address _transactor) {
  (Putting lpToken in memory saves gas)
  LiquidityProviders.sol::182 - if (supply > 0) {
  LiquidityProviders.sol::239 - require(_amount > 0, "ERR__AMOUNT_IS_0");
  LiquidityProviders.sol::283 - require(_amount > 0, "ERR__AMOUNT_IS_0");
  LiquidityProviders.sol::410 - require(lpFeeAccumulated > 0, "ERR__NO_REWARDS_TO_CLAIM");
  (This is disputed. != is a cheaper than > for unit comparision. (Compiler optimizes this, might remove.))

TokenManager.sol -

 TokenManager.sol::93 -  function setDepositConfig(uint256[] memory toChainId, address[] memory tokenAddresses,TokenConfig[] memory tokenConfig)
 (Calldata can be used as the location instead since the array values are only read)
 3 optims: TokenManager.sol::102 - for (uint256 index = 0; index < tokenConfig.length; ++index) {
 (The increment in for loop post condition can be made unchecked. Intitializes with default unit256 value, uses more gas. (Compiler optimizes this, might remove) Array length is read each iteration - uses more gas than caching len outside of loop.)

WhitelistPeriodManager.sol -

 3 optims:   
 WhitelistPeriodManager.sol::270 - for (uint256 i = 0; i < _addresses.length; ++i) {
 WhitelistPeriodManager.sol::323 - for (uint256 i = 0; i < _tokens.length; ++i) {
 (The increment in for loop post condition can be made unchecked. Intitializes with default unit256 value, uses more gas.(Compiler optimizes this, might remove) Array length is read each iteration - uses more gas than caching len outside of loop.)
 WhitelistPeriodManager.sol::356 - uint256 maxLp = 0;
 (Shorten revert string to =< 32 bytes)

LPToken.sol -

 3 optims:
 LPToken.sol::142 - for (uint256 i = 0; i < nftIds.length; ++i) {
 (The increment in for loop post condition can be made unchecked. Intitializes with default unit256 value, uses more gas. (Compiler optimizes this, might remove.) Array length is read each iteration - uses more gas than caching len outside of loop.)
 LPToken.sol::87 - require(_whiteListPeriodManager != address(0), "ERR_INVALID_WHITELIST_PERIOD_MANAGER");
 (Shorten revert string to =< 32 bytes)