AccountERC7702.t.sol 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {Test} from "forge-std/Test.sol";
  4. import {AccountERC7702Mock} from "@openzeppelin/contracts/mocks/account/AccountMock.sol";
  5. import {CallReceiverMock} from "@openzeppelin/contracts/mocks/CallReceiverMock.sol";
  6. import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
  7. import {
  8. ERC7579Utils,
  9. Execution,
  10. Mode,
  11. ModeSelector,
  12. ModePayload
  13. } from "@openzeppelin/contracts/account/utils/draft-ERC7579Utils.sol";
  14. import {ERC4337Utils, IEntryPointExtra} from "@openzeppelin/contracts/account/utils/draft-ERC4337Utils.sol";
  15. import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
  16. import {PackedUserOperation} from "@openzeppelin/contracts/interfaces/draft-IERC4337.sol";
  17. import {ERC7821} from "@openzeppelin/contracts/account/extensions/draft-ERC7821.sol";
  18. contract AccountERC7702MockConstructor is AccountERC7702Mock {
  19. constructor() EIP712("MyAccount", "1") {}
  20. }
  21. contract AccountERC7702Test is Test {
  22. using ERC7579Utils for *;
  23. using ERC4337Utils for PackedUserOperation;
  24. using Strings for *;
  25. uint256 private constant MAX_ETH = type(uint128).max;
  26. // Test accounts
  27. CallReceiverMock private _target;
  28. // ERC-4337 signer
  29. uint256 private _signerPrivateKey;
  30. AccountERC7702MockConstructor private _signer;
  31. function setUp() public {
  32. // Deploy target contract
  33. _target = new CallReceiverMock();
  34. // Setup signer
  35. _signerPrivateKey = 0x1234;
  36. _signer = AccountERC7702MockConstructor(payable(vm.addr(_signerPrivateKey)));
  37. vm.deal(address(_signer), MAX_ETH);
  38. // Sign and attach delegation
  39. vm.signAndAttachDelegation(address(new AccountERC7702MockConstructor()), _signerPrivateKey);
  40. // Setup entrypoint
  41. vm.deal(address(ERC4337Utils.ENTRYPOINT_V08), MAX_ETH);
  42. vm.etch(address(ERC4337Utils.ENTRYPOINT_V08), vm.readFileBinary("test/bin/EntryPoint070.bytecode"));
  43. }
  44. function testExecuteBatch(uint256 argA, uint256 argB) public {
  45. // Create the mode for batch execution
  46. Mode mode = ERC7579Utils.CALLTYPE_BATCH.encodeMode(
  47. ERC7579Utils.EXECTYPE_DEFAULT,
  48. ModeSelector.wrap(0x00000000),
  49. ModePayload.wrap(0x00000000)
  50. );
  51. Execution[] memory execution = new Execution[](2);
  52. execution[0] = Execution({
  53. target: address(_target),
  54. value: 1 ether,
  55. callData: abi.encodeCall(CallReceiverMock.mockFunctionExtra, ())
  56. });
  57. execution[1] = Execution({
  58. target: address(_target),
  59. value: 0,
  60. callData: abi.encodeCall(CallReceiverMock.mockFunctionWithArgs, (argA, argB))
  61. });
  62. // Pack the batch within a PackedUserOperation
  63. PackedUserOperation[] memory ops = new PackedUserOperation[](1);
  64. ops[0] = PackedUserOperation({
  65. sender: address(_signer),
  66. nonce: 0,
  67. initCode: bytes(""),
  68. callData: abi.encodeCall(ERC7821.execute, (Mode.unwrap(mode), execution.encodeBatch())),
  69. preVerificationGas: 100000,
  70. accountGasLimits: bytes32(abi.encodePacked(uint128(100000), uint128(100000))),
  71. gasFees: bytes32(abi.encodePacked(uint128(1000000), uint128(1000000))),
  72. paymasterAndData: bytes(""),
  73. signature: bytes("")
  74. });
  75. (uint8 v, bytes32 r, bytes32 s) = vm.sign(
  76. _signerPrivateKey,
  77. IEntryPointExtra(address(ERC4337Utils.ENTRYPOINT_V08)).getUserOpHash(ops[0])
  78. );
  79. ops[0].signature = abi.encodePacked(r, s, v);
  80. // Expect the events to be emitted
  81. vm.expectEmit(true, true, true, true);
  82. emit CallReceiverMock.MockFunctionCalledExtra(address(_signer), 1 ether);
  83. vm.expectEmit(true, true, true, true);
  84. emit CallReceiverMock.MockFunctionCalledWithArgs(argA, argB);
  85. // Execute the batch
  86. _signer.entryPoint().handleOps(ops, payable(makeAddr("beneficiary")));
  87. }
  88. }