Summary
- Non-Fungible Tokens (NFTs) are SPL Tokens with an associated metadata account, 0 decimals, and a maximum supply of 1
- Metadata attaches additional properties to token mints (both NFTs and regular tokens). For NFTs, metadata includes the token name and a link to an offchain JSON file. This JSON file contains links to artwork and other media files, any special traits the NFT has, and more.
- The Metaplex Token Metadata program is an onchain program that attaches metadata to a token mint. We can interact with the Token Metadata program using the Token Metadata package via Umi, a tool made by Metaplex for working with onchain programs.
Lesson
Solana Non-Fungible Tokens (NFTs) are SPL tokens created using the Token program. These tokens, however, also have an additional metadata account associated with each token mint.
In this lesson, we'll cover the basics of how NFTs are represented on Solana,
how to create and update them using the mpl-token-metadata
npm module.
NFTs on Solana
An NFT is a standard token from the Token Program with the following characteristics:
- Has 0 decimals, so it cannot be divided into parts
- Comes from a token mint with a supply of 1, so only 1 of these tokens exists
- Comes from a token mint whose authority is set to
null
(to ensure that the supply never changes) - Has an associated account that stores metadata - things like a name, symbol, images, etc.
While the first three points can be achieved with the SPL Token Program, the associated metadata requires an additional program. This is the Metadata program.
The Metaplex Token Metadata program
The most popular way Solana NFTs have been created is by using the Metaplex Token Metadata program.
Metadata
-
When creating an NFT, the Token Metadata program creates an onchain metadata account using a Program Derived Address (PDA) with the token mint as a seed. This allows the metadata account for any NFT to be located deterministically using the address of the token mint. The onchain metadata contains a URI field that points to an offchain
.json
file. -
The offchain metadata in the JSON file stores the link to the media (images, videos, 3D files) of the NFT, any traits the NFT may have, and additional metadata (see this example JSON file). Permanent data storage systems such as Arweave are often used to store the offchain component of NFT metadata.
In the following sections, we'll cover the basics of using the
metaplex-foundation/token-metadata
plugin with Umi to prepare assets, create
NFTs, update NFTs, and associate an NFT with a broader collection. For more
information on metaplex-foundation/token-metadata
see the
developer docs for Token Metadata.
Metaplex Core, is an NFT standard from Metaplex where asset details such as the owner, name, uri e.t.c are stored on a single account. However, the most common style of NFT is still by making a Solana SPL token with some Metadata attached via the Metaplex Metadata program, so that's what we'll be using in this tutorial.
UMI instance
Umi is a framework for making JS/TS clients for onchain programs, that was created by Metaplex. Umi can create JS/TS clients for many programs, but in practice, it's most commonly used to communicate to the Token Metadata program.
Note that Umi has different implementations for many concepts than web3.js, including Keypairs, PublicKeys, and Connections. However, it is easy to convert from web3.js versions of these items to the Umi equivalents.
Installation and setting up Umi
First we create a new Umi instance. We can do this by either providing our own
RPC endpoint, or use the public facing Solana endpoints provided by the
clusterApiUrl
method.
Finally, we pass in the identity for our Umi instance (this is the keypair that
will be used to sign transactions) and the plugins that we will use, in our
case, this is the metaplex-foundation/mpl-token-metadata
.
Uploading assets
Before creating an NFT, you must prepare and upload any assets you plan to associate with the NFT. While this doesn't have to be an image, most NFTs have an image associated with them.
Preparing and uploading an image involves converting the image to a buffer,
converting the file to a
generic file using
the createGenericFile()
function and finally uploading it to the designated
Storage Driver.
The GenericFile
type allows Umi to support different file variations despite
the difference of browser files and local file system files i.e. those on your
computer.
In action, uploading an image named random-image.png
from your computer would
take the following steps:
-
Reading the file using
readFile
into a buffer. -
Creating a generic file type with the files MIME Type from the buffer and filePath.
-
Uploading file to designated storage provider.
The function's return value will be the URI where the image was stored.
Upload metadata
After uploading an image, it's time to upload the offchain JSON metadata using
the uploadJson()
method. This will return a URI where the JSON metadata is
stored.
Remember, the offchain portion of the metadata includes things like the image URI as well as additional information like the name and description of the NFT. While you can technically include anything you'd like in this JSON object, in most cases, you should follow the NFT standard to ensure compatibility with wallets, programs, and applications.
To create the metadata, use the uploadJson()
method provided by the SDK. This
method accepts a metadata object and returns a URI that points to the uploaded
metadata.
Create the NFT
After uploading the NFT's metadata, you can finally create the NFT on the
network. The mplTokenMetadata
plugin we added earlier provides the required
helpers to create an NFT or any other token with minimal configuration. The
helper createNft
method will create the mint account, token account, metadata
account, and master edition account for you. The data provided to this method
will represent the onchain portion of the NFT metadata. You can explore the SDK
to see all the other input optionally supplied to this method.
The sendAndConfirm
method is what takes care of signing our transaction and
sending it. It also provides other options to set pre-flight checks and our
desired commitment for the transaction, which defaults to confirmed
if not
provided.
This method returns an object containing the transaction signature and a result.
The result object contains the outcome of our transaction. If successful, the
err
inside this will be set to null otherwise it'll contain the error for the
failed transaction.
By default, the SDK sets the isMutable
property to true, allowing for updates
to be made to the NFT's metadata. However, you can choose to set isMutable
to
false, making the NFT's metadata immutable.
Update the NFT
If you've left isMutable
as true, you may update your NFT's metadata.
The SDK's updateV1
method allows you to update both the onchain and offchain
portions of the NFT's metadata. To update the offchain metadata, you'll need to
repeat the steps of uploading a new image and metadata URI (as outlined in the
previous steps), then provide the new metadata URI to this method. This will
change the URI that the onchain metadata points to, effectively updating the
offchain metadata as well.
Note that any fields you don't include in the call to updateV1
will stay the
same, by design.
Add the NFT to a collection
A
Certified Collection
is an NFT that individual NFTs can belong to. Think of a large NFT collection
like Solana Monkey Business. If you look at an individual NFT's
Metadata
you will see a collection
field with a key
that points to the
Certified Collection
NFT.
Simply put, NFTs that are part of a collection are associated with another NFT
that represents the collection itself.
Certified collections are important because they mean the collection owner has verified that each NFT actually belongs to the collection!
To add an NFT to a collection, first, the Collection NFT has to be created. The
process is the same as before, except you'll include one additional field on our
NFT Metadata: isCollection
. This field tells the token program that this NFT
is a Collection NFT.
To mint an NFT into this collection, the
Collection type
which has two fields, the address of the collectionMint
generated above and
the verified field.
When you checkout the metadata on your newly created NFT, you should now see a
collection
field like so:
The last thing you need to do is verify the NFT. This effectively just flips the
verified
field above to true, but it's incredibly important. This is what lets
consuming programs and apps, including wallets and art marketplaces, know that
your NFT is in fact part of the collection - because the Collection's owner has
signed a transaction making the NFT a member of that collection. You can do this
using the verifyCollectionV1
function:
Lab
In this lab, we'll go through the steps to create an NFT using the Metaplex Umi framework, update the NFT's metadata after the fact, and then associate the NFT with a collection. By the end, you will have a basic understanding of how to use the Metaplex Umi and the mplTokenMetadata library to interact with NFTs on Solana.
Part 1: Creating an NFT collection
To begin, make a new folder and install the relevant dependencies:
Then create a file called create-metaplex-nft-collection.ts
, and add our
imports:
Connect to devnet, load a user and Airdrop some SOL if needed:
Create a new Umi instance, assign it the loaded keypair, load the
mplTokenMetadata
to interact with the metadata program and irysUploader
to
upload our files.
Download the image assets the collection image from the links below and save them inside your working directory,
-
collection image: https://github.com/solana-developers/professional-education/blob/main/labs/metaplex-umi/collection.png
-
NFT image: https://github.com/solana-developers/professional-education/blob/main/labs/metaplex-umi/nft.png
We will use these images as our collection and nft cover images respectively.
We will use Irys as our storage provider, and Metaplex conveniently ships the
umi-uploader-irys
plugin we can use to upload our files. The plugin, also
takes care of storage fees so that we don't have to worry about making this on
our own.
Upload the offchain metadata to Irys:
Then actually make the collection:
We advise using esrun to run the scripts because it allows you to use top level await without having to wrap your code inside asynchronous function.
Run the create-metaplex-nft-collection.ts
script
The output should look like this:
Congratulations! You've created a Metaplex Collection. Check this out on Solana Explorer using the URL above which should resemble
Solana Explorer with details about created collection
If you have any trouble, try and fix it yourself, but if you need to you can also check out the solution code.
We'll use the collection NFT address in the next step.
2. Creating a Metaplex NFT inside the collection
We'll now make a Metaplex NFT that's a member of the collection we just made.
Make a new file called create-metaplex-nft.ts
. The setup for this will look
the same as the previous file, with slightly different imports:
Now let's tell Metaplex our collection, and the NFT we want to make:
We can then put out files into Irys:
And then create an NFT using the URI from the metadata:
Run npx esrun create-metaplex-nft.ts
. If all goes well, you will see the
following:
Inspect your NFT at the address given! If you have any trouble, try and fix it yourself, but if you need to you can also check out the solution code.
You should have something similar to this image on your explorer page Solana Explorer with details about created NFT
Finally, let's verify our mint as being part of our collection. This makes it so
the verified
field in the onchain metadata is set to true
, so consuming
programs and apps can know for sure that the NFT in fact belongs to the
collection.
Create a new file verify-metaplex-nft.ts
, import the required libraries and
instantiate a new umi Instance.
Verifying an NFT will require you to have the collectionAddress
you used
created in the creation of a collection stage, and we will use the
verifyCollectionV1
method.
Run npx esrun verify-metaplex-nft.ts
. If all goes well, you will see the
following:
Inspect your verified NFT at the address given! If you have any trouble, try and fix it yourself, but if you need to you can also check out the solution code.
The verified flag on your NFT should now be set to 1
-> true
showing that
it's verified. To confirm this, look under the metadata tab on the Solana
Explorer to confirm that your NFT is verified as part of the collection.
Solana Explorer with details about created NFT
Remember the NFT address, we'll use it in the next step.
3. Update the NFT
Create a new file, called update-metaplex-nft.ts
. The imports will be similar
to our previous files:
Let's load our NFT, specifying the address from the previous example, and set up what we'd like to update:
We can then use Metaplex to update our NFT:
Run npx esrun update-metaplex-nft.ts
. You should see something like:
Inspect the updated NFT on Solana Explorer! Just like previously, if you have any issues, you should fix them yourself, but if needed the solution code is available.
Solana Explorer with details about the updated NFT
Congratulations! You've successfully learned how to use the Metaplex SDK to create, update, and verify NFTs as part of a collection. That's everything you need to build out your own collection for just about any use case. You could build a new event ticketing platform, revamp a retail business membership program, or even digitize your school's student ID system. The possibilities are endless!
Challenge
The steps covered above for creating an NFT would be incredibly tedious to execute for thousands of NFTs in one go. Many providers, including Metaplex, MagicEden, and Tensor have so-called 'fair launch' tools that take care of minting large quantities of NFTs and ensuring they are sold within the parameters set by their creators. Dive into one of these fair launch platforms and create an NFT. This hands-on experience will not only reinforce your understanding of the tools but also boost your confidence in your ability to use them effectively in the future.
Push your code to GitHub and tell us what you thought of this lesson!