An educational project demonstrating account abstraction and sponsored transaction execution using EIP-7702. This project uses Foundry for deployment, scripting, and testing.
The BatchCallAndSponsor contract enables batch execution of calls by verifying signatures over a nonce and batched call data. It supports:
- Direct execution: by the smart account itself.
- Sponsored execution: via an off-chain signature (by a sponsor).
Replay protection is provided by an internal nonce that increments after each batch execution.
- Batch transaction execution
- Off-chain signature verification using ECDSA
- Replay protection through nonce incrementation
- Support for both ETH and ERC-20 token transfers
- 📚 EIP-7702 核心机制文档 - 详细解析 EIP-7702 赞助交易的核心技术概念和安全机制
- 🔄 ERC20 赞助转账指南 - ERC-20 代币赞助转账的实现指南
- 📋 EIP-7702 赞助转账文档 - EIP-7702 赞助转账功能的技术文档
- 🛠️ Foundry 使用指南 - Foundry 框架的使用说明
- Foundry
- Solidity ^0.8.20
curl -L https://foundry.paradigm.xyz | bash
git clone https://github.com/quiknode-labs/qn-guide-examples.git
cd qn-guide-examples/ethereum/eip-7702forge install OpenZeppelin/openzeppelin-contracts
forge install foundry-rs/forge-std
forge remappings > remappings.txtRun the following command on your terminal to start a local network with the Prague hardfork.
anvil --hardfork pragueOn another terminal, run the following command to build the contract.
forge buildAfter building the contract, run the following command to run the test cases. If you want to display stack traces for all tests, use -vvvv flag instead of -vvv.
forge test -vvvThe output should look like this:
Ran 4 tests for test/BatchCallAndSponsor.t.sol:BatchCallAndSponsorTest
[PASS] testDirectExecution() (gas: 128386)
Logs:
Sending 1 ETH from Alice to Bob and transferring 100 tokens to Bob in a single transaction
[PASS] testReplayAttack() (gas: 114337)
Logs:
Test replay attack: Reusing the same signature should revert.
[PASS] testSponsoredExecution() (gas: 110461)
Logs:
Sending 1 ETH from Alice to a random address while the transaction is sponsored by Bob
[PASS] testWrongSignature() (gas: 37077)
Logs:
Test wrong signature: Execution should revert with 'Invalid signature'.
Suite result: ok. 4 passed; 0 failed; 0 skipped;Now that you’ve set up the project, it’s time to run the deployment script. This script deploys the contract, mints tokens, and tests both batch execution and sponsored execution features.
We use the following command:
--broadcast: Broadcasts the transactions to your local network.--rpc-url 127.0.0.1:8545: Connects to your local network.--tc BatchCallAndSponsorScript: Specifies the target contract for the script.
forge script ./script/BatchCallAndSponsor.s.sol --tc BatchCallAndSponsorScript --broadcast --rpc-url 127.0.0.1:8545