MulticallUpgradeable.sol 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0-rc.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. __Multicall_init_unchained();
  14. }
  15. function __Multicall_init_unchained() internal onlyInitializing {
  16. }
  17. /**
  18. * @dev Receives and executes a batch of function calls on this contract.
  19. */
  20. function multicall(bytes[] calldata data) external virtual returns (bytes[] memory results) {
  21. results = new bytes[](data.length);
  22. for (uint256 i = 0; i < data.length; i++) {
  23. results[i] = _functionDelegateCall(address(this), data[i]);
  24. }
  25. return results;
  26. }
  27. /**
  28. * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
  29. * but performing a delegate call.
  30. *
  31. * _Available since v3.4._
  32. */
  33. function _functionDelegateCall(address target, bytes memory data) private returns (bytes memory) {
  34. require(AddressUpgradeable.isContract(target), "Address: delegate call to non-contract");
  35. // solhint-disable-next-line avoid-low-level-calls
  36. (bool success, bytes memory returndata) = target.delegatecall(data);
  37. return AddressUpgradeable.verifyCallResult(success, returndata, "Address: low-level delegate call failed");
  38. }
  39. uint256[50] private __gap;
  40. }