Platform: Code4rena
Start Date: 01/08/2022
Pot Size: $50,000 USDC
Total HM: 26
Participants: 133
Period: 5 days
Judge: Jack the Pug
Total Solo HM: 6
Id: 151
League: ETH
Rank: 22/133
Findings: 3
Award: $489.26
π Selected for report: 0
π Solo Findings: 0
addMember function makes a call to 'ecrecover()' to grab the 'community owner' & 'new member'. The problem is that if 'ecrecover()' fails it will return a 0 which is currently not guarded against. This was a problem in the famous Polygon Matic token bug. There should be validation that the return value from 'ecrecover()' is not zero to prevent malicious hashes from being passed in. Also The next 4 lines of code present a problem which will never allow the if statement to run even when it should.
Overall, system uses ecrecover on the protocol implementation.
Code Review
Add: address signaturesigner = ecrecover(hash, o.v, o.r, o.s); require(signaturesidnger != 0, "Recovery failed")
Also the 'require(signaturesigner == o.signer, 'invalid signature');' should be removed and everywhere 'validateOrder()' is called the Order status should be validated by reverting if it is 0.
#0 - parv3213
2022-08-11T13:35:46Z
Duplicate of #371
π Selected for report: Lambda
Also found by: 0x1f8b, 0x52, 0xA5DF, 0xNazgul, 0xNineDec, 0xSmartContract, 0xSolus, 0xf15ers, 0xkatana, 0xsolstars, 8olidity, Aymen0909, Bahurum, Bnke0x0, CertoraInc, Chom, CodingNameKiki, Deivitto, Dravee, ElKu, Extropy, Funen, GalloDaSballo, Guardian, IllIllI, JC, Jujic, MEP, Noah3o6, ReyAdmirado, Rohan16, Rolezn, Ruhum, Sm4rty, SooYa, Soosh, Throne6g, TomJ, Tomio, TrungOre, Waze, Yiko, _Adam, __141345__, a12jmx, ajtra, ak1, arcoun, asutorufos, ayeslick, benbaessler, berndartmueller, bin2chen, bobirichman, brgltd, bulej93, byndooa, c3phas, codexploder, cryptonue, cryptphi, defsec, delfin454000, dipp, djxploit, erictee, exd0tpy, fatherOfBlocks, gogo, hake, hansfriese, horsefacts, hyh, ignacio, indijanc, joestakey, kaden, mics, minhquanym, neumo, obront, oyc_109, p_crypt0, pfapostol, poirots, rbserver, robee, rokinot, rotcivegaf, sach1r0, saian, samruna, saneryee, scaraven, sikorico, simon135, sseefried, supernova
40.6234 USDC - $40.62
The protocol is using low level calls with solidity version 0.8.x which can result in optimizer bug.
https://medium.com/certora/overly-optimistic-optimizer-certora-bug-disclosure-2101e3f7994d
https://github.com/code-423n4/2022-08-rigor/blob/5ab7ea84a1516cb726421ef690af5bc41029f88f/contracts/libraries/SignatureDecoder.sol#L75
Code Review
Consider upgrading to solidity 0.8.15.
Owner role has absolute power across the contracts with several onlyOwner functions. There is no ability to change admin to a new address or renounce it which is helpful for lost/compromised admin keys or to delegate control to a different governance/DAO address in future.
The project does not use the widely used OpenZeppelin Ownable library which provides transfer/renounce functions to mitigate such compromised/accidental situations with admin keys. This makes admin role/key a single-point of failure.
Ensure admins are reasonably redundant/independent (3/7 or 5/9) multisigs and add transfer/renounce functionality for admin. Consider using OpenZeppelinβs Ownable library.
Missing checks for zero-addresses may lead to infunctional protocol, if the variable addresses are updated incorrectly.
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/HomeFi.sol#L200
Code Review
Consider adding zero-address checks in the discussed constructors: require(newAddr != address(0));.
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
None
Implement zero address check and Consider implementing a two step process where the owner nominates an account and the nominated account needs to call an acceptOwnership() function for the transfer of ownership to fully succeed. This ensures the nominated EOA account is a valid and active account.
Rigor 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.
2022-08-rigor/contracts/Community.sol:321: _community.currency.safeTransferFrom( 2022-08-rigor/contracts/Community.sol:443: _currency.safeTransferFrom(_msgSender(), homeFi.treasury(), _lenderFee); 2022-08-rigor/contracts/Community.sol:446: _currency.safeTransferFrom(_msgSender(), _project, _amountToProject); 2022-08-rigor/contracts/Community.sol:474: _communities[_communityID].currency.safeTransferFrom( 2022-08-rigor/contracts/Project.sol:206: currency.safeTransferFrom(_sender, address(this), _cost); 2022-08-rigor/contracts/Project.sol:353: currency.safeTransfer( 2022-08-rigor/contracts/Project.sol:381: _token.safeTransfer(builder, _leftOutTokens); 2022-08-rigor/contracts/Project.sol:775: currency.safeTransfer(builder, _amount);
Manual Code Review
The use of _msgSender() when there is no implementation of a meta transaction mechanism that uses it, such as EIP-2771, very slightly increases gas consumption.
_msgSender() is utilized three times where msg.sender could have been used in the following function.
2022-08-rigor/contracts/ProjectFactory.sol:65: _msgSender() == IHomeFi(homeFi).admin(), 2022-08-rigor/contracts/ProjectFactory.sol:84: require(_msgSender() == homeFi, "PF::!HomeFiContract"); 2022-08-rigor/contracts/DebtToken.sol:32: communityContract == _msgSender(), 2022-08-rigor/contracts/mock/HomeFiV3Mock.sol:22: emit TrustedForwarderChangedWithSender(_newForwarder, _msgSender()); 2022-08-rigor/contracts/mock/HomeFiMock.sol:42: require(admin == _msgSender(), "HomeFi::!Admin"); 2022-08-rigor/contracts/mock/HomeFiMock.sol:69: admin = _msgSender(); 2022-08-rigor/contracts/mock/HomeFiMock.sol:145: address _sender = _msgSender(); 2022-08-rigor/contracts/mock/HomeFiMock.sol:210: function _msgSender() 2022-08-rigor/contracts/mock/HomeFiMock.sol:216: // this is same as ERC2771ContextUpgradeable._msgSender(); 2022-08-rigor/contracts/mock/HomeFiMock.sol:217: // We want to use the _msgSender() implementation of ERC2771ContextUpgradeable 2022-08-rigor/contracts/mock/HomeFiMock.sol:218: return super._msgSender(); 2022-08-rigor/contracts/Community.sol:75: require(_msgSender() == homeFi.admin(), "Community::!admin"); 2022-08-rigor/contracts/Community.sol:91: _msgSender() == IProject(_project).builder(), 2022-08-rigor/contracts/Community.sol:128: address _sender = _msgSender(); 2022-08-rigor/contracts/Community.sol:160: _communities[_communityID].owner == _msgSender(), 2022-08-rigor/contracts/Community.sol:322: _msgSender(), 2022-08-rigor/contracts/Community.sol:380: address _sender = _msgSender(); 2022-08-rigor/contracts/Community.sol:443: _currency.safeTransferFrom(_msgSender(), homeFi.treasury(), _lenderFee); 2022-08-rigor/contracts/Community.sol:446: _currency.safeTransferFrom(_msgSender(), _project, _amountToProject); 2022-08-rigor/contracts/Community.sol:475: _msgSender(), 2022-08-rigor/contracts/Community.sol:492: _msgSender() == _communities[_communityID].owner, 2022-08-rigor/contracts/Community.sol:503: approvedHashes[_msgSender()][_hash] = true; 2022-08-rigor/contracts/Community.sol:505: emit ApproveHash(_hash, _msgSender()); 2022-08-rigor/contracts/Community.sol:562: emit RestrictedToAdmin(_msgSender()); 2022-08-rigor/contracts/Community.sol:573: emit UnrestrictedToAdmin(_msgSender()); 2022-08-rigor/contracts/Community.sol:898: /// @dev This is same as ERC2771ContextUpgradeable._msgSender() 2022-08-rigor/contracts/Community.sol:899: function _msgSender() 2022-08-rigor/contracts/Community.sol:905: // We want to use the _msgSender() implementation of ERC2771ContextUpgradeable 2022-08-rigor/contracts/Community.sol:906: return super._msgSender(); 2022-08-rigor/contracts/Disputes.sol:46: require(homeFi.admin() == _msgSender(), "Disputes::!Admin"); 2022-08-rigor/contracts/Disputes.sol:52: require(homeFi.isProjectExist(_msgSender()), "Disputes::!Project"); 2022-08-rigor/contracts/Disputes.sol:134: assertMember(_dispute.project, _dispute.taskID, _msgSender()); 2022-08-rigor/contracts/Disputes.sol:137: emit DisputeAttachmentAdded(_disputeID, _msgSender(), _attachment); 2022-08-rigor/contracts/HomeFi.sol:73: require(admin == _msgSender(), "HomeFi::!Admin"); 2022-08-rigor/contracts/HomeFi.sol:113: admin = _msgSender(); 2022-08-rigor/contracts/HomeFi.sol:218: address _sender = _msgSender(); 2022-08-rigor/contracts/HomeFi.sol:302: /// @dev This is same as ERC2771ContextUpgradeable._msgSender() 2022-08-rigor/contracts/HomeFi.sol:303: function _msgSender() 2022-08-rigor/contracts/HomeFi.sol:309: // We want to use the _msgSender() implementation of ERC2771ContextUpgradeable 2022-08-rigor/contracts/HomeFi.sol:310: return super._msgSender(); 2022-08-rigor/contracts/Project.sol:109: address _sender = _msgSender(); 2022-08-rigor/contracts/Project.sol:150: require(_msgSender() == builder, "Project::!B"); 2022-08-rigor/contracts/Project.sol:186: address _sender = _msgSender(); 2022-08-rigor/contracts/Project.sol:224: if (_msgSender() != disputes) { 2022-08-rigor/contracts/Project.sol:302: _msgSender() == builder || _msgSender() == contractor, 2022-08-rigor/contracts/Project.sol:323: tasks[_taskList[i]].acceptInvitation(_msgSender()); 2022-08-rigor/contracts/Project.sol:344: if (_msgSender() != disputes) { 2022-08-rigor/contracts/Project.sol:400: if (_msgSender() != disputes) {
None
Replace _msgSender() with msg.sender if there is no mechanism to support meta-transactions like EIP-2771 implemented.
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:
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.
Code Review
/// @custom:oz-upgrades-unsafe-allow constructor constructor() { _disableInitializers(); }
Refer to the link below:
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
2022-08-rigor/contracts/HomeFi.sol:123: function setAddr( 2022-08-rigor/contracts/HomeFi.sol:200: function setTrustedForwarder(address _newForwarder) 2022-08-rigor/contracts/Project.sol:330: function setComplete(bytes calldata _data, bytes calldata _signature) 2022-08-rigor/contracts/interfaces/IHomeFi.sol:65: function setAddr( 2022-08-rigor/contracts/interfaces/IHomeFi.sol:115: function setTrustedForwarder(address _newForwarder) external; 2022-08-rigor/contracts/interfaces/IProject.sol:170: function setComplete(bytes calldata _data, bytes calldata _signature)
Code Review
Lack of two-step procedure for critical operations leaves them error-prone. Consider adding two step procedure on the critical functions.
During the manual code review, It has been observed that re-entrancy guard contract is not initialized. The contract re-entrancy guard is not upgradeable. You need to - manually call the __{contract}_init(); method of every parent contract with appropriate parameters.
Code Review
Ensure that all necessary contracts are initialized from the upgradeable contracts. The sample can be seen from below.
function initialize() public initializer { __ReentrancyGuard_init(); }
π Selected for report: c3phas
Also found by: 0x040, 0x1f8b, 0xA5DF, 0xNazgul, 0xSmartContract, 0xSolus, 0xc0ffEE, 0xkatana, 0xsam, 8olidity, Aymen0909, Bnke0x0, CertoraInc, Chinmay, Chom, CodingNameKiki, Deivitto, Dravee, ElKu, Extropy, Fitraldys, Funen, GalloDaSballo, Guardian, IllIllI, JC, Lambda, MEP, Metatron, MiloTruck, Noah3o6, NoamYakov, PaludoX0, ReyAdmirado, Rohan16, Rolezn, Ruhum, Sm4rty, SooYa, TomJ, Tomio, Waze, _Adam, __141345__, a12jmx, ajtra, ak1, apostle0x01, asutorufos, ballx, benbaessler, bharg4v, bobirichman, brgltd, cryptonue, defsec, delfin454000, dharma09, djxploit, durianSausage, eierina, erictee, fatherOfBlocks, gerdusx, gogo, hake, hyh, ignacio, jag, kaden, kyteg, lucacez, mics, minhquanym, oyc_109, pfapostol, rbserver, ret2basic, robee, rokinot, sach1r0, saian, samruna, scaraven, sikorico, simon135, supernova, teddav, tofunmi, zeesaw
25.3906 USDC - $25.39
[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.
> 0
can be replaced with != 0
for gas optimizationFor 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.
2022-08-rigor/contracts/Community.sol::624 => for (uint256 i = 0; i < _communities[_communityID].memberCount; i++) { 2022-08-rigor/contracts/HomeFiProxy.sol::87 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/HomeFiProxy.sol::136 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/Project.sol::248 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/Project.sol::311 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/Project.sol::322 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/libraries/Tasks.sol::181 => for (uint256 i = 0; i < _length; i++) _alerts[i] = _self.alerts[i];
None
Consider applying unchecked arithmetic where overflow/underflow is not possible. Example can be seen from below.
Unchecked{i++};
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.
https://github.com/code-423n4/2022-08-rigor/blob/5ab7ea84a1516cb726421ef690af5bc41029f88f/contracts/Project.sol#L353 2022-08-rigor/contracts/Community.sol:442: // Transfer _lenderFee to HomeFi treasury from lender account 2022-08-rigor/contracts/Community.sol:443: _currency.safeTransferFrom(_msgSender(), homeFi.treasury(), _lenderFee); 2022-08-rigor/contracts/Community.sol:445: // Transfer _amountToProject to _project from lender account 2022-08-rigor/contracts/Community.sol:446: _currency.safeTransferFrom(_msgSender(), _project, _amountToProject); 2022-08-rigor/contracts/Community.sol:473: // Transfer repayment to lender 2022-08-rigor/contracts/Community.sol:474: _communities[_communityID].currency.safeTransferFrom( 2022-08-rigor/contracts/Project.sol:205: // Transfer assets from builder to this contract 2022-08-rigor/contracts/Project.sol:206: currency.safeTransferFrom(_sender, address(this), _cost); 2022-08-rigor/contracts/Project.sol:352: // Transfer funds to subcontractor. 2022-08-rigor/contracts/Project.sol:353: currency.safeTransfer( 2022-08-rigor/contracts/Project.sol:381: _token.safeTransfer(builder, _leftOutTokens); 2022-08-rigor/contracts/Project.sol:765: * @dev Transfer excess funds back to builder wallet. 2022-08-rigor/contracts/Project.sol:774: // Transfer amount to builder address 2022-08-rigor/contracts/Project.sol:775: currency.safeTransfer(builder, _amount);
All Contracts
None
Consider checking amount != 0.
Uint is default initialized to 0. There is no need assign false to variable.
2022-08-rigor/contracts/Community.sol::624 => for (uint256 i = 0; i < _communities[_communityID].memberCount; i++) { 2022-08-rigor/contracts/HomeFiProxy.sol::87 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/HomeFiProxy.sol::136 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/Project.sol::248 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/Project.sol::311 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/Project.sol::322 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/libraries/Tasks.sol::181 => for (uint256 i = 0; i < _length; i++) _alerts[i] = _self.alerts[i];
Code Review
uint x = 0 costs more gas than uint x without having any different functionality.
Using double require instead of operator && can save more gas.
2022-08-rigor/contracts/libraries/SignatureDecoder.sol:35: if (v != 27 && v != 28) { 2022-08-rigor/contracts/Community.sol:354: _lendingNeeded >= _communityProject.totalLent && 2022-08-rigor/contracts/Disputes.sol:62: _disputeID < disputeCount && 2022-08-rigor/contracts/Disputes.sol:107: _actionType > 0 && _actionType <= uint8(ActionType.TaskPay),
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
Strict inequalities add a check of non equality which costs around 3 gas.
2022-08-rigor/contracts/Community.sol::624 => for (uint256 i = 0; i < _communities[_communityID].memberCount; i++) { 2022-08-rigor/contracts/HomeFiProxy.sol::87 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/HomeFiProxy.sol::136 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/Project.sol::248 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/Project.sol::311 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/Project.sol::322 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/libraries/Tasks.sol::181 => for (uint256 i = 0; i < _length; i++) _alerts[i] = _self.alerts[i];
Code Review
Use >= or <= instead of > and < when possible.
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.
2022-08-rigor/contracts/HomeFiProxy.sol::133 => require(_length == _contractAddresses.length, "Proxy::Lengths !match");
None
Consider to cache array length.
2022-08-rigor/contracts/Disputes.sol:236: function executeTaskAdd(address _project, bytes memory _actionData) 2022-08-rigor/contracts/Disputes.sol:254: function executeTaskChange(address _project, bytes memory _actionData) 2022-08-rigor/contracts/Disputes.sol:268: function executeTaskPay(address _project, bytes memory _actionData) 2022-08-rigor/contracts/HomeFi.sol:210: function createProject(bytes memory _hash, address _currency)
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.
2022-08-rigor/contracts/Disputes.sol:236: function executeTaskAdd(address _project, bytes memory _actionData) 2022-08-rigor/contracts/Disputes.sol:254: function executeTaskChange(address _project, bytes memory _actionData) 2022-08-rigor/contracts/Disputes.sol:268: function executeTaskPay(address _project, bytes memory _actionData) 2022-08-rigor/contracts/HomeFi.sol:210: function createProject(bytes memory _hash, address _currency)
None
Some parameters in examples given above are later hashed. It may be beneficial for those parameters to be in memory rather than calldata.
++i is more gas efficient than i++ in loops forwarding.
2022-08-rigor/contracts/Community.sol::624 => for (uint256 i = 0; i < _communities[_communityID].memberCount; i++) { 2022-08-rigor/contracts/HomeFiProxy.sol::87 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/HomeFiProxy.sol::136 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/Project.sol::248 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/Project.sol::311 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/Project.sol::322 => for (uint256 i = 0; i < _length; i++) { 2022-08-rigor/contracts/libraries/Tasks.sol::181 => for (uint256 i = 0; i < _length; i++) _alerts[i] = _self.alerts[i];
Code Review
It is recommend to use unchecked{++i} and change i declaration to uint256.
> 0
can be replaced with != 0
for gas optimization!= 0
is a cheaper operation compared to > 0
, when dealing with uint.
2022-08-rigor/contracts/Community.sol::261 => if (projectPublished[_project] > 0) { 2022-08-rigor/contracts/Community.sol::425 => // First claim interest if principal lent > 0 2022-08-rigor/contracts/Community.sol::427 => _communities[_communityID].projectDetails[_project].lentAmount > 0 2022-08-rigor/contracts/Community.sol::764 => require(_repayAmount > 0, "Community::!repay"); 2022-08-rigor/contracts/Community.sol::840 => if (_interestEarned > 0) { 2022-08-rigor/contracts/Disputes.sol::107 => _actionType > 0 && _actionType <= uint8(ActionType.TaskPay), 2022-08-rigor/contracts/HomeFi.sol::245 => return projectTokenId[_project] > 0; 2022-08-rigor/contracts/Project.sol::195 => require(_cost > 0, "Project::!value>0"); 2022-08-rigor/contracts/Project.sol::380 => if (_leftOutTokens > 0) { 2022-08-rigor/contracts/Project.sol::601 => if (_changeOrderedTask.length > 0) { 2022-08-rigor/contracts/Project.sol::691 => if (_loopCount > 0) emit TaskAllocated(_tasksAllocated); 2022-08-rigor/contracts/mock/HomeFiMock.sol::171 => return projectTokenId[_project] > 0;
None
Consider to replace > 0
with != 0
for gas optimization.
Using newer compiler versions and the optimizer gives gas optimizations and additional safety checks are available for free.
All Contracts
Solidity 0.8.13 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.15 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
None
Consider to upgrade pragma to at least 0.8.15.
Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met)
Source Custom Errors in Solidity:
Starting from Solidity v0.8.4, there is a convenient and gas-efficient way to explain to users why an operation failed through the use of custom errors. Until now, you could already use strings to give more information about failures (e.g., revert("Insufficient funds.");), but they are rather expensive, especially when it comes to deploy cost, and it is difficult to use dynamic information in them.
Custom errors are defined using the error statement, which can be used inside and outside of contracts (including interfaces and libraries).
Instances include:
All require Statements
Code Review
Recommended to replace revert strings with custom errors.
13. Function Ordering via Method ID
Context: All Contracts
Description:
Contracts most called functions could simply save gas by function ordering via Method ID. Calling a function at runtime will be cheaper if the function is positioned earlier in the order (has a relatively lower Method ID) because 22 gas are added to the cost of a function for every position that came before it. The caller can save on gas if you prioritize most called functions. One could use This tool to help find alternative function names with lower Method IDs while keeping the original name intact.
Recommendation:
Find a lower method ID name for the most called functions for example mostCalled()
vs. mostCalled_41q()
is cheaper by 44 gas.
Solidity 0.6.5 introduced immutable as a major feature. It allows setting contract-level variables at construction time which gets stored in code rather than storage.
Consider the following generic example:
contract C { /// The owner is set during contruction time, and never changed afterwards. address public owner = msg.sender; }
In the above example, each call to the function owner() reads from storage, using a sload. After EIP-2929, this costs 2100 gas cold or 100 gas warm. However, the following snippet is more gas efficient:
contract C { /// The owner is set during contruction time, and never changed afterwards. address public immutable owner = msg.sender; }
In the above example, each storage read of the owner state variable is replaced by the instruction push32 value, where value is set during contract construction time. Unlike the last example, this costs only 3 gas.
None
Consider using immutable variable.