Juicebox V2 contest - defsec's results

The decentralized fundraising and treasury protocol.

General Information

Platform: Code4rena

Start Date: 01/07/2022

Pot Size: $75,000 USDC

Total HM: 17

Participants: 105

Period: 7 days

Judge: Jack the Pug

Total Solo HM: 5

Id: 143

League: ETH

Juicebox

Findings Distribution

Researcher Performance

Rank: 29/105

Findings: 3

Award: $184.91

🌟 Selected for report: 0

🚀 Solo Findings: 0

Awards

14.8726 USDC - $14.87

Labels

bug
duplicate
3 (High Risk)
valid

External Links

Lines of code

https://github.com/jbx-protocol/juice-contracts-v2-code4rena/blob/828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBChainlinkV3PriceFeed.sol#L44

Vulnerability details

Impact

The currentPrice function in the contract JBChainlinkV3PriceFeed.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)

Timestamp and Round ID are not checked in the function. That can also cause stale price.

Proof of Concept

  1. Navigate to the following contract.

https://github.com/jbx-protocol/juice-contracts-v2-code4rena/blob/828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBChainlinkV3PriceFeed.sol#L44

  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. Timestamp and 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 - mejango

2022-07-12T18:51:25Z

dup #138

ISSUE LIST

C4-001 : Critical changes should use two-step procedure - Non Critical
C4-002 : Use safeTransfer/safeTransferFrom consistently instead of transfer/transferFrom - Low
C4-003 : Missing zero-address check in the setter functions and initiliazers - Low
C4-004 : Low level calls with solidity version 0.8.14 can result in optimiser bug. - LOW
C4-005 : The Contract Should safeApprove(0) first - LOW
C4-006 : Use of Block.timestamp - Non-critical
C4-007 : Incompatibility With Rebasing/Deflationary/Inflationary tokens - LOW
C4-008 : Add disableInitializers to Prevent Front-running - LOW
C4-009 : ERC20 approve method missing return value check - LOW

ISSUES

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

Impact - NON CRITICAL

The critical procedures should be two step process. The contracts inherit OpenZeppelin's Ownable contract which enables the onlyOwner role to transfer ownership to another address. It's possible that the onlyOwner role mistakenly transfers ownership to the wrong address, resulting in a loss of the onlyOwner role. The current ownership transfer process involves the current owner calling Unlock.transferOwnership(). This function checks the new owner is not the zero address and proceeds to write the new owner's address into the owner's state variable. If the nominated EOA account is not a valid account, it is entirely possible the owner may accidentally transfer ownership to an uncontrolled account, breaking all functions with the onlyOwner() modifier. Lack of two-step procedure for critical operations leaves them error-prone if the address is incorrect, the new address will take on the functionality of the new role immediately

for Ex : -Alice deploys a new version of the whitehack group address. When she invokes the whitehack group address setter to replace the address, she accidentally enters the wrong address. The new address now has access to the role immediately and is too late to revert

Proof of Concept

  1. Navigate to the following contract.
https://github.com/jbx-protocol/juice-contracts-v2-code4rena/blob/828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBPrices.sol#L20

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-002 : Use safeTransfer/safeTransferFrom consistently instead of transfer/transferFrom

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.

juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBERC20PaymentTerminal.sol::87 => ? IERC20(token).transfer(_to, _amount) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBERC20PaymentTerminal.sol::88 => : IERC20(token).transferFrom(_from, _to, _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBERC20PaymentTerminal.sol::99 => IERC20(token).approve(_to, _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20ProjectPayer.sol::271 => IERC20(_token).transferFrom(msg.sender, address(this), _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20ProjectPayer.sol::315 => IERC20(_token).transferFrom(msg.sender, address(this), _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20ProjectPayer.sol::364 => if (_token != JBTokens.ETH) IERC20(_token).approve(address(_terminal), _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20ProjectPayer.sol::412 => if (_token != JBTokens.ETH) IERC20(_token).approve(address(_terminal), _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::256 => IERC20(_token).transferFrom(msg.sender, address(this), _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::301 => IERC20(_token).transfer( juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::348 => IERC20(_token).transferFrom(msg.sender, address(this), _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::384 => IERC20(_token).transfer( juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::493 => IERC20(_token).approve(address(_split.allocator), _splitAmount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::534 => IERC20(_token).transfer(

Tools Used

Code Review

Consider using safeTransfer/safeTransferFrom or require() consistently.

C4-003 : # 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/jbx-protocol/juice-contracts-v2-code4rena/blob/828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBERC20PaymentTerminal.sol#L37

Tools Used

Code Review

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

C4-004 : Low level calls with solidity version 0.8.6 can result in optimiser bug.

Impact

The protocol is using low level calls with solidity version 0.8.6 which can result in optimizer bug.

https://medium.com/certora/overly-optimistic-optimizer-certora-bug-disclosure-2101e3f7994d

Proof of Concept

https://github.com/jbx-protocol/juice-contracts-v2-code4rena/blob/828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHPaymentTerminal.sol#L25

Tools Used

Code Review

Consider upgrading to solidity 0.8.15.

C4-005 : The Contract Should safeApprove(0) first - LOW

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.

juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBERC20PaymentTerminal.sol::99 => IERC20(token).approve(_to, _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::493 => IERC20(_token).approve(address(_split.allocator), _splitAmount);

When trying to re-approve an already approved token, all transactions revert and the protocol cannot be used.

Proof of Concept

(https://github.com/code-423n4/2022-06-yieldy/blob/main/src/contracts/LiquidityReserve.sol#L81)

Tools Used

None

Approve with a zero amount first before setting the actual amount.

C4-006 : 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.
juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol:206: if (block.timestamp >= _currentSplits[_i].lockedUntil) continue; juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol:162: if (fundingCycle.start > block.timestamp) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol:230: if (!_isApproved(_projectId, _fundingCycle) || block.timestamp < _fundingCycle.start) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol:332: uint256 _configuration = block.timestamp; juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol:340: _mustStartAtOrAfter > block.timestamp ? _mustStartAtOrAfter : block.timestamp juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol:408: if (!_isApproved(_projectId, _baseFundingCycle) || block.timestamp < _baseFundingCycle.start) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol:550: if (block.timestamp >= _fundingCycle.start) return 0; juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol:561: block.timestamp < _fundingCycle.start - _baseFundingCycle.duration juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol:589: _fundingCycle.duration > 0 && block.timestamp >= _fundingCycle.start + _fundingCycle.duration juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol:593: if (block.timestamp >= _fundingCycle.start) return _fundingCycle.configuration; juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol:602: block.timestamp >= _baseFundingCycle.start + _baseFundingCycle.duration juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol:632: ? block.timestamp + 1 juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol:633: : block.timestamp - _baseFundingCycle.duration + 1; juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol:815: else if (_ballotFundingCycle.ballot.duration() >= block.timestamp - _configuration) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/system_tests/TestTokenFlow.sol:64: 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-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.

Example Test

During the lending, If the inflationary/deflationary tokens are used excepted amount will be lower than deposit.

Proof of Concept

  1. Navigate to the following contract.
juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBERC20PaymentTerminal.sol::87 => ? IERC20(token).transfer(_to, _amount) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBERC20PaymentTerminal.sol::88 => : IERC20(token).transferFrom(_from, _to, _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBERC20PaymentTerminal.sol::99 => IERC20(token).approve(_to, _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20ProjectPayer.sol::271 => IERC20(_token).transferFrom(msg.sender, address(this), _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20ProjectPayer.sol::315 => IERC20(_token).transferFrom(msg.sender, address(this), _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20ProjectPayer.sol::364 => if (_token != JBTokens.ETH) IERC20(_token).approve(address(_terminal), _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20ProjectPayer.sol::412 => if (_token != JBTokens.ETH) IERC20(_token).approve(address(_terminal), _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::256 => IERC20(_token).transferFrom(msg.sender, address(this), _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::301 => IERC20(_token).transfer( juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::348 => IERC20(_token).transferFrom(msg.sender, address(this), _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::384 => IERC20(_token).transfer( juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::493 => IERC20(_token).approve(address(_split.allocator), _splitAmount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::534 => IERC20(_token).transfer(

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 : Add disableInitializers to Prevent Front-running

Impact

Defining initial values for variables when declaring them in a contract like in the code below does not work for upgradeable contracts.

Refer to explanation below:

https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable#avoid-initial-values-in-field-declarations

Also, one should not leave the implementation contract uninitialized. None of the implementation contracts in the code base contains the code recommended by OpenZeppelin below, or an empty constructor with the initializer modifier.

Tools Used

Code Review

/// @custom:oz-upgrades-unsafe-allow constructor constructor() { _disableInitializers(); }

Refer to the link below:

https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable#initializing_the_implementation_contract

C4-009 : ERC20 approve method missing return value check - LOW

Impact

The following contract functions performs an ERC20.approve() call but does not check the success return value. Some tokens do not revert if the approval failed but return false instead.

Proof of Concept

  1. Navigate to the following contracts.
juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBERC20PaymentTerminal.sol::99 => IERC20(token).approve(_to, _amount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::493 => IERC20(_token).approve(address(_split.allocator), _splitAmount);
  1. Tokens that don't actually perform the approve and return false are still counted as a correct approve.

Tools Used

None

Its recommend to using OpenZeppelin’s SafeERC20 versions with the safeApprove function that handles the return value check as well as non-standard-compliant tokens.

Reference : https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v4.1/contracts/token/ERC20/utils/SafeERC20.sol#L74

#0 - drgorillamd

2022-07-12T21:59:34Z

This looks like a copy-paste of tooling with some "issues" not part of Juicebox codebase

[S]: Suggested optimation, save a decent amount of gas without compromising readability;

[M]: Minor optimation, the amount of gas saved is minor, change when you see fit;

[N]: Non-preferred, the amount of gas saved is at cost of readability, only apply when gas saving is a top priority.

ISSUE LIST

C4-001 : Adding unchecked directive can save gas [S]
C4-002 : Check if amount > 0 before token transfer can save gas [S]
C4-003 : There is no need to assign default values to variables [S]
C4-004 : Using operator && used more gas [S]
C4-005 : Non-strict inequalities are cheaper than strict ones [M]
C4-006 : Use Shift Right/Left instead of Division/Multiplication if possible [S]
C4-007 : Cache array length in for loops can save gas [S]
C4-008 : Use calldata instead of memory for function parameters [M]
C4-009 : ++i is more gas efficient than i++ in loops forwarding
C4-010 : > 0 can be replaced with != 0 for gas optimization
C4-011 : Keccak functions in constants waste gas [M]
C4-012 : Free gas savings for using solidity 0.8.10+ [S]

C4-001 : 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

juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::438 => if (_terminals.length > 0) directory.setTerminalsOf(projectId, _terminals); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::498 => if (_terminals.length > 0) directory.setTerminalsOf(_projectId, _terminals); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::913 => for (uint256 _i = 0; _i < _splits.length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::1014 => for (uint256 _i; _i < _fundAccessConstraints.length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBDirectory.sol::139 => for (uint256 _i; _i < _terminalsOf[_projectId].length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBDirectory.sol::167 => for (uint256 _i; _i < _terminalsOf[_projectId].length; _i++) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBDirectory.sol::274 => if (_terminals.length > 1) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBDirectory.sol::275 => for (uint256 _i; _i < _terminals.length; _i++) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBDirectory.sol::276 => for (uint256 _j = _i + 1; _j < _terminals.length; _j++) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::466 => for (uint256 i = 0; i < _splits.length; i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBOperatorStore.sol::85 => for (uint256 _i = 0; _i < _permissionIndexes.length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBOperatorStore.sol::135 => for (uint256 _i = 0; _i < _operatorData.length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBOperatorStore.sol::165 => for (uint256 _i = 0; _i < _indexes.length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBProjects.sol::143 => if (bytes(_metadata.content).length > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSingleTokenPaymentTerminalStore.sol::862 => for (uint256 _i = 0; _i < _terminals.length; _i++) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol::161 => // Push array length in stack juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol::162 => uint256 _groupedSplitsLength = _groupedSplits.length; juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol::204 => for (uint256 _i = 0; _i < _currentSplits.length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol::211 => for (uint256 _j = 0; _j < _splits.length; _j++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol::229 => for (uint256 _i = 0; _i < _splits.length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol::278 => // Set the new length of the splits. juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol::279 => _splitCountOf[_projectId][_domain][_group] = _splits.length; juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol::300 => // Initialize an array to be returned that has the set length. juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBTokenStore.sol::194 => if (bytes(_name).length == 0) revert EMPTY_NAME(); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBTokenStore.sol::197 => if (bytes(_symbol).length == 0) revert EMPTY_SYMBOL(); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::590 => // Push array length in stack juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::591 => uint256 _heldFeeLength = _heldFees.length; juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::1008 => for (uint256 _i = 0; _i < _splits.length; ) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::1392 => // Push length in stack juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::1393 => uint256 _heldFeesLength = _heldFees.length;

Tools Used

None

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

Unchecked{i++};

C4-002 : 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

https://github.com/jbx-protocol/juice-contracts-v2-code4rena/blob/828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBERC20PaymentTerminal.sol#L87

All Contracts

Tools Used

None

Consider checking amount != 0.

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

Impact - Gas Optimization

Uint is default initialized to 0. There is no need assign false to variable.

Proof of Concept

juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBOperatorStore.sol::85 => for (uint256 _i = 0; _i < _permissionIndexes.length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBOperatorStore.sol::135 => for (uint256 _i = 0; _i < _operatorData.length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBOperatorStore.sol::165 => for (uint256 _i = 0; _i < _indexes.length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::1008 => for (uint256 _i = 0; _i < _splits.length; ) {

Tools Used

Code Review

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

C4-004 : 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.
juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBOperatable.sol:99: msg.sender != _account && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBOperatable.sol:100: !operatorStore.hasPermission(msg.sender, _account, _domain, _permissionIndex) && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBOperatable.sol:121: !_override && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBOperatable.sol:122: msg.sender != _account && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBOperatable.sol:123: !operatorStore.hasPermission(msg.sender, _account, _domain, _permissionIndex) && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol:214: _splits[_j].percent == _currentSplits[_i].percent && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol:215: _splits[_j].beneficiary == _currentSplits[_i].beneficiary && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol:216: _splits[_j].allocator == _currentSplits[_i].allocator && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol:217: _splits[_j].projectId == _currentSplits[_i].projectId && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBTokenStore.sol:242: if (_token == IJBToken(address(0)) && requireClaimFor[_projectId]) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBTokenStore.sol:249: if (_token != IJBToken(address(0)) && _token.decimals() != 18) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBTokenStore.sol:265: if (_newOwner != address(0) && oldToken != IJBToken(address(0))) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBTokenStore.sol:293: bool _shouldClaimTokens = (requireClaimFor[_projectId] || _preferClaimedTokens) && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBDirectory.sol:134: _primaryTerminalOf[_projectId][_token] != IJBPaymentTerminal(address(0)) && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBDirectory.sol:219: (isAllowedToSetFirstController[msg.sender] && controllerOf[_projectId] == address(0))) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBDirectory.sol:230: msg.sender != address(controllerOf[_projectId]) && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBDirectory.sol:231: controllerOf[_projectId] != address(0) && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBDirectory.sol:266: msg.sender != address(controllerOf[_projectId]) && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBDirectory.sol:364: msg.sender != address(controllerOf[_projectId]) && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol:560: _baseFundingCycle.duration > 0 && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol:589: _fundingCycle.duration > 0 && block.timestamp >= _fundingCycle.start + _fundingCycle.duration juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol:601: _baseFundingCycle.duration > 0 && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol:655: !_fundingCycle.mintingAllowed() && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol:656: !directory.isTerminalOf(_projectId, IJBPaymentTerminal(msg.sender)) && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol:736: _fundingCycle.burnPaused() &&

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

C4-005 : 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

juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::438 => if (_terminals.length > 0) directory.setTerminalsOf(projectId, _terminals); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::481 => if (fundingCycleStore.latestConfigurationOf(_projectId) > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::498 => if (_terminals.length > 0) directory.setTerminalsOf(_projectId, _terminals); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::875 => if (_leftoverTokenCount > 0) tokenStore.mintFor(_owner, _projectId, _leftoverTokenCount, false); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::925 => if (_tokenCount > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::1032 => if (_constraints.distributionLimit > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::1040 => if (_constraints.overflowAllowance > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20ProjectPayer.sol::268 => if (msg.value > 0) revert NO_MSG_VALUE_ALLOWED(); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20ProjectPayer.sol::312 => if (msg.value > 0) revert NO_MSG_VALUE_ALLOWED(); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::253 => if (msg.value > 0) revert NO_MSG_VALUE_ALLOWED(); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::275 => if (_leftoverAmount > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::345 => if (msg.value > 0) revert NO_MSG_VALUE_ALLOWED(); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::367 => if (_leftoverAmount > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::477 => if (_splitAmount > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::149 => if (_standbyFundingCycleConfiguration > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::210 => if (_fundingCycleConfiguration > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::347 => _data.duration > 0 || juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::348 => _data.discountRate > 0 juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::364 => if (_metadata > 0) _metadataOf[_projectId][_configuration] = _metadata; juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::469 => _weight = _weight > 0 juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::560 => _baseFundingCycle.duration > 0 && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::589 => _fundingCycle.duration > 0 && block.timestamp >= _fundingCycle.start + _fundingCycle.duration juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::601 => _baseFundingCycle.duration > 0 && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBProjects.sol::143 => if (bytes(_metadata.content).length > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSingleTokenPaymentTerminalStore.sol::475 => if (_currentOverflow > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSingleTokenPaymentTerminalStore.sol::518 => if (reclaimAmount > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol::259 => if (_splits[_i].lockedUntil > 0 || _splits[_i].allocator != IJBSplitAllocator(address(0))) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol::272 => } else if (_packedSplitParts2Of[_projectId][_domain][_group][_i] > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol::326 => if (_packedSplitPart2 > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBTokenStore.sol::356 => if (_unclaimedTokensToBurn > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBTokenStore.sol::367 => if (_claimedTokensToBurn > 0) _token.burn(_projectId, _holder, _claimedTokensToBurn); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::346 => if (msg.value > 0) revert NO_MSG_VALUE_ALLOWED(); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::516 => if (balance > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::552 => if (msg.value > 0) revert NO_MSG_VALUE_ALLOWED(); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::745 => if (_tokenCount > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::772 => if (reclaimAmount > 0) _transferFrom(address(this), _beneficiary, reclaimAmount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::884 => if (netLeftoverDistributionAmount > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::964 => if (netDistributedAmount > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::1022 => if (_payoutAmount > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::1297 => if (_tokenCount > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/system_tests/TestDistributeHeldFee.sol::154 => if (fee > 0 && payAmountInWei > 0) {

Tools Used

Code Review

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

C4-006 : 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.

Proof of Concept

Contracts

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-007 : 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.
juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBOperatorStore.sol::85 => for (uint256 _i = 0; _i < _permissionIndexes.length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBOperatorStore.sol::135 => for (uint256 _i = 0; _i < _operatorData.length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBOperatorStore.sol::165 => for (uint256 _i = 0; _i < _indexes.length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::1008 => for (uint256 _i = 0; _i < _splits.length; ) {

Tools Used

None

Consider to cache array length.

C4-008 : Use calldata instead of memory for function parameters

Impact

In some cases, having function arguments in calldata instead of memory is more optimal.

Consider the following generic example:

contract C { function add(uint[] memory arr) external returns (uint sum) { uint length = arr.length; for (uint i = 0; i < arr.length; i++) { sum += arr[i]; } } }

In the above example, the dynamic array arr has the storage location memory. When the function gets called externally, the array values are kept in calldata and copied to memory during ABI decoding (using the opcode calldataload and mstore). And during the for loop, arr[i] accesses the value in memory using a mload. However, for the above example this is inefficient. Consider the following snippet instead:

contract C { function add(uint[] calldata arr) external returns (uint sum) { uint length = arr.length; for (uint i = 0; i < arr.length; i++) { sum += arr[i]; } } }

In the above snippet, instead of going via memory, the value is directly read from calldata using calldataload. That is, there are no intermediate memory operations that carries this value.

Gas savings: In the former example, the ABI decoding begins with copying value from calldata to memory in a for loop. Each iteration would cost at least 60 gas. In the latter example, this can be completely avoided. This will also reduce the number of instructions and therefore reduces the deploy time cost of the contract.

In short, use calldata instead of memory if the function argument is only read.

Note that in older Solidity versions, changing some function arguments from memory to calldata may cause "unimplemented feature error". This can be avoided by using a newer (0.8.*) Solidity compiler.

Proof of Concept

  1. Navigate to the following smart contract line.
juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/libraries/JBFundingCycleMetadataResolver.sol:11: function global(JBFundingCycle memory _fundingCycle) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/libraries/JBFundingCycleMetadataResolver.sol:14: returns (JBGlobalFundingCycleMetadata memory metadata) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/libraries/JBFundingCycleMetadataResolver.sol:19: function reservedRate(JBFundingCycle memory _fundingCycle) internal pure returns (uint256) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/libraries/JBFundingCycleMetadataResolver.sol:23: function redemptionRate(JBFundingCycle memory _fundingCycle) internal pure returns (uint256) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/libraries/JBFundingCycleMetadataResolver.sol:28: function ballotRedemptionRate(JBFundingCycle memory _fundingCycle) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/libraries/JBFundingCycleMetadataResolver.sol:37: function payPaused(JBFundingCycle memory _fundingCycle) internal pure returns (bool) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/libraries/JBFundingCycleMetadataResolver.sol:41: function distributionsPaused(JBFundingCycle memory _fundingCycle) internal pure returns (bool) {

Tools Used

None

Some parameters in examples given above are later hashed. It may be beneficial for those parameters to be in memory rather than calldata.

C4-009 : ++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.
juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBOperatorStore.sol::85 => for (uint256 _i = 0; _i < _permissionIndexes.length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBOperatorStore.sol::135 => for (uint256 _i = 0; _i < _operatorData.length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBOperatorStore.sol::165 => for (uint256 _i = 0; _i < _indexes.length; _i++) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::1008 => for (uint256 _i = 0; _i < _splits.length; ) {

Tools Used

Code Review

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

C4-010 : > 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 contract sections.
juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::438 => if (_terminals.length > 0) directory.setTerminalsOf(projectId, _terminals); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::481 => if (fundingCycleStore.latestConfigurationOf(_projectId) > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::498 => if (_terminals.length > 0) directory.setTerminalsOf(_projectId, _terminals); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::875 => if (_leftoverTokenCount > 0) tokenStore.mintFor(_owner, _projectId, _leftoverTokenCount, false); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::925 => if (_tokenCount > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::1032 => if (_constraints.distributionLimit > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBController.sol::1040 => if (_constraints.overflowAllowance > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20ProjectPayer.sol::268 => if (msg.value > 0) revert NO_MSG_VALUE_ALLOWED(); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20ProjectPayer.sol::312 => if (msg.value > 0) revert NO_MSG_VALUE_ALLOWED(); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::253 => if (msg.value > 0) revert NO_MSG_VALUE_ALLOWED(); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::275 => if (_leftoverAmount > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::345 => if (msg.value > 0) revert NO_MSG_VALUE_ALLOWED(); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::367 => if (_leftoverAmount > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20SplitsPayer.sol::477 => if (_splitAmount > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::149 => if (_standbyFundingCycleConfiguration > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::210 => if (_fundingCycleConfiguration > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::347 => _data.duration > 0 || juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::348 => _data.discountRate > 0 juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::364 => if (_metadata > 0) _metadataOf[_projectId][_configuration] = _metadata; juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::469 => _weight = _weight > 0 juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::560 => _baseFundingCycle.duration > 0 && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::589 => _fundingCycle.duration > 0 && block.timestamp >= _fundingCycle.start + _fundingCycle.duration juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBFundingCycleStore.sol::601 => _baseFundingCycle.duration > 0 && juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBProjects.sol::143 => if (bytes(_metadata.content).length > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSingleTokenPaymentTerminalStore.sol::475 => if (_currentOverflow > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSingleTokenPaymentTerminalStore.sol::518 => if (reclaimAmount > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol::259 => if (_splits[_i].lockedUntil > 0 || _splits[_i].allocator != IJBSplitAllocator(address(0))) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol::272 => } else if (_packedSplitParts2Of[_projectId][_domain][_group][_i] > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBSplitsStore.sol::326 => if (_packedSplitPart2 > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBTokenStore.sol::356 => if (_unclaimedTokensToBurn > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBTokenStore.sol::367 => if (_claimedTokensToBurn > 0) _token.burn(_projectId, _holder, _claimedTokensToBurn); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::346 => if (msg.value > 0) revert NO_MSG_VALUE_ALLOWED(); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::516 => if (balance > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::552 => if (msg.value > 0) revert NO_MSG_VALUE_ALLOWED(); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::745 => if (_tokenCount > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::772 => if (reclaimAmount > 0) _transferFrom(address(this), _beneficiary, reclaimAmount); juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::884 => if (netLeftoverDistributionAmount > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::964 => if (netDistributedAmount > 0) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::1022 => if (_payoutAmount > 0) { juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/abstract/JBPayoutRedemptionPaymentTerminal.sol::1297 => if (_tokenCount > 0)

Tools Used

None

Consider to replace > 0 with != 0 for gas optimization.

C4-011 : Keccak functions in constants waste gas

Impact

The contracts assigns two constants to the result of a keccak operation, which results in gas waste since the expression is computed each time the constant is accessed.

See this issue for more context: ethereum/solidity#9232 (https://github.com/ethereum/solidity/issues/9232)

Proof of Concept

juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20ProjectPayer.sol:215: if (keccak256(abi.encodePacked(_memo)) != keccak256(abi.encodePacked(defaultMemo))) juice-contracts-v2-code4rena-828bf2f3e719873daa08081cfa0d0a6deaa5ace5/contracts/JBETHERC20ProjectPayer.sol:219: if (keccak256(abi.encodePacked(_metadata)) != keccak256(abi.encodePacked(defaultMetadata)))

Tools Used

None

Replace the constant directive with immutable, or assign the already hashed value to the constants.

C4-012 : 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/

Solidity 0.8.13 has some improvements too but not well tested.

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.

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