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 #150

Open
code423n4 opened this issue Aug 6, 2022 · 0 comments
Open

Gas Optimizations #150

code423n4 opened this issue Aug 6, 2022 · 0 comments

Comments

@code423n4
Copy link
Contributor

  1. HomeFi.sol never utilizes the onlyInitializing modifier from the initializable.sol openzeppelin contract. Therefore the initializer modifier is gas inefficient, as it unnecessarily performs logic for _initializing.

Potentially inherit from another more gas-efficient initializable implementation or implement a simpler initializable functionality that avoids such gas inefficiencies.

  1. Variables should not be initialized to defaults (uint256 default is 0):
  contracts/Community.sol::624 => for (uint256 i = 0; i < _communities[_communityID].memberCount; i++) {
  contracts/HomeFiProxy.sol::87 => for (uint256 i = 0; i < _length; i++) {
  contracts/HomeFiProxy.sol::136 => for (uint256 i = 0; i < _length; i++) {
  contracts/Project.sol::248 => for (uint256 i = 0; i < _length; i++) {
  contracts/Project.sol::311 => for (uint256 i = 0; i < _length; i++) {
  contracts/Project.sol::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];
  1. Length of array should be computed outside of for-loop:
  contracts/Community.sol::618 => // Initiate empty equal equal to member count length
  contracts/HomeFiProxy.sol::78 => uint256 _length = allContractNames.length;
  contracts/HomeFiProxy.sol::80 => // Revert if _implementations length is wrong. Indicating wrong set of _implementations.
  contracts/HomeFiProxy.sol::81 => require(_length == _implementations.length, "Proxy::Lengths !match");
  contracts/HomeFiProxy.sol::87 => for (uint256 i = 0; i < _length; i++) {
  contracts/HomeFiProxy.sol::130 => uint256 _length = _contractNames.length;
  contracts/HomeFiProxy.sol::132 => // Revert if _contractNames and _contractAddresses length mismatch
  contracts/HomeFiProxy.sol::133 => require(_length == _contractAddresses.length, "Proxy::Lengths !match");
  contracts/HomeFiProxy.sol::136 => for (uint256 i = 0; i < _length; i++) {
  contracts/Project.sol::243 => // Revert if IPFS hash array length is not equal to task cost array length.
  contracts/Project.sol::244 => uint256 _length = _hash.length;
  contracts/Project.sol::245 => require(_length == _taskCosts.length, "Project::Lengths !match");
  contracts/Project.sol::248 => for (uint256 i = 0; i < _length; i++) {
  contracts/Project.sol::306 => // Revert if taskList array length not equal to scList array length.
  contracts/Project.sol::307 => uint256 _length = _taskList.length;
  contracts/Project.sol::308 => require(_length == _scList.length, "Project::Lengths !match");
  contracts/Project.sol::311 => for (uint256 i = 0; i < _length; i++) {
  contracts/Project.sol::321 => uint256 _length = _taskList.length;
  contracts/Project.sol::322 => for (uint256 i = 0; i < _length; i++) {
  contracts/Project.sol::367 => uint256 _length = taskCount;
  contracts/Project.sol::368 => for (uint256 _taskID = 1; _taskID <= _length; _taskID++) {
  contracts/Project.sol::592 => taskCount - j + _changeOrderedTask.length - i
  contracts/Project.sol::601 => if (_changeOrderedTask.length > 0) {
  contracts/Project.sol::602 => // Loop from lastAllocatedChangeOrderTask to _changeOrderedTask length (until _maxLoop)
  contracts/Project.sol::603 => for (; i < _changeOrderedTask.length; i++) {
  contracts/Project.sol::635 => if (i == _changeOrderedTask.length) {
  contracts/Project.sol::707 => uint256 _length = taskCount;
  contracts/Project.sol::710 => for (uint256 _taskID = 1; _taskID <= _length; _taskID++) {
  contracts/libraries/SignatureDecoder.sol::25 => if (messageSignatures.length % 65 != 0) {
  contracts/libraries/Tasks.sol::180 => uint256 _length = _alerts.length;
  contracts/libraries/Tasks.sol::181 => for (uint256 i = 0; i < _length; i++) _alerts[i] = _self.alerts[i];
  1. != is more efficient than > 0 for uint comparisons:
  contracts/Community.sol::261 => if (projectPublished[_project] > 0) {
  contracts/Community.sol::425 => // First claim interest if principal lent > 0
  contracts/Community.sol::427 => _communities[_communityID].projectDetails[_project].lentAmount > 0
  contracts/Community.sol::764 => require(_repayAmount > 0, "Community::!repay");
  contracts/Community.sol::840 => if (_interestEarned > 0) {
  contracts/Disputes.sol::107 => _actionType > 0 && _actionType <= uint8(ActionType.TaskPay),
  contracts/HomeFi.sol::245 => return projectTokenId[_project] > 0;
  contracts/Project.sol::195 => require(_cost > 0, "Project::!value>0");
  contracts/Project.sol::380 => if (_leftOutTokens > 0) {
  contracts/Project.sol::601 => if (_changeOrderedTask.length > 0) {
  contracts/Project.sol::691 => if (_loopCount > 0) emit TaskAllocated(_tasksAllocated);
  1. Switching from division/multiplication to right-shift/left-shift can save gas:
  contracts/Community.sol::686 => _communityProject.lastTimestamp) / 86400; // 24*60*60
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants