---
eip: 5185
label: ERC-5185
title: NFT Updatable Metadata Extension
status: Stagnant
type: Standards Track
category: ERC
created: 2022-06-27
requires: 721, 1155
official: https://eips.ethereum.org/EIPS/eip-5185
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-5185.md
canonical: https://eips.huatuo.xyz/zh/eip-5185/
lang: zh
tldr: 让 NFT 元数据只能通过原始元数据中预先声明的配方（recipe）演进，每批更新由链上事件公布，使任何人都能确定性地重新计算并验证代币的当前元数据。
tags: [nft, metadata, gaming, json]
---

# ERC-5185: NFT Updatable Metadata Extension

> An interface extension for ERC-721/ERC-1155 controlled metadata updates

**TL;DR:** 让 NFT 元数据只能通过原始元数据中预先声明的配方（recipe）演进，每批更新由链上事件公布，使任何人都能确定性地重新计算并验证代币的当前元数据。

## 它解决什么问题？

NFT 元数据通常放在 `tokenURI` 上。如果该 URI 指向不可变的文件（例如 IPFS），就永远无法改变任何内容；如果指向服务器，发行方则可以随时更改任何内容。许多用例介于两者之间：等级会上升但外观固定的游戏角色，或者所有者历史只能追加的房产权属证明。

ERC-5185 增加了一个折中方案：原始元数据声明允许哪些变换，更新则作为可验证的、有序的日志来应用。

## 它如何运作

1. **原始元数据**（来自 [ERC-721](/eip-721/) 的 `tokenURI` 或 [ERC-1155](/eip-1155/) 的 `uri`）增加一个 `updatable` 对象，包含：
   - `engine`：变换语言及其版本。目前定义的只有 `jsonata@1.8.*`。
   - `recipes`：命名的公式（`eval` 字符串），用于变换上一版 JSON。
   - 可选的 `schema`：每个更新后的版本都必须满足的 JSON Schema。
2. 合约触发 `MetadataUpdates(string URI)`。该 URI 指向一个列出各项更新的 JSON 文件，每项更新包含 `tokenId`、`recipeKey` 和可选的 `args`。一个事件可以批量包含针对许多代币的数千项更新。
3. 要获得当前元数据，客户端从原始元数据开始，按事件顺序应用该代币的所有更新：`next = jsonata(eval).evaluate(prev, args)`。
   - 配方键未知、求值出错或违反 schema：该更新被忽略，保留 `prev`。

由于该过程是确定性的，市场可以缓存中间结果，只应用新的事件。gas 成本只是每批一个事件。

## 示例代码

合约端：

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

interface IERC5185UpdatableMetadata {
    event MetadataUpdates(string URI);
}

contract Monsters is IERC5185UpdatableMetadata /*, ERC721 */ {
    address public gameMaster;
    function publishUpdates(string calldata uri) external {
        require(msg.sender == gameMaster, "only GM");
        emit MetadataUpdates(uri); // e.g. "ipfs://<cid>/updates-0042.json"
    }
}
```

带配方的原始元数据，以及一个更新文件（字段名遵循规范中的 schema）：

```json
{
  "name": "Monster 1",
  "attributes": [{ "trait_type": "Level", "value": 0 }],
  "updatable": {
    "engine": "jsonata@1.8.*",
    "recipes": {
      "levelUp": { "eval": "$ ~> | attributes[trait_type='Level'] | {'value': value + 1} |" }
    }
  }
}
```

```json
{ "updates": [ { "tokenId": "1", "recipeKey": "levelUp" }, { "tokenId": "1", "recipeKey": "levelUp" } ] }
```

客户端重放（TypeScript，使用 `jsonata` 包）：

```ts
import jsonata from "jsonata";

async function current(original: any, updates: { tokenId: string; recipeKey: string; args?: any }[], id: string) {
  let meta = original;
  for (const u of updates.filter((u) => u.tokenId === id)) {
    const recipe = original.updatable?.recipes?.[u.recipeKey];
    if (!recipe) continue;
    try { meta = await jsonata(recipe.eval).evaluate(meta, u.args); } catch { /* keep prev */ }
  }
  return meta;
}
```

## 需要注意

- 状态为 **Stagnant**：该提案没有进展，不应假定市场会支持。
- 规范自相矛盾：其 schema 把该字段称为 `recipeKey`（`args` 为字符串），而示例使用的是 `action`，并把 `args` 作为对象传入。请选定一种并写入文档。
- 配方会在第三方基础设施上运行任意 JSONata；规范警告这可能成为 DoS 攻击向量，因此求值方应使用沙箱并限制处理量。
- 如果合约之后更改了 `tokenURI`，"原始"元数据也会随之改变，之前的更新可能不再能干净地应用。
- 如果只是想发出"元数据已更改，请刷新"的信号，被广泛支持的选择是 [ERC-4906](/eip-4906/)。


---

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