ERC-5646 · Final · ERC · 2022-09-11
ERC-5646: Token State Fingerprint
Adds getStateFingerprint(tokenId) so protocols can detect when a mutable NFT's state has changed, without understanding how that token stores its state.
What problem does it solve?
Many NFTs are not static pictures. An LP position NFT holds amounts that change, a bond NFT matures, a game item gains stats. If a marketplace or lending protocol makes an offer for "token #42", the owner could change the token's state (say, withdraw the liquidity) right before accepting the offer, and the counterparty gets something worth much less.
To protect against that, a protocol needs an identifier for "token #42 in this exact state". Until now, it had to understand each token's internal fields and hash them itself, which means custom integration work for every new token type.
ERC-5646 moves that job into the token: the token itself publishes a fingerprint of its current state.
How it works
The token contract implements one view function:
function getStateFingerprint(uint256 tokenId) external view returns (bytes32);
The rules:
- The value must change whenever the token's state changes, and must not change if the state stays the same.
- It must cover every property that can change during the token's life. Immutable properties can be left out, because the token id already identifies them.
- It may include computed values (for example whether the token has expired, based on the current timestamp) and the metadata URI.
supportsInterface(0xf5112315)must returntrue(ERC-165).
A protocol stores the fingerprint when an offer is made and checks it again at execution. If they differ, the token changed in between and the trade is rejected.
Sample code
A token computing its fingerprint (adapted from the spec's reference implementation):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IERC5646 {
function getStateFingerprint(uint256 tokenId) external view returns (bytes32);
}
contract LPPosition is IERC5646 {
struct State {
address asset1;
address asset2;
uint256 amount1;
uint256 amount2;
uint256 fee; // immutable -> not in fingerprint
uint256 expiration; // fingerprint uses "expired or not"
}
mapping(uint256 => State) internal states;
function getStateFingerprint(uint256 tokenId) public view returns (bytes32) {
State storage s = states[tokenId];
return keccak256(abi.encode(
s.asset1, s.asset2, s.amount1, s.amount2,
block.timestamp >= s.expiration
));
}
function supportsInterface(bytes4 id) public pure returns (bool) {
return id == 0x01ffc9a7 || id == 0xf5112315;
}
}
A marketplace guarding an offer against state changes:
struct Offer { address collection; uint256 tokenId; bytes32 fingerprint; uint256 price; }
function accept(Offer calldata o) external {
require(
IERC5646(o.collection).getStateFingerprint(o.tokenId) == o.fingerprint,
"token state changed"
);
// ... transfer NFT and payment
}
Things to know
- Status: Final.
- Fingerprints are only meaningful within one contract. Two different contracts can produce the same value, so always compare them together with the collection address.
- The safety of the scheme depends entirely on the token implementation. If a mutable field is left out of the hash, the owner can change it without changing the fingerprint.
- It does not help when a protocol actually needs to read specific state properties (for example to value the token); it only tells you "same or different".