Minting
This is the first of many tutorials in a series where you'll be creating a complete NFT smart contract from scratch that conforms with all the NEAR NFT standards. Today you'll learn how to create the logic needed to mint NFTs and have them show up in your NEAR wallet. You will be modifying a bare-bones skeleton smart contract by filling in the necessary code snippets needed to add minting functionalities.
Introduction
To get started, switch to the 1.skeleton
branch in our repo. If you haven't cloned the repository, refer to the Contract Architecture to get started.
git checkout 1.skeleton
If you wish to see the finished code for the minting portion of the tutorial, that can be found on the 2.minting
branch.
Modifications to the skeleton contract
In order to implement the logic needed for minting, we should break it up into smaller tasks and handle those one-by-one. Let's step back and think about the best way to do this by asking ourselves a simple question: what does it mean to mint an NFT?
To mint a non-fungible token, in the most simple way possible, a contract needs to be able to associate a token with an owner on the blockchain. This means you'll need:
- A way to keep track of tokens and other information on the contract.
- A way to store information for each token such as
metadata
(more on that later). - A way to link a token with an owner.
That's it! We've now broken down the larger problem into some smaller, less daunting, subtasks. Let's start by tackling the first and work our way through the rest.
Storing information on the contract
Start by navigating to nft-contract/src/index.ts
and filling in some of the code blocks.
You need to be able to store important information on the contract such as the list of tokens that an account has.
The first thing to do is add the information to the contract class.
Loading...
This allows you to get the information stored in these data structures from anywhere in the contract. The code above has created 3 token specific storages:
- tokensPerOwner: allows you to keep track of the tokens owned by any account. It will map the account address to a set of token ID strings owned by that account.
- tokensById: returns all the information about a specific token. It will map a token ID string to a
Token
object. - tokenMetadataById: returns just the metadata for a specific token. It wil map a token ID string to a
TokenMetadata
object.
In addition, you'll keep track of the owner of the contract as well as the metadata for the contract.