code-423n4 / 2022-04-backd-findings

6 stars 4 forks source link

Gas Optimizations #192

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

C4-001: Revert String Size Optimization

Impact

Shortening revert strings to fit in 32 bytes will decrease deploy time gas and will decrease runtime gas when the revert condition has been met.

Revert strings that are longer than 32 bytes require at least one additional mstore, along with additional overhead for computing memory offset, etc.

Proof of Concept

Revert strings > 32 bytes are here:

https://github.com/code-423n4/2022-04-backd/blob/c856714a50437cb33240a5964b63687c9876275b/backd/libraries/EnumerableMapping.sol#L104

Tools Used

Manual Review

Recommended Mitigation Steps

Shorten the revert strings to fit in 32 bytes. That will affect gas optimization.

C4-002 : Adding unchecked directive can save gas

Impact

For the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.

Proof of Concept

  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::133 => uint256 totalAvailableToWithdraw = 0;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::310 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/Controller.sol::114 => uint256 totalEthRequired = 0;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/Controller.sol::117 => for (uint256 i = 0; i < numActions; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/StakerVault.sol::144 => uint256 startingAllowance = 0;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/StakerVault.sol::260 => for (uint256 i = 0; i < actions.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/access/RoleManager.sol::80 => for (uint256 i = 0; i < roles.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::188 => for (uint256 i = 0; i < protocols.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::452 => uint256 totalEthRequired = 0;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::456 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::479 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::506 => for (uint256 i = 0; i < howMany; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::891 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::43 => for (uint256 i = 0; i < users.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::46 => for (uint256 j = 0; j < positions.length; j++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::72 => for (uint256 i = 0; i < keys.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::93 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::165 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CTokenRegistry.sol::61 => for (uint256 i = 0; i < ctokens.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CompoundHandler.sol::135 => for (uint256 i = 0; i < assets.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/pool/LiquidityPool.sol::483 => uint256 currentFeeRatio = 0;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::313 => for (uint256 i = 0; i < _rewardTokens.length(); i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::380 => for (uint256 i = 0; i < _rewardTokens.length(); i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/testing/MockStableSwap.sol::29 => uint256 total = 0;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/testing/MockStableSwap.sol::30 => for (uint256 i = 0; i < 3; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/testing/MockStableSwap.sol::41 => uint256 total = 0;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/testing/MockStableSwap.sol::42 => for (uint256 i = 0; i < 2; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/testing/MockStableSwap.sol::70 => for (uint256 i = 0; i < 3; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::91 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::105 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::109 => for (uint256 i = 0; i < stakerVaults.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::114 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::166 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::191 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::259 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::283 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::357 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::381 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::403 => bool keeperGaugeExists = false;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::404 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::445 => for (uint256 i = 0; i < length; i++) {

Tools Used

None

Recommended Mitigation Steps

Consider applying unchecked arithmetic where overflow/underflow is not possible. Example can be seen from below.

Unchecked{i++};

C4-003 : Cache array length in for loops can save gas

Impact

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.

Proof of Concept

  1. Navigate to the following smart contract line.
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::313 => for (uint256 i = 0; i < _rewardTokens.length(); i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::380 => for (uint256 i = 0; i < _rewardTokens.length(); i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CTokenRegistry.sol::61 => for (uint256 i = 0; i < ctokens.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CompoundHandler.sol::135 => for (uint256 i = 0; i < assets.length; i++) {

Tools Used

None

Recommended Mitigation Steps

Consider to cache array length.

C4-004 : Non-strict inequalities are cheaper than strict ones

Impact

Strict inequalities add a check of non equality which costs around 3 gas.

Proof of Concept

  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/AddressProvider.sol::149 => return _vaults.length();
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/AddressProvider.sol::365 => return _tokenToPools.length();
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::135 => uint256 length = stashedWithdraws.length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::136 => require(length > 0, "No entries");
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::137 => uint256 i = length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::143 => stashedWithdraws[i] = stashedWithdraws[stashedWithdraws.length - 1];
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::309 => uint256 length = _replacedRewardTokens.length();
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::310 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/Controller.sol::116 => uint256 numActions = actions.length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/StakerVault.sol::260 => for (uint256 i = 0; i < actions.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/access/RoleManager.sol::80 => for (uint256 i = 0; i < roles.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/access/RoleManager.sol::127 => return _roleMembers[role].length();
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::185 => require(protocols.length == handlers.length, Error.INVALID_ARGUMENT);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::188 => for (uint256 i = 0; i < protocols.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::455 => uint256 length = userRecordsMeta.length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::456 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::477 => uint256 length = _supportedProtocols.length();
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::478 => bytes32[] memory protocols = new bytes32[](length);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::479 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::499 => uint256 length = _usersWithPositions.length();
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::500 => if (cursor >= length) return (new address[](0), 0);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::501 => if (howMany >= length - cursor) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::502 => howMany = length - cursor;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::878 => if (_userPositions[payer].length == 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::890 => uint256 length = positionsMeta.length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::891 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::894 => positionsMeta[i] = positionsMeta[length - 1];
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::42 => if (users.length == 0) return (_shortenTopups(executableTopups, topupsAdded), 0);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::43 => for (uint256 i = 0; i < users.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::46 => for (uint256 j = 0; j < positions.length; j++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::51 => uint256 offset = j == positions.length - 1 ? 1 : 0;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::71 => bool[] memory results = new bool[](keys.length);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::72 => for (uint256 i = 0; i < keys.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::91 => uint256 length = userRecordsMeta.length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::92 => ITopUpAction.RecordWithMeta[] memory result = new ITopUpAction.RecordWithMeta[](length);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::93 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::154 => * @dev Shortens a list of topups by truncating it to a given length.
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::156 => * @param length The length to trucate the list of topups to.
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::159 => function _shortenTopups(TopupData[] memory topups, uint256 length)
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::164 => TopupData[] memory shortened = new TopupData[](length);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::165 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CTokenRegistry.sol::61 => for (uint256 i = 0; i < ctokens.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CompoundHandler.sol::135 => for (uint256 i = 0; i < assets.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::313 => for (uint256 i = 0; i < _rewardTokens.length(); i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::380 => for (uint256 i = 0; i < _rewardTokens.length(); i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::90 => uint256 length = liquidityPools.length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::91 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::104 => uint256 length = _keeperGauges.length();
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::105 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::109 => for (uint256 i = 0; i < stakerVaults.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::113 => length = _ammGauges.length();
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::114 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::163 => uint256 length = pools.length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::164 => require(length == weights.length, Error.INVALID_ARGUMENT);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::166 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::189 => uint256 length = pools.length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::191 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::256 => uint256 length = lpTokens.length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::257 => require(length == weights.length, "Invalid length of arguments");
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::259 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::282 => uint256 length = lpTokens.length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::283 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::354 => uint256 length = tokens.length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::356 => require(length == weights.length, "Invalid length of arguments");
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::357 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::377 => uint256 length = tokens.length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::381 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::402 => uint256 length = _keeperGauges.length();
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::404 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::411 => if (!keeperGaugeExists && weightBasedKeeperDistributionDeactivated && length >= 1) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::444 => uint256 length = _ammGauges.length();
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::445 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/VestedEscrow.sol::93 => for (uint256 i = 0; i < amounts.length; i++) {

Tools Used

Code Review

Recommended Mitigation Steps

Use >= or <= instead of > and < when possible.

C4-005: Use of constant keccak variables results in extra hashing (and so gas).

Impact

That would Increase gas costs on all privileged operations.

Proof of Concept

The following role variables are marked as constant.

  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::943 => return keccak256(abi.encodePacked(_TOP_UP_HANDLER_KEY, protocol));
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpActionFeeHandler.sol::232 => return keccak256(abi.encodePacked(_KEEPER_GAUGE_KEY, lpToken));
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CTokenRegistry.sol::67 => keccak256(abi.encodePacked(ctoken.symbol())) == keccak256(abi.encodePacked("cETH"))
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::619 => return keccak256(abi.encodePacked(_KEEPER_WEIGHT_KEY, pool));
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::623 => return keccak256(abi.encodePacked(_AMM_WEIGHT_KEY, token));
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/InflationManager.sol::627 => return keccak256(abi.encodePacked(_LP_WEIGHT_KEY, vault));

This results in the keccak operation being performed whenever the variable is used, increasing gas costs relative to just storing the output hash. Changing to immutable will only perform hashing on contract deployment which will save gas.

See: ethereum/solidity#9232 (https://github.com/ethereum/solidity/issues/9232#issuecomment-646131646)

Tools Used

Code Review

Recommended Mitigation Steps

Consider to change the variable to be immutable rather than constant.

C4-006 : Check if amount > 0 before token transfer can save gas

Impact

Since _amount can be 0. Checking if (_amount != 0) before the transfer can potentially save an external call and the unnecessary gas cost of a 0 token transfer.

Proof of Concept

 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::62 => stakerVault.transferFrom(payer, address(this), amountLeft);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::291 => staker.transfer(payer, position.depositTokenBalance);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::692 => IStakerVault(vault).transfer(payer, position.depositTokenBalance);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CompoundHandler.sol::79 => bool success = ctoken.transfer(addr, mintedTokens);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/pool/EthPool.sol::30 => to.transfer(amount);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdEthCvx.sol::77 => payable(vault).transfer(amount);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdEthCvx.sol::93 => payable(vault).transfer(amount);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdEthCvx.sol::117 => payable(vault).transfer(underlyingBalance);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/StrategySwapper.sol::63 => weth_.transferFrom(msg.sender, address(this), wethBalance_);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol::62 => IERC20(fromToken).transferFrom(msg.sender, address(this), swapAmount);

All Contracts

Tools Used

None

Recommended Mitigation Steps

Consider checking amount != 0.

C4-007 : There is no need to assign default values to variables

Impact - Gas Optimization

When a variable is declared solidity assigns the default value. In case the contract assigns the value again, it costs extra gas.

Example: uint x = 0 costs more gas than uint x without having any different functionality.

Proof of Concept

  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::310 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/Controller.sol::116 => uint256 numActions = actions.length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/StakerVault.sol::260 => for (uint256 i = 0; i < actions.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/access/RoleManager.sol::80 => for (uint256 i = 0; i < roles.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/access/RoleManager.sol::127 => return _roleMembers[role].length();
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::185 => require(protocols.length == handlers.length, Error.INVALID_ARGUMENT);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::188 => for (uint256 i = 0; i < protocols.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::455 => uint256 length = userRecordsMeta.length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::456 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::477 => uint256 length = _supportedProtocols.length();
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::478 => bytes32[] memory protocols = new bytes32[](length);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::479 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::499 => uint256 length = _usersWithPositions.length();
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::500 => if (cursor >= length) return (new address[](0), 0);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::501 => if (howMany >= length - cursor) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::502 => howMany = length - cursor;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::878 => if (_userPositions[payer].length == 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::890 => uint256 length = positionsMeta.length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::891 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::894 => positionsMeta[i] = positionsMeta[length - 1];
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::42 => if (users.length == 0) return (_shortenTopups(executableTopups, topupsAdded), 0);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::43 => for (uint256 i = 0; i < users.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::46 => for (uint256 j = 0; j < positions.length; j++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::51 => uint256 offset = j == positions.length - 1 ? 1 : 0;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::71 => bool[] memory results = new bool[](keys.length);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::72 => for (uint256 i = 0; i < keys.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::91 => uint256 length = userRecordsMeta.length;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::92 => ITopUpAction.RecordWithMeta[] memory result = new ITopUpAction.RecordWithMeta[](length);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::93 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::154 => * @dev Shortens a list of topups by truncating it to a given length.
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::156 => * @param length The length to trucate the list of topups to.
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::159 => function _shortenTopups(TopupData[] memory topups, uint256 length)
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::164 => TopupData[] memory shortened = new TopupData[](length);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpKeeperHelper.sol::165 => for (uint256 i = 0; i < length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CTokenRegistry.sol::61 => for (uint256 i = 0; i < ctokens.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CompoundHandler.sol::135 => for (uint256 i = 0; i < assets.length; i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::313 => for (uint256 i = 0; i < _rewardTokens.length(); i++) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::380 => for (uint256 i = 0; i < _rewardTokens.length(); i++) {

Tools Used

Code Review

Recommended Mitigation Steps

uint x = 0 costs more gas than uint x without having any different functionality.

C4-008 : Use Shift Right/Left instead of Division/Multiplication if possible

Impact

A division/multiplication by any number x being a power of 2 can be calculated by shifting log2(x) to the right/left.

While the DIV opcode uses 5 gas, the SHR opcode only uses 3 gas. Furthermore, Solidity's division operation also includes a division-by-0 prevention which is bypassed using shifting.

Tools Used

None

Recommended Mitigation Steps

A division/multiplication by any number x being a power of 2 can be calculated by shifting log2(x) to the right/left.

C4-009: > 0 can be replaced with != 0 for gas optimization

Impact

!= 0 is a cheaper operation compared to > 0, when dealing with uint.

Proof of Concept

  1. Navigate to the following contracts.
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::90 => require(amount > 0, Error.INVALID_AMOUNT);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::91 => require(totalLockedBoosted > 0, Error.NOT_ENOUGH_FUNDS);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::136 => require(length > 0, "No entries");
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::138 => while (i > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::253 => if (userBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::300 => if (userBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::308 => if (lastUpdated[user] < lastMigrationEvent && userBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/CvxCrvRewardsLocker.sol::173 => if (cvxcrvBal > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/CvxCrvRewardsLocker.sol::261 => if (IERC20(CVX_CRV).balanceOf(address(this)) > 0)
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/LpToken.sol::87 => if (amount > 0) ILiquidityPool(minter).handleLpTokenTransfer(from, to, amount); // add check to not break 0 transfers
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::47 => if (depositAmount > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::57 => if (amountLeft > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::210 => require(record.singleTopUpAmount > 0, Error.INVALID_AMOUNT);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::554 => require(position.totalTopUpAmount > 0, Error.INSUFFICIENT_BALANCE);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::690 => if (position.depositTokenBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::907 => if (IERC20(token).allowance(address(this), spender) > 0) return;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpActionFeeHandler.sol::123 => require(totalClaimable > 0, Error.NOTHING_TO_CLAIM);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/AaveHandler.sol::58 => if (variableDebt + stableDebt > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/pool/LiquidityPool.sol::401 => require(_depositCap > 0, Error.INVALID_AMOUNT);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/pool/LiquidityPool.sol::471 => require(underlyingAmount > 0, Error.INVALID_AMOUNT);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/pool/LiquidityPool.sol::473 => require(lpToken_.balanceOf(account) > 0, Error.INSUFFICIENT_BALANCE);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/pool/LiquidityPool.sol::549 => require(redeemLpTokens > 0, Error.INVALID_AMOUNT);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdEthCvx.sol::100 => if (stakedBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdEthCvx.sol::106 => if (lpBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::151 => if (underlyingBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::159 => if (hopLpBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::211 => if (hopLpBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::358 => if (stakedBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::364 => if (lpBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::399 => if (cvxCommunityReserveShare_ > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::402 => if (cvxBalance_ > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::410 => if (crvCommunityReserveShare_ > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::413 => if (crvBalance_ > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/StrategySwapper.sol::111 => require(slippageTolerance_ > 0.8e18, Error.INVALID_SLIPPAGE_TOLERANCE);

Tools Used

Code Review

Recommended Mitigation Steps

Use "!=0" instead of ">0" for the gas optimization.

C4-0010 : Free gas savings for using solidity 0.8.10+

Impact

Using newer compiler versions and the optimizer gives gas optimizations and additional safety checks are available for free.

Proof of Concept

All Contracts

Solidity 0.8.10 has a useful change which reduced gas costs of external calls which expect a return value: https://blog.soliditylang.org/2021/11/09/solidity-0.8.10-release-announcement/

Code Generator: Skip existence check for external contract if return data is expected. In this case, the ABI decoder will revert if the contract does not exist

All Contracts

Tools Used

None

Recommended Mitigation Steps

Consider to upgrade pragma to at least 0.8.10.

C4-0011 : ++i is more gas efficient than i++ in loops forwarding

Impact

++i is more gas efficient than i++ in loops forwarding.

Proof of Concept

  1. Navigate to the following contracts.
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::90 => require(amount > 0, Error.INVALID_AMOUNT);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::91 => require(totalLockedBoosted > 0, Error.NOT_ENOUGH_FUNDS);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::136 => require(length > 0, "No entries");
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::138 => while (i > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::253 => if (userBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::300 => if (userBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol::308 => if (lastUpdated[user] < lastMigrationEvent && userBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/CvxCrvRewardsLocker.sol::173 => if (cvxcrvBal > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/CvxCrvRewardsLocker.sol::261 => if (IERC20(CVX_CRV).balanceOf(address(this)) > 0)
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/LpToken.sol::87 => if (amount > 0) ILiquidityPool(minter).handleLpTokenTransfer(from, to, amount); // add check to not break 0 transfers
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::47 => if (depositAmount > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::57 => if (amountLeft > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::210 => require(record.singleTopUpAmount > 0, Error.INVALID_AMOUNT);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::554 => require(position.totalTopUpAmount > 0, Error.INSUFFICIENT_BALANCE);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::690 => if (position.depositTokenBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::907 => if (IERC20(token).allowance(address(this), spender) > 0) return;
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpActionFeeHandler.sol::123 => require(totalClaimable > 0, Error.NOTHING_TO_CLAIM);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/AaveHandler.sol::58 => if (variableDebt + stableDebt > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/pool/LiquidityPool.sol::401 => require(_depositCap > 0, Error.INVALID_AMOUNT);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/pool/LiquidityPool.sol::471 => require(underlyingAmount > 0, Error.INVALID_AMOUNT);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/pool/LiquidityPool.sol::473 => require(lpToken_.balanceOf(account) > 0, Error.INSUFFICIENT_BALANCE);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/pool/LiquidityPool.sol::549 => require(redeemLpTokens > 0, Error.INVALID_AMOUNT);
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdEthCvx.sol::100 => if (stakedBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdEthCvx.sol::106 => if (lpBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::151 => if (underlyingBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::159 => if (hopLpBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::211 => if (hopLpBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::358 => if (stakedBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::364 => if (lpBalance > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::399 => if (cvxCommunityReserveShare_ > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::402 => if (cvxBalance_ > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::410 => if (crvCommunityReserveShare_ > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::413 => if (crvBalance_ > 0) {
  2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/StrategySwapper.sol::111 => require(slippageTolerance_ > 0.8e18, Error.INVALID_SLIPPAGE_TOLERANCE);

Tools Used

Code Review

Recommended Mitigation Steps

It is recommend to use unchecked{++i} and change i declaration to uint256.

C4-0012 : Using operator && used more gas

Impact

Using double require instead of operator && can save more gas.

Proof of Concept

  1. Navigate to the following contracts.
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/AddressProvider.sol:343:        return exists && pool == poolAddress;
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/AddressProvider.sol:405:        return exists && vault == stakerVault;
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/SwapperRegistry.sol:36:            fromToken != toToken &&
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/SwapperRegistry.sol:37:                fromToken != address(0) &&
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/SwapperRegistry.sol:38:                toToken != address(0) &&
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/BkdLocker.sol:308:        if (lastUpdated[user] < lastMigrationEvent && userBalance > 0) {
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/access/RoleManager.sol:95:        if (role == Roles.ADDRESS_PROVIDER && index == 0) {
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/access/RoleManager.sol:97:        } else if (role == Roles.POOL_FACTORY && index == 0) {
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/access/RoleManager.sol:99:        } else if (role == Roles.CONTROLLER && index == 0) {
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol:59:            fromToken == TRI_CRV && ((toToken == DAI) || (toToken == USDC) || (toToken == USDT)),
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol:76:            fromToken == TRI_CRV && ((toToken == DAI) || (toToken == USDC) || (toToken == USDT)),
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol:128:        if (daiOutput > usdcOutput && daiOutput > usdtOutput) {
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/StakerVault.sol:386:        if (src != msg.sender && allowance_ != type(uint256).max && address(pool) != msg.sender) {
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol:360:            newSwapperSlippage >= _MIN_SWAPPER_SLIPPAGE &&
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol:676:        require(vars.success && abi.decode(vars.topupResult, (bool)), Error.TOP_UP_FAILED);
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol:893:            if (positionMeta.account == account && positionMeta.protocol == protocol) {
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CTokenRegistry.sol:50:        if (ensureExists && (address(ctoken) == address(0) || !_isCTokenUsable(ctoken))) {
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CTokenRegistry.sol:79:        bool deprecated = address(comptroller) == COMPTROLLER_MAINNET_ADDRESS &&
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CTokenRegistry.sol:81:        return listed && !deprecated;
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol:274:            token_ != address(_CVX) && token_ != address(underlying) && token_ != address(_CRV),

Tools Used

Code Review

Recommended Mitigation Steps

Example


using &&:

function check(uint x)public view{
    require(x == 0 && x < 1 );
}
// gas cost 21630

using double require:

    require(x == 0 );
    require( x < 1);
    }
}
// gas cost 21622