Skip to content

Commit

Permalink
add test for try execute delegate call
Browse files Browse the repository at this point in the history
  • Loading branch information
livingrockrises committed Jul 7, 2024
1 parent 7b3c243 commit f0593e2
Showing 1 changed file with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ contract TestAccountExecution_TryExecuteSingle is TestAccountExecution_Base {


// Build UserOperation for single execution
PackedUserOperation[] memory userOps = buildPackedUserOperation(BOB, BOB_ACCOUNT, EXECTYPE_TRY, execution, address(VALIDATOR_MODULE));
PackedUserOperation[] memory userOps = buildPackedUserOperation(BOB, BOB_ACCOUNT, EXECTYPE_DEFAULT, execution, address(VALIDATOR_MODULE));

bytes memory userOpCalldata = abi.encodeCall(
Nexus.execute,
Expand All @@ -56,4 +56,48 @@ contract TestAccountExecution_TryExecuteSingle is TestAccountExecution_Base {
// Assert that the value was set ie that execution was successful
assertTrue(valueTarget.balance == value);
}

/// @notice Tests successful execution of a single operation.
function test_TryExecuteDelegateCall_Success() public {

(bool res, ) = payable(address(BOB_ACCOUNT)).call{ value: 2 ether}(""); // Fund BOB_ACCOUNT
assertEq(res, true, "Funding BOB_ACCOUNT should succeed");

// Initial state assertion
assertEq(counter.getNumber(), 0, "Counter should start at 0");
// Create calldata for the account to execute
address valueTarget = makeAddr("valueTarget");
uint256 value = 1 ether;

bytes memory sendValue =
abi.encodeWithSelector(MockDelegateTarget.sendValue.selector, valueTarget, value);

// placeholder
Execution[] memory execution = new Execution[](1);
execution[0] = Execution(address(counter), 0, abi.encodeWithSelector(Counter.incrementNumber.selector));


// Build UserOperation for single execution
PackedUserOperation[] memory userOps = buildPackedUserOperation(BOB, BOB_ACCOUNT, EXECTYPE_TRY, execution, address(VALIDATOR_MODULE));

bytes memory userOpCalldata = abi.encodeCall(
Nexus.execute,
(
ModeLib.encode(
CALLTYPE_DELEGATECALL, EXECTYPE_TRY, MODE_DEFAULT, ModePayload.wrap(0x00)
),
abi.encodePacked(address(delegateTarget), sendValue)
)
);

userOps[0].callData = userOpCalldata;

// Sign the operation
bytes32 userOpHash = ENTRYPOINT.getUserOpHash(userOps[0]);
userOps[0].signature = signMessage(BOB, userOpHash);

ENTRYPOINT.handleOps(userOps, payable(address(BOB.addr)));
// Assert that the value was set ie that execution was successful
assertTrue(valueTarget.balance == (value));
}
}

0 comments on commit f0593e2

Please sign in to comment.