draft-ERC7579Utils.sol 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 call in the execution batch.
  32. * @param returndata The returned data from the failed call.
  33. */
  34. event ERC7579TryExecuteFail(uint256 batchExecutionIndex, bytes returndata);
  35. /// @dev The provided {CallType} is not supported.
  36. error ERC7579UnsupportedCallType(CallType callType);
  37. /// @dev The provided {ExecType} is not supported.
  38. error ERC7579UnsupportedExecType(ExecType execType);
  39. /// @dev The provided module doesn't match the provided module type.
  40. error ERC7579MismatchedModuleTypeId(uint256 moduleTypeId, address module);
  41. /// @dev The module is not installed.
  42. error ERC7579UninstalledModule(uint256 moduleTypeId, address module);
  43. /// @dev The module is already installed.
  44. error ERC7579AlreadyInstalledModule(uint256 moduleTypeId, address module);
  45. /// @dev The module type is not supported.
  46. error ERC7579UnsupportedModuleType(uint256 moduleTypeId);
  47. /// @dev Executes a single call.
  48. function execSingle(
  49. ExecType execType,
  50. bytes calldata executionCalldata
  51. ) internal returns (bytes[] memory returnData) {
  52. (address target, uint256 value, bytes calldata callData) = decodeSingle(executionCalldata);
  53. returnData = new bytes[](1);
  54. returnData[0] = _call(0, execType, target, value, callData);
  55. }
  56. /// @dev Executes a batch of calls.
  57. function execBatch(
  58. ExecType execType,
  59. bytes calldata executionCalldata
  60. ) internal returns (bytes[] memory returnData) {
  61. Execution[] calldata executionBatch = decodeBatch(executionCalldata);
  62. returnData = new bytes[](executionBatch.length);
  63. for (uint256 i = 0; i < executionBatch.length; ++i) {
  64. returnData[i] = _call(
  65. i,
  66. execType,
  67. executionBatch[i].target,
  68. executionBatch[i].value,
  69. executionBatch[i].callData
  70. );
  71. }
  72. }
  73. /// @dev Executes a delegate call.
  74. function execDelegateCall(
  75. ExecType execType,
  76. bytes calldata executionCalldata
  77. ) internal returns (bytes[] memory returnData) {
  78. (address target, bytes calldata callData) = decodeDelegate(executionCalldata);
  79. returnData = new bytes[](1);
  80. returnData[0] = _delegatecall(0, execType, target, callData);
  81. }
  82. /// @dev Encodes the mode with the provided parameters. See {decodeMode}.
  83. function encodeMode(
  84. CallType callType,
  85. ExecType execType,
  86. ModeSelector selector,
  87. ModePayload payload
  88. ) internal pure returns (Mode mode) {
  89. return
  90. Mode.wrap(
  91. CallType
  92. .unwrap(callType)
  93. .pack_1_1(ExecType.unwrap(execType))
  94. .pack_2_4(bytes4(0))
  95. .pack_6_4(ModeSelector.unwrap(selector))
  96. .pack_10_22(ModePayload.unwrap(payload))
  97. );
  98. }
  99. /// @dev Decodes the mode into its parameters. See {encodeMode}.
  100. function decodeMode(
  101. Mode mode
  102. ) internal pure returns (CallType callType, ExecType execType, ModeSelector selector, ModePayload payload) {
  103. return (
  104. CallType.wrap(Packing.extract_32_1(Mode.unwrap(mode), 0)),
  105. ExecType.wrap(Packing.extract_32_1(Mode.unwrap(mode), 1)),
  106. ModeSelector.wrap(Packing.extract_32_4(Mode.unwrap(mode), 6)),
  107. ModePayload.wrap(Packing.extract_32_22(Mode.unwrap(mode), 10))
  108. );
  109. }
  110. /// @dev Encodes a single call execution. See {decodeSingle}.
  111. function encodeSingle(
  112. address target,
  113. uint256 value,
  114. bytes calldata callData
  115. ) internal pure returns (bytes memory executionCalldata) {
  116. return abi.encodePacked(target, value, callData);
  117. }
  118. /// @dev Decodes a single call execution. See {encodeSingle}.
  119. function decodeSingle(
  120. bytes calldata executionCalldata
  121. ) internal pure returns (address target, uint256 value, bytes calldata callData) {
  122. target = address(bytes20(executionCalldata[0:20]));
  123. value = uint256(bytes32(executionCalldata[20:52]));
  124. callData = executionCalldata[52:];
  125. }
  126. /// @dev Encodes a delegate call execution. See {decodeDelegate}.
  127. function encodeDelegate(
  128. address target,
  129. bytes calldata callData
  130. ) internal pure returns (bytes memory executionCalldata) {
  131. return abi.encodePacked(target, callData);
  132. }
  133. /// @dev Decodes a delegate call execution. See {encodeDelegate}.
  134. function decodeDelegate(
  135. bytes calldata executionCalldata
  136. ) internal pure returns (address target, bytes calldata callData) {
  137. target = address(bytes20(executionCalldata[0:20]));
  138. callData = executionCalldata[20:];
  139. }
  140. /// @dev Encodes a batch of executions. See {decodeBatch}.
  141. function encodeBatch(Execution[] memory executionBatch) internal pure returns (bytes memory executionCalldata) {
  142. return abi.encode(executionBatch);
  143. }
  144. /// @dev Decodes a batch of executions. See {encodeBatch}.
  145. function decodeBatch(bytes calldata executionCalldata) internal pure returns (Execution[] calldata executionBatch) {
  146. assembly ("memory-safe") {
  147. let ptr := add(executionCalldata.offset, calldataload(executionCalldata.offset))
  148. // Extract the ERC7579 Executions
  149. executionBatch.offset := add(ptr, 32)
  150. executionBatch.length := calldataload(ptr)
  151. }
  152. }
  153. /// @dev Executes a `call` to the target with the provided {ExecType}.
  154. function _call(
  155. uint256 index,
  156. ExecType execType,
  157. address target,
  158. uint256 value,
  159. bytes calldata data
  160. ) private returns (bytes memory) {
  161. (bool success, bytes memory returndata) = target.call{value: value}(data);
  162. return _validateExecutionMode(index, execType, success, returndata);
  163. }
  164. /// @dev Executes a `delegatecall` to the target with the provided {ExecType}.
  165. function _delegatecall(
  166. uint256 index,
  167. ExecType execType,
  168. address target,
  169. bytes calldata data
  170. ) private returns (bytes memory) {
  171. (bool success, bytes memory returndata) = target.delegatecall(data);
  172. return _validateExecutionMode(index, execType, success, returndata);
  173. }
  174. /// @dev Validates the execution mode and returns the returndata.
  175. function _validateExecutionMode(
  176. uint256 index,
  177. ExecType execType,
  178. bool success,
  179. bytes memory returndata
  180. ) private returns (bytes memory) {
  181. if (execType == ERC7579Utils.EXECTYPE_DEFAULT) {
  182. Address.verifyCallResult(success, returndata);
  183. } else if (execType == ERC7579Utils.EXECTYPE_TRY) {
  184. if (!success) emit ERC7579TryExecuteFail(index, returndata);
  185. } else {
  186. revert ERC7579UnsupportedExecType(execType);
  187. }
  188. return returndata;
  189. }
  190. }
  191. // Operators
  192. using {eqCallType as ==} for CallType global;
  193. using {eqExecType as ==} for ExecType global;
  194. using {eqModeSelector as ==} for ModeSelector global;
  195. using {eqModePayload as ==} for ModePayload global;
  196. /// @dev Compares two `CallType` values for equality.
  197. function eqCallType(CallType a, CallType b) pure returns (bool) {
  198. return CallType.unwrap(a) == CallType.unwrap(b);
  199. }
  200. /// @dev Compares two `ExecType` values for equality.
  201. function eqExecType(ExecType a, ExecType b) pure returns (bool) {
  202. return ExecType.unwrap(a) == ExecType.unwrap(b);
  203. }
  204. /// @dev Compares two `ModeSelector` values for equality.
  205. function eqModeSelector(ModeSelector a, ModeSelector b) pure returns (bool) {
  206. return ModeSelector.unwrap(a) == ModeSelector.unwrap(b);
  207. }
  208. /// @dev Compares two `ModePayload` values for equality.
  209. function eqModePayload(ModePayload a, ModePayload b) pure returns (bool) {
  210. return ModePayload.unwrap(a) == ModePayload.unwrap(b);
  211. }