This repository demonstrates EIP-7702: EOA Code Setting, a significant Ethereum upgrade introduced in Pectra upgrade.
EIP-7702 allows Externally Owned Accounts (EOAs) to have code through a delegation mechanism. This brings smart contract capabilities to EOAs without requiring users to migrate to new accounts.
- Set delegation code on EOAs
- Execute contract code in EOA context
- Maintain EOA transaction signing ability
- Support for cross-chain authorizations
- Transaction batching
- Gas sponsorship
- Granular permissions
- Account abstraction features
Our repository includes a complete example of gas sponsorship using EIP-7702:
// Alice authorizes Sponsor contract
bytes memory code = abi.encodePacked(hex"ef0100", address(sponsor));
vm.etch(alice, code); // Sets delegation code
// Execute sponsored transfer where:
// 1. Relayer pays gas
// 2. Value comes from Alice
// 3. Bob receives the transfer
vm.prank(alice);
address(alice).call{value: 1 ether}(
abi.encodeWithSelector(Sponsor.sponsoredTransfer.selector, bob)
);
Party | Pays |
---|---|
Alice | Transfer value (e.g., 1 ETH) |
Relayer | Gas fees |
Bob | Nothing (recipient) |
bytes memory delegationCode = abi.encodePacked(
hex"ef0100", // EIP-7702 magic bytes
address(contract) // Target contract address
);
// Create a sponsor contract
Sponsor sponsor = new Sponsor();
// Set delegation on EOA (only works in Prague+)
bytes memory code = abi.encodePacked(hex"ef0100", address(sponsor));
vm.etch(eoa, code); // Sets delegation code
// Any call to EOA is delegated to sponsor contract
// but executes in EOA's context
The repository includes tests that verify EIP-7702 functionality across different EVM versions:
# Should pass - Prague supports EIP-7702
FOUNDRY_PROFILE=prague forge test
# Alternatively
pnpm test
# Should fail - Shanghai doesn't support EIP-7702
FOUNDRY_PROFILE=shanghai forge test
# Alternatively
pnpm test:shanghai
src/Sponsor.sol
: Example contract for gas sponsorshipsrc/EIP7702Demonstrator.sol
: Helper contract for demonstrationstest/SponsoredTransferTest.t.sol
: Test suite showing sponsored transferstest/EIP7702Support.t.sol
: Test suite for EVM version compatibility
- Foundry
v1.0
(learn more here)
forge install
# Build
forge build
# Test with specific EVM version
FOUNDRY_PROFILE=prague forge test -vvv
First, start a local Anvil node:
anvil
Then in a new terminal, deploy using one of the following commands:
Deploy with default (Prague) settings:
forge script script/Deploy.s.sol --broadcast --fork-url http://localhost:8545
Deploy with Prague profile explicitly:
FOUNDRY_PROFILE=prague forge script script/Deploy.s.sol --broadcast --fork-url http://localhost:8545
Deploy with Shanghai profile:
FOUNDRY_PROFILE=shanghai forge script script/Deploy.s.sol --broadcast --fork-url http://localhost:8545
For these scripts to work, you need to have a MNEMONIC
environment variable set to a valid
BIP39 mnemonic.
For instructions on how to deploy to a testnet or mainnet, check out the Solidity Scripting tutorial.
Feature | Prague | Shanghai |
---|---|---|
Set EOA Code | ✅ | ❌ |
Code Size | 23 bytes | 0 bytes |
Delegation | Supported | Not Supported |
Gas Sponsorship | ✅ | ❌ |
=== EIP-7702 Sponsored Transfer Details ===
Delegation code size: 23
Gas used: 68690
Value transferred: 1000000000000000000
Sender (Alice) balance change: 1000000000000000000
Recipient (Bob) balance change: 1000000000000000000
Feel free to reach out to Julien through:
- Element: @julienbrg:matrix.org
- Farcaster: julien-
- Telegram: @julienbrg
- Twitter: @julienbrg
- Discord: julienbrg
- LinkedIn: julienberanger