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

feat(contracts-rfq): Multicall target abstraction [SLT-134] #3078

Merged
merged 7 commits into from
Sep 10, 2024

Conversation

ChiTimesChi
Copy link
Collaborator

@ChiTimesChi ChiTimesChi commented Aug 29, 2024

Description
Adds an abstract contract that is capable of receiving the multicall (batched calls) instructions from any account, including the EOAs (which don't have the built-in multicall feature on the vast majority of EVM chains).

The contract has two point of entries:

  • multicallNoResults for doing batched calls when the returned value could be discarded. This is meant for EOAs to submit the transactions, as this saves gas and behaves identical to multicallWithResults outside of not forwarding the returned values from the calls.
    • To my understanding, there's no mechanism to publish a transaction where the call has a returned value AND retrieve this value, therefore publishing a multicallWithResults transaction doesn't have any additional benefit.
  • multicallWithResults for doing batched calls when the returned values are forwarded. This is meant for using via eth_call to effectively simulate the transaction execution without publishing and obtaining the results (this could also include the view functions). This could be also used by another contracts on-chain, if they care about getting the returned values and don't have an alternative way do do a batched call.

Metadata
Completes SLT-134

Summary by CodeRabbit

  • New Features

    • Introduced a new interface and contract for efficient batch processing of smart contract calls on Ethereum, allowing users to execute multiple calls in a single transaction.
    • Added functionality to ignore reverts for batch calls, enhancing flexibility in error handling.
  • Tests

    • Implemented a comprehensive test suite for the new batch processing functionality, ensuring reliability and correctness across various scenarios.

Comment on lines +19 to +30
function multicallNoResults(bytes[] calldata data, bool ignoreReverts) external {
for (uint256 i = 0; i < data.length; ++i) {
// We perform a delegate call to ourself to preserve the msg.sender. This is identical to `msg.sender`
// calling the functions directly one by one, therefore doesn't add any security risks.
// Note: msg.value is also preserved when doing a delegate call, but this function is not payable,
// so it's always 0 and not a security risk.
(bool success, bytes memory result) = address(this).delegatecall(data[i]);
if (!success && !ignoreReverts) {
_bubbleRevert(result);
}
}
}

Check notice

Code scanning / Slither

Calls inside a loop Low

Comment on lines +19 to +30
function multicallNoResults(bytes[] calldata data, bool ignoreReverts) external {
for (uint256 i = 0; i < data.length; ++i) {
// We perform a delegate call to ourself to preserve the msg.sender. This is identical to `msg.sender`
// calling the functions directly one by one, therefore doesn't add any security risks.
// Note: msg.value is also preserved when doing a delegate call, but this function is not payable,
// so it's always 0 and not a security risk.
(bool success, bytes memory result) = address(this).delegatecall(data[i]);
if (!success && !ignoreReverts) {
_bubbleRevert(result);
}
}
}

Check warning

Code scanning / Slither

Low-level calls Warning

Comment on lines +40 to +58
function multicallWithResults(
bytes[] calldata data,
bool ignoreReverts
)
external
returns (Result[] memory results)
{
results = new Result[](data.length);
for (uint256 i = 0; i < data.length; ++i) {
// We perform a delegate call to ourself to preserve the msg.sender. This is identical to `msg.sender`
// calling the functions directly one by one, therefore doesn't add any security risks.
// Note: msg.value is also preserved when doing a delegate call, but this function is not payable,
// so it's always 0 and not a security risk.
(results[i].success, results[i].returnData) = address(this).delegatecall(data[i]);
if (!results[i].success && !ignoreReverts) {
_bubbleRevert(results[i].returnData);
}
}
}

Check notice

Code scanning / Slither

Calls inside a loop Low

Comment on lines +40 to +58
function multicallWithResults(
bytes[] calldata data,
bool ignoreReverts
)
external
returns (Result[] memory results)
{
results = new Result[](data.length);
for (uint256 i = 0; i < data.length; ++i) {
// We perform a delegate call to ourself to preserve the msg.sender. This is identical to `msg.sender`
// calling the functions directly one by one, therefore doesn't add any security risks.
// Note: msg.value is also preserved when doing a delegate call, but this function is not payable,
// so it's always 0 and not a security risk.
(results[i].success, results[i].returnData) = address(this).delegatecall(data[i]);
if (!results[i].success && !ignoreReverts) {
_bubbleRevert(results[i].returnData);
}
}
}
Comment on lines +63 to +75
function _bubbleRevert(bytes memory returnData) internal pure {
// Look for revert reason and bubble it up if present
if (returnData.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
// solhint-disable-next-line no-inline-assembly
assembly {
let returndata_size := mload(returnData)
revert(add(32, returnData), returndata_size)
}
} else {
revert MulticallTarget__UndeterminedRevert();
}
}

Check warning

Code scanning / Slither

Assembly usage Warning

Copy link
Contributor

coderabbitai bot commented Aug 29, 2024

Walkthrough

The recent changes introduce a new interface and contract for handling multiple calls in Ethereum smart contracts, enhancing batch processing capabilities. The IMulticallTarget interface defines methods for executing multiple function calls efficiently, while the MulticallTarget contract implements these methods. A comprehensive test suite and a testing harness were also added to validate the functionality and error handling of the new features.

Changes

File(s) Change Summary
packages/contracts-rfq/contracts/interfaces/IMulticallTarget.sol New interface IMulticallTarget with Result struct and functions multicallNoResults and multicallWithResults.
packages/contracts-rfq/contracts/utils/MulticallTarget.sol New contract MulticallTarget implementing IMulticallTarget, with functions for batch processing and error handling.
packages/contracts-rfq/test/MulticallTarget.t.sol New test suite for MulticallTarget, validating multiple call scenarios and error handling.
packages/contracts-rfq/test/harnesses/MulticallTargetHarness.sol New testing harness MulticallTargetHarness with functions to manipulate state and simulate behaviors.

Poem

In the meadow, hops a rabbit bright,
With calls so swift, oh what a sight!
Multicalls dance in a joyful spree,
Saving time, oh so happily!
With tests that leap, and harnesses strong,
We celebrate these changes, a merry song! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Aug 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 25.87753%. Comparing base (eccbfd1) to head (63f82fd).
Report is 55 commits behind head on master.

Additional details and impacted files
@@                 Coverage Diff                 @@
##              master       #3078         +/-   ##
===================================================
+ Coverage   23.09876%   25.87753%   +2.77877%     
===================================================
  Files            193         178         -15     
  Lines          11624       10484       -1140     
  Branches          82         119         +37     
===================================================
+ Hits            2685        2713         +28     
+ Misses          8697        7532       -1165     
+ Partials         242         239          -3     
Flag Coverage Δ
packages 90.56974% <ø> (ø)
promexporter ?
solidity 88.54962% <100.00000%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between eccbfd1 and 63f82fd.

Files selected for processing (4)
  • packages/contracts-rfq/contracts/interfaces/IMulticallTarget.sol (1 hunks)
  • packages/contracts-rfq/contracts/utils/MulticallTarget.sol (1 hunks)
  • packages/contracts-rfq/test/MulticallTarget.t.sol (1 hunks)
  • packages/contracts-rfq/test/harnesses/MulticallTargetHarness.sol (1 hunks)
Additional context used
GitHub Check: Slither
packages/contracts-rfq/contracts/utils/MulticallTarget.sol

[notice] 19-30: Calls inside a loop
MulticallTarget.multicallNoResults(bytes[],bool) (contracts/utils/MulticallTarget.sol#19-30) has external calls inside a loop: (success,result) = address(this).delegatecall(data[i]) (contracts/utils/MulticallTarget.sol#25)


[warning] 19-30: Low-level calls
Low level call in MulticallTarget.multicallNoResults(bytes[],bool) (contracts/utils/MulticallTarget.sol#19-30):
- (success,result) = address(this).delegatecall(data[i]) (contracts/utils/MulticallTarget.sol#25)


[notice] 40-58: Calls inside a loop
MulticallTarget.multicallWithResults(bytes[],bool) (contracts/utils/MulticallTarget.sol#40-58) has external calls inside a loop: (results[i].success,results[i].returnData) = address(this).delegatecall(data[i]) (contracts/utils/MulticallTarget.sol#53)


[warning] 40-58: Low-level calls
Low level call in MulticallTarget.multicallWithResults(bytes[],bool) (contracts/utils/MulticallTarget.sol#40-58):
- (results[i].success,results[i].returnData) = address(this).delegatecall(data[i]) (contracts/utils/MulticallTarget.sol#53)


[warning] 63-75: Assembly usage
MulticallTarget._bubbleRevert(bytes) (contracts/utils/MulticallTarget.sol#63-75) uses assembly
- INLINE ASM (contracts/utils/MulticallTarget.sol#68-71)

Additional comments not posted (38)
packages/contracts-rfq/contracts/interfaces/IMulticallTarget.sol (4)

1-2: LGTM!

The SPDX license identifier and pragma directive are correctly specified.


6-10: LGTM!

The interface and struct are correctly defined.


12-12: LGTM!

The function multicallNoResults is correctly defined.


13-18: LGTM!

The function multicallWithResults is correctly defined.

packages/contracts-rfq/test/harnesses/MulticallTargetHarness.sol (3)

1-2: LGTM!

The SPDX license identifier and pragma directive are correctly specified.


4-4: LGTM!

The import statement is correctly specified.


6-42: LGTM!

The contract and its functions are correctly defined.

packages/contracts-rfq/contracts/utils/MulticallTarget.sol (3)

1-2: LGTM!

The SPDX license identifier and pragma directive are correctly specified.


4-4: LGTM!

The import statement is correctly specified.


6-75: LGTM!

The contract and its functions are correctly defined.

Tools
GitHub Check: Slither

[notice] 19-30: Calls inside a loop
MulticallTarget.multicallNoResults(bytes[],bool) (contracts/utils/MulticallTarget.sol#19-30) has external calls inside a loop: (success,result) = address(this).delegatecall(data[i]) (contracts/utils/MulticallTarget.sol#25)


[warning] 19-30: Low-level calls
Low level call in MulticallTarget.multicallNoResults(bytes[],bool) (contracts/utils/MulticallTarget.sol#19-30):
- (success,result) = address(this).delegatecall(data[i]) (contracts/utils/MulticallTarget.sol#25)


[notice] 40-58: Calls inside a loop
MulticallTarget.multicallWithResults(bytes[],bool) (contracts/utils/MulticallTarget.sol#40-58) has external calls inside a loop: (results[i].success,results[i].returnData) = address(this).delegatecall(data[i]) (contracts/utils/MulticallTarget.sol#53)


[warning] 40-58: Low-level calls
Low level call in MulticallTarget.multicallWithResults(bytes[],bool) (contracts/utils/MulticallTarget.sol#40-58):
- (results[i].success,results[i].returnData) = address(this).delegatecall(data[i]) (contracts/utils/MulticallTarget.sol#53)


[warning] 63-75: Assembly usage
MulticallTarget._bubbleRevert(bytes) (contracts/utils/MulticallTarget.sol#63-75) uses assembly
- INLINE ASM (contracts/utils/MulticallTarget.sol#68-71)

packages/contracts-rfq/test/MulticallTarget.t.sol (28)

1-2: LGTM!

The SPDX license identifier and pragma directive are correct.


4-7: LGTM!

The imports are necessary and correctly specified.


9-13: LGTM!

The contract declaration and state variables are necessary for the test setup.


15-19: LGTM!

The setUp function correctly initializes the test environment.


21-23: LGTM!

The getEncodedStringRevertMessage function correctly encodes the string revert message.


25-32: LGTM!

The getMsgSenderData function correctly encodes the function calls and returns them as an array.


34-41: LGTM!

The getMsgSenderResults function correctly creates and returns the array of results.


43-50: LGTM!

The getNoRevertsData function correctly encodes the function calls and returns them as an array.


52-59: LGTM!

The getNoRevertsResults function correctly creates and returns the array of results.


61-67: LGTM!

The getCustomErrorRevertData function correctly encodes the function calls and returns them as an array.


70-77: LGTM!

The getCustomErrorRevertResults function correctly creates and returns the array of results.


79-85: LGTM!

The getStringRevertData function correctly encodes the function calls and returns them as an array.


88-95: LGTM!

The getStringRevertResults function correctly creates and returns the array of results.


97-103: LGTM!

The getUndeterminedRevertData function correctly encodes the function calls and returns them as an array.


106-113: LGTM!

The getUndeterminedRevertResults function correctly creates and returns the array of results.


117-123: LGTM!

The test_multicallNoResults_ignoreReverts_noReverts function correctly tests the multicallNoResults function.


125-132: LGTM!

The test_multicallNoResults_ignoreReverts_withMsgSender function correctly tests the multicallNoResults function.


134-140: LGTM!

The test_multicallNoResults_ignoreReverts_withCustomErrorRevert function correctly tests the multicallNoResults function.


142-148: LGTM!

The test_multicallNoResults_ignoreReverts_withStringRevert function correctly tests the multicallNoResults function.


150-156: LGTM!

The test_multicallNoResults_ignoreReverts_withUndeterminedRevert function correctly tests the multicallNoResults function.


158-164: LGTM!

The test_multicallNoResults_dontIgnoreReverts_noReverts function correctly tests the multicallNoResults function.


166-173: LGTM!

The test_multicallNoResults_dontIgnoreReverts_withMsgSender function correctly tests the multicallNoResults function.


175-179: LGTM!

The test_multicallNoResults_dontIgnoreReverts_withCustomErrorRevert function correctly tests the multicallNoResults function.


181-186: LGTM!

The test_multicallNoResults_dontIgnoreReverts_withStringRevert function correctly tests the multicallNoResults function.


188-192: LGTM!

The test_multicallNoResults_dontIgnoreReverts_withUndeterminedRevert function correctly tests the multicallNoResults function.


196-203: LGTM!

The test_multicallWithResults_ignoreReverts_noReverts function correctly tests the multicallWithResults function.


205-213: LGTM!

The test_multicallWithResults_ignoreReverts_withMsgSender function correctly tests the multicallWithResults function.


215-222: LGTM!

The test_multicallWithResults_ignoreReverts_withCustomErrorRevert function correctly tests the multicallWithResults function.

@ChiTimesChi ChiTimesChi changed the title feat(contracts-rfq): Multicall target abstraction feat(contracts-rfq): Multicall target abstraction [SLT-134] Sep 10, 2024
@ChiTimesChi ChiTimesChi merged commit 100324f into master Sep 10, 2024
55 checks passed
@ChiTimesChi ChiTimesChi deleted the feat/multicall-target branch September 10, 2024 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants