|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.20; |
| 3 | + |
| 4 | +import "forge-std/Script.sol"; |
| 5 | +import {Vm} from "forge-std/Vm.sol"; |
| 6 | +import {BatchCallAndSponsor} from "../src/BatchCallAndSponsor.sol"; |
| 7 | +import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; |
| 8 | +import {MessageHashUtils} from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol"; |
| 9 | + |
| 10 | +contract MockERC20 is ERC20 { |
| 11 | + constructor() ERC20("Mock Token", "MOCK") {} |
| 12 | + |
| 13 | + function mint(address to, uint256 amount) external { |
| 14 | + _mint(to, amount); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +contract BatchCallAndSponsorScript is Script { |
| 19 | + // Alice's address and private key (EOA with no initial contract code). |
| 20 | + address payable ALICE_ADDRESS = payable(0x70997970C51812dc3A010C7d01b50e0d17dc79C8); |
| 21 | + uint256 constant ALICE_PK = 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d; |
| 22 | + |
| 23 | + // Bob's address and private key (Bob will execute transactions on Alice's behalf). |
| 24 | + address constant BOB_ADDRESS = 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC; |
| 25 | + uint256 constant BOB_PK = 0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a; |
| 26 | + |
| 27 | + // The contract that Alice will delegate execution to. |
| 28 | + BatchCallAndSponsor public implementation; |
| 29 | + |
| 30 | + // ERC-20 token contract for minting test tokens. |
| 31 | + MockERC20 public token; |
| 32 | + |
| 33 | + function run() external { |
| 34 | + // Start broadcasting transactions with Alice's private key. |
| 35 | + vm.startBroadcast(ALICE_PK); |
| 36 | + |
| 37 | + // Deploy the delegation contract (Alice will delegate calls to this contract). |
| 38 | + implementation = new BatchCallAndSponsor(); |
| 39 | + |
| 40 | + // Deploy an ERC-20 token contract where Alice is the minter. |
| 41 | + token = new MockERC20(); |
| 42 | + |
| 43 | + // // Fund accounts |
| 44 | + token.mint(ALICE_ADDRESS, 1000e18); |
| 45 | + |
| 46 | + vm.stopBroadcast(); |
| 47 | + |
| 48 | + // Perform direct execution |
| 49 | + performDirectExecution(); |
| 50 | + |
| 51 | + // Perform sponsored execution |
| 52 | + performSponsoredExecution(); |
| 53 | + } |
| 54 | + |
| 55 | + function performDirectExecution() internal { |
| 56 | + BatchCallAndSponsor.Call[] memory calls = new BatchCallAndSponsor.Call[](2); |
| 57 | + |
| 58 | + // ETH transfer |
| 59 | + calls[0] = BatchCallAndSponsor.Call({to: BOB_ADDRESS, value: 1 ether, data: ""}); |
| 60 | + |
| 61 | + // Token transfer |
| 62 | + calls[1] = BatchCallAndSponsor.Call({ |
| 63 | + to: address(token), |
| 64 | + value: 0, |
| 65 | + data: abi.encodeCall(ERC20.transfer, (BOB_ADDRESS, 100e18)) |
| 66 | + }); |
| 67 | + |
| 68 | + vm.signAndAttachDelegation(address(implementation), ALICE_PK); |
| 69 | + vm.startPrank(ALICE_ADDRESS); |
| 70 | + BatchCallAndSponsor(ALICE_ADDRESS).execute(calls); |
| 71 | + vm.stopPrank(); |
| 72 | + |
| 73 | + console.log("Bob's balance after direct execution:", BOB_ADDRESS.balance); |
| 74 | + console.log("Bob's token balance after direct execution:", token.balanceOf(BOB_ADDRESS)); |
| 75 | + } |
| 76 | + |
| 77 | + function performSponsoredExecution() internal { |
| 78 | + console.log("Sending 1 ETH from Alice to a random address, the transaction is sponsored by Bob"); |
| 79 | + |
| 80 | + BatchCallAndSponsor.Call[] memory calls = new BatchCallAndSponsor.Call[](1); |
| 81 | + address recipient = makeAddr("recipient"); |
| 82 | + calls[0] = BatchCallAndSponsor.Call({to: recipient, value: 1 ether, data: ""}); |
| 83 | + |
| 84 | + // Alice signs a delegation allowing `implementation` to execute transactions on her behalf. |
| 85 | + Vm.SignedDelegation memory signedDelegation = vm.signDelegation(address(implementation), ALICE_PK); |
| 86 | + |
| 87 | + // Bob attaches the signed delegation from Alice and broadcasts it. |
| 88 | + vm.startBroadcast(BOB_PK); |
| 89 | + vm.attachDelegation(signedDelegation); |
| 90 | + |
| 91 | + // Verify that Alice's account now temporarily behaves as a smart contract. |
| 92 | + bytes memory code = address(ALICE_ADDRESS).code; |
| 93 | + require(code.length > 0, "no code written to Alice"); |
| 94 | + // console.log("Code on Alice's account:", vm.toString(code)); |
| 95 | + |
| 96 | + bytes memory encodedCalls = ""; |
| 97 | + for (uint256 i = 0; i < calls.length; i++) { |
| 98 | + encodedCalls = abi.encodePacked(encodedCalls, calls[i].to, calls[i].value, calls[i].data); |
| 99 | + } |
| 100 | + bytes32 digest = keccak256(abi.encodePacked(BatchCallAndSponsor(ALICE_ADDRESS).nonce(), encodedCalls)); |
| 101 | + (uint8 v, bytes32 r, bytes32 s) = vm.sign(ALICE_PK, MessageHashUtils.toEthSignedMessageHash(digest)); |
| 102 | + bytes memory signature = abi.encodePacked(r, s, v); |
| 103 | + |
| 104 | + // As Bob, execute the transaction via Alice's temporarily assigned contract. |
| 105 | + BatchCallAndSponsor(ALICE_ADDRESS).execute(calls, signature); |
| 106 | + |
| 107 | + vm.stopBroadcast(); |
| 108 | + |
| 109 | + console.log("Recipient balance after sponsored execution:", recipient.balance); |
| 110 | + } |
| 111 | +} |
0 commit comments