Backd contest - defsec's results

Maximize the power of your assets and start earning yield

General Information

Platform: Code4rena

Start Date: 21/04/2022

Pot Size: $100,000 USDC

Total HM: 18

Participants: 60

Period: 7 days

Judge: gzeon

Total Solo HM: 10

Id: 112

League: ETH

Backd

Findings Distribution

Researcher Performance

Rank: 11/60

Findings: 4

Award: $1,642.81

🌟 Selected for report: 0

🚀 Solo Findings: 0

Findings Information

🌟 Selected for report: IllIllI

Also found by: Dravee, defsec

Labels

bug
duplicate
2 (Med Risk)

Awards

1042.2314 USDC - $1,042.23

External Links

Originally submitted by warden defsec in #198, duplicate of #178 related to the use of safeApprove. This is upgraded from a QA report to standalone issue because it correctly described the revert when trying to call safeApprove on non-zero allowance. QA report that only describe safeApprove as deprecated and unable to identify the revert problem are intentionally not upgraded. Duplicate of #180.

The Contract Should approve(0) first

Impact

Some tokens (like USDT L199) do not work when changing the allowance from an existing non-zero allowance value. They must first be approved by zero and then the actual allowance must be approved.

IERC20(token).safeApprove(address(operator), 0); IERC20(token).safeApprove(address(operator), amount);

