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

Reduce NativeTokenHome's receive method gas cost #723

Merged
merged 1 commit into from
Feb 18, 2025

Conversation

cam-schultz
Copy link
Contributor

Why this should be merged

Fixes #722

How this works

Rather than call the public method getTokenAddress, which results in an external call, we can fetch the same address directly from NativeTokenHome's state.

How this was tested

Manual testing with unoptimized contract builds. To reproduce:

  1. Disable optimization by adding optimizer = false to the default profile in foundry.toml
  2. Run a test case that withdraws from the wrapped contract, with gas reporting enabled:

forge test --match-test testReceiveWithdrawSuccess --match-contract NativeTokenHomeTest -vvvv --gas-report

  1. Observe the gas used by the receive method in the trace (263 versus 2311 before this change)

To observe the test failing as a result of disabling optimization (and without this change, of course), replace sendValue with transfer in the test wrapped token contract.

Automated testing

Automated tests would require a separate test profile that disables optimization. I consider that out of scope for this issue.

How is this documented

N/A

Comment on lines +123 to +126
require(
msg.sender == address(_getNativeTokenHomeStorage()._wrappedToken),
"NativeTokenHome: invalid receive payable sender"
);

Check notice

Code scanning / Semgrep OSS

Semgrep Finding: solidity.performance.use-custom-error-not-require.use-custom-error-not-require Note

Consider using custom errors as they are more gas efficient while allowing developers to describe the error in detail using NatSpec.
Comment on lines +123 to +126
require(
msg.sender == address(_getNativeTokenHomeStorage()._wrappedToken),
"NativeTokenHome: invalid receive payable sender"
);

Check notice

Code scanning / Semgrep OSS

Semgrep Finding: solidity.performance.use-short-revert-string.use-short-revert-string Note

Shortening revert strings to fit in 32 bytes will decrease gas costs for deployment and gas costs when the revert condition has been met.
@cam-schultz cam-schultz changed the title fetch token address locally Reduce NativeTokenHome's receive method gas cost Feb 18, 2025
@cam-schultz cam-schultz merged commit 7a095cd into main Feb 18, 2025
17 checks passed
@cam-schultz cam-schultz deleted the unoptimized-transfer-compat branch February 18, 2025 22:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done ✅
Development

Successfully merging this pull request may close these issues.

[Bug]: Gas Limit Conflict Between NativeTokenHome and Legacy Wrapped Token Contracts
3 participants