-
Notifications
You must be signed in to change notification settings - Fork 31
optimize gas usage with common techniques and refactor code structure #341
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
Changes from all commits
f337815
d0674a1
f3f2162
0eaf592
ca01cee
9e87299
da74666
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,32 +37,20 @@ contract SmartOrderStrategy is ISmartOrderStrategy, AdminManagement { | |
| } | ||
|
|
||
| /// @inheritdoc IStrategy | ||
| function executeStrategy(address inputToken, address outputToken, uint256 inputAmount, bytes calldata data) external payable onlyGenericSwap { | ||
| if (inputAmount == 0) revert ZeroInput(); | ||
|
|
||
| Operation[] memory ops = abi.decode(data, (Operation[])); | ||
| function executeStrategy(address targetToken, bytes calldata strategyData) external payable onlyGenericSwap { | ||
| Operation[] memory ops = abi.decode(strategyData, (Operation[])); | ||
| if (ops.length == 0) revert EmptyOps(); | ||
|
|
||
| // wrap ETH to WETH if inputToken is ETH | ||
| if (Asset.isETH(inputToken)) { | ||
| if (msg.value != inputAmount) revert InvalidMsgValue(); | ||
| // the coverage report indicates that the following line causes this branch to not be covered by our tests | ||
| // even though we tried all possible success and revert scenarios | ||
| IWETH(weth).deposit{ value: inputAmount }(); | ||
|
Contributor
Author
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. We don't wrap ETH directly, as it could prevent |
||
| } else { | ||
| if (msg.value != 0) revert InvalidMsgValue(); | ||
| } | ||
|
|
||
| uint256 opsCount = ops.length; | ||
| for (uint256 i; i < opsCount; ++i) { | ||
| Operation memory op = ops[i]; | ||
| _call(op.dest, op.inputToken, op.ratioNumerator, op.ratioDenominator, op.dataOffset, op.value, op.data); | ||
| } | ||
|
|
||
| // unwrap WETH to ETH if outputToken is ETH | ||
| if (Asset.isETH(outputToken)) { | ||
| // unwrap WETH to ETH if targetToken is ETH | ||
| if (Asset.isETH(targetToken)) { | ||
| // the if statement is not fully covered by the tests even replacing `makerToken.isETH()` with `makerToken == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE` | ||
| // and crafting some cases where outputToken is ETH and non-ETH | ||
| // and crafting some cases where targetToken is ETH and non-ETH | ||
| uint256 wethBalance = IWETH(weth).balanceOf(address(this)); | ||
|
|
||
| if (wethBalance > 0) { | ||
|
|
@@ -71,15 +59,15 @@ contract SmartOrderStrategy is ISmartOrderStrategy, AdminManagement { | |
| } | ||
| } | ||
|
|
||
| uint256 selfBalance = Asset.getBalance(outputToken, address(this)); | ||
| uint256 selfBalance = Asset.getBalance(targetToken, address(this)); | ||
| if (selfBalance > 1) { | ||
| unchecked { | ||
| --selfBalance; | ||
| } | ||
| } | ||
|
|
||
| // transfer output tokens back to the generic swap contract | ||
| Asset.transferTo(outputToken, payable(genericSwap), selfBalance); | ||
| Asset.transferTo(targetToken, payable(genericSwap), selfBalance); | ||
| } | ||
|
|
||
| /// @dev This function adjusts the input amount based on a ratio if specified, then calls the destination contract with data. | ||
|
|
@@ -102,12 +90,6 @@ contract SmartOrderStrategy is ISmartOrderStrategy, AdminManagement { | |
| // adjust amount if ratio != 0 | ||
| if (_ratioNumerator != 0) { | ||
| uint256 inputTokenBalance = IERC20(_inputToken).balanceOf(address(this)); | ||
| // leaving one wei for gas optimization | ||
| if (inputTokenBalance > 1) { | ||
|
Contributor
Author
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. Leaving one wei of the |
||
| unchecked { | ||
| --inputTokenBalance; | ||
| } | ||
| } | ||
|
|
||
| // calculate input amount if ratio should be applied | ||
| if (_ratioNumerator != _ratioDenominator) { | ||
|
|
||
| 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"; | ||
|
Contributor
Author
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. This variable is no longer needed after rewriting the |
||
|
|
||
| // 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,29 @@ | |
|
|
||
| /// @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(); | ||
|
|
||
| // reference: | ||
| // 1. solady: https://github.com/Vectorized/solady/blob/main/src/utils/EIP712.sol#L138-L147 | ||
| // 2. 1inch: https://etherscan.io/address/0x111111125421cA6dc452d289314280a0f8842A65#code (line 1204~1209) | ||
| // solhint-disable no-inline-assembly | ||
| assembly { | ||
|
Contributor
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. Are those inline assemblies copied from some standard library? How much gas does it save?
Contributor
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. Is it possible to use OZ's 712 implementation?
Contributor
Author
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.
The assembly code was referenced from soladay's implementaion and 1inch's implementation (line 1204~1209)
Basically, it saves 135 gas when testing the getEIP712Hash function. However, the main value of using assembly here is that it helps avoid quadratic memory expansion costs, as we reuse the same memory slot for hashing. reference: https://x.com/optimizoor/status/1825913380244435408?mx=2
Our implementation declares some variables as
Contributor
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. Perhaps add a comment referencing the code copied from |
||
| // Compute the digest. | ||
| mstore(0x00, 0x1901000000000000000000000000000000000000000000000000000000000000) // Store "\x19\x01". | ||
| mstore(0x02, digest) // Store the domain separator. | ||
| 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(); | ||
| } | ||
| } | ||
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.
This if statement duplicates the logic in the
GenericSwapcontract: https://github.com/consenlabs/tokenlon-contracts/blob/v6.0.1/contracts/GenericSwap.sol#L68-L75