---
eip: 5008
label: ERC-5008
title: ERC-721 Nonce Extension
status: Last Call
type: Standards Track
category: ERC
created: 2022-04-10
requires: 165, 721
official: https://eips.ethereum.org/EIPS/eip-5008
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-5008.md
canonical: https://eips.huatuo.xyz/zh/eip-5008/
lang: zh
tldr: 为 ERC-721 增加每个代币独立的 `nonce`，每次转移时都会变化，使市场订单可以绑定某个 nonce，这样即使 NFT 离开钱包后又回来，过期的挂单也无法重新生效。
tags: [nft, erc-721, marketplace, orders, security]
---

# ERC-5008: ERC-721 Nonce Extension

> Add a `nonce` function to ERC-721.

**TL;DR:** 为 ERC-721 增加每个代币独立的 `nonce`，每次转移时都会变化，使市场订单可以绑定某个 nonce，这样即使 NFT 离开钱包后又回来，过期的挂单也无法重新生效。

## 它解决什么问题？

NFT 市场通常使用链下签名订单："我愿以 X 的价格出售代币 #42"。如果所有者之后把 NFT 转到另一个钱包，该订单就会失效，而他们可能忘了这件事。当 NFT 回到原来的钱包时，旧订单又变得有效，价格可能远低于当前市价。攻击者已经利用过这类被遗忘的挂单。

仅凭 ERC-721，市场很难判断代币自订单签署以来是否发生过转移。

## 它如何运作

代币合约公开 `nonce(tokenId)`，这个数字在代币每次转移时都会变化（在其他重要更新时也可以变化）。变化时会触发 `NonceChanged(tokenId, nonce)` 事件。

市场把当前的 nonce 放进签名订单。成交时，它将订单中的 nonce 与 `nonce(tokenId)` 比较。如果代币在任何时候移动过，即使又回到了同一个所有者手中，nonce 也不同，订单就会被拒绝。

该接口可通过 [ERC-165](/eip-165/) 发现，ID 为 `0xce03fdab`。之所以选用 `nonce` 而不是 `transferCount` 这个名字，是因为实现可能出于转移以外的原因递增它。

## 示例代码

接口，以及基于 OpenZeppelin ERC-721（v5）的最小实现：

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

import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";

interface IERC5008 {
    event NonceChanged(uint256 tokenId, uint256 nonce);
    function nonce(uint256 tokenId) external view returns (uint256);
}

contract NoncedNFT is ERC721, IERC5008 {
    mapping(uint256 => uint256) private _nonces;

    constructor() ERC721("Nonced", "NNC") {}

    function nonce(uint256 tokenId) external view returns (uint256) {
        _requireOwned(tokenId);
        return _nonces[tokenId];
    }

    function _update(address to, uint256 tokenId, address auth)
        internal override returns (address from)
    {
        from = super._update(to, tokenId, auth);
        emit NonceChanged(tokenId, ++_nonces[tokenId]);
    }

    function supportsInterface(bytes4 id) public view override returns (bool) {
        return id == type(IERC5008).interfaceId || super.supportsInterface(id);
    }
}
```

## 需要注意

- 状态：**Last Call**（其 Last Call 截止日期已于 2023 年 8 月过去），尚未 Final。
- 只有当市场真的把 nonce 放进订单并在成交时检查，它才有用。
- `nonce` 必须是 `view` 函数，并且对不存在的代币必须 revert。
- 所有者仍应取消不再需要的挂单；nonce 是安全网，不能替代取消操作。


---

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