Platform: Code4rena
Start Date: 26/07/2022
Pot Size: $75,000 USDC
Total HM: 29
Participants: 179
Period: 6 days
Judge: LSDan
Total Solo HM: 6
Id: 148
League: ETH
Rank: 116/179
Findings: 2
Award: $56.49
π Selected for report: 0
π Solo Findings: 0
π Selected for report: IllIllI
Also found by: 0x1f8b, 0x4non, 0x52, 0xA5DF, 0xDjango, 0xLovesleep, 0xNazgul, 0xNineDec, 0xSmartContract, 0xackermann, 0xc0ffEE, 0xf15ers, 0xmatt, 0xsanson, 0xsolstars, 8olidity, AuditsAreUS, Bahurum, Bnke0x0, CRYP70, CertoraInc, Ch_301, Chom, CryptoMartian, Deivitto, DevABDee, Dravee, ElKu, Franfran, Funen, GalloDaSballo, GimelSec, GiveMeTestEther, Green, JC, Jmaxmanblue, JohnSmith, Jujic, Junnon, Kenshin, Krow10, Kumpa, Lambda, MEP, Maxime, MiloTruck, Mohandes, NoamYakov, Picodes, RedOneN, Rohan16, Rolezn, Ruhum, RustyRabbit, Sm4rty, Soosh, StErMi, StyxRave, Tadashi, TomJ, Treasure-Seeker, TrungOre, Waze, _Adam, __141345__, ajtra, ak1, apostle0x01, arcoun, asutorufos, async, benbaessler, berndartmueller, bin2chen, brgltd, c3phas, cRat1st0s, carlitox477, chatch, codetilda, codexploder, cryptonue, cryptphi, csanuragjain, cthulhu_cult, delfin454000, dipp, dirk_y, djxploit, ellahi, exd0tpy, fatherOfBlocks, giovannidisiena, hansfriese, horsefacts, hyh, idkwhatimdoing, indijanc, jayfromthe13th, jayphbee, joestakey, kenzo, kyteg, lucacez, luckypanda, mics, minhquanym, obront, oyc_109, pedr02b2, rajatbeladiya, rbserver, reassor, robee, rokinot, rotcivegaf, sach1r0, saian, saneryee, sashik_eth, scaraven, shenwilly, simon135, sseefried, supernova, teddav, ych18, zuhaibmohd, zzzitron
35.1687 USDC - $35.17
You allow in some arrays to have duplicates. Sometimes you assumes there are no duplicates in the array.
VoteEscrowDelegation.delegate pushes (tokenId)
To improve algorithm precision instead using division in comparison use multiplication in the following scenario:
Instead a < b / c use a * c < b.
In all of the big and trusted contracts this rule is maintained.
GolomTrader.sol, 211, require( o.totalAmt >= o.exchange.paymentAmt + o.prePayment.paymentAmt + o.refererrAmt + (o.totalAmt * 50) / 10000, 'amt not matching' );
Some fee parameters of functions are not checked for invalid values. Validate the parameters:
RewardDistributor.addFee (fee) DummyRewardDistributor.addFee (fee)
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 GolomAirdrop.sol line 94: token.safeApprove(_ve, type(uint256).max);
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: RewardDistributor.sol, In line 158 with Empty Require message. Solidity file: VoteEscrowCore.sol, In line 944 with Empty Require message. Solidity file: VoteEscrowCore.sol, In line 895 with Empty Require message. Solidity file: GolomTrader.sol, In line 350 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: GolomTrader.sol, In line 217 with Require message: mgmtm
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.
GolomToken.sol.setMinter _minter RewardDistributor.sol.traderClaim addr
WETH address is hardcoded but it may differ on other chains, e.g. Polygon, so make sure to check this before deploying and update if necessary You should consider injecting WETH address via the constructor. (previous issue: https://github.com/code-423n4/2021-10-ambire-findings/issues/54)
Hardcoded weth address in GolomTrader.sol
From solidity docs: Properly functioning code should never reach a failing assert statement; if this happens there is a bug in your contract which you should fix. With assert the user pays the gas and with require it doesn't. The ETH network gas isn't cheap and users can see it as a scam.
VoteEscrowCore.sol : reachable assert in line 860 VoteEscrowCore.sol : reachable assert in line 518 VoteEscrowCore.sol : reachable assert in line 665 VoteEscrowCore.sol : reachable assert in line 1205 VoteEscrowCore.sol : reachable assert in line 492 VoteEscrowCore.sol : reachable assert in line 990
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).
GolomAirdrop.sol, updateEndTimestamp
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.
DummyRewardDistributor.sol: function addFee parameter fee isn't used. (addFee is public) DummyRewardDistributor.sol: function addFee parameter addr isn't used. (addFee is public)
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-07-golom/tree/main/contracts/vote-escrow/VoteEscrowCore.sol#L682 https://github.com/code-423n4/2022-07-golom/tree/main/contracts/core/GolomTrader.sol#L154 https://github.com/code-423n4/2022-07-golom/tree/main/contracts/rewards/GolomAirdrop.sol#L203 https://github.com/code-423n4/2022-07-golom/tree/main/contracts/vote-escrow/VoteEscrowCore.sol#L861 https://github.com/code-423n4/2022-07-golom/tree/main/contracts/rewards/DummyRewardDistributor.sol#L44
To give more trust to users: functions that set key/critical variables should be put behind a timelock.
https://github.com/code-423n4/2022-07-golom/tree/main/contracts/governance/GolomToken.sol#L58 https://github.com/code-423n4/2022-07-golom/tree/main/contracts/rewards/GolomAirdrop.sol#L142 https://github.com/code-423n4/2022-07-golom/tree/main/contracts/governance/Timlock.sol#L55 https://github.com/code-423n4/2022-07-golom/tree/main/contracts/vote-escrow/VoteEscrowCore.sol#L868 https://github.com/code-423n4/2022-07-golom/tree/main/contracts/governance/Timlock.sol#L72 https://github.com/code-423n4/2022-07-golom/tree/main/contracts/vote-escrow/VoteEscrowCore.sol#L664 https://github.com/code-423n4/2022-07-golom/tree/main/contracts/core/GolomTrader.sol#L444
Some tokens (like USDT) 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.
approve without approving 0 first GolomAirdrop.sol, 94, token.safeApprove(_ve, type(uint256).max);
Division by 0 can lead to accidentally revert, (An example of a similar issue - https://github.com/code-423n4/2021-10-defiprotocol-findings/issues/84)
https://github.com/code-423n4/2022-07-golom/tree/main/contracts/rewards/RewardDistributor.sol#L195 epochs might be 0 https://github.com/code-423n4/2022-07-golom/tree/main/contracts/vote-escrow/VoteEscrowCore.sol#L1219 point might be 0
There are ERC20 tokens that charge fee for every transfer() / transferFrom().
Vault.sol#addValue() assumes that the received amount is the same as the transfer amount, and uses it to calculate attributions, balance amounts, etc. But, the actual transferred amount can be lower for those tokens. Therefore it's recommended to use the balance change before and after the transfer instead of the amount. This way you also support the tokens with transfer fee - that are popular.
https://github.com/code-423n4/2022-07-golom/tree/main/contracts/vote-escrow/VoteEscrowCore.sol#L861
Need to use safeTransfer instead of transfer. As there are popular tokens, such as USDT that transfer/trasnferFrom method doesnβt return anything. The transfer return value has to be checked (as there are some other tokens that returns false instead revert), that means you must
RewardDistributor.sol, 209 (multiStakerClaim), weth.transfer(tokenowner, rewardEth); RewardDistributor.sol, 208 (multiStakerClaim), rewardToken.transfer(tokenowner, reward); RewardDistributor.sol, 165 (exchangeClaim), rewardToken.transfer(addr, reward); GolomTrader.sol, 301 (fillBid), nftcontract.transferFrom(msg.sender, o.signer, o.tokenId); RewardDistributor.sol, 151 (traderClaim), rewardToken.transfer(addr, reward); GolomTrader.sol, 154 (payEther), payable(payAddress).transfer(payAmt); // royalty transfer to royaltyaddress GolomTrader.sol, 361 (fillCriteriaBid), nftcontract.transferFrom(msg.sender, o.signer, tokenId);
π Selected for report: JohnSmith
Also found by: 0x1f8b, 0xA5DF, 0xDjango, 0xKitsune, 0xLovesleep, 0xNazgul, 0xSmartContract, 0xmatt, 0xsam, Aymen0909, Bnke0x0, CRYP70, Chandr, Chinmay, CodingNameKiki, Deivitto, Dravee, ElKu, Fitraldys, Funen, GalloDaSballo, Green, IllIllI, JC, Jmaxmanblue, Junnon, Kaiziron, Kenshin, Krow10, Maxime, Migue, MiloTruck, Noah3o6, NoamYakov, Randyyy, RedOneN, ReyAdmirado, Rohan16, Rolezn, Ruhum, Sm4rty, StyxRave, TomJ, Tomio, _Adam, __141345__, ajtra, ak1, apostle0x01, asutorufos, async, benbaessler, brgltd, c3phas, cRat1st0s, carlitox477, delfin454000, djxploit, durianSausage, ellahi, erictee, fatherOfBlocks, gerdusx, gogo, hyh, jayfromthe13th, jayphbee, joestakey, kaden, kenzo, kyteg, ladboy233, lucacez, m_Rassska, mics, minhquanym, oyc_109, pfapostol, rbserver, reassor, rfa, robee, rokinot, sach1r0, saian, samruna, sashik_eth, simon135, supernova, tofunmi, zuhaibmohd
21.3211 USDC - $21.32
In the following files there are state variables that could be set immutable to save gas.
trader in DummyRewardDistributor.sol eip712DomainHash in Emitter.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.
VoteEscrowCore.sol, decimals RewardDistributor.sol, rewardLP VoteEscrowCore.sol, version RewardDistributor.sol, claimedUpto VoteEscrowCore.sol, ERC165_INTERFACE_ID VoteEscrowCore.sol, ERC721_METADATA_INTERFACE_ID VoteEscrowCore.sol, ERC721_INTERFACE_ID
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++) { ... }
RewardDistributor.sol, epochs, 157 GolomTrader.sol, proof, 415 VoteEscrowDelegation.sol, delegated, 171 VoteEscrowDelegation.sol, _array, 199 RewardDistributor.sol, tokenids, 180 VoteEscrowDelegation.sol, delegatednft, 189 RewardDistributor.sol, epochs, 143
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: VoteEscrowDelegation.sol, i, 199 just change to unchecked: VoteEscrowCore.sol, i, 1044 just change to unchecked: VoteEscrowCore.sol, i, 1167 change to prefix increment and unchecked: GolomTrader.sol, i, 415 change to prefix increment and unchecked: VoteEscrowDelegation.sol, index, 171 change to prefix increment and unchecked: RewardDistributor.sol, index, 226 change to prefix increment and unchecked: RewardDistributor.sol, index, 143 change to prefix increment and unchecked: RewardDistributor.sol, index, 157 change to prefix increment and unchecked: VoteEscrowDelegation.sol, index, 189 change to prefix increment and unchecked: RewardDistributor.sol, tindex, 180 change to prefix increment and unchecked: RewardDistributor.sol, index, 273 just change to unchecked: VoteEscrowCore.sol, i, 1115 change to prefix increment and unchecked: RewardDistributor.sol, index, 258
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:
RewardDistributor.sol, 157 VoteEscrowCore.sol, 1044 VoteEscrowCore.sol, 1167 VoteEscrowDelegation.sol, 189 VoteEscrowCore.sol, 1115 VoteEscrowDelegation.sol, 171 RewardDistributor.sol, 273 RewardDistributor.sol, 143 RewardDistributor.sol, 258 RewardDistributor.sol, 180 RewardDistributor.sol, 226 GolomTrader.sol, 415
Unnecessary default assignments, you can just declare and it will save gas and have the same meaning.
RewardDistributor.sol (L#45) : uint256 public epoch = 0; VoteEscrowDelegation.sol (L#50) : uint256 public MIN_VOTING_POWER_REQUIRED = 0;
You can change the order of the storage variables to decrease memory uses.
In VoteEscrowCore.sol,rearranging the storage fields can optimize to: 12 slots from: 14 slots. The new order of types (you choose the actual variables): 1. uint256 2. uint256 3. uint256 4. uint256 5. uint256 6. string 7. string 8. string 9. uint256 10. address 11. bytes4 12. bytes4 13. bytes4 14. address 15. uint8 16. uint8 17. uint8 18. uint8 19. int128
Use bytes32 instead of string to save gas whenever possible. String is a dynamic data structure and therefore is more gas consuming then bytes32.
TokenUriHelper.sol (L106), string memory json = Base64.encode( bytes( string( abi.encodePacked( '{"name": "Lock #', toString(_tokenId), '", "description": "Golom locks, can be used to claim protocol fees and token emission", "attributes": [{"trait_type":"Lock End","value":"', toString(_locked_end),
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: RewardDistributor.sol, In line 292, Require message length to shorten: 36, The message: RewardDistributor: time not over yet Solidity file: VoteEscrowCore.sol, In line 983, Require message length to shorten: 36, The message: Cannot add to expired lock. Withdraw Solidity file: VoteEscrowCore.sol, In line 929, Require message length to shorten: 36, The message: Cannot add to expired lock. Withdraw Solidity file: RewardDistributor.sol, In line 309, Require message length to shorten: 36, The message: RewardDistributor: time not over yet Solidity file: VoteEscrowCore.sol, In line 945, Require message length to shorten: 38, The message: Can only lock until time in the future Solidity file: VoteEscrowDelegation.sol, In line 73, Require message length to shorten: 36, The message: VEDelegation: Need more voting power
Using != 0 is slightly cheaper than > 0. (see https://github.com/code-423n4/2021-12-maple-findings/issues/75 for similar issue)
VoteEscrowCore.sol, 981: change '_value > 0' to '_value != 0' VoteEscrowCore.sol, 727: change 'epoch > 0' to 'epoch != 0' VoteEscrowDelegation.sol, 103: change 'nCheckpoints > 0' to 'nCheckpoints != 0' VoteEscrowCore.sol, 944: change '_value > 0' to '_value != 0' VoteEscrowCore.sol, 927: change '_value > 0' to '_value != 0' VoteEscrowDelegation.sol, 73: change 'balance > 0' to 'balance != 0' GolomTrader.sol, 152: change 'payAmt > 0' to 'payAmt != 0'
Order GolomTrader.sol.cancelOrder - unnecessary casting Order(o) Order GolomTrader.sol.fillBid - unnecessary casting Order(o) Order GolomTrader.sol.fillAsk - unnecessary casting Order(o) Order GolomTrader.sol.validateOrder - unnecessary casting Order(o) Order GolomTrader.sol.fillCriteriaBid - unnecessary casting Order(o) Order Emitter.sol.pushOrder - unnecessary casting Order(o)
You can use unchecked in the following calculations since there is no risk to overflow:
VoteEscrowCore.sol (L#942) - uint256 unlock_time = ((block.timestamp + _lock_duration) / WEEK) * WEEK; RewardDistributor.sol (L#286) - traderEnableDate = block.timestamp + 1 days; RewardDistributor.sol (L#302) - voteEscrowEnableDate = block.timestamp + 1 days; GolomTrader.sol (L#449) - distributorEnableDate = block.timestamp + 1 days; VoteEscrowCore.sol (L#999) - require(unlock_time <= block.timestamp + MAXTIME, 'Voting lock can be 4 years max'); VoteEscrowCore.sol (L#946) - require(unlock_time <= block.timestamp + MAXTIME, 'Voting lock can be 4 years max'); ++tokenId; GolomToken.sol (L#60) - minterEnableDate = block.timestamp + 1 days; VoteEscrowCore.sol (L#994) - uint256 unlock_time = ((block.timestamp + _lock_duration) / WEEK) * WEEK; RewardDistributor.sol (L#106) - if (block.timestamp > startTime + (epoch) * secsInDay) { GolomAirdrop.sol (L#160) - require(block.timestamp + 30 days > newEndTimestamp, 'Owner: New timestamp too far');
Empty else statement can be removed to save gas.
RewardDistributor.sol.addFee
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.)
VoteEscrowCore.sol, _balance, { return ownerToNFTokenCount[_owner]; } GolomTrader.sol, _hashOrder, { return _hashOrderinternal(o, [o.nonce, o.deadline]); } Emitter.sol, hashOrder, { return _hashOrderinternal(o, [o.nonce, o.deadline]); }
The following functions are used exactly once. Therefore you can inline them and save gas and improve code clearness.
VoteEscrowCore.sol, _balanceOfAtNFT Emitter.sol, hashOrder Emitter.sol, _hashOrderinternal VoteEscrowCore.sol, _isContract VoteEscrowCore.sol, _mint VoteEscrowCore.sol, _addTokenToOwnerList GolomTrader.sol, _hashOrderinternal VoteEscrowCore.sol, _clearApproval VoteEscrowCore.sol, _removeTokenFromOwnerList Emitter.sol, emitOrder GolomTrader.sol, _hashOrder
You calculate the power of 10 every time you use it instead of caching it once as a constant variable and using it instead. Fix the following code lines:
RewardDistributor.sol, 99 : You should cache the used power of 10 as constant state variable since it's used several times (2): if (rewardToken.totalSupply() > 1000000000 * 10**18) {
Change if -> revert pattern to 'require' to save gas and improve code quality, if (some_condition) { revert(revert_message) }
to: require(!some_condition, revert_message)
In the following locations:
GolomTrader.sol, 426
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-07-golom/tree/main/contracts/core/GolomTrader.sol#L225 https://github.com/code-423n4/2022-07-golom/tree/main/contracts/governance/Timlock.sol#L66 https://github.com/code-423n4/2022-07-golom/tree/main/contracts/vote-escrow/VoteEscrowDelegation.sol#L54 https://github.com/code-423n4/2022-07-golom/tree/main/contracts/vote-escrow/VoteEscrowCore.sol#L855