draft-ERC7579Utils.sol 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.2.0-rc.0) (account/utils/draft-ERC7579Utils.sol)
  3. pragma solidity ^0.8.20;
  4. import {Execution} from "../../interfaces/draft-IERC7579.sol";
  5. import {Packing} from "../../utils/Packing.sol";
  6. import {Address} from "../../utils/Address.sol";
  7. type Mode is bytes32;
  8. type CallType is bytes1;
  9. type ExecType is bytes1;
  10. type ModeSelector is bytes4;
  11. type ModePayload is bytes22;
  12. /**
  13. * @dev Library with common ERC-7579 utility functions.
  14. *
  15. * See https://eips.ethereum.org/EIPS/eip-7579[ERC-7579].
  16. */
  17. // slither-disable-next-line unused-state
  18. library ERC7579Utils {
  19. using Packing for *;
  20. /// @dev A single `call` execution.
  21. CallType internal constant CALLTYPE_SINGLE = CallType.wrap(0x00);
  22. /// @dev A batch of `call` executions.
  23. CallType internal constant CALLTYPE_BATCH = CallType.wrap(0x01);
  24. /// @dev A `delegatecall` execution.
  25. CallType internal constant CALLTYPE_DELEGATECALL = CallType.wrap(0xFF);
  26. /// @dev Default execution type that reverts on failure.
  27. ExecType internal constant EXECTYPE_DEFAULT = ExecType.wrap(0x00);
  28. /// @dev Execution type that does not revert on failure.
  29. ExecType internal constant EXECTYPE_TRY = ExecType.wrap(0x01);
  30. /**
  31. * @dev Emits when an {EXECTYPE_TRY} execution fails.
  32. * @param batchExecutionIndex The index of the failed call in the execution batch.
  33. * @param returndata The returned data from the failed call.
  34. */
  35. event ERC7579TryExecuteFail(uint256 batchExecutionIndex, bytes returndata);
  36. /// @dev The provided {CallType} is not supported.
  37. error ERC7579UnsupportedCallType(CallType callType);
  38. /// @dev The provided {ExecType} is not supported.
  39. error ERC7579UnsupportedExecType(ExecType execType);
  40. /// @dev The provided module doesn't match the provided module type.
  41. error ERC7579MismatchedModuleTypeId(uint256 moduleTypeId, address module);
  42. /// @dev The module is not installed.
  43. error ERC7579UninstalledModule(uint256 moduleTypeId, address module);
  44. /// @dev The module is already installed.
  45. error ERC7579AlreadyInstalledModule(uint256 moduleTypeId, address module);
  46. /// @dev The module type is not supported.
  47. error ERC7579UnsupportedModuleType(uint256 moduleTypeId);
  48. /// @dev Executes a single call.
  49. function execSingle(
  50. ExecType execType,
  51. bytes calldata executionCalldata
  52. ) internal returns (bytes[] memory returnData) {
  53. (address target, uint256 value, bytes calldata callData) = decodeSingle(executionCalldata);
  54. returnData = new bytes[](1);
  55. returnData[0] = _call(0, execType, target, value, callData);
  56. }
  57. /// @dev Executes a batch of calls.
  58. function execBatch(
  59. ExecType execType,
  60. bytes calldata executionCalldata
  61. ) internal returns (bytes[] memory returnData) {
  62. Execution[] calldata executionBatch = decodeBatch(executionCalldata);
  63. returnData = new bytes[](executionBatch.length);
  64. for (uint256 i = 0; i < executionBatch.length; ++i) {
  65. returnData[i] = _call(
  66. i,
  67. execType,
  68. executionBatch[i].target,
  69. executionBatch[i].value,
  70. executionBatch[i].callData
  71. );
  72. }
  73. }
  74. /// @dev Executes a delegate call.
  75. function execDelegateCall(
  76. ExecType execType,
  77. bytes calldata executionCalldata
  78. ) internal returns (bytes[] memory returnData) {
  79. (address target, bytes calldata callData) = decodeDelegate(executionCalldata);
  80. returnData = new bytes[](1);
  81. returnData[0] = _delegatecall(0, execType, target, callData);
  82. }
  83. /// @dev Encodes the mode with the provided parameters. See {decodeMode}.
  84. function encodeMode(
  85. CallType callType,
  86. ExecType execType,
  87. ModeSelector selector,
  88. ModePayload payload
  89. ) internal pure returns (Mode mode) {
  90. return
  91. Mode.wrap(
  92. CallType
  93. .unwrap(callType)
  94. .pack_1_1(ExecType.unwrap(execType))
  95. .pack_2_4(bytes4(0))
  96. .pack_6_4(ModeSelector.unwrap(selector))
  97. .pack_10_22(ModePayload.unwrap(payload))
  98. );
  99. }
  100. /// @dev Decodes the mode into its parameters. See {encodeMode}.
  101. function decodeMode(
  102. Mode mode
  103. ) internal pure returns (CallType callType, ExecType execType, ModeSelector selector, ModePayload payload) {
  104. return (
  105. CallType.wrap(Packing.extract_32_1(Mode.unwrap(mode), 0)),
  106. ExecType.wrap(Packing.extract_32_1(Mode.unwrap(mode), 1)),
  107. ModeSelector.wrap(Packing.extract_32_4(Mode.unwrap(mode), 6)),
  108. ModePayload.wrap(Packing.extract_32_22(Mode.unwrap(mode), 10))
  109. );
  110. }
  111. /// @dev Encodes a single call execution. See {decodeSingle}.
  112. function encodeSingle(
  113. address target,
  114. uint256 value,
  115. bytes calldata callData
  116. ) internal pure returns (bytes memory executionCalldata) {
  117. return abi.encodePacked(target, value, callData);
  118. }
  119. /// @dev Decodes a single call execution. See {encodeSingle}.
  120. function decodeSingle(
  121. bytes calldata executionCalldata
  122. ) internal pure returns (address target, uint256 value, bytes calldata callData) {
  123. target = address(bytes20(executionCalldata[0:20]));
  124. value = uint256(bytes32(executionCalldata[20:52]));
  125. callData = executionCalldata[52:];
  126. }
  127. /// @dev Encodes a delegate call execution. See {decodeDelegate}.
  128. function encodeDelegate(
  129. address target,
  130. bytes calldata callData
  131. ) internal pure returns (bytes memory executionCalldata) {
  132. return abi.encodePacked(target, callData);
  133. }
  134. /// @dev Decodes a delegate call execution. See {encodeDelegate}.
  135. function decodeDelegate(
  136. bytes calldata executionCalldata
  137. ) internal pure returns (address target, bytes calldata callData) {
  138. target = address(bytes20(executionCalldata[0:20]));
  139. callData = executionCalldata[20:];
  140. }
  141. /// @dev Encodes a batch of executions. See {decodeBatch}.
  142. function encodeBatch(Execution[] memory executionBatch) internal pure returns (bytes memory executionCalldata) {
  143. return abi.encode(executionBatch);
  144. }
  145. /// @dev Decodes a batch of executions. See {encodeBatch}.
  146. function decodeBatch(bytes calldata executionCalldata) internal pure returns (Execution[] calldata executionBatch) {
  147. assembly ("memory-safe") {
  148. let ptr := add(executionCalldata.offset, calldataload(executionCalldata.offset))
  149. // Extract the ERC7579 Executions
  150. executionBatch.offset := add(ptr, 32)
  151. executionBatch.length := calldataload(ptr)
  152. }
  153. }
  154. /// @dev Executes a `call` to the target with the provided {ExecType}.
  155. function _call(
  156. uint256 index,
  157. ExecType execType,
  158. address target,
  159. uint256 value,
  160. bytes calldata data
  161. ) private returns (bytes memory) {
  162. (bool success, bytes memory returndata) = target.call{value: value}(data);
  163. return _validateExecutionMode(index, execType, success, returndata);
  164. }
  165. /// @dev Executes a `delegatecall` to the target with the provided {ExecType}.
  166. function _delegatecall(
  167. uint256 index,
  168. ExecType execType,
  169. address target,
  170. bytes calldata data
  171. ) private returns (bytes memory) {
  172. (bool success, bytes memory returndata) = target.delegatecall(data);
  173. return _validateExecutionMode(index, execType, success, returndata);
  174. }
  175. /// @dev Validates the execution mode and returns the returndata.
  176. function _validateExecutionMode(
  177. uint256 index,
  178. ExecType execType,
  179. bool success,
  180. bytes memory returndata
  181. ) private returns (bytes memory) {
  182. if (execType == ERC7579Utils.EXECTYPE_DEFAULT) {
  183. Address.verifyCallResult(success, returndata);
  184. } else if (execType == ERC7579Utils.EXECTYPE_TRY) {
  185. if (!success) emit ERC7579TryExecuteFail(index, returndata);
  186. } else {
  187. revert ERC7579UnsupportedExecType(execType);
  188. }
  189. return returndata;
  190. }
  191. }
  192. // Operators
  193. using {eqCallType as ==} for CallType global;
  194. using {eqExecType as ==} for ExecType global;
  195. using {eqModeSelector as ==} for ModeSelector global;
  196. using {eqModePayload as ==} for ModePayload global;
  197. /// @dev Compares two `CallType` values for equality.
  198. function eqCallType(CallType a, CallType b) pure returns (bool) {
  199. return CallType.unwrap(a) == CallType.unwrap(b);
  200. }
  201. /// @dev Compares two `ExecType` values for equality.
  202. function eqExecType(ExecType a, ExecType b) pure returns (bool) {
  203. return ExecType.unwrap(a) == ExecType.unwrap(b);
  204. }
  205. /// @dev Compares two `ModeSelector` values for equality.
  206. function eqModeSelector(ModeSelector a, ModeSelector b) pure returns (bool) {
  207. return ModeSelector.unwrap(a) == ModeSelector.unwrap(b);
  208. }
  209. /// @dev Compares two `ModePayload` values for equality.
  210. function eqModePayload(ModePayload a, ModePayload b) pure returns (bool) {
  211. return ModePayload.unwrap(a) == ModePayload.unwrap(b);
  212. }