---
eip: 7085
label: ERC-7085
title: NFT Relationship Enhancement
status: Draft
type: Standards Track
category: ERC
created: 2023-05-02
requires: 721, 1155
official: https://eips.ethereum.org/EIPS/eip-7085
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7085.md
canonical: https://eips.huatuo.xyz/zh/eip-7085/
lang: zh
tldr: "一份草案扩展，让 NFT 合约可以记录代币之间带权重的\"原作到衍生\"链接，以及每个代币上具名的数值属性，把一个系列变成可查询的图。"
tags: [nft, erc721, erc1155, metadata, graph]
---

# ERC-7085: NFT Relationship Enhancement

> Establish relationships between NFTs and setting quantifiable attributes for those relationships.

**TL;DR:** 一份草案扩展，让 NFT 合约可以记录代币之间带权重的"原作到衍生"链接，以及每个代币上具名的数值属性，把一个系列变成可查询的图。

## 它解决什么问题？

[ERC-721](/eip-721/) 和 [ERC-1155](/eip-1155/) 把每个代币都视为孤立的。链上没有办法表达"代币 7 是代币 3 的衍生品，权重为 50"或"代币 9 的 `power = 12`"。需要二次创作、家谱、合成配方或游戏数值的项目只能自行设计存储和函数，因此工具无法通用地读取它们。

ERC-7085 为这些**关系**和**可量化属性**提出了一个小型通用接口，使一组 NFT 可以被当作图来处理。

## 它如何运作

NFT 合约中增加了五个函数：

| 函数 | 用途 |
| --- | --- |
| `setRelationship(originalID, derivativeID, attribute)` | 用一个数值把衍生品链接到原作（默认为 1） |
| `getRelationship(originalID, derivativeID)` | 读取该值（`uint256`） |
| `setAttribute(id, name, value)` | 为代币设置一个具名数值属性 |
| `getAttribute(id, name)` | 读取属性（以 `bytes32` 返回） |
| `getAttributeNames(id)` | 列出代币的属性名称 |

规范中的设计选择：任意一对代币之间只能存在**一个**关系，这让图保持简单；属性每次设置一个，以降低每次调用的成本。该接口（`INFTGraph`）可通过 [ERC-165](/eip-165/) 发现。

谁可以设置关系或属性并未定义。规范只是警告实现者必须添加访问控制，并验证这些关系已获得相关方的授权。

## 示例代码

接口的一个最小可编译实现，只有所有者可以写入：

```solidity
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;

contract NFTGraph {
    address public immutable admin = msg.sender;

    mapping(uint256 => mapping(uint256 => uint256)) private _rel;
    mapping(uint256 => mapping(bytes32 => bytes32)) private _attr;
    mapping(uint256 => bytes32[]) private _names;

    modifier onlyAdmin() { require(msg.sender == admin, "not admin"); _; }

    function setRelationship(uint256 original, uint256 derivative, uint256 value) external onlyAdmin {
        require(original != derivative, "same token");
        _rel[original][derivative] = value == 0 ? 1 : value;
    }

    function getRelationship(uint256 original, uint256 derivative) external view returns (uint256) {
        return _rel[original][derivative];
    }

    function setAttribute(uint256 id, string calldata name, uint256 value) external onlyAdmin {
        bytes32 key = keccak256(bytes(name));
        if (_attr[id][key] == 0) _names[id].push(key);
        _attr[id][key] = bytes32(value);
    }

    function getAttribute(uint256 id, string calldata name) external view returns (bytes32) {
        return _attr[id][keccak256(bytes(name))];
    }

    function getAttributeNames(uint256 id) external view returns (bytes32[] memory) {
        return _names[id]; // hashed keys in this sketch
    }
}
```

## 需要注意

- 状态为 **Draft**，且文本内部不一致：`setAttribute` 接收 `uint256` 值，而 `getAttribute` 返回 `bytes32`，`getAttributeNames` 为字符串名称返回 `bytes32[]`，参考合约按原样无法编译。预计各实现会有差异。
- 除 `type(INFTGraph).interfaceId` 之外，规范没有固定 ERC-165 接口 ID，而它取决于最终的确切签名。
- 关系值只是数字；其含义（版税份额、相似度、数量）由各应用自行决定。
- 如果没有严格的访问控制，任何人都可以把不相关的代币链接起来或覆盖属性，这也是规范列出的主要安全问题。


---

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