Introduction
Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.
I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.
I also build and publish a variety of tools, libraries, and open-source projects.
This article examines ERC-5008, which adds a function to ERC-721 NFTs for reading a nonce incremented when the NFT is transferred.
This explanation translates, summarizes, and expands on the following specification.
ERC-5008 was in Last Call status as of September 3, 2023.
Overview
The proposal extends ERC-721 with the concept of a nonce. Although nonces are commonly used in transaction sequencing, here the nonce tracks specific actions or changes associated with an NFT.
A nonce function is added to ERC-721 tokens. Each token has its own nonce, which increases when relevant token state changes. Querying it makes it possible to determine whether relevant actions occurred and reason about their sequence.
This extension supports more accurate tracking of token changes and transaction history, improving transparency and token management.
Motivation
NFT marketplaces may execute orders that sellers no longer intend to honor, potentially selling NFTs below their current market price. This can occur when a user transfers an NFT to another wallet and later returns it to the original wallet. An old order may become executable again.
A user places a marketplace order to sell an NFT, then transfers the NFT to another wallet and back. The original order may become active again, allowing it to be sold at an unexpectedly low price.
ERC-5008 addresses this problem by introducing a nonce property that changes when a token is transferred. A marketplace that binds orders to the nonce can validate the current value against the order and reject stale orders.
When an NFT owner transfers the token, its nonce changes. If an order includes the nonce, the marketplace can determine that the relevant token state has changed and prevent unintended execution at an outdated price.
The proposal provides a mechanism intended to reduce unintended trades and improve safety for NFT users and developers.
Specification
/// @dev the ERC-165 identifier for this interface is 0xce03fdab.
interface IERC5008 /* is IERC165 */ {
/// @notice Emitted when the `nonce` of an NFT is changed
event NonceChanged(uint256 tokenId, uint256 nonce);
/// @notice Get the nonce of an NFT
/// Throws if `tokenId` is not a valid NFT
/// @param tokenId The id of the NFT
/// @return The nonce of the NFT
function nonce(uint256 tokenId) external view returns(uint256);
}The nonce(uint256 tokenId) function is read-only and must return the nonce associated with the specified token ID without modifying other data.
The supportsInterface function must return true when called with interface ID 0xce03fdab. This enables external contracts and applications to determine whether the token contract supports ERC-5008.
Additional Considerations
The proposal initially considered the function name transferCount. However, an NFT may undergo changes besides transfers, such as an owner's modification of an important token attribute. In such cases, the nonce may also need to change.
The more general name nonce was chosen to cover these situations rather than limiting the concept to transfers. Implementations can use nonce changes to protect operations affected by relevant attribute changes as well.
Backward Compatibility
The extension is compatible with ERC-721.
Tests
The test code is available at:
Source: https://eips.ethereum.org/EIPS/eip-5008
Reference Implementation
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "./IERC5008.sol";
contract ERC5008 is ERC721, IERC5008 {
mapping(uint256 => uint256) private _tokenNonce;
constructor(string memory name_, string memory symbol_)ERC721(name_, symbol_){
}
/// @notice Get the nonce of an NFT
/// Throws if `tokenId` is not a valid NFT
/// @param tokenId The NFT to get the nonce for
/// @return The nonce of this NFT
function nonce(uint256 tokenId) public virtual override view returns(uint256) {
require(_exists(tokenId), "Error: query for nonexistent token");
return _tokenNonce[tokenId];
}
function _beforeTokenTransfer(
address from,
address to,
uint256 tokenId
) internal virtual override{
super._beforeTokenTransfer(from, to, tokenId);
_tokenNonce[tokenId]++;
emit NonceChanged(tokenId, _tokenNonce[tokenId]);
}
/// @dev See {IERC165-supportsInterface}.
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return interfaceId == type(IERC5008).interfaceId || super.supportsInterface(interfaceId);
}
}Citation
Anders (@0xanders), Lance (@LanceSnow), Shrug shrug@emojidao.org, "ERC-5008: ERC-721 Nonce Extension [DRAFT]," Ethereum Improvement Proposals, no. 5008, April 2022. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-5008.
Conclusion
ERC-5008 adds a per-token nonce to ERC-721 NFTs, enabling applications to detect relevant state changes and invalidate stale operations.
Further reading and author links:


