EIPs Explained

ERC-7496 · Draft · ERC · 2023-07-28

ERC-7496: NFT Dynamic Traits

Adds a generic on-chain key-value trait store to ERC-721 and ERC-1155 tokens, so traits like 'redeemed' or 'points' can change over time and marketplaces can check them at sale time to stop front-running.

What problem does it solve?

NFT traits normally live in off-chain JSON metadata. Contracts cannot read or change them, and that creates a real attack: a seller lists an NFT that still has an unused redeemable benefit, then redeems it just before the buyer's purchase executes. The buyer pays for something that is gone.

ERC-7496 puts changeable traits on-chain behind a standard interface. Other contracts, especially marketplaces, can read the current values and refuse a sale if an important trait changed after the offer was made.

How it works

Each trait is identified by a bytes32 traitKey (recommended: keccak256 of the trait name) and holds a bytes32 value:

  • getTraitValue(tokenId, traitKey) / getTraitValues(tokenId, keys[]) read values. An unknown key reverts with TraitDoesNotExist; a known key that is unset returns bytes32(0).
  • setTrait(tokenId, traitKey, newValue) writes a value. It must be permissioned, except for traits marked tokenOwnerCanUpdateValue, which the token owner can set.
  • Every change emits one of the TraitUpdated* events (single token, range, or list, with "uniform value" variants for bulk updates). Values must change only through explicit actions, never implicitly with time, so indexers can follow events.
  • getTraitMetadataURI() returns a JSON document describing every trait: display name, data type (string, decimal, boolean, epochSeconds), optional valueMappings from raw bytes32 to labels, and validateOnSale.

validateOnSale tells marketplaces how to protect buyers: requireEq, requireNeq, or numeric comparisons such as requireUintGte between the value at offer time and at fulfilment. If the check fails, the marketplace must not complete the sale.

Contracts advertise support via ERC-165 interface id 0xaf332f3e. On-chain trait values override conflicting values from the ERC-721/1155 metadata URI.

Sample code

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

interface IERC7496 {
    event TraitUpdated(bytes32 indexed traitKey, uint256 tokenId, bytes32 traitValue);
    event TraitMetadataURIUpdated();
    function getTraitValue(uint256 tokenId, bytes32 traitKey) external view returns (bytes32);
    function getTraitValues(uint256 tokenId, bytes32[] calldata traitKeys) external view returns (bytes32[] memory);
    function getTraitMetadataURI() external view returns (string memory);
    function setTrait(uint256 tokenId, bytes32 traitKey, bytes32 newValue) external;
}

// Marketplace-side check for a trait marked "validateOnSale": "requireEq"
function _checkRedeemed(IERC7496 nft, uint256 id, bytes32 valueAtOffer) view {
    bytes32 key = keccak256("redeemed");
    require(nft.getTraitValue(id, key) == valueAtOffer, "trait changed");
}

A trait definition in the metadata JSON:

{
  "traits": {
    "redeemed": {
      "displayName": "Redeemed",
      "dataType": { "type": "boolean" },
      "validateOnSale": "requireEq"
    }
  }
}

Things to know

  • Status is Draft.
  • Booleans default to bytes32(0) for false and bytes32(uint256(1)) for true unless valueMappings says otherwise.
  • The spec warns buyers and sellers not to rely solely on marketplaces: check current trait values yourself before trading.
  • Use the UniformValue events for bulk updates so indexers do not have to query every token.

#nft #traits #metadata #marketplace #erc721

Details

Authors
Adam Montgomery (@montasaurus), Ryan Ghods (@ryanio), 0age (@0age), James Wenzel (@emo-eth), Stephan Min (@stephankmin)
Status
Draft
Type
Standards Track / ERC
Created
2023-07-28
Requires

Machine-readable: Markdown · JSON