Building the onchain program
In this developer guide, we will demonstrate how to build these Token Extension based NFTs and custom metadata using an Anchor program. This program will save the level and the collected resources of a game player within an NFT. This NFT will be created by the Anchor program so it is very easy to mint from the JavaScript client. Each NFT will have some basic structure provided via the Token Metadata interface:- default onchain fields -
name,symbol, anduri- the
uriis a link to an offchain json file which contains the offchain metadata of the NFT
- the
- we will also have custom “additional fields” that we define
Video and Source CodeYou can find a video walkthrough of this example on the Solana Foundation
Youtube channel:
Other use case within games
These types of NFTs with customizable onchain metadata open up many interesting possibilities for game developers. Especially since this metadata can be directly interacted with or managed by an onchain program. Some of these gaming related use cases include:- save the level and XP of the player
- the current weapon and armor
- the current quest
- and more.
Minting the NFT
To create the NFT, the program performs the following steps:- Create a mint account
- Initialize the mint account
- Create a metadata pointer account
- Initialize the metadata pointer account
- Create the metadata account
- Initialize the metadata account
- Create the associated token account
- Mint the token to the associated token account
- Remove the mint authority (set it to
None) so no more tokens can be minted
Rust program code
Here is the rust code used to mint the NFT using the Token extension program:Program
JavaScript client code
Calling mint NFT from the client is very easy:Client
Run the example
The code above is adapted from thenft-meta-data-pointer
example in the Solana program examples repository: an Anchor program with a
LiteSVM test that mints the NFT and checks its metadata. The example has no web
client; the JavaScript snippet above shows how a client calls mint_nft.
Setup your local environment
In order to run this example locally, you will need to make sure you have setup your local environment for Solana development, including installing and configuring the Anchor CLI. If you do not already, you can follow the previously linked setup guide to do so.Build and test the program
Clone the repository, then build and test the example:- The example’s
Anchor.tomlpins Anchor 1.2.1 and Solana CLI 4.3.2. If you installed Anchor with AVM, as the installer sets it up, the firstanchor buildinstalls those versions and uses them for this project. - The example has no
Cargo.lockof its own, so copy the repository’s. Without it, Cargo resolves newer Agave crates that need a newer Rust than the repository’s toolchain (Rust 1.97, set inrust-toolchain.toml), and the IDL build fails withrustc 1.97.1 is not supported by the following packages. cargo testruns the LiteSVM test intests/test_mint_nft.rsagainst the built program, so it doesn’t need a running validator. (anchor testfirst deploys to a local validator.)