Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #20

Open
code423n4 opened this issue Aug 3, 2022 · 1 comment
Open

Gas Optimizations #20

code423n4 opened this issue Aug 3, 2022 · 1 comment
Labels

Comments

@code423n4
Copy link
Contributor

Using > 0 costs more gas than != 0 when used on a uint in a require() statement > 0

contracts\Community.sol:

  763          // Revert if repayment amount is zero.
  764:         require(_repayAmount > 0, "Community::!repay");



contracts\Project.sol:
  194          // Revert if try to lend 0
  195:         require(_cost > 0, "Project::!value>0");

It costs more gas to initialize variables to zero than to let the default of zero be applied

contracts\Project.sol:
  247          // Loop over all the new tasks.
  248:         for (uint256 i = 0; i < _length; i++) {
  249              // Increment local task counter.

  310          // Invite subcontractor for each task.
  311:         for (uint256 i = 0; i < _length; i++) {
  312              _inviteSC(_taskList[i], _scList[i], false);

  321          uint256 _length = _taskList.length;
  322:         for (uint256 i = 0; i < _length; i++) {
  323              tasks[_taskList[i]].acceptInvitation(_msgSender());

x = x + y is cheaper than x += y

contracts\Community.sol:
  422              .projectDetails[_project]
  423:             .totalLent += _amountToProject;
  424  

  434              .projectDetails[_project]
  435:             .lentAmount += _lendingAmount;
  436  

contracts\HomeFi.sol:
  288          // Project count starts from 1
  289:         projectCount += 1;
  290  

contracts\Project.sol:
  178          // Increment to ensure a set of data and signature cannot be re-used.
  179:         hashChangeNonce += 1;
  180  

  249              // Increment local task counter.
  250:             _taskCount += 1;
  251  

  289          // Increment to ensure a set of data and signature cannot be re-used.
  290:         hashChangeNonce += 1;
  291  

  439                      // Increase the difference of new cost and old cost to total allocated.
  440:                     totalAllocated += _newCost - _taskCost;
  441                  }

  710          for (uint256 _taskID = 1; _taskID <= _length; _taskID++) {
  711:             _cost += tasks[_taskID].cost;
  712          }
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Aug 3, 2022
code423n4 added a commit that referenced this issue Aug 3, 2022
@zgorizzo69
Copy link
Collaborator

thanks for your work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants