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 filling a bare-bones skeleton smart contract to add minting functionalities.
You can find the skeleton contract in our Skeleton folder
A completed version of this tutorial can be found in the Basic NFT folder
Introduction
To get started, go to the nft-contract-skeleton folder in our repo. If you haven't cloned the repository, refer to the Contract Architecture to get started.
cd nft-contract-skeleton/
If you wish to see the finished code of this step-by-step basic NFT contract tutorial, that can be found on the nft-contract-basic folder.
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-skeleton/src/lib.rs 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.
Contract Struct
The first thing to do is modifying the contract struct as follows:
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:
- tokens_per_owner: allows you to keep track of the tokens owned by any account
- tokens_by_id: returns all the information about a specific token
- token_metadata_by_id: returns just the metadata for a specific token
In addition, you'll keep track of the owner of the contract as well as the metadata for the contract.
You might be confused as to some of the types that are being used. In order to make the code more readable, we've introduced custom data types which we'll briefly outline below:
- AccountId: a string that ensures there are no special or unsupported characters.
- TokenId: simply a string.
As for the Token, TokenMetadata, and NFTContractMetadata data types, those are structs that we'll define later in this tutorial.
Initialization Functions
Next, create what's called an initialization function; we will name it new, but you can choose any name you prefer.
This function needs to be invoked when you first deploy the contract. It will initialize all the contract's fields that you've defined above with default values.
Don't forget to add the owner_id and metadata fields as parameters to the function, so only those can be customized.
This function will default all the collections to be empty and set the owner and metadata equal to what you pass in.
Loading...
More often than not when doing development, you'll need to deploy contracts several times. You can imagine that it might get tedious to have to pass in metadata every single time you want to initialize the contract. For this reason, let's create a function that can initialize the contract with a set of default metadata. You can call it new_default_meta and it'll only take the owner_id as a parameter.
Loading...