EIP-7819 · Draft · Core · 2024-11-18
EIP-7819: SETDELEGATE instruction
Draft opcode that lets a contract create or update EIP-7702 style delegation accounts at deterministic addresses, a protocol-level alternative to minimal proxies and upgradeable proxies.
What problem does it solve?
Many apps deploy the same code over and over: smart-account wallets, vaults, per-user contracts. To save gas they deploy small proxies:
- ERC-1167 clones hard-code the implementation address. Cheap, but never upgradable.
- Upgradeable proxies such as ERC-1967 read the implementation from storage. Flexible, but every call pays for a storage read and the proxy is larger (~708 bytes for OpenZeppelin's).
Both forward calls in EVM code, copying calldata to memory and calling DELEGATECALL. EIP-7702 already added a protocol-native redirect for EOAs: a 23-byte delegation indicator (0xef0100 ++ address). EIP-7819 lets contracts create the same object for new addresses.
How it works
A new instruction SETDELEGATE at 0xf6:
- Charges
EMPTY_ACCOUNT_COST(25,000 gas); fails in a static call. - Pops
saltandtarget. - Computes
location = keccak256(0xef0100 ++ caller ++ salt)[12:], so each caller and salt pair always maps to the same address. - Fails if
locationalready has code that is not a delegation indicator. - Writes
0xef0100 ++ targetas the code atlocation. Iftargetis zero, the code is cleared instead. - Sets the nonce to 1 if it was 0, so the account never counts as empty.
- Refunds
EMPTY_ACCOUNT_COST - BASE_COST(12,500) if the account already existed, and pusheslocation.
The result behaves exactly like an EIP-7702 delegated account: calls to location run target's code in location's storage context. The calling factory can call SETDELEGATE again with the same salt to point to a new target (upgrade) or to zero (remove). Whether that is allowed is up to the factory's own logic. The 55-byte preimage cannot collide with CREATE or CREATE2 addresses.
Sample code
A factory written against the proposed opcode. Solidity has no built-in for it yet, so this is illustrative:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract AccountFactory {
address public owner = msg.sender;
mapping(bytes32 => bool) public locked;
function deploy(bytes32 salt, address impl, bytes calldata init) external returns (address acct) {
// acct = setdelegate(salt, impl) // hypothetical opcode 0xf6
// Initialise atomically in the same call to avoid front-running:
// (bool ok,) = acct.call(init); require(ok);
}
function upgrade(bytes32 salt, address newImpl) external {
require(msg.sender == owner && !locked[salt], "not allowed");
// setdelegate(salt, newImpl) // same salt => same address, new target
}
function lock(bytes32 salt) external {
require(msg.sender == owner);
locked[salt] = true; // factory-level immutability
}
}
Things to know
- Status is Draft; it requires EIP-7702 and is not scheduled for a fork.
- Gas parameters are copied from EIP-7702 for simplicity; the authors note the real cost could be lower.
- Delegation chains are not followed (as in 7702). A factory should check that
targetis not itself a delegation indicator. - These accounts cannot be removed with
SELFDESTRUCT; only the creating contract can clear them via a zero target. - Changing the target several times inside one transaction means the same address can run different code within that transaction, much like an upgradeable proxy changing its implementation slot.
- Motivation includes ERC-4337 smart accounts and the account deployments implied by EIP-8141.