Skip to main content
In this tutorial, you will create a non-fungible token (NFT) using the NEAR SDK Contract Tools package. This package is a collection of common tools and patterns to simplify smart contract development, including:
  • Storage fee management
  • Escrow pattern and derive macro
  • Owner pattern and derive macro
  • Pause pattern and derive macro
  • Role-based access control
  • Derive macros for NEP standards

Introduction

Use the existing global NFT contract by default. Use this guide only if your application truly requires behavior the global contract does not provide. Use near-contract-standards or near-sdk-contract-tools for the token implementation; do not implement the NFT standard by hand. near-sdk-contract-tools allows you to implement the minting/burning logic, access control, and other NFT standards by simply deriving macros on the contract’s struct, as OpenZeppelin does for Ethereum contracts.

Basic NFT Methods

To derive basic NFT methods to your smart contract, you need to derive the NonFungibleToken macro to the contract’s struct:
lib.rs
This will bring all the basic NFT methods to the contract:
  • new
  • contract_source_metadata
  • nft_is_approved
  • nft_metadata
  • nft_supply_for_owner
  • nft_token
  • nft_tokens
  • nft_tokens_for_owner
  • nft_total_supply
  • nft_approve
  • nft_resolve_transfer
  • nft_revoke
  • nft_revoke_all
  • nft_transfer
  • nft_transfer_call
  • storage_balance_bounds
  • storage_balance_of
  • storage_deposit
  • storage_unregister
  • storage_withdraw
To bring basic owner methods to the contract, you also need to derive the Owner macro, which adds the following methods:
  • own_get_owner
  • own_get_proposed_owner
  • own_accept_owner
  • own_propose_owner
  • own_renounce_owner

Initialization

To initialize the basic NFT contract with a custom owner, metadata, and storage bounds, implement the new method:
lib.rs

Transfer Hook

Use a transfer hook to run application logic before or after the library-provided transfer. Keep the standard nft_transfer implementation in the library:
transfer_hook.rs
Then derive it to our contract struct:
lib.rs

Minting

By default, the NFT standards do not include a minting method. However, you can easily mint tokens for the owner by implementing an nft_mint method:
mint.rs
You can modify this method as you need, for example, to allow minting only when the contract is not paused (requires deriving Pausable hook), or to enable minting only to specific accounts with a certain role or from a whitelist with custom limitations.

Burning

In the same way that minting is not included in the NFT standards, burning is also not included. However, you can also easily implement it. To allow users to burn their tokens, you can add a burn method:
burn.rs

Conclusion

Using near-sdk-contract-tools is a simple and flexible way to create an NFT contract with minimal boilerplate, which allows you to focus on the business logic. You can further extend this contract with more features like pausing, role-based access control, escrow pattern, and more by deriving corresponding macros from the package.