-
Notifications
You must be signed in to change notification settings - Fork 25
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
optimize gas usage with common techniques and refactor code structure #341
Open
alex0207s
wants to merge
5
commits into
master
Choose a base branch
from
refactor/gas-opt-techniques
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+298
−322
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f337815
optimize gas usage with common techniques and refactor code structure
alex0207s d0674a1
add forge config to allow internal expect revert in tests
alex0207s f3f2162
upgrade checkout from v3 to v4 to resolve CI issues
alex0207s 0eaf592
fix gas diff ci
alex0207s ca01cee
add error handling for zero swap amount in GenericSwap
alex0207s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,9 +6,6 @@ | |||||
/// @notice This contract implements the EIP-712 standard for structured data hashing and signing. | ||||||
/// @dev This contract provides functions to handle EIP-712 domain separator and hash calculation. | ||||||
abstract contract EIP712 { | ||||||
// EIP-191 Header | ||||||
string public constant EIP191_HEADER = "\x19\x01"; | ||||||
|
||||||
// EIP-712 Domain | ||||||
string public constant EIP712_NAME = "Tokenlon"; | ||||||
string public constant EIP712_VERSION = "v6"; | ||||||
|
@@ -16,8 +13,8 @@ | |||||
bytes32 private constant EIP712_HASHED_NAME = keccak256(bytes(EIP712_NAME)); | ||||||
bytes32 private constant EIP712_HASHED_VERSION = keccak256(bytes(EIP712_VERSION)); | ||||||
|
||||||
uint256 public immutable originalChainId; | ||||||
bytes32 public immutable originalEIP712DomainSeparator; | ||||||
|
||||||
/// @notice Initialize the original chain ID and domain separator. | ||||||
constructor() { | ||||||
|
@@ -28,7 +25,7 @@ | |||||
/// @notice Internal function to build the EIP712 domain separator hash. | ||||||
/// @return The EIP712 domain separator hash. | ||||||
function _buildDomainSeparator() private view returns (bytes32) { | ||||||
return keccak256(abi.encode(EIP712_TYPE_HASH, EIP712_HASHED_NAME, EIP712_HASHED_VERSION, block.chainid, address(this))); | ||||||
} | ||||||
|
||||||
/// @notice Internal function to get the current EIP712 domain separator. | ||||||
|
@@ -43,14 +40,26 @@ | |||||
|
||||||
/// @notice Calculate the EIP712 hash of a structured data hash. | ||||||
/// @param structHash The hash of the structured data. | ||||||
/// @return The EIP712 hash of the structured data. | ||||||
function getEIP712Hash(bytes32 structHash) internal view returns (bytes32) { | ||||||
return keccak256(abi.encodePacked(EIP191_HEADER, _getDomainSeparator(), structHash)); | ||||||
/// @return digest The EIP712 hash of the structured data. | ||||||
function getEIP712Hash(bytes32 structHash) internal view returns (bytes32 digest) { | ||||||
// return keccak256(abi.encodePacked("\x19\x01", _getDomainSeparator(), structHash)); | ||||||
|
||||||
digest = _getDomainSeparator(); | ||||||
|
||||||
// solhint-disable no-inline-assembly | ||||||
assembly { | ||||||
// Compute the digest. | ||||||
mstore(0x00, 0x1901000000000000000000000000000000000000000000000000000000000000) // Store "\x19\x01". | ||||||
mstore(0x2, digest) // Store the domain separator. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe align with other hex values:
Suggested change
|
||||||
mstore(0x22, structHash) // Store the struct hash. | ||||||
digest := keccak256(0x0, 0x42) | ||||||
mstore(0x22, 0) // Restore the part of the free memory slot that was overwritten. | ||||||
} | ||||||
} | ||||||
|
||||||
/// @notice Get the current EIP712 domain separator. | ||||||
/// @return The current EIP712 domain separator. | ||||||
function EIP712_DOMAIN_SEPARATOR() external view returns (bytes32) { | ||||||
return _getDomainSeparator(); | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are those inline assemblies copied from some standard library? How much gas does it save?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to use OZ's 712 implementation?