Skip to content
With the Token Extension program, you can create NFTs and digital assets using the metadata extensions. Together, these extensions (metadata pointer and token metadata) allow you to put any desired metadata natively onchain. All within a customizable key-value data store directly on the token’s mint account, reducing costs and complexity. These can be especially great for web3 games since we can now have these “additional metadata fields” within an onchain key-value store, allowing games to save/access unique state within the NFT itself (like for a game character’s stats or inventory).

​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, and uri
    • the uri is a link to an offchain json file which contains the offchain metadata of the NFT
  • we will also have custom “additional fields” that we define
All of these fields are saved using the metadata extension which is pointed to the NFT’s mint account, making them accessible to anyone or any program.
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:
  1. Create a mint account
  2. Initialize the mint account
  3. Create a metadata pointer account
  4. Initialize the metadata pointer account
  5. Create the metadata account
  6. Initialize the metadata account
  7. Create the associated token account
  8. Mint the token to the associated token account
  9. 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 the nft-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.toml pins Anchor 1.2.1 and Solana CLI 4.3.2. If you installed Anchor with AVM, as the installer sets it up, the first anchor build installs those versions and uses them for this project.
  • The example has no Cargo.lock of 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 in rust-toolchain.toml), and the IDL build fails with rustc 1.97.1 is not supported by the following packages.
  • cargo test runs the LiteSVM test in tests/test_mint_nft.rs against the built program, so it doesn’t need a running validator. (anchor test first deploys to a local validator.)
The test passes with:

​Project structure

The entry point is in the lib.rs file. Here we define the program id and the instructions. The instructions are defined in the instructions folder. The state is defined in the state folder. So the calls arrive in the lib.rs file and are then forwarded to the instructions. The instructions then call the state to get the data and update it. You can find the mint NFT instruction in the instructions folder.