Multicall.sol 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.1) (utils/Multicall.sol)
  3. pragma solidity ^0.8.20;
  4. import {Address} from "./Address.sol";
  5. import {Context} from "./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. abstract contract Multicall is Context {
  19. /**
  20. * @dev Receives and executes a batch of function calls on this contract.
  21. * @custom:oz-upgrades-unsafe-allow-reachable delegatecall
  22. */
  23. function multicall(bytes[] calldata data) external virtual returns (bytes[] memory results) {
  24. bytes memory context = msg.sender == _msgSender()
  25. ? new bytes(0)
  26. : msg.data[msg.data.length - _contextSuffixLength():];
  27. results = new bytes[](data.length);
  28. for (uint256 i = 0; i < data.length; i++) {
  29. results[i] = Address.functionDelegateCall(address(this), bytes.concat(data[i], context));
  30. }
  31. return results;
  32. }
  33. }