Search code examples
ethereumsoliditynfthardhat

what does _safeMint() do under the hood?


The following is some stripped down sample code which I am using to formulate my question. This pertains to the Youtube Video by Patrick Collins entitled: How to make NFT Art with On-Chain Metadata | FULL HARDHART / JS TUTORIAL! (w/ Polygon & Opensea)

contract RandomSVG is ERC721URIStorage, VRFConsumerBase {
    ....
    ....    
    function create() public returns (bytes32 requestId) {
        requestId = requestRandomness(keyhash, fee);
        ...
    ...
    }
 

    function fulfillRandomness(bytes32 requestId, uint256 randomNumber)
        internal
        override
    {
       
    ...
    ...
        _safeMint(nftOwner, tokenId); 
    ...
    }

}

The RandomSVG is a contract that will create random NFTs.

Question 1 : I read the documentation but i'm still unable to understand. What does the _safeMint() function exactly do ?

Question 2: At what point in the contract should the _safeMint() function be called ?

In his example, Patrick is using the _safeMint() function inside the fulfillRandomness() callback. Why is the _safeMint() called even before the Image has been created ? Shouldn't it be called After the TokenURI has been created ?


Solution

  • _safeMint() is used along with IERC721Receiver that checks if you are sending the minted token to a Contract that is capable to manage NFTs or not. This is to prevent tokens to be lost.