This repository has been archived by the owner on Jan 29, 2025. It is now read-only.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Old Code vs Optimized Code (with 100 optimizer runs)

Changes Implemented:
Modified Structs
• What: Packed data types efficiently within structs.
• Why: Writing/reading to storage incurs higher gas costs as data length increases. Optimizing struct alignment minimizes these costs.
Added Custom Errors
• What: Replaced all require statements with if conditions and used custom errors for reverting.
• Why:
• Custom errors reduce bytecode size.
• Saves gas by avoiding the overhead of require’s default error messages.
Changed Zero Values to Non-Zero Values
• What: Initialized storage variables with bytes32(bytes1(0x01)) instead of 0.
• Why:
• Changing storage from zero to non-zero requires 20,000 gas for storage write + 2100 gas for cold storage access.
• Pre-initializing with non-zero reduces this cost significantly: updating a non-zero value only requires 5000 gas.
Old Method:
hashlock = bytes32(0);
// Writing 0 to a zero slot.
// Changing
hashlock
later incurs 22,100 gas for storage updates.New Method:
hashlock = bytes32(bytes1(0x01));
// Writes a non-zero value during initialization, incurring little gas.
// Updates to
hashlock
cost only 5000 gas as it's non-zero to non-zero.Changed Default DataType for timestamp
• What: Reduced timestamp data type from uint256 to uint48.
• Why:
• A uint48 can store timestamps for millions of years (block increments every ~12 seconds).
• Smaller data types pack better in storage, reducing gas usage.
Changed Parameters from memory to calldata
• What: Used calldata for external function parameters instead of memory.
• Why:
• calldata avoids copying data into memory, which is expensive.
• Saves gas for functions that don’t modify the input parameters.
Replaced Arrays with Mappings
• What: Replaced arrays with mappings where applicable.
• Why:
• Reading an index from an array incurs additional gas due to boundary checks (e.g., 2102 gas for index >= length).
• Mappings eliminate boundary checks, reducing gas.
• Important: Ensure code prevents reading unallocated indices.
Old Method (Array):
uint[] public values;
function getValue(uint index) external view returns (uint) {
return values[index]; // Implicit boundary check incurs gas.
}
New Method (Mapping):
mapping(uint => uint) public values;
function getValue(uint index) external view returns (uint) {
return values[index]; // No boundary checks, direct storage access.
}
Explanation of Optimizations:
Why These Changes?
These changes demonstrate a significant reduction in gas consumption while maintaining functionality and security.