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
[G-01] memory KEYWORD CAN BE USED INSTEAD OF storage KEYWORD
When there is no need to make a reference to a state value, the memory keyword can be used instead of the storage keyword to save gas. Accessing storage using sload is more expensive than accessing memory using mload. The memory keyword can be used for _communityProject below.
contracts\Community.sol
679-698:
// Local instance of variables. For saving gas.
ProjectDetails storage _communityProject = _communities[_communityID]
.projectDetails[_project];
uint256 _lentAmount = _communityProject.lentAmount;
// Calculate number of days difference current and last timestamp
uint256 _noOfDays = (block.timestamp -
_communityProject.lastTimestamp) / 86400; // 24*60*60
/// Interest formula = (principal * APR * days) / (365 * 1000)
// prettier-ignore
uint256 _unclaimedInterest =
_lentAmount *
_communities[_communityID].projectDetails[_project].apr *
_noOfDays /
365000;
// Old (already rTokens claimed) + new interest
uint256 _totalInterest = _unclaimedInterest +
_communityProject.interest;
[G-02] VARIABLE DOES NOT NEED TO BE INITIALIZED TO ITS DEFAULT VALUE
Explicitly initializing a variable with its default value costs more gas than uninitializing it. For example, uint256 i can be used instead of uint256 i = 0 in the following code.
contracts\Community.sol
624: for (uint256 i = 0; i < _communities[_communityID].memberCount; i++) {
contracts\HomeFiProxy.sol
87: for (uint256 i = 0; i < _length; i++) {
136: for (uint256 i = 0; i < _length; i++) {
contracts\Project.sol
248: for (uint256 i = 0; i < _length; i++) {
311: for (uint256 i = 0; i < _length; i++) {
322: for (uint256 i = 0; i < _length; i++) {
contracts\libraries\Tasks.sol
181: for (uint256 i = 0; i < _length; i++) _alerts[i] = _self.alerts[i];
[G-03] ARRAY LENGTH CAN BE CACHED OUTSIDE OF LOOP
Caching the array length outside of the loop and using the cached length in the loop costs less gas than reading the array length for each iteration. For example, _changeOrderedTask.length in the following code can be cached outside of the loop like uint256 _changeOrderedTaskLength = _changeOrderedTask.length, and i < _changeOrderedTaskLength can be used for each iteration.
contracts\Community.sol
624: for (uint256 i = 0; i < _communities[_communityID].memberCount; i++) {
contracts\Project.sol
603: for (; i < _changeOrderedTask.length; i++) {
[G-04] ++VARIABLE CAN BE USED INSTEAD OF VARIABLE++
++variable costs less gas than variable++. For example, i++ can be changed to ++i in the following code.
contracts\Community.sol
624: for (uint256 i = 0; i < _communities[_communityID].memberCount; i++) {
contracts\HomeFiProxy.sol
87: for (uint256 i = 0; i < _length; i++) {
136: for (uint256 i = 0; i < _length; i++) {
contracts\Project.sol
248: for (uint256 i = 0; i < _length; i++) {
311: for (uint256 i = 0; i < _length; i++) {
322: for (uint256 i = 0; i < _length; i++) {
368: for (uint256 _taskID = 1; _taskID <= _length; _taskID++) {
603: for (; i < _changeOrderedTask.length; i++) {
650: for (++j; j <= taskCount; j++) {
710: for (uint256 _taskID = 1; _taskID <= _length; _taskID++) {
contracts\libraries\Tasks.sol
181: for (uint256 i = 0; i < _length; i++) _alerts[i] = _self.alerts[i];
[G-05] X = X + Y OR X = X - Y CAN BE USED INSTEAD OF X += Y OR X -= Y
x = x + y or x = x - y costs less gas than x += y or x -= y. For example, _interest -= _repayAmount can be changed to _interest = _interest - _repayAmount in the following code.
[G-06] ARITHMETIC OPERATIONS THAT DO NOT OVERFLOW CAN BE UNCHECKED
Explicitly unchecking arithmetic operations that do not overflow by wrapping these in unchecked {} costs less gas than implicitly checking these.
For the following loops, if increasing the counter variable is very unlikely to overflow, then unchecked {++i} at the end of the loop block can be used, where i is the counter variable.
contracts\Community.sol
624: for (uint256 i = 0; i < _communities[_communityID].memberCount; i++) {
contracts\HomeFiProxy.sol
87: for (uint256 i = 0; i < _length; i++) {
136: for (uint256 i = 0; i < _length; i++) {
contracts\Project.sol
248: for (uint256 i = 0; i < _length; i++) {
311: for (uint256 i = 0; i < _length; i++) {
322: for (uint256 i = 0; i < _length; i++) {
368: for (uint256 _taskID = 1; _taskID <= _length; _taskID++) {
603: for (; i < _changeOrderedTask.length; i++) {
650: for (++j; j <= taskCount; j++) {
710: for (uint256 _taskID = 1; _taskID <= _length; _taskID++) {
contracts\libraries\Tasks.sol
181: for (uint256 i = 0; i < _length; i++) _alerts[i] = _self.alerts[i];
[G-07] REVERT WITH CUSTOM ERROR CAN BE USED INSTEAD OF REQUIRE() OR REVERT() WITH REASON STRING
revert with custom error can cost less gas than require() or revert() with reason string. Please consider using revert with custom error to replace the following require() and revert().
The protocol can benefit from more gas-efficient features and fixes by using a newer version of Solidity. Changes for newer Solidity versions can be viewed here.
[G-01] memory KEYWORD CAN BE USED INSTEAD OF storage KEYWORD
When there is no need to make a reference to a state value, the
memory
keyword can be used instead of thestorage
keyword to save gas. Accessing storage using sload is more expensive than accessing memory using mload. Thememory
keyword can be used for_communityProject
below.[G-02] VARIABLE DOES NOT NEED TO BE INITIALIZED TO ITS DEFAULT VALUE
Explicitly initializing a variable with its default value costs more gas than uninitializing it. For example,
uint256 i
can be used instead ofuint256 i = 0
in the following code.[G-03] ARRAY LENGTH CAN BE CACHED OUTSIDE OF LOOP
Caching the array length outside of the loop and using the cached length in the loop costs less gas than reading the array length for each iteration. For example,
_changeOrderedTask.length
in the following code can be cached outside of the loop likeuint256 _changeOrderedTaskLength = _changeOrderedTask.length
, andi < _changeOrderedTaskLength
can be used for each iteration.[G-04] ++VARIABLE CAN BE USED INSTEAD OF VARIABLE++
++variable costs less gas than variable++. For example,
i++
can be changed to++i
in the following code.[G-05] X = X + Y OR X = X - Y CAN BE USED INSTEAD OF X += Y OR X -= Y
x = x + y or x = x - y costs less gas than x += y or x -= y. For example,
_interest -= _repayAmount
can be changed to_interest = _interest - _repayAmount
in the following code.[G-06] ARITHMETIC OPERATIONS THAT DO NOT OVERFLOW CAN BE UNCHECKED
Explicitly unchecking arithmetic operations that do not overflow by wrapping these in
unchecked {}
costs less gas than implicitly checking these.For the following loops, if increasing the counter variable is very unlikely to overflow, then
unchecked {++i}
at the end of the loop block can be used, wherei
is the counter variable.[G-07] REVERT WITH CUSTOM ERROR CAN BE USED INSTEAD OF REQUIRE() OR REVERT() WITH REASON STRING
revert
with custom error can cost less gas thanrequire()
orrevert()
with reason string. Please consider usingrevert
with custom error to replace the followingrequire()
andrevert()
.[G-08] NEWER VERSION OF SOLIDITY CAN BE USED
The protocol can benefit from more gas-efficient features and fixes by using a newer version of Solidity. Changes for newer Solidity versions can be viewed
here
.The text was updated successfully, but these errors were encountered: