MulticallUpgradeable.sol 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0) (utils/Multicall.sol)
  3. pragma solidity ^0.8.0;
  4. import "./AddressUpgradeable.sol";
  5. import "../proxy/utils/Initializable.sol";
  6. /**
  7. * @dev Provides a function to batch together multiple calls in a single external call.
  8. *
  9. * _Available since v4.1._
  10. */
  11. abstract contract MulticallUpgradeable is Initializable {
  12. function __Multicall_init() internal onlyInitializing {
  13. }
  14. function __Multicall_init_unchained() internal onlyInitializing {
  15. }
  16. /**
  17. * @dev Receives and executes a batch of function calls on this contract.
  18. */
  19. function multicall(bytes[] calldata data) external virtual returns (bytes[] memory results) {
  20. results = new bytes[](data.length);
  21. for (uint256 i = 0; i < data.length; i++) {
  22. results[i] = _functionDelegateCall(address(this), data[i]);
  23. }
  24. return results;
  25. }
  26. /**
  27. * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
  28. * but performing a delegate call.
  29. *
  30. * _Available since v3.4._
  31. */
  32. function _functionDelegateCall(address target, bytes memory data) private returns (bytes memory) {
  33. require(AddressUpgradeable.isContract(target), "Address: delegate call to non-contract");
  34. // solhint-disable-next-line avoid-low-level-calls
  35. (bool success, bytes memory returndata) = target.delegatecall(data);
  36. return AddressUpgradeable.verifyCallResult(success, returndata, "Address: low-level delegate call failed");
  37. }
  38. /**
  39. * This empty reserved space is put in place to allow future versions to add new
  40. * variables without shifting down storage in the inheritance chain.
  41. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  42. */
  43. uint256[50] private __gap;
  44. }