EIPs Explained

ERC-6047 · Stagnant · ERC · 2022-11-26

ERC-6047: ERC-721 Balance indexing via Transfer event

Closes an ERC-721 loophole by requiring NFT contracts to emit Transfer events even for tokens minted inside the constructor, so indexers can track every token from logs alone.

What problem does it solve?

ERC-721 says a Transfer event must be emitted for every mint (from 0x0), transfer and burn (to 0x0), except during contract creation. A contract can therefore mint tokens in its constructor without logging them.

Indexers, marketplaces and wallets typically reconstruct who owns what by replaying Transfer logs. Tokens minted silently at deployment are invisible to them unless they add special-case logic. ERC-1155 has no such exception, which is why its supply is easier to index.

ERC-6047 removes the exception for contracts that opt in.

How it works

The specification is two rules:

  1. A compliant contract must implement ERC-721.
  2. It must emit Transfer whenever a token is transferred, minted or burned, including during contract creation.

No new event is introduced. Reusing the existing Transfer(address indexed from, address indexed to, uint256 indexed tokenId) means existing indexers work without changes.

Every ERC-6047 contract is a valid ERC-721 contract; the reverse is not true.

Sample code

Minting in the constructor while still logging each token:

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

contract GenesisNFT {
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);

    mapping(uint256 => address) public ownerOf;
    mapping(address => uint256) public balanceOf;

    constructor(address[] memory recipients) {
        for (uint256 id = 0; id < recipients.length; id++) {
            ownerOf[id] = recipients[id];
            balanceOf[recipients[id]] += 1;
            emit Transfer(address(0), recipients[id], id); // required by ERC-6047
        }
    }
    // ... rest of ERC-721 omitted
}

Rebuilding ownership purely from logs (ethers v6):

import { Contract, JsonRpcProvider } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const nft = new Contract(process.env.NFT!, [
  "event Transfer(address indexed from, address indexed to, uint256 indexed tokenId)",
], provider);

const owners = new Map<bigint, string>();
for (const log of await nft.queryFilter(nft.filters.Transfer(), Number(process.env.DEPLOY_BLOCK))) {
  const { to, tokenId } = (log as any).args;
  owners.set(tokenId, to);
}

Things to know

  • Status is Stagnant: it is not an adopted requirement, and nothing marks a contract as compliant on-chain.
  • Popular libraries already emit Transfer from their internal _mint, so constructor mints using them normally produce logs anyway. The gap mainly affects hand-rolled or gas-optimised contracts that write storage directly.
  • Indexers should still start scanning from the deployment block (including the creation transaction) to catch constructor mints.
  • Emitting one event per token in a constructor costs gas; very large genesis mints may hit block gas limits.

#nft #erc721 #events #indexing

Details

Authors
Zainan Victor Zhou (@xinbenlv)
Status
Stagnant
Type
Standards Track / ERC
Created
2022-11-26
Requires

Machine-readable: Markdown · JSON