|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.27; |
| 3 | + |
| 4 | +import '../../../utils/TestBase.sol'; |
| 5 | + |
| 6 | +/// @title TestERC4337Account_AddDeposit |
| 7 | +/// @dev Tests for the addDeposit function in the ERC4337 account. |
| 8 | +contract TestERC4337Account_AddDeposit is TestBase { |
| 9 | + uint256 defaultMaxPercentDelta; |
| 10 | + uint256 defaultDepositAmount; |
| 11 | + |
| 12 | + /// @notice Sets up the testing environment. |
| 13 | + function setUp() public { |
| 14 | + super.init(); |
| 15 | + BOB_ACCOUNT = BOB_ACCOUNT; |
| 16 | + defaultMaxPercentDelta = 100_000_000_000; |
| 17 | + defaultDepositAmount = 1 ether; |
| 18 | + } |
| 19 | + |
| 20 | + /// @notice Tests successful deposit addition. |
| 21 | + function test_AddDeposit_Success() public { |
| 22 | + uint256 depositBefore = ENTRYPOINT.balanceOf(address(BOB_ACCOUNT)); |
| 23 | + BOB_ACCOUNT.addDeposit{value: defaultDepositAmount}(); |
| 24 | + assertEq( |
| 25 | + depositBefore + defaultDepositAmount, |
| 26 | + ENTRYPOINT.balanceOf(address(BOB_ACCOUNT)), |
| 27 | + 'Deposit should be added to EntryPoint' |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + /// @notice Tests that the Deposited event is emitted on deposit. |
| 32 | + function test_AddDeposit_EventEmitted() public { |
| 33 | + prefundSmartAccountAndAssertSuccess(address(BOB_ACCOUNT), defaultDepositAmount); |
| 34 | + vm.expectEmit(true, true, true, true); |
| 35 | + uint256 expectedDeposit = ENTRYPOINT.getDepositInfo(address(BOB_ACCOUNT)).deposit + defaultDepositAmount; |
| 36 | + emit Deposited(address(BOB_ACCOUNT), expectedDeposit); |
| 37 | + BOB_ACCOUNT.addDeposit{value: defaultDepositAmount}(); |
| 38 | + } |
| 39 | + |
| 40 | + /// @notice Tests that adding a deposit with no value reverts. |
| 41 | + function test_RevertIf_AddDeposit_NoValue() public { |
| 42 | + BOB_ACCOUNT.addDeposit(); |
| 43 | + } |
| 44 | + |
| 45 | + /// @notice Tests deposit addition via handleOps. |
| 46 | + function test_AddDeposit_DepositViaHandleOps() public { |
| 47 | + prefundSmartAccountAndAssertSuccess(address(BOB_ACCOUNT), defaultDepositAmount + 1 ether); |
| 48 | + uint256 depositBefore = ENTRYPOINT.balanceOf(address(BOB_ACCOUNT)); |
| 49 | + |
| 50 | + Execution[] memory executions = |
| 51 | + prepareSingleExecution(address(BOB_ACCOUNT), defaultDepositAmount, abi.encodeWithSignature('addDeposit()')); |
| 52 | + PackedUserOperation[] memory userOps = |
| 53 | + buildPackedUserOperation(BOB, BOB_ACCOUNT, EXECTYPE_DEFAULT, executions, address(VALIDATOR_MODULE), 0); |
| 54 | + uint256 gasUsed = handleUserOpAndMeasureGas(userOps, BOB.addr); |
| 55 | + |
| 56 | + almostEq( |
| 57 | + depositBefore + defaultDepositAmount - (gasUsed * tx.gasprice), |
| 58 | + ENTRYPOINT.balanceOf(address(BOB_ACCOUNT)), |
| 59 | + defaultMaxPercentDelta |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + /// @notice Tests batch deposit addition via handleOps. |
| 64 | + function test_AddDeposit_BatchDepositViaHandleOps() public { |
| 65 | + uint256 executionsNumber = 5; |
| 66 | + prefundSmartAccountAndAssertSuccess(address(BOB_ACCOUNT), defaultDepositAmount * 10); |
| 67 | + uint256 depositBefore = ENTRYPOINT.balanceOf(address(BOB_ACCOUNT)); |
| 68 | + |
| 69 | + Execution memory execution = |
| 70 | + Execution(address(BOB_ACCOUNT), defaultDepositAmount, abi.encodeWithSignature('addDeposit()')); |
| 71 | + Execution[] memory executions = prepareSeveralIdenticalExecutions(execution, executionsNumber); |
| 72 | + PackedUserOperation[] memory userOps = |
| 73 | + buildPackedUserOperation(BOB, BOB_ACCOUNT, EXECTYPE_DEFAULT, executions, address(VALIDATOR_MODULE), 0); |
| 74 | + uint256 gasUsed = handleUserOpAndMeasureGas(userOps, BOB.addr); |
| 75 | + |
| 76 | + almostEq( |
| 77 | + depositBefore + (defaultDepositAmount * executionsNumber) - (gasUsed * tx.gasprice), |
| 78 | + ENTRYPOINT.balanceOf(address(BOB_ACCOUNT)), |
| 79 | + defaultMaxPercentDelta |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + /// @notice Tests deposit addition via handleOps with EXECTYPE_TRY. |
| 84 | + function test_AddDeposit_Try_DepositViaHandleOps() public { |
| 85 | + prefundSmartAccountAndAssertSuccess(address(BOB_ACCOUNT), defaultDepositAmount + 1 ether); |
| 86 | + uint256 depositBefore = ENTRYPOINT.balanceOf(address(BOB_ACCOUNT)); |
| 87 | + |
| 88 | + Execution[] memory executions = |
| 89 | + prepareSingleExecution(address(BOB_ACCOUNT), defaultDepositAmount, abi.encodeWithSignature('addDeposit()')); |
| 90 | + PackedUserOperation[] memory userOps = |
| 91 | + buildPackedUserOperation(BOB, BOB_ACCOUNT, EXECTYPE_TRY, executions, address(VALIDATOR_MODULE), 0); |
| 92 | + uint256 gasUsed = handleUserOpAndMeasureGas(userOps, BOB.addr); |
| 93 | + |
| 94 | + almostEq( |
| 95 | + depositBefore + defaultDepositAmount - (gasUsed * tx.gasprice), |
| 96 | + ENTRYPOINT.balanceOf(address(BOB_ACCOUNT)), |
| 97 | + defaultMaxPercentDelta |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + /// @notice Tests batch deposit addition via handleOps with EXECTYPE_TRY. |
| 102 | + function test_AddDeposit_Try_BatchDepositViaHandleOps() public { |
| 103 | + prefundSmartAccountAndAssertSuccess(address(BOB_ACCOUNT), defaultDepositAmount * 10); |
| 104 | + uint256 depositBefore = ENTRYPOINT.balanceOf(address(BOB_ACCOUNT)); |
| 105 | + uint256 executionsNumber = 5; |
| 106 | + |
| 107 | + Execution memory execution = |
| 108 | + Execution(address(BOB_ACCOUNT), defaultDepositAmount, abi.encodeWithSignature('addDeposit()')); |
| 109 | + Execution[] memory executions = prepareSeveralIdenticalExecutions(execution, executionsNumber); |
| 110 | + PackedUserOperation[] memory userOps = |
| 111 | + buildPackedUserOperation(BOB, BOB_ACCOUNT, EXECTYPE_TRY, executions, address(VALIDATOR_MODULE), 0); |
| 112 | + uint256 gasUsed = handleUserOpAndMeasureGas(userOps, BOB.addr); |
| 113 | + |
| 114 | + almostEq( |
| 115 | + depositBefore + (defaultDepositAmount * executionsNumber) - (gasUsed * tx.gasprice), |
| 116 | + ENTRYPOINT.balanceOf(address(BOB_ACCOUNT)), |
| 117 | + defaultMaxPercentDelta |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + /// @notice Tests that the addDeposit function reverts if calling the wrong entryPoint. |
| 122 | + function test_AddDeposit_RevertIf_WrongEntryPoint() public { |
| 123 | + vm.etch(address(0x0000000071727De22E5E9d8BAf0edAc6f37da032), address(VALIDATOR_MODULE).code); |
| 124 | + vm.expectRevert(); |
| 125 | + BOB_ACCOUNT.addDeposit{value: defaultDepositAmount}(); |
| 126 | + } |
| 127 | +} |
0 commit comments