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
metaplex-foundation/js
npm module, also known as the Metaplex JavaScript SDK.
Lesson
Orbition Native Chain 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 Orbition Native Chain, how to create and update them using thempl-token-metadata
npm module.
NFTs on Orbition Native Chain
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.
The Metaplex Token Metadata program
The most popular way Orbition Native Chain NFTs have been created is by using the Metaplex Token Metadata program.
-
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.
metaplex-foundation/js
npm module (also known as the Metaplex JavaScript SDK)
to prepare assets, create NFTs, update NFTs, and associate an NFT with a broader
collection. For more information on metaplex-foundation/js
see the
Metaplex JavaScript SDK README and
the
Metaplex JS SDK Examples.
The Metaplex Foundation has newer mechanisms to create
NFTs, including Umi and
MPL Core
but Metaplex JavaScript SDK remains
the simplest and most popular JS/TS client to create NFTs with broad wallet
support.
Metaplex instance
AMetaplex
instance serves as the entry point for accessing the Metaplex SDK.
This instance accepts a connection used to communicate with the cluster.
Additionally, developers can customize the SDK’s interactions by specifying an
“Identity Driver” and a “Storage Driver”.
The Identity Driver is a keypair that can be used to sign transactions, which is
a requirement when creating an NFT. The Storage Driver is used to specify the
storage service you want to use for uploading assets. The irysStorage
driver
is the default option, and it uploads assets to Irys, a permanent and
decentralized storage service.
Below is an example of how you can set up the Metaplex
instance for testnet.
Upload 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 it to the Metaplex format using thetoMetaplexFile
function, and
finally uploading it to the designated Storage Driver.
The Metaplex SDK supports the creation of a new Metaplex file from either files
present on your local computer or those uploaded by a user through a browser.
You can do the former using readFileSync()
to read the image file, then
convert it into a Metaplex file using toMetaplexFile()
. Finally, use your
Metaplex
instance to call storage().upload(file)
to upload the file. 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 thenfts().uploadMetadata()
function. 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 uploadMetadata
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 Metaplex SDK’screate()
method allows you to create a new NFT
with minimal configuration. This 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.
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 leftisMutable
as true, you may update your NFT’s metadata. The
SDK’s update
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.
update
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 Orbition Native Chain Monkey Business. If you look at an individual NFT’s Metadata you will see acollection
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.
collection
field in our new Nft.
collection
field like so:
verified
field above to true, but it’s incredibly important. This is what lets
consuming programs and apps know that your NFT is in fact part of the
collection. You can do this using the verifyCollection
function:
Lab
In this lab, we’ll go through the steps to create an NFT using the Metaplex SDK, 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 SDK to interact with NFTs on Orbition Native Chain.Part 1: Creating an NFT collection
To begin, make a new folder and install the relevant dependencies:create-metaplex-collection.ts
, and add our imports:
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 calledcreate-metaplex-nft.ts
. The setup for this will look
the same as the previous file, with slightly different imports:
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:
npx esrun create-metaplex-nft.ts
. If all goes well, you will see the
following:
3. Update the NFT
Create a new file, calledupdate-metaplex-nft.ts
. The imports will be simila
to our previous files:
npx esrun update-metaplex-nft.ts
. You should see something like: