Skip to content

Commit

Permalink
Added a multicall file.
Browse files Browse the repository at this point in the history
  • Loading branch information
Foivos committed Sep 4, 2023
1 parent c888072 commit 9d3afe9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
21 changes: 21 additions & 0 deletions contracts/interfaces/IMulticall.sol
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);
}
35 changes: 35 additions & 0 deletions contracts/utils/Multicall.sol
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;
}
}
}

0 comments on commit 9d3afe9

Please sign in to comment.