-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @title IMulticall | ||
* @notice This contract is a multi-functional smart contract which allows for multiple | ||
* contract calls in a single transaction. | ||
*/ | ||
interface IMulticall { | ||
error MulticallFailed(bytes err); | ||
|
||
/** | ||
* @notice Performs multiple delegate calls and returns the results of all calls as an array | ||
* @dev This function requires that the contract has sufficient balance for the delegate calls. | ||
* If any of the calls fail, the function will revert with the failure message. | ||
* @param data An array of encoded function calls | ||
* @return results An bytes array with the return data of each function call | ||
*/ | ||
function multicall(bytes[] calldata data) external payable returns (bytes[] memory results); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import { IMulticall } from '../interfaces/IMulticall.sol'; | ||
|
||
/** | ||
* @title Multicall | ||
* @notice This contract is a multi-functional smart contract which allows for multiple | ||
* contract calls in a single transaction. | ||
*/ | ||
contract Multicall is IMulticall { | ||
|
||
/** | ||
* @notice Performs multiple delegate calls and returns the results of all calls as an array | ||
* @dev This function requires that the contract has sufficient balance for the delegate calls. | ||
* If any of the calls fail, the function will revert with the failure message. | ||
* @param data An array of encoded function calls | ||
* @return results An bytes array with the return data of each function call | ||
*/ | ||
function multicall(bytes[] calldata data) public payable returns (bytes[] memory results) { | ||
results = new bytes[](data.length); | ||
bool success; | ||
bytes memory result; | ||
for (uint256 i = 0; i < data.length; ++i) { | ||
(success, result) = address(this).delegatecall(data[i]); | ||
|
||
if (!success) { | ||
revert(string(result)); | ||
} | ||
|
||
results[i] = result; | ||
} | ||
} | ||
} |