4

What does the internal method call _mint(msg.sender, tokenId) really do in the background when used with ERC721 token implementation?

2

Let's take the open-zeppelin implementation of ERC721:

_mint() function

  /**
   * @dev Internal function to mint a new token
   * Reverts if the given token ID already exists
   * @param to The address that will own the minted token
   * @param tokenId uint256 ID of the token to be minted by the msg.sender
   */
  function _mint(address to, uint256 tokenId) internal {
    require(to != address(0));
    _addTokenTo(to, tokenId);
    emit Transfer(address(0), to, tokenId);
}

Here it checks that the to address isnt the 0x0 address, and then calls the _addTokenTo function.

_addTokenTo() function

  /**
   * @dev Internal function to add a token ID to the list of a given address
   * @param to address representing the new owner of the given token ID
   * @param tokenId uint256 ID of the token to be added to the tokens list of the given address
   */
  function _addTokenTo(address to, uint256 tokenId) internal {
    require(_tokenOwner[tokenId] == address(0));
    _tokenOwner[tokenId] = to;
    _ownedTokensCount[to] = _ownedTokensCount[to].add(1);
}

Here it checks that a token with the same token ID is not already owned by someone else (by checking that it is owned by the 0x0 address, which is default). If no one owns that token ID, then it sets the owner of that token ID to the to address, and increases the count of owned tokens for that to address.

Ultimately these are the steps required to create a new token and assign that token an owner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.