Writing NFT smart contracts
Ethereum is the “birthplace” of NFTs and remains one of the most popular platforms for the NFT market and creators.
The most widely used and well supported standards are ERC-721 and ERC-1155. Adopting one of these interfaces will allow your NFTs to receive broad support "out of the box" from wallets and other NFT applications without any special coordination or effort on your part.
Both ERC standards define methods for retrieving the URI associated with a token. In ERC-721, the method is called tokenURI, while ERC-1155 uses uri.
In general, you will mint new tokens by calling a smart contract function that assigns a new token ID and sets the metadata URI. The exact name of this function may vary from contract to contract, if you are writing your own contract you can call it however you want.
Here's an example contract from the OpenZeppelin ERC-721 guide, using their excellent base contract:
Here, the minting function is called awardItem, which creates a new token and assigns it to the player address.
The second parameter is tokenURI, which sets the value returned by the contract's tokenURI method. Here you can put the IPFS URI of the NFT metadata.
After a token is minted, anyone can call the tokenURI method (defined in the underlying contract) and find the URI of any token. Using URIs, callers can use a peer-to-peer IPFS network or HTTP gateway to get metadata, images, and other assets that define the NFT.
Last updated