-2

I am learning about how nft smart contracts work. I could not understand why emitting an event receives address(0).

This is the _mint function from Openzeppeling ERC721

function _mint(address to, uint256 tokenId) internal virtual {
        require(to != address(0), "ERC721: mint to the zero address");
        require(!_exists(tokenId), "ERC721: token already minted");

        _beforeTokenTransfer(address(0), to, tokenId);

        _balances[to] += 1;
        _owners[tokenId] = to;
        // i am stuck at here
        emit Transfer(address(0), to, tokenId);
        _afterTokenTransfer(address(0), to, tokenId);
    }

When we emit the Transfer event, why do we use address(0). address(0) stands for empty address. Here is the Transfer event:

event Transfer(
     address indexed from, 
     address indexed to, 
     uint256 indexed tokenId);
Yilmaz
  • 35,338
  • 10
  • 157
  • 202

1 Answers1

0

Passing the zero address as the sender param when minting a token is defined in the ERC-721 standard (which is considered to be the first NFT standard):

/// @dev This emits when ownership of any NFT changes by any mechanism.
///  This event emits when NFTs are created (`from` == 0) and destroyed
///  (`to` == 0). Exception: during contract creation, any number of NFTs
///  may be created and assigned without emitting Transfer. At the time of
///  any transfer, the approved address for that NFT (if any) is reset to none.
event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • why not to use dedicated mint event ? – Ujjwal Kumar Gupta Oct 31 '22 at 01:53
  • @UjjwalKumarGupta I can't speak for the authors and reviewers of the ERC-721 standard, why they chose this specific way. But from my understanding, it was already a common practice to emit `Transfer` event log with zero sender address when minting ERC-20 tokens, when they were creating the 721 standard. So one of the reasons might have been reusability of code for offchain apps such as blockchain explorers, to be able to handle token minting in a more generalized way. – Petr Hejda Oct 31 '22 at 08:11