Multicall.sol 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.4) (utils/Multicall.sol)
  3. pragma solidity ^0.8.0;
  4. import "./Address.sol";
  5. import "./Context.sol";
  6. /**
  7. * @dev Provides a function to batch together multiple calls in a single external call.
  8. *
  9. * Consider any assumption about calldata validation performed by the sender may be violated if it's not especially
  10. * careful about sending transactions invoking {multicall}. For example, a relay address that filters function
  11. * selectors won't filter calls nested within a {multicall} operation.
  12. *
  13. * NOTE: Since 5.0.1 and 4.9.4, this contract identifies non-canonical contexts (i.e. `msg.sender` is not {_msgSender}).
  14. * If a non-canonical context is identified, the following self `delegatecall` appends the last bytes of `msg.data`
  15. * to the subcall. This makes it safe to use with {ERC2771Context}. Contexts that don't affect the resolution of
  16. * {_msgSender} are not propagated to subcalls.
  17. *
  18. * _Available since v4.1._
  19. */
  20. abstract contract Multicall is Context {
  21. /**
  22. * @dev Receives and executes a batch of function calls on this contract.
  23. * @custom:oz-upgrades-unsafe-allow-reachable delegatecall
  24. */
  25. function multicall(bytes[] calldata data) external virtual returns (bytes[] memory results) {
  26. bytes memory context = msg.sender == _msgSender()
  27. ? new bytes(0)
  28. : msg.data[msg.data.length - _contextSuffixLength():];
  29. results = new bytes[](data.length);
  30. for (uint256 i = 0; i < data.length; i++) {
  31. results[i] = Address.functionDelegateCall(address(this), data[i]);
  32. results[i] = Address.functionDelegateCall(address(this), bytes.concat(data[i], context));
  33. }
  34. return results;
  35. }
  36. }