---
eip: 7695
label: ERC-7695
title: Ownership Delegation and Context for ERC-721
status: Draft
type: Standards Track
category: ERC
created: 2024-04-02
requires: 165, 721
official: https://eips.ethereum.org/EIPS/eip-7695
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7695.md
canonical: https://eips.huatuo.xyz/zh/eip-7695/
lang: zh
tldr: "一个 ERC-721 扩展，让 NFT 无需所有者转移，就能在不同应用的\"上下文\"中被使用、锁定甚至临时委托，使质押、租赁和借贷不再让所有者失去持有者权益。"
tags: [nft, erc721, delegation, rental, staking]
---

# ERC-7695: Ownership Delegation and Context for ERC-721

> Introduces contexts and ownership delegation for ERC-721 tokens, expanding dApps and financial use cases without transferring ownership

**TL;DR:** 一个 ERC-721 扩展，让 NFT 无需所有者转移，就能在不同应用的"上下文"中被使用、锁定甚至临时委托，使质押、租赁和借贷不再让所有者失去持有者权益。

## 它解决什么问题？

如今要对 ERC-721 NFT 进行质押、出租或抵押借款，通常需要把它转入协议合约。这样 NFT 就不再留在你的钱包里，其他奖励持有者的应用（空投、分级福利）就不再认得你，除非它们集成了所有的质押合约。

ERC-7695 让 NFT 留在所有者手中，转而在 [ERC-721](/eip-721/) 之上增加两个概念：**上下文（context）**，应用可以在其中指定使用者并锁定代币；以及**所有权委托**，所有者临时把所有权的*权利*交给别人。

## 它如何运作

**上下文。**任何人都可以调用 `createContext(controller, detachingDuration, ctxMsg)`，它会返回一个 `ctxHash`。每个上下文包含：

- 一个 **Controller**，可以设置该上下文的 **User**（`setContextUser`），并在该上下文中锁定或解锁代币（`setContextLock`）；
- 一个 `detachingDuration`，上限为合约的 `maxDetachingDuration()`。

所有者通过 `attachContext` 附加代币。要离开时调用 `requestDetachContext`：如果代币未锁定则立即分离；如果已锁定，则必须等待 `detachingDuration`，然后调用 `execDetachContext`。Controller 无法阻止这一过程，因此丢失或恶意的 Controller 无法把代币永远困住。作为合约的 Controller 会收到回调（`onAttached`、`onDetachRequested`、`onExecDetachContext`）；只有 `onAttached` 可以拒绝。

**所有权委托。**所有者调用 `startDelegateOwnership(tokenId, delegatee, until)`；被委托方通过 `acceptOwnershipDelegation` 确认。到期之前，被委托方是**所有权管理者**：可以转移代币，并附加/分离上下文。所有权在 `until` 自动恢复，或在被委托方调用 `stopOwnershipDelegation` 时提前恢复。

**转移**会撤销所有委托并分离所有上下文；如果某个已锁定的上下文尚未完成分离等待，转移会 revert。

接口 ID 为 `0x486b6fba`；可选的 enumerable 扩展为 `0xcebf44b7`，Controller 回调接口为 `0xad0491f1`。

## 示例代码

作为上下文 Controller 的租赁应用（ethers v6）：

```ts
import { Contract } from "ethers";

const nft = new Contract(nftAddress, [
  "function createContext(address,uint64,bytes) returns (bytes32)",
  "function attachContext(bytes32,uint256,bytes)",
  "function setContextUser(bytes32,uint256,address)",
  "function setContextLock(bytes32,uint256,bool)",
  "function getContextUser(bytes32,uint256) view returns (address)",
], signer);

// Owner attaches token 42 to the rental app's context
await (await nft.connect(owner).attachContext(ctxHash, 42n, "0x")).wait();

// The app (controller) locks it and assigns the renter as user
await (await nft.connect(controller).setContextLock(ctxHash, 42n, true)).wait();
await (await nft.connect(controller).setContextUser(ctxHash, 42n, renter)).wait();

console.log(await nft.getContextUser(ctxHash, 42n)); // renter
```

## 需要注意

- 状态为 **Draft**。
- 如果只是租赁场景，有更简单的标准：[ERC-4907](/eip-4907/) 和 [ERC-5006](/eip-5006/) 增加了限时的"user"角色。
- **钓鱼风险：**诱骗所有者调用 `startDelegateOwnership` 与恶意的 `setApprovalForAll` 一样危险，因为被委托方可以转移代币。
- 基于委托构建的系统应要求比 `maxDetachingDuration` 更长的委托期，否则所有者可能在另一个上下文中锁定代币，使其在委托结束前无法移动。
- Controller 的回调函数应检查 `msg.sender` 是否为预期的代币合约。
- 同一应用中的两个上下文可能导致同一代币被重复使用；Controller 之间应当协调。


---

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