Platform: Code4rena
Start Date: 26/05/2022
Pot Size: $75,000 USDT
Total HM: 31
Participants: 71
Period: 7 days
Judge: GalloDaSballo
Total Solo HM: 18
Id: 126
League: ETH
Rank: 38/71
Findings: 2
Award: $174.32
π Selected for report: 0
π Solo Findings: 0
π Selected for report: IllIllI
Also found by: 0x1f8b, 0x29A, 0xDjango, 0xNazgul, 0xf15ers, BouSalman, Chom, Deivitto, Dravee, ElKu, FSchmoede, Funen, GimelSec, Hawkeye, MiloTruck, Picodes, SecureZeroX, SmartSek, TerrierLover, WatchPug, _Adam, asutorufos, berndartmueller, c3phas, catchup, cccz, cogitoergosumsw, cryptphi, csanuragjain, delfin454000, dipp, ellahi, gzeon, hansfriese, horsefacts, hyh, kirk-baird, minhquanym, oyc_109, pauliax, reassor, robee, sashik_eth, shenwilly, simon135, sorrynotsorry, sseefried, unforgiven, xiaoming90, z3s
101.0887 USDT - $101.09
This issue is about arithmetic computation that could have been done more percise. The following are places in the codebase in which you multiplied after the divisions. Doing the multiplications at start lead to more accurate calculations. This is a list of places in the code that this appears (Solidity file, line number, actual line):
VE3DLocker.sol, 376, uint256 currentEpoch = block.timestamp.div(rewardsDuration).mul(rewardsDuration); VE3DLocker.sol, 121, uint256 currentEpoch = block.timestamp.div(rewardsDuration).mul(rewardsDuration); VE3DLocker.sol, 485, uint256 nextEpoch = block.timestamp.div(rewardsDuration).mul(rewardsDuration).add( rewardsDuration ); VE3DLocker.sol, 535, uint256 lockEpoch = block.timestamp.div(rewardsDuration).mul(rewardsDuration); VE3DLocker.sol, 422, _time = _time.div(rewardsDuration).mul(rewardsDuration);
Some fee parameters of functions are not checked for invalid values. Validate the parameters:
Booster.setFees (_stakerLockIncentiveFee) Booster.setFees (_lockFees) Booster.setFeeManager (_feeM) Booster.setFees (_callerFees) VeAssetDepositor.setFeeManager (_feeManager)
You use safeApprove of openZeppelin although it's deprecated. (see https://github.com/OpenZeppelin/openzeppelin-contracts/blob/566a774222707e424896c0c390a84dc3c13bdcb2/contracts/token/ERC20/utils/SafeERC20.sol#L38) You should change it to increase/decrease Allowance as OpenZeppilin says.
Deprecated safeApprove in VE3DLocker.sol line 214: IERC20(rewardData[_rewardsToken].ve3Token).safeApprove( Deprecated safeApprove in VoterProxy.sol line 160: IERC20(veAsset).safeApprove(escrow, _value); Deprecated safeApprove in VE3DLocker.sol line 220: IERC20(_rewardsToken).safeApprove(rewardData[_rewardsToken].veAssetDeposits, 0); Deprecated safeApprove in VE3DRewardPool.sol line 286: IERC20(_rewardToken).safeApprove(rewardTokenInfo[_rewardToken].veAssetDeposits, 0); Deprecated safeApprove in VeAssetDepositor.sol line 161: IERC20(minter).safeApprove(_stakeAddress, _amount);
The following requires are with empty messages. This is very important to add a message for any require. So the user has enough information to know the reason of failure.
Solidity file: VE3DLocker.sol, In line 154 with Empty Require message. Solidity file: VE3DLocker.sol, In line 180 with Empty Require message. Solidity file: VE3DLocker.sol, In line 155 with Empty Require message.
The following requires has a non comprehensive messages. This is very important to add a comprehensive message for any require. Such that the user has enough information to know the reason of failure:
Solidity file: RewardFactory.sol, In line 80 with Require message: !auth Solidity file: Booster.sol, In line 124 with Require message: !auth Solidity file: VoterProxy.sol, In line 167 with Require message: !auth Solidity file: Booster.sol, In line 468 with Require message: !auth Solidity file: Booster.sol, In line 261 with Require message: !add
external / public functions parameters should be validated to make sure the address is not 0. Otherwise if not given the right input it can mistakenly lead to loss of user funds.
VE3DLocker.sol.approveRewardDistributor _rewardsToken BaseRewardPool.sol.stakeFor _for PoolManager.sol.addPool _lptoken VeTokenMinter.sol.withdraw _destination SmartWalletWhitelist.sol.approveWallet _wallet
Make sure the treasury is not address(0).
Booster.sol.setTreasury _treasury
The project is compiled with different versions of solidity, which is not recommended because it can lead to undefined behaviors.
You should use safe math for solidity version <8 since there is no default over/under flow check it suchversions of solidity.
The contract Babylonian.sol doesn't use safe math and is of solidity version < 8 The contract FullMath.sol doesn't use safe math and is of solidity version < 8 The contract BitMath.sol doesn't use safe math and is of solidity version < 8 The contract FixedPoint.sol doesn't use safe math and is of solidity version < 8 The contract Migrations.sol doesn't use safe math and is of solidity version < 8
owner param should be validated to make sure the owner address is not address(0). Otherwise if not given the right input all only owner accessible functions will be unaccessible.
Booster.sol.setOwner _owner VoterProxy.sol.setOwner _owner
Users can mistakenly think that the return value is the named return, but it is actually the actualreturn statement that comes after. To know that the user needs to read the code and is confusing. Furthermore, removing either the actual return or the named return will save gas.
VE3DLocker.sol, balanceAtEpochOf VE3DLocker.sol, findEpochId VE3DLocker.sol, totalSupplyAtEpoch VE3DLocker.sol, lockedBalances VE3DLocker.sol, totalSupply
The following contracts have a function that allows them an admin to change it to a different address. If the admin accidentally uses an invalid address for which they do not have the private key, then the system gets locked. It is important to have two steps admin change where the first is announcing a pending new admin and the new address should then claim its ownership. A similar issue was reported in a previous contest and was assigned a severity of medium: code-423n4/2021-06-realitycards-findings#105
VoterProxy.sol VeAssetDepositor.sol IPools.sol Booster.sol
The following functions are missing reentrancy modifier although some other pulbic/external functions does use reentrancy modifer. Even though I did not find a way to exploit it, it seems like those functions should have the nonReentrant modifier as the other functions have it as well..
VE3DLocker.sol, recoverERC20 is missing a reentrancy modifier VE3DLocker.sol, setApprovals is missing a reentrancy modifier
In the following functions no value is returned, due to which by default value of return will be 0. We assumed that after the update you return the latest new value. (similar issue here: https://github.com/code-423n4/2021-10-badgerdao-findings/issues/85).
VeTokenMinter.sol, updateveAssetWeight
Those are functions and parameters pairs that the function doesn't use the parameter. In case those functions are external/public this is even worst since the user is required to put value that never used and can misslead him and waste its time.
VoterProxy.sol: function execute parameter _data isn't used. (execute is external) VoterProxy.sol: function execute parameter _value isn't used. (execute is external) VoterProxy.sol: function execute parameter _to isn't used. (execute is external)
Transferring tokens to the zero address is usually prohibited to accidentally avoid "burning" tokens by sending them to an unrecoverable zero address.
https://github.com/code-423n4/2022-05-vetoken/tree/main/contracts/ExtraRewardStashV2.sol#L96 https://github.com/code-423n4/2022-05-vetoken/tree/main/contracts/VE3DLocker.sol#L683 https://github.com/code-423n4/2022-05-vetoken/tree/main/contracts/VeTokenMinter.sol#L78 https://github.com/code-423n4/2022-05-vetoken/tree/main/contracts/VeTokenMinter.sol#L72 https://github.com/code-423n4/2022-05-vetoken/tree/main/contracts/BaseRewardPool.sol#L208
In the following functions no value is returned, due to which by default value of return will be 0. We assumed that after the update you return the latest new value. (similar issue here: https://github.com/code-423n4/2021-10-badgerdao-findings/issues/85).
VeTokenMinter.sol, updateveAssetWeight
Those are functions and parameters pairs that the function doesn't use the parameter. In case those functions are external/public this is even worst since the user is required to put value that never used and can misslead him and waste its time.
VoterProxy.sol: function execute parameter _data isn't used. (execute is external) VoterProxy.sol: function execute parameter _value isn't used. (execute is external) VoterProxy.sol: function execute parameter _to isn't used. (execute is external)
To give more trust to users: functions that set key/critical variables should be put behind a timelock.
https://github.com/code-423n4/2022-05-vetoken/tree/main/contracts/Booster.sol#L243 https://github.com/code-423n4/2022-05-vetoken/tree/main/contracts/RewardFactory.sol#L79 https://github.com/code-423n4/2022-05-vetoken/tree/main/contracts/token/VE3Token.sol#L21 https://github.com/code-423n4/2022-05-vetoken/tree/main/contracts/Booster.sol#L141 https://github.com/code-423n4/2022-05-vetoken/tree/main/contracts/VE3DLocker.sol#L206
#0 - GalloDaSballo
2022-07-09T17:58:31Z
Invalid as the code is meant to calculate epochs
##Β Does not validate the input fee parameter Valid Low
Valid NC
Valid NC
Disagree as they are permissioned functions
Rest looks invalid
1L, 2 NC
π Selected for report: IllIllI
Also found by: 0x1f8b, 0x29A, 0xKitsune, 0xNazgul, 0xf15ers, 0xkatana, Cityscape, Dravee, ElKu, FSchmoede, Funen, GalloDaSballo, Hawkeye, Kaiziron, MiloTruck, Randyyy, RoiEvenHaim, Ruhum, SecureZeroX, SmartSek, TerrierLover, TomJ, Tomio, WatchPug, Waze, _Adam, asutorufos, c3phas, catchup, cogitoergosumsw, delfin454000, ellahi, fatherOfBlocks, gzeon, hansfriese, horsefacts, jonatascm, minhquanym, oyc_109, pauliax, reassor, robee, sach1r0, saian, sashik_eth, simon135, z3s
73.2316 USDT - $73.23
Boolean variables can be checked within conditionals directly without the use of equality operators to true/false.
RewardFactory.sol, 40: require(rewardAccess[msg.sender] == true, "!auth"); ExtraRewardStashV1.sol, 152: if (token == address(0)) return true; RewardFactory.sol, 103: require(operators.contains(_msgSender()) || rewardAccess[_msgSender()] == true, "!auth"); VoterProxy.sol, 96: if (protectedTokens[_gauge] == false) { Booster.sol, 498: require(pool.shutdown == false, "pool is closed");
In the following files there are state variables that could be set immutable to save gas.
_symbol in VE3DLocker.sol veToken in VeTokenMinter.sol _name in VE3DLocker.sol rewardFactory in StashFactory.sol totalCliffs in VeTokenMinter.sol
Unused state variables are gas consuming at deployment (since they are located in storage) and are a bad code practice. Removing those variables will decrease deployment gas cost and improve code quality. This is a full list of all the unused storage variables we found in your code base.
ExtraRewardStashV1.sol, maxRewards ExtraRewardStashV3.sol, WEEK
Unused local variables are gas consuming, since the initial value assignment costs gas. And are a bad code practice. Removing those variables will decrease the gas cost and improve code quality. This is a full list of all the unused storage variables we found in your code base.
VE3DLocker.sol, _checkpointEpoch, nextEpochDate VE3DLocker.sol, constructor, currentEpoch
Caching the array length is more gas efficient. This is because access to a local variable in solidity is more efficient than query storage / calldata / memory. We recommend to change from:
for (uint256 i=0; i<array.length; i++) { ... }
to:
uint len = array.length for (uint256 i=0; i<len; i++) { ... }
BaseRewardPool.sol, extraRewards, 218 Booster.sol, poolInfo, 329 VE3DLocker.sol, userRewards, 286 VE3DLocker.sol, locks, 457 VE3DLocker.sol, rewardTokens, 720
Prefix increments are cheaper than postfix increments.
Further more, using unchecked {++x} is even more gas efficient, and the gas saving accumulates every iteration and can make a real change
There is no risk of overflow caused by increamenting the iteration index in for loops (the ++i
in for (uint256 i = 0; i < numIterations; ++i)
).
But increments perform overflow checks that are not necessary in this case.
These functions use not using prefix increments (++x
) or not using the unchecked keyword:
change to prefix increment and unchecked: VE3DRewardPool.sol, i, 257 change to prefix increment and unchecked: VE3DLocker.sol, i, 720 change to prefix increment and unchecked: Booster.sol, i, 329 change to prefix increment and unchecked: ExtraRewardStashV2.sol, i, 213 change to prefix increment and unchecked: VE3DLocker.sol, i, 457
In for loops you initialize the index to start from 0, but it already initialized to 0 in default and this assignment cost gas. It is more clear and gas efficient to declare without assigning 0 and will have the same meaning:
ExtraRewardStashV2.sol, 137 RewardFactory.sol, 49 ExtraRewardStashV3.sol, 84 BaseRewardPool.sol, 199 VE3DRewardPool.sol, 148
Unnecessary default assignments, you can just declare and it will save gas and have the same meaning.
BaseRewardPool.sol (L#70) : uint256 public queuedRewards = 0; VeAssetDepositor.sol (L#28) : uint256 public incentiveVeAsset = 0; VirtualBalanceRewardPool.sol (L#74) : uint256 public periodFinish = 0; BaseRewardPool.sol (L#71) : uint256 public currentRewards = 0; VirtualBalanceRewardPool.sol (L#78) : uint256 public queuedRewards = 0;
You can change the order of the storage variables to decrease memory uses.
In VoterProxy.sol,rearranging the storage fields can optimize to: 9 slots from: 10 slots. The new order of types (you choose the actual variables): 1. string 2. IVoteEscrow.EscrowModle 3. address 4. bytes4 5. address 6. address 7. address 8. address 9. address 10. address
In VE3DLocker.sol,rearranging the storage fields can optimize to: 11 slots from: 12 slots. The new order of types (you choose the actual variables): 1. IERC20 2. EnumerableSet.AddressSet 3. uint256 4. uint256 5. uint256 6. uint256 7. uint256 8. uint256 9. string 10. string 11. uint8 12. bool
The following require messages are of length more than 32 and we think are short enough to short them into exactly 32 characters such that it will be placed in one slot of memory and the require function will cost less gas. The list:
Solidity file: BitMath.sol, In line 45, Require message length to shorten: 34, The message: BitMath::leastSignificantBit: zero Solidity file: FixedPoint.sol, In line 85, Require message length to shorten: 33, The message: FixedPoint::muluq: upper overflow Solidity file: FixedPoint.sol, In line 105, Require message length to shorten: 35, The message: FixedPoint::divuq: division by zero Solidity file: BitMath.sol, In line 8, Require message length to shorten: 33, The message: BitMath::mostSignificantBit: zero Solidity file: FixedPoint.sol, In line 131, Require message length to shorten: 38, The message: FixedPoint::fraction: division by zero
Using != 0 is slightly cheaper than > 0. (see https://github.com/code-423n4/2021-12-maple-findings/issues/75 for similar issue)
BaseRewardPool.sol, 196: change '_amount > 0' to '_amount != 0' VE3DLocker.sol, 649: change '_checkDelay > 0' to '_checkDelay != 0' VE3DLocker.sol, 626: change '_checkDelay > 0' to '_checkDelay != 0' Booster.sol, 590: change 'stakerLockFeesIncentive > 0' to 'stakerLockFeesIncentive != 0' BitMath.sol, 8: change 'x > 0' to 'x != 0'
You can use unchecked in the following calculations since there is no risk to overflow:
VeAssetDepositor.sol (L#73) - uint256 unlockAt = block.timestamp + maxTime; ExtraRewardStashV1.sol (L#106) - if (block.timestamp > tokenInfo.lastActiveTime + WEEK) { VeAssetDepositor.sol (L#102) - uint256 unlockAt = block.timestamp + maxTime; ExtraRewardStashV2.sol (L#187) - if (block.timestamp <= t.lastActiveTime + WEEK) { ExtraRewardStashV2.sol (L#101) - if (block.timestamp > tokenInfo[i].lastActiveTime + WEEK) {
You can inline the following functions instead of writing a specific function to save gas. (see https://github.com/code-423n4/2021-11-nested-findings/issues/167 for a similar issue.)
FixedPoint.sol, decode, { return uint112(self._x >> RESOLUTION); } FixedPoint.sol, decode144, { return uint144(self._x >> RESOLUTION); } FixedPoint.sol, encode144, { return uq144x112(uint256(x) << RESOLUTION); } FixedPoint.sol, encode, { return uq112x112(uint224(x) << RESOLUTION); } VE3DLocker.sol, _lastTimeRewardApplicable, { return Math.min(block.timestamp, _finishTime); }
The following functions are used exactly once. Therefore you can inline them and save gas and improve code clearness.
VE3DLocker.sol, _notifyReward ExtraRewardStashV3.sol, checkForNewRewardTokens VoterProxy.sol, _withdrawSome Booster.sol, _earmarkRewards ExtraRewardStashV1.sol, setToken
Using newer compiler versions and the optimizer gives gas optimizations and additional safety checks are available for free.
The advantages of versions 0.8.* over <0.8.0 are:
1. Safemath by default from 0.8.0 (can be more gas efficient than library based safemath.) 2. Low level inliner : from 0.8.2, leads to cheaper runtime gas. Especially relevant when the contract has small functions. For example, OpenZeppelin libraries typically have a lot of small helper functions and if they are not inlined, they cost an additional 20 to 40 gas because of 2 extra jump instructions and additional stack operations needed for function calls. 3. Optimizer improvements in packed structs: Before 0.8.3, storing packed structs, in some cases used an additional storage read operation. After EIP-2929, if the slot was already cold, this means unnecessary stack operations and extra deploy time costs. However, if the slot was already warm, this means additional cost of 100 gas alongside the same unnecessary stack operations and extra deploy time costs. 4. Custom errors from 0.8.4, leads to cheaper deploy time cost and run time cost. Note: the run time cost is only relevant when the revert condition is met. In short, replace revert strings by custom errors.
FullMath.sol Babylonian.sol FixedPoint.sol BitMath.sol Migrations.sol
We recommend not to cache msg.sender since calling it is 2 gas while reading a variable is more.
https://github.com/code-423n4/2022-05-vetoken/tree/main/contracts/VoterProxy.sol#L52 https://github.com/code-423n4/2022-05-vetoken/tree/main/contracts/VeAssetDepositor.sol#L48 https://github.com/code-423n4/2022-05-vetoken/tree/main/contracts/token/VE3Token.sol#L19 https://github.com/code-423n4/2022-05-vetoken/tree/main/contracts/token/VeToken.sol#L16 https://github.com/code-423n4/2022-05-vetoken/tree/main/contracts/Booster.sol#L112
#0 - GalloDaSballo
2022-07-18T23:26:27Z
5 immutables will save 2.1k * 5 = 10500 gas
Rest will save less than 500 gas
Let's estimate at 11k saved