ERC-4906 · Final · ERC · 2022-03-13
ERC-4906: EIP-721 Metadata Update Extension
Adds MetadataUpdate and BatchMetadataUpdate events to ERC-721, so marketplaces and indexers know exactly when to refresh an NFT's metadata instead of polling or guessing.
What problem does it solve?
Many NFTs change after mint: game items level up, reveals swap placeholder art for the real image, and dynamic NFTs react to on-chain state. Marketplaces and indexers cache each token's metadata, so they need to know when to fetch it again.
Before ERC-4906, each collection signaled changes with its own custom event, or did not signal them at all. Platforms had to write a separate integration per collection, or offer a manual "refresh metadata" button. ERC-4906 standardizes one event that everyone can listen for.
How it works
It is an optional ERC-721 extension that adds two events and no functions:
event MetadataUpdate(uint256 _tokenId);
event BatchMetadataUpdate(uint256 _fromTokenId, uint256 _toTokenId);
- Emit
MetadataUpdatewhen the JSON metadata of one token changes. - Emit
BatchMetadataUpdatewhen a consecutive range of token IDs changes, for example a whole-collection reveal. - Emitting them is not recommended on mint or burn, or when
tokenURIchanges but the JSON behind it is the same. supportsInterface(0x49064906)must returntrue(ERC-165).
The events deliberately carry no new metadata values. When a listener sees one, it calls tokenURI(tokenId) again and re-downloads the JSON. For a batch event it does this for each token in the range.
Sample code
A reveal-style collection that emits the events (OpenZeppelin 5 ships IERC4906):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {IERC4906} from "@openzeppelin/contracts/interfaces/IERC4906.sol";
contract Revealable is ERC721, Ownable, IERC4906 {
string private base;
uint256 public minted;
constructor() ERC721("Reveal", "RVL") Ownable(msg.sender) {}
function reveal(string calldata newBase) external onlyOwner {
base = newBase;
if (minted > 0) emit BatchMetadataUpdate(0, minted - 1);
}
function _baseURI() internal view override returns (string memory) { return base; }
function supportsInterface(bytes4 id) public view override(ERC721, IERC165) returns (bool) {
return id == bytes4(0x49064906) || super.supportsInterface(id);
}
}
Listening as an indexer (ethers v6):
nft.on("MetadataUpdate", async (tokenId) => refresh(await nft.tokenURI(tokenId)));
nft.on("BatchMetadataUpdate", async (from, to) => {
for (let id = from; id <= to; id++) refresh(await nft.tokenURI(id));
});
Things to know
- Status: Final and widely supported. OpenZeppelin's
ERC721URIStorageemitsMetadataUpdatefrom_setTokenURI. - If metadata changes off-chain (for example, a mutable server URL), the contract never knows. You can add an access-controlled function that only emits the event. Make sure only authorized parties can call it, or anyone could spam refreshes.
- For very large ranges, consumers may throttle or ignore huge batches. Emit the narrowest range that actually changed.
- For on-chain, typed traits rather than whole-JSON refreshes, see ERC-7496 (Dynamic Traits, Draft).