draft-ERC7579Utils.sol 8.9 KB

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