Proof of Concept

  1. Navigate to the following contracts.
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/CvxCrvRewardsLocker.sol::53 => IERC20(CRV).safeApprove(CRV_DEPOSITOR, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/CvxCrvRewardsLocker.sol::56 => IERC20(CVX_CRV).safeApprove(CVX_CRV_STAKING, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/CvxCrvRewardsLocker.sol::59 => IERC20(CRV).safeApprove(CVX_CRV_CRV_CURVE_POOL, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/CvxCrvRewardsLocker.sol::62 => IERC20(CVX).safeApprove(CVX_LOCKER, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::50 => IERC20(token).safeApprove(stakerVaultAddress, depositAmount); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::847 => IERC20(depositToken).safeApprove(feeHandler, feeAmount); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::908 => IERC20(token).safeApprove(spender, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/AaveHandler.sol::53 => IERC20(underlying).safeApprove(address(lendingPool), amount); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CompoundHandler.sol::71 => IERC20(underlying).safeApprove(address(ctoken), amount); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CompoundHandler.sol::120 => IERC20(underlying).safeApprove(address(ctoken), debt); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/pool/LiquidityPool.sol::721 => IERC20(lpToken_).safeApprove(staker_, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdEthCvx.sol::43 => IERC20(lp_).safeApprove(address(_BOOSTER), type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::71 => IERC20(underlying_).safeApprove(curveHopPool_, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::72 => IERC20(hopLp_).safeApprove(curvePool_, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::73 => IERC20(lp_).safeApprove(address(_BOOSTER), type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::129 => IERC20(hopLp).safeApprove(curvePool_, 0); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::130 => IERC20(hopLp).safeApprove(curvePool_, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::131 => IERC20(lp_).safeApprove(address(_BOOSTER), 0); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::132 => IERC20(lp_).safeApprove(address(_BOOSTER), type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::107 => _CRV.safeApprove(address(_strategySwapper), type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::108 => _CVX.safeApprove(address(_strategySwapper), type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::109 => _WETH.safeApprove(address(_strategySwapper), type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::279 => IERC20(token_).safeApprove(address(_strategySwapper), 0); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::280 => IERC20(token_).safeApprove(address(_strategySwapper), type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/StrategySwapper.sol::209 => IERC20(token_).safeApprove(spender_, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol::43 => IERC20(DAI).safeApprove(SUSHISWAP, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol::44 => IERC20(USDC).safeApprove(SUSHISWAP, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol::45 => IERC20(USDT).safeApprove(SUSHISWAP, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol::47 => IERC20(DAI).safeApprove(UNISWAP, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol::48 => IERC20(USDC).safeApprove(UNISWAP, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol::49 => IERC20(USDT).safeApprove(UNISWAP, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/AmmConvexGauge.sol::62 => IERC20(ammToken).safeApprove(booster, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/vault/Erc20Vault.sol::21 => IERC20(underlying_).safeApprove(address(reserve), type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/vault/Erc20Vault.sol::22 => IERC20(underlying_).safeApprove(_pool, type(uint256).max);
  1. When trying to re-approve an already approved token, all transactions revert and the protocol cannot be used.

Tools Used

None

Approve with a zero amount first before setting the actual amount. Consider use safeIncreaseAllowance and safeDecreaseAllowance.

#0 - JeeberC4

2022-05-13T19:39:24Z

Manually created required json file

Findings Information

Labels

bug
duplicate
2 (Med Risk)
reviewed

Awards

58.8714 USDC - $58.87

External Links

Lines of code

https://github.com/code-423n4/2022-04-backd/blob/main/backd/contracts/oracles/ChainlinkOracleProvider.sol#L55

Vulnerability details

Impact

The getPriceUSD function in the contract ChainlinkOracleProvider.sol fetches the asset price from a Chainlink aggregator using the latestRoundData function. However, there are no checks on roundID nor timeStamp, resulting in stale prices. The oracle wrapper calls out to a chainlink oracle receiving the latestRoundData(). It then checks freshness by verifying that the answer is indeed for the last known round. The returned updatedAt timestamp is not checked.

If there is a problem with chainlink starting a new round and finding consensus on the new value for the oracle (e.g. chainlink nodes abandon the oracle, chain congestion, vulnerability/attacks on the chainlink system) consumers of this contract may continue using outdated stale data (if oracles are unable to submit no new round is started)

Although there is a timestamp check, Round ID is not checked in the function. That can also cause stale price.

Proof of Concept

  1. Navigate to the following contract.

https://github.com/code-423n4/2022-04-backd/blob/main/backd/contracts/oracles/ChainlinkOracleProvider.sol#L55

  1. Stale prices could put funds at risk. According to Chainlink's documentation, This function does not error if no answer has been reached but returns 0, causing an incorrect price fed to the PriceOracle. The external Chainlink oracle, which provides index price information to the system, introduces risk inherent to any dependency on third-party data sources. For example, the oracle could fall behind or otherwise fail to be maintained, resulting in outdated data being fed to the index price calculations of the AMM. Oracle reliance has historically resulted in crippled on-chain systems, and complications that lead to these outcomes can arise from things as simple as network congestion.

Medium Severity Issue From The FEI Protocol : https://consensys.net/diligence/audits/2021/09/fei-protocol-v2-phase-1/#chainlinkoraclewrapper-latestrounddata-might-return-stale-results

  1. Although there is a timestamp check, Round ID is not checked in the function. That can also cause stale price.

Tools Used

Code Review

Consider to add checks on the return data with proper revert messages if the price is stale or the round is incomplete, for example:

(uint80 roundID, int256 price, , uint256 timeStamp, uint80 answeredInRound) = ETH_CHAINLINK.latestRoundData(); require(price > 0, "Chainlink price <= 0"); require(answeredInRound >= roundID, "..."); require(timeStamp != 0, "...");

Consider checking the oracle responses updatedAt value after calling out to chainlinkOracle.latestRoundData() verifying that the result is within an allowed margin of freshness.

#0 - chase-manning

2022-04-28T11:28:31Z

Duplicate of #17

Awards

374.5223 USDC - $374.52

Labels

bug
QA (Quality Assurance)
resolved
reviewed

External Links

C4-001 :Use safeTransfer/safeTransferFrom consistently instead of transfer/transferFrom

Impact - LOW

Impact

It is good to add a require() statement that checks the return value of token transfers or to use something like OpenZeppelin’s safeTransfer/safeTransferFrom unless one is sure the given token reverts in case of a failure. Failure to do so will cause silent failures of transfers and affect token accounting in contract.

Reference: This similar medium-severity finding from Consensys Diligence Audit of Fei Protocol: https://consensys.net/diligence/audits/2021/01/fei-protocol/#unchecked-return-value-for-iweth-transfer-call

Proof of Concept

  1. Navigate to the following contract.

  2. transfer/transferFrom functions are used instead of safe transfer/transferFrom on the following contracts.

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);

Tools Used

Code Review

Consider using safeTransfer/safeTransferFrom or require() consistently.

C4-002 : Use of Block.timestamp

Impact - Non-Critical

Block timestamps have historically been used for a variety of applications, such as entropy for random numbers (see the Entropy Illusion for further details), locking funds for periods of time, and various state-changing conditional statements that are time-dependent. Miners have the ability to adjust timestamps slightly, which can prove to be dangerous if block timestamps are used incorrectly in smart contracts.

Proof of Concept

  1. Navigate to the following contract.
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/oracles/ChainlinkOracleProvider.sol:57: require(block.timestamp <= updatedAt + stalePriceDelay, Error.STALE_PRICE); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/vault/VaultReserve.sol:78: _lastWithdrawal[msg.sender] = block.timestamp; 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/vault/VaultReserve.sol:103: return block.timestamp >= _lastWithdrawal[vault] + minWithdrawalDelay; 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol:110: block.timestamp

Tools Used

Manual Code Review

Block timestamps should not be used for entropy or generating random numbers—i.e., they should not be the deciding factor (either directly or through some derivation) for winning a game or changing an important state.

Time-sensitive logic is sometimes required; e.g., for unlocking contracts (time-locking), completing an ICO after a few weeks, or enforcing expiry dates. It is sometimes recommended to use block.number and an average block time to estimate times; with a 10 second block time, 1 week equates to approximately, 60480 blocks. Thus, specifying a block number at which to change a contract state can be more secure, as miners are unable to easily manipulate the block number.

C4-003 : Missing events for only functions that change critical parameters

Impact - Non critical

The afunctions that change critical parameters should emit events. Events allow capturing the changed parameters so that off-chain tools/interfaces can register such changes with timelocks that allow users to evaluate them and consider if they would like to engage/exit based on how they perceive the changes as affecting the trustworthiness of the protocol or profitability of the implemented financial services. The alternative of directly querying on-chain contract state for such changes is not considered practical for most users/usages.

Missing events and timelocks do not promote transparency and if such changes immediately affect users’ perception of fairness or trustworthiness, they could exit the protocol causing a reduction in liquidity which could negatively impact protocol TVL and reputation.

Proof of Concept

  1. Navigate to the following contract.
https://github.com/code-423n4/2022-04-backd/blob/c856714a50437cb33240a5964b63687c9876275b/backd/contracts/Controller.sol#L31

See similar High-severity H03 finding OpenZeppelin’s Audit of Audius (https://blog.openzeppelin.com/audius-contracts-audit/#high) and Medium-severity M01 finding OpenZeppelin’s Audit of UMA Phase 4 (https://blog.openzeppelin.com/uma-audit-phase-4/)

Tools Used

None

Add events to all functions that change critical parameters.

C4-004 : # DoS With Block Gas Limit

Impact - Non-Critical

When smart contracts are deployed or functions inside them are called, the execution of these actions always requires a certain amount of gas, based of how much computation is needed to complete them. The Ethereum network specifies a block gas limit and the sum of all transactions included in a block can not exceed the threshold.

Programming patterns that are harmless in centralized applications can lead to Denial of Service conditions in smart contracts when the cost of executing a function exceeds the block gas limit. Modifying an array of unknown size, that increases in size over time, can lead to such a Denial of Service condition.

Proof of Concept

  1. Follow the functions shown below.
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;

Tools Used

Code Review

Caution is advised when you expect to have large arrays that grow over time. Actions that require looping across the entire data structure should be avoided.

If you absolutely must loop over an array of unknown size, then you should plan for it to potentially take multiple blocks, and therefore require multiple transactions.

C4-005 : Critical changes should use two-step procedure

Impact - NON CRITICAL

The critical procedures should be two step process.

Proof of Concept

  1. Navigate to the following contract.
https://github.com/code-423n4/2022-04-backd/blob/c856714a50437cb33240a5964b63687c9876275b/backd/contracts/access/RoleManager.sol#L149 https://github.com/code-423n4/2022-04-backd/blob/c856714a50437cb33240a5964b63687c9876275b/backd/contracts/access/RoleManager.sol#L155

Tools Used

Code Review

Lack of two-step procedure for critical operations leaves them error-prone. Consider adding two step procedure on the critical functions.

C4-006 : Front-runnable Initializers

Impact - LOW

All contract initializers were missing access controls, allowing any user to initialize the contract. By front-running the contract deployers to initialize the contract, the incorrect parameters may be supplied, leaving the contract needing to be redeployed.

Proof of Concept

  1. Navigate to the following contracts.
https://github.com/code-423n4/2022-04-backd/blob/c856714a50437cb33240a5964b63687c9876275b/backd/contracts/vault/Erc20Vault.sol#L12
  1. initialize functions does not have access control. They are vulnerable to front-running.

Tools Used

Manual Code Review

While the code that can be run in contract constructors is limited, setting the owner in the contract's constructor to the msg.sender and adding the onlyOwner modifier to all initializers would be a sufficient level of access control.

C4-007 : Incompatibility With Rebasing/Deflationary/Inflationary tokens

Impact - LOW

PrePo protocol do not appear to support rebasing/deflationary/inflationary tokens whose balance changes during transfers or over time. The necessary checks include at least verifying the amount of tokens transferred to contracts before and after the actual transfer to infer any fees/interest.

Proof of Concept

  1. Navigate to the following contract.
https://github.com/code-423n4/2022-04-backd/blob/c856714a50437cb33240a5964b63687c9876275b/backd/contracts/pool/LiquidityPool.sol#L523

Tools Used

Manual Code Review

  • Ensure that to check previous balance/after balance equals to amount for any rebasing/inflation/deflation
  • Add support in contracts for such tokens before accepting user-supplied tokens
  • Consider supporting deflationary / rebasing / etc tokens by extra checking the balances before/after or strictly inform your users not to use such tokens if they don't want to lose them.

C4-008 : # Pragma Version

Impact

In the contracts, floating pragmas should not be used. Contracts should be deployed with the same compiler version and flags that they have been tested with thoroughly. Locking the pragma helps to ensure that contracts do not accidentally get deployed using, for example, an outdated compiler version that might introduce bugs that affect the contract system negatively.

## Proof of Concept

https://swcregistry.io/docs/SWC-103

All Contracts

Tools Used

Manual code review

Lock the pragma version: delete pragma solidity 0.8.10 in favor of pragma solidity 0.8.10

C4-009 : # Missing zero-address check in the setter functions and initiliazers

Impact

Missing checks for zero-addresses may lead to infunctional protocol, if the variable addresses are updated incorrectly.

Proof of Concept

  1. Navigate to the following contracts.
https://github.com/code-423n4/2022-04-backd/blob/c856714a50437cb33240a5964b63687c9876275b/backd/contracts/oracles/ChainlinkUsdWrapper.sol#L32 https://github.com/code-423n4/2022-04-backd/blob/c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/AaveHandler.sol#L24 https://github.com/code-423n4/2022-04-backd/blob/c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CompoundHandler.sol#L38 https://github.com/code-423n4/2022-04-backd/blob/c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdEthCvx.sol#L18

Tools Used

Code Review

Consider adding zero-address checks in the discussed constructors: require(newAddr != address(0));.

C4-010 : # Lack of setter function for the oracle

Impact

On the UniswapV2PathPriceOracle contract, there is not setter function on the oracles addresses. This can cause misfunctionality on the uniswap oracle contract.

Proof of Concept

  1. Navigate to "https://github.com/code-423n4/2022-04-backd/blob/c856714a50437cb33240a5964b63687c9876275b/backd/contracts/oracles/ChainlinkUsdWrapper.sol#L32" contract.
  2. Oracle addresses are set on the constructor.
  3. Setter function is missing on the contract. Misdeployed contract can cause failure of oracle integration.

Tools Used

None

Consider to add setter function for oracles addresses.

C4-0011 : The Contract Should approve(0) first

Impact - LOW

Some tokens (like USDT L199) do not work when changing the allowance from an existing non-zero allowance value. They must first be approved by zero and then the actual allowance must be approved.

IERC20(token).safeApprove(address(operator), 0); IERC20(token).safeApprove(address(operator), amount);

Proof of Concept

  1. Navigate to the following contracts.
2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/CvxCrvRewardsLocker.sol::53 => IERC20(CRV).safeApprove(CRV_DEPOSITOR, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/CvxCrvRewardsLocker.sol::56 => IERC20(CVX_CRV).safeApprove(CVX_CRV_STAKING, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/CvxCrvRewardsLocker.sol::59 => IERC20(CRV).safeApprove(CVX_CRV_CRV_CURVE_POOL, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/CvxCrvRewardsLocker.sol::62 => IERC20(CVX).safeApprove(CVX_LOCKER, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::50 => IERC20(token).safeApprove(stakerVaultAddress, depositAmount); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::847 => IERC20(depositToken).safeApprove(feeHandler, feeAmount); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/TopUpAction.sol::908 => IERC20(token).safeApprove(spender, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/AaveHandler.sol::53 => IERC20(underlying).safeApprove(address(lendingPool), amount); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CompoundHandler.sol::71 => IERC20(underlying).safeApprove(address(ctoken), amount); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/actions/topup/handlers/CompoundHandler.sol::120 => IERC20(underlying).safeApprove(address(ctoken), debt); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/pool/LiquidityPool.sol::721 => IERC20(lpToken_).safeApprove(staker_, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdEthCvx.sol::43 => IERC20(lp_).safeApprove(address(_BOOSTER), type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::71 => IERC20(underlying_).safeApprove(curveHopPool_, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::72 => IERC20(hopLp_).safeApprove(curvePool_, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::73 => IERC20(lp_).safeApprove(address(_BOOSTER), type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::129 => IERC20(hopLp).safeApprove(curvePool_, 0); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::130 => IERC20(hopLp).safeApprove(curvePool_, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::131 => IERC20(lp_).safeApprove(address(_BOOSTER), 0); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdTriHopCvx.sol::132 => IERC20(lp_).safeApprove(address(_BOOSTER), type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::107 => _CRV.safeApprove(address(_strategySwapper), type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::108 => _CVX.safeApprove(address(_strategySwapper), type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::109 => _WETH.safeApprove(address(_strategySwapper), type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::279 => IERC20(token_).safeApprove(address(_strategySwapper), 0); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/ConvexStrategyBase.sol::280 => IERC20(token_).safeApprove(address(_strategySwapper), type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/StrategySwapper.sol::209 => IERC20(token_).safeApprove(spender_, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol::43 => IERC20(DAI).safeApprove(SUSHISWAP, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol::44 => IERC20(USDC).safeApprove(SUSHISWAP, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol::45 => IERC20(USDT).safeApprove(SUSHISWAP, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol::47 => IERC20(DAI).safeApprove(UNISWAP, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol::48 => IERC20(USDC).safeApprove(UNISWAP, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/swappers/Swapper3Crv.sol::49 => IERC20(USDT).safeApprove(UNISWAP, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/tokenomics/AmmConvexGauge.sol::62 => IERC20(ammToken).safeApprove(booster, type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/vault/Erc20Vault.sol::21 => IERC20(underlying_).safeApprove(address(reserve), type(uint256).max); 2022-04-backd-c856714a50437cb33240a5964b63687c9876275b/backd/contracts/vault/Erc20Vault.sol::22 => IERC20(underlying_).safeApprove(_pool, type(uint256).max);
  1. When trying to re-approve an already approved token, all transactions revert and the protocol cannot be used.

Tools Used

None

Approve with a zero amount first before setting the actual amount. Consider use safeIncreaseAllowance and safeDecreaseAllowance.

C4-0012 : The Contract Should approve(0) first

Description

The transfer() and send() functions forward a fixed amount of 2300 gas. Historically, it has often been recommended to use these functions for value transfers to guard against reentrancy attacks. However, the gas cost of EVM instructions may change significantly during hard forks which may break already deployed contract systems that make fixed assumptions about gas costs. For example. EIP 1884 broke several existing smart contracts due to a cost increase of the SLOAD instruction.

Proof of Concept

  1. Navigate to the following contracts.
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);

Remediation

Avoid the use of transfer() and send() and do not otherwise specify a fixed amount of gas when performing calls. Use .call.value(...)("") instead. Use the checks-effects-interactions pattern and/or reentrancy locks to prevent reentrancy attacks.

C4-0013 : # Missing Re-entrancy Guard

Impact - LOW

The re-entrancy guard is missing on the some of the functions. The external interactions can cause to the re-entrancy vulnerability.

Proof of Concept

  1. Navigate to the following contract.
https://github.com/code-423n4/2022-04-backd/blob/c856714a50437cb33240a5964b63687c9876275b/backd/contracts/strategies/BkdEthCvx.sol#L18 https://github.com/code-423n4/2022-04-backd/blob/c856714a50437cb33240a5964b63687c9876275b/backd/contracts/vault/Vault.sol#L118 https://github.com/code-423n4/2022-04-backd/blob/c856714a50437cb33240a5964b63687c9876275b/backd/contracts/vault/Vault.sol#L669

Tools Used

Code Review

Follow the check effect interaction pattern or put re-entrancy guard.

Awards

167.187 USDC - $167.19

Labels

bug
G (Gas Optimization)
resolved
reviewed

External Links

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

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

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

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

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

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

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

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

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

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

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

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

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
AuditHub

A portfolio for auditors, a security profile for protocols, a hub for web3 security.

Built bymalatrax © 2024

Auditors

Browse

Contests

Browse

Get in touch

ContactTwitter