---
eip: 7819
label: EIP-7819
title: SETDELEGATE instruction
status: Draft
type: Standards Track
category: Core
created: 2024-11-18
requires: 7702
official: https://eips.ethereum.org/EIPS/eip-7819
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7819.md
canonical: https://eips.huatuo.xyz/zh/eip-7819/
lang: zh
tldr: 草案操作码：让合约在确定性地址上创建或更新 EIP-7702 风格的委托账户，是替代最小代理和可升级代理的协议层方案。
tags: [evm, opcode, proxies, clones, account-abstraction]
---

# EIP-7819: SETDELEGATE instruction

> Introduce a new instruction allowing contracts to create clones using EIP-7702 delegation designations

**TL;DR:** 草案操作码：让合约在确定性地址上创建或更新 EIP-7702 风格的委托账户，是替代最小代理和可升级代理的协议层方案。

## 它解决什么问题？

许多应用会一遍又一遍地部署相同的代码：智能账户钱包、金库、每个用户一份的合约。为了节省 gas，它们会部署小型代理：

- [ERC-1167](/eip-1167/) 克隆把实现地址硬编码。便宜，但永远无法升级。
- [ERC-1967](/eip-1967/) 之类的可升级代理从存储中读取实现地址。灵活，但每次调用都要为一次存储读取付费，而且代理体积更大（OpenZeppelin 的约 708 字节）。

两者都在 EVM 代码中转发调用：把 calldata 复制到内存，再调用 `DELEGATECALL`。[EIP-7702](/eip-7702/) 已经为 EOA 加入了协议原生的重定向：一个 23 字节的**委托标识**（`0xef0100 ++ address`）。EIP-7819 让合约能够为新地址创建同样的对象。

## 它如何运作

新增指令 `SETDELEGATE`，位于 `0xf6`：

1. 收取 `EMPTY_ACCOUNT_COST`（25,000 gas）；在静态调用中失败。
2. 弹出 `salt` 和 `target`。
3. 计算 `location = keccak256(0xef0100 ++ caller ++ salt)[12:]`，因此每一对调用者与 salt 始终映射到同一个地址。
4. 如果 `location` 上已有不是委托标识的代码，则失败。
5. 把 `0xef0100 ++ target` 写为 `location` 的代码。如果 `target` 为零，则改为清除代码。
6. 如果 nonce 为 0，则设为 1，这样账户永远不会被视为空账户。
7. 如果账户已经存在，则退还 `EMPTY_ACCOUNT_COST - BASE_COST`（12,500），并压入 `location`。

其结果的行为与 EIP-7702 委托账户完全相同：对 `location` 的调用会在 `location` 的存储上下文中运行 `target` 的代码。调用方工厂可以用同一个 salt 再次调用 `SETDELEGATE`，指向新的 target（升级）或零（移除）。是否允许这样做，取决于工厂自己的逻辑。55 字节的原像不会与 `CREATE` 或 `CREATE2` 地址冲突。

## 示例代码

针对该提议操作码编写的工厂合约。Solidity 目前还没有对应的内置支持，所以这只是示意：

```solidity
// 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
    }
}
```

## 需要注意

- 状态为 **Draft**；它依赖 [EIP-7702](/eip-7702/)，也没有排入任何分叉。
- gas 参数为简单起见直接复制自 EIP-7702；作者指出真实成本可能更低。
- 不会跟随委托链（与 7702 相同）。工厂应当检查 `target` 本身不是委托标识。
- 这类账户无法用 `SELFDESTRUCT` 移除；只有创建它的合约才能通过把 target 设为零来清除。
- 在同一笔交易内多次更改 target，意味着同一个地址在该交易内可以运行不同的代码，这很像可升级代理更改其实现槽。
- 动机包括 [ERC-4337](/eip-4337/) 智能账户，以及 [EIP-8141](/eip-8141/) 所隐含的账户部署。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-7819
