Tutorial_4: Minting NFTs

Introduction​

Prerequisites​

  1. Rust toolchain
  2. A NEAR account
  3. NEAR command-line interface (near-cli)
  4. Working knowledge of Rust

Getting started

Editing Cargo.toml

cargo.toml
cargo.toml

Building the NFT contracts

use external crate
contract starter struct
contract struct init
contract starter impl
contract impl init

Adding NFT functionalities

Adding Minting Functionality

  • 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.
struct element
  • 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
  • AccountId: a string that ensures there are no special or unsupported characters.
  • TokenId: simply a string.
contract initializer
contract initializer
default contract initialiser
NFTContractMetadata
NFTContractMetadata data type
TokenMetadata data type
Token struct
Token data type
JsonToken data type
JsonToken data type

Function for querying contract metadata

nft_metadata()
nft_metadata()

Minting Logic

notable contract element
  • Add the token ID into the set of tokens that the receiver owns. This will be done on the tokens_per_owner field.
  • Create a token object and map the token ID to that token object in the tokens_by_id field.
  • Map the token ID to its metadata using the token_metadata_by_id.
nft_mint()
nft_mint()
internal functions
internal refund_deposit functions
internal function
internal add token to owner function
sha256 helper

Tidying up the smart contract

  • Initialization
  • Metadata
  • Minting
  • internal functions
top of metadata.rs
top of internal.rs
top of mint.rs
use internal modules
helper key storage

Querying for token information

query nft metadata by id

Wrapping up

Versioning for this article

  • cargo 1.62.0 (a748cf5a3 2022–06–08)
  • rustc 1.62.0 (a8314ef7d 2022–06–27)
  • near-cli: 3.4.0

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Near Protocol Nigeria (NPK Guild)

Near Protocol Nigeria (NPK Guild)

NPK Guild is a Near Protocol Community based in Nigeria, aimed at educating and incentivising young Nigerians onBlockchain technology and the NEAR ECOSYSTEM