You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Wrap line 794 with unchecked since underflow is not possible due to line 792 check
792: require(_lentAndInterest >= _repayAmount, "Community::!Liquid");
794: _lentAmount = _lentAndInterest - _repayAmount;
Wrap line 427 with unchecked since underflow is not possible due to line 425 check
425: if (_newCost < _taskCost) {
427: uint256 _withdrawDifference = _taskCost - _newCost;
Wrap line 616 with unchecked since underflow is not possible due to line 614 check
614: if (_costToAllocate >= _taskCost) {
616: _costToAllocate -= _taskCost;
Wrap line 663 with unchecked since underflow is not possible due to line 661 check
661: if (_costToAllocate >= _taskCost) {
663: _costToAllocate -= _taskCost;
When certain state variable is read more than once, cache it to local variable to save gas.
Defined Variables Used Only Once
Issue
Certain variables is defined even though they are used only once.
Remove these unnecessary variables to save gas.
For cases where it will reduce the readability, one can use comments to help describe
what the code is doing.
else if (totalLent - totalAllocated >= _newCost - _taskCost) {
Mitigation
Don't define variable that is used only once.
Storage Variables can be Packed into Fewer Storage Slots
Issue
The order of storage variables can be reordered in a way to reduce the usage amount of storage slots.
Reference from solidity documentation:
Finally, in order to allow the EVM to optimize for this, ensure that you try to order your storage
variables and struct members such that they can be packed tightly. For example, declaring your
storage variables in the order of uint128, uint128, uint256 instead of uint128, uint256, uint128,
as the former will only take up two slots of storage whereas the latter will take up three.
Project.sol
We can save 1 storage slot by reordering it like below.
Move bool variable (1 byte size) to pack it within 1 slot (32 bytes size) with
address variable (20 bytes size) and other bool variable.
I recommend making duplicate require statement into modifier or a function.
Both named returns and return statement are used
Issue
In some function return statement are used even though named returns is set.
This is redundant because return statement is not needed when using named returns and
named returns is not needed when using return statement.
Removing unused named returns variable in below code can save gas and improve code readability.
Remove unused named returns variable as mentioned in above PoC.
Use require instead of &&
Issue
When there are multiple conditions in require statement, break down the require statement into
multiple require statements instead of using && can save gas.
I recommend to not define above functions and instead inline it at place it is called.
Use Calldata instead of Memory for Read Only Function Parameters
Issue
It is cheaper gas to use calldata than memory if the function parameter is read only.
Calldata is a non-modifiable, non-persistent area where function arguments are stored,
and behaves mostly like memory. More details on following link.
Using Elements Smaller than 32 bytes (256 bits) Might Use More Gas
Issue
Since EVM operates on 32 bytes at a time, if the element is smaller than that, the EVM must use more operations
in order to reduce the elements from 32 bytes to specified size.
./Community.sol:624: for (uint256 i = 0; i < _communities[_communityID].memberCount; i++) {
./Tasks.sol:181: for (uint256 i = 0; i < _length; i++) _alerts[i] = _self.alerts[i];
./Project.sol:248: for (uint256 i = 0; i < _length; i++) {
./Project.sol:311: for (uint256 i = 0; i < _length; i++) {
./Project.sol:322: for (uint256 i = 0; i < _length; i++) {
./HomeFiProxy.sol:87: for (uint256 i = 0; i < _length; i++) {
./HomeFiProxy.sol:136: for (uint256 i = 0; i < _length; i++) {
./Project.sol:412: bool _unapproved = false;
Mitigation
I suggest removing default value initialization.
For example,
bool _unapproved;
Store Array's Length as a Variable
Issue
By storing an array's length as a variable before the for-loop,
can save 3 gas per iteration.
PoC
Total of 1 issue found.
./Project.sol:603: for (; i < _changeOrderedTask.length; i++) {
Mitigation
Store array's length as a variable before looping it.
For example, I suggest changing it to
uint256 length = _changeOrderedTask.length;
for (; i < length; i++) {
++i Costs Less Gas than i++
Issue
Prefix increments/decrements (++i or --i) costs cheaper gas than
postfix increment/decrements (i++ or i--).
PoC
Total of 11 issues found.
./Community.sol:624: for (uint256 i = 0; i < _communities[_communityID].memberCount; i++) {
./Tasks.sol:181: for (uint256 i = 0; i < _length; i++) _alerts[i] = _self.alerts[i];
./Project.sol:248: for (uint256 i = 0; i < _length; i++) {
./Project.sol:311: for (uint256 i = 0; i < _length; i++) {
./Project.sol:322: for (uint256 i = 0; i < _length; i++) {
./Project.sol:368: for (uint256 _taskID = 1; _taskID <= _length; _taskID++) {
./Project.sol:603: for (; i < _changeOrderedTask.length; i++) {
./Project.sol:650: for (++j; j <= taskCount; j++) {
./Project.sol:710: for (uint256 _taskID = 1; _taskID <= _length; _taskID++) {
./HomeFiProxy.sol:87: for (uint256 i = 0; i < _length; i++) {
./HomeFiProxy.sol:136: for (uint256 i = 0; i < _length; i++) {
Mitigation
Change it to postfix increments/decrements.
It saves 6 gas per loop.
For example,
for (uint256 i = 0; i < _communities[_communityID].memberCount; ++i) {
!= 0 costs less gass than > 0
Issue
!= 0 costs less gas when optimizer is enabled and is used for unsigned integers in require statement.
Table of Contents
Should Use Unchecked Block where Over/Underflow is not Possible
Issue
Since Solidity 0.8.0, all arithmetic operations revert on overflow and underflow by default.
However in places where overflow and underflow is not possible, it is better to use unchecked block to reduce the gas usage.
Reference: https://docs.soliditylang.org/en/v0.8.15/control-structures.html#checked-or-unchecked-arithmetic
PoC
Total of 4 issues found.
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Community.sol#L792-L794
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L425-L427
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L614-L616
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L661-L663
Mitigation
Wrap the code with uncheck like described in above PoC.
Minimize the Number of SLOADs by Caching State Variable
Issue
SLOADs cost 100 gas where MLOADs/MSTOREs cost only 3 gas.
Whenever function reads storage value more than once, it should be cached to save gas.
PoC
2 SLOADs to 1 SLOAD, 1 MSTORE and 2 MLOAD
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/ProjectFactory.sol#L84
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/ProjectFactory.sol#L90
3 SLOADs to 1 SLOAD, 1 MSTORE and 3 MLOAD
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/HomeFi.sol#L228-L231
4 SLOADs to 1 SLOAD, 1 MSTORE and 4 MLOAD
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/HomeFi.sol#L292-L296
3 SLOADs to 1 SLOAD, 1 MSTORE and 3 MLOAD
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L350
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L354-L355
8 SLOADs to 1 SLOAD, 1 MSTORE and 8 MLOAD
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L409
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L423
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L449
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L452
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L464
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L470
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L474
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L485
2 SLOADs to 1 SLOAD, 1 MSTORE and 2 MLOAD
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L524
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L528
3 SLOADs to 1 SLOAD, 1 MSTORE and 3 MLOAD
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L553-L555
2 SLOADs to 1 SLOAD, 1 MSTORE and 2 MLOAD
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L190
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L204
5 or more SLOADs to 1 SLOAD, 1 MSTORE and 5 or more MLOAD
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L592
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L648
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L650
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L681
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L682
Mitigation
When certain state variable is read more than once, cache it to local variable to save gas.
Defined Variables Used Only Once
Issue
Certain variables is defined even though they are used only once.
Remove these unnecessary variables to save gas.
For cases where it will reduce the readability, one can use comments to help describe
what the code is doing.
PoC
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L420
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L438
Delete line 420 and replace line 438 to line shown below
Mitigation
Don't define variable that is used only once.
Storage Variables can be Packed into Fewer Storage Slots
Issue
The order of storage variables can be reordered in a way to reduce the usage amount of storage slots.
https://docs.soliditylang.org/en/v0.8.15/internals/layout_in_storage.html#layout-of-state-variables-in-storage
PoC
Total of 1 issue found.
We can save 1 storage slot by reordering it like below.
Move bool variable (1 byte size) to pack it within 1 slot (32 bytes size) with
address variable (20 bytes size) and other bool variable.
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L65-L84
Mitigation
Reorder storage variables like shown in above PoC.
Use Already Defined Variable
Issue
Use already defined variable rather than reading the storage variable again and wasting gas.
PoC
Use cached variable of _sender instead of reading the storage variable
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Community.sol#L380
Change _msgSender() to _sender
Mitigation
Use the already defined variable like shown in above PoC.
Duplicate require() Checks Should be a Modifier or a Function
Issue
Since below require checks are used more than once,
I recommend making these to a modifier or a function.
PoC
Mitigation
I recommend making duplicate require statement into modifier or a function.
Both named returns and return statement are used
Issue
In some function return statement are used even though named returns is set.
This is redundant because return statement is not needed when using named returns and
named returns is not needed when using return statement.
Removing unused named returns variable in below code can save gas and improve code readability.
PoC
1, Remove returns variable "sender" of _msgSender() Community.sol
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Community.sol#L903
Mitigation
Remove unused named returns variable as mentioned in above PoC.
Use require instead of &&
Issue
When there are multiple conditions in require statement, break down the require statement into
multiple require statements instead of using && can save gas.
PoC
Total of 4 issues found.
Mitigation
Break down into several require statement.
For example,
Internal Function Called Only Once Can be Inlined
Issue
Certain function is defined even though it is called only once.
Inline it instead to where it is called to avoid usage of extra gas.
PoC
Total of 3 issues found.
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Disputes.sol#L207
resolveHandler function called only once at line 149
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Disputes.sol#L149
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L770
autoWithdraw function called only once at line 435
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L435
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/HomeFiProxy.sol#L197-L200
_replaceImplementation function called only once at line 137
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/HomeFiProxy.sol#L137
Mitigation
I recommend to not define above functions and instead inline it at place it is called.
Use Calldata instead of Memory for Read Only Function Parameters
Issue
It is cheaper gas to use calldata than memory if the function parameter is read only.
Calldata is a non-modifiable, non-persistent area where function arguments are stored,
and behaves mostly like memory. More details on following link.
link: https://docs.soliditylang.org/en/v0.8.15/types.html#data-location
PoC
Total of 2 issues found.
Mitigation
Change memory to calldata
Using Elements Smaller than 32 bytes (256 bits) Might Use More Gas
Issue
Since EVM operates on 32 bytes at a time, if the element is smaller than that, the EVM must use more operations
in order to reduce the elements from 32 bytes to specified size.
Reference: https://docs.soliditylang.org/en/v0.8.15/internals/layout_in_storage.html
PoC
Total of 8 issues found.
Mitigation
I suggest using uint256 instead of anything smaller or downcast where needed.
Unnecessary Default Value Initialization
Issue
When variable is not initialized, it will have its default values.
For example, 0 for uint, false for bool and address(0) for address.
Reference: https://docs.soliditylang.org/en/v0.8.15/control-structures.html#scoping-and-declarations
PoC
Total of 8 issues found.
Mitigation
I suggest removing default value initialization.
For example,
Store Array's Length as a Variable
Issue
By storing an array's length as a variable before the for-loop,
can save 3 gas per iteration.
PoC
Total of 1 issue found.
Mitigation
Store array's length as a variable before looping it.
For example, I suggest changing it to
++i Costs Less Gas than i++
Issue
Prefix increments/decrements (++i or --i) costs cheaper gas than
postfix increment/decrements (i++ or i--).
PoC
Total of 11 issues found.
Mitigation
Change it to postfix increments/decrements.
It saves 6 gas per loop.
For example,
!= 0 costs less gass than > 0
Issue
!= 0 costs less gas when optimizer is enabled and is used for unsigned integers in require statement.
PoC
Total of 2 issues found.
Mitigation
I suggest changing > 0 to != 0
For example:
Use Custom Errors to Save Gas
Issue
Custom errors from Solidity 0.8.4 are cheaper than revert strings.
Details are explained here: https://blog.soliditylang.org/2021/04/21/custom-errors/
PoC
Total of 74 issues found.
Mitigation
I suggest implementing custom errors to save gas.
The text was updated successfully, but these errors were encountered: