Config
evm_version = "prague"
Cheatcode
vm.signDelegation
vm.attachDelegation
vm.signAndAttachDelegation
Sample
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.26;
import "forge-std/Test.sol";
contract MiniAccount {
struct Call {
bytes data;
address to;
uint256 value;
}
event Executed(address indexed to, uint256 value, bytes data);
function execute(Call[] memory calls) external payable {
for (uint256 i = 0; i < calls.length; i++) {
Call memory call = calls[i];
(bool success,) = call.to.call{value: call.value}(call.data);
require(success, "Call failed");
emit Executed(call.to, call.value, call.data);
}
}
receive() external payable {}
}
contract BaseTest is Test {
Account alice;
MiniAccount impl_7702;
function setUp() public virtual {
alice = makeAccount("alice");
impl_7702 = new MiniAccount();
// sign delegation
Vm.SignedDelegation memory signedDelegation = vm.signDelegation(address(impl_7702), alice.key);
vm.attachDelegation(signedDelegation);
// deal
vm.deal(alice.addr, 10 ether);
}
function test_setUp() external view {
bytes memory alice_code = alice.addr.code;
assertEq(alice_code, bytes.concat(hex"ef0100", bytes20(address(impl_7702))));
assertEq(vm.getNonce(alice.addr), 0);
}
function test_remove_setup() external {
// remove delegation
Vm.SignedDelegation memory signedDelegation = vm.signDelegation(address(0), alice.key);
vm.attachDelegation(signedDelegation);
// assertion
bytes memory alice_code = alice.addr.code;
assertEq(alice_code, new bytes(0));
assertEq(vm.getNonce(alice.addr), 0);
}
function test_alice_scw() external {
// calls
MiniAccount.Call[] memory calls = new MiniAccount.Call[](2);
calls[0].to = address(100);
calls[0].value = 1 ether;
calls[1].to = address(101);
calls[1].value = 2 ether;
// execute
vm.startPrank(alice.addr);
MiniAccount(payable(alice.addr)).execute(calls);
vm.stopPrank();
// assert
assertEq(address(100).balance, 1 ether);
assertEq(address(101).balance, 2 ether);
}
function test_alice_eoa() external {
// execute
vm.startPrank(alice.addr);
address(100).call{value: 1 ether}("");
address(101).call{value: 2 ether}("");
vm.stopPrank();
// assert
assertEq(address(100).balance, 1 ether);
assertEq(address(101).balance, 2 ether);
}
}
Reference