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

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

Gas Optimizations #352

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

Comments

@code423n4
Copy link
Contributor

Title: Custom errors from Solidity 0.8.4 are cheaper than revert strings

Impact:
Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met) while providing the same amount of information

Custom errors are defined using the error statement
reference: https://blog.soliditylang.org/2021/04/21/custom-errors/

Proof of Concept:
Tasks.sol#L124
Community.sol (various line)

Recommended Mitigation Steps:
Replace require statements with custom errors.


Title: Default value initialization

Impact:
If a variable is not set/initialized, it is assumed to have the default value (0, false, 0x0 etc depending on the data type). Explicitly initializing it with its default value is an anti-pattern and wastes gas.

Proof of Concept:
Tasks.sol#L181
Community.sol#L624
Project.sol#L412

Recommended Mitigation Steps:
Remove explicit initialization for default values.


Title: Using unchecked and prefix increment is more effective for gas saving:

Proof of Concept:
Tasks.sol#L181
Community.sol#L624

Recommended Mitigation Steps:
Change to:

	for (uint256 i = 0; i < _length;) _alerts[i] = _self.alerts[i];
  	unchecked { ++i; }
    }

Title: Gas savings for using solidity 0.8.10

Proof of Concept:
all contract

Recommended Mitigation Steps:
Consider to upgrade pragma to at least 0.8.10.

Solidity 0.8.10 has a useful change which reduced gas costs of external calls
Reference: here


Title: Using multiple require instead && can save gas

Proof of Concept:
Community.sol#L353-L357
Disputes.sol#L107

Recommended Mitigation Steps:

	require(_lendingNeeded >= _communityProject.totalLent, "Community::invalid lending");
	require(_lendingNeeded <= IProject(_project).projectCost(), "Community::invalid lending");

Title: Comparison operators

Proof of Concept:
Community.sol#L353-L357
Community.sol#L792

Recommended Mitigation Steps:
Replace <= with <, and >= with > for gas optimization


Title: Using != is more gas efficient in require statement

Proof of Concept:
Community.sol#L764
Project.sol#L195
Disputes.sol#L107

Recommended Mitigation Steps:
Change to !=

	require(_repayAmount != 0, "Community::!repay");

Title: Use unchecked can save gas

Proof of Concept:
Community.sol#L794 (because of require() L#792)
Community.sol#L798(because of if() L#785)
Project.sol#L427(because of if() L#425)
Project.sol#L616(because of if() L#614)

Recommended Mitigation Steps:
Use unchecked


Title: Consider make constant as private to save gas

Proof of Concept:
Project.sol#L60

Recommended Mitigation Steps:
I suggest changing the visibility from public to internal or private


@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Aug 6, 2022
code423n4 added a commit that referenced this issue Aug 6, 2022
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

2 participants