Create Nexis Native Chain NFTs With Metaplex
How to create NFTs in TypeScript with Metaplex Metadata program and Irys permanent storage service.
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
Nexis 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 Nexis Native Chain,
how to create and update them using the mpl-token-metadata
npm module.
NFTs on Nexis 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.
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 Nexis 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.
In the following sections, we’ll cover the basics of using the
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.
Metaplex instance
A Metaplex
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 devnet.
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 the toMetaplexFile
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
the nfts().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’s create()
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.
This method returns an object containing information about the newly created
NFT. 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 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.
Note that any fields you don’t include in the call to 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 Nexis Native Chain 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.
You then list the collection’s Mint Address as the reference for the
collection
field in our new Nft.
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 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 Nexis Native Chain.
Part 1: Creating an NFT collection
To begin, make a new folder and install the relevant dependencies:
Then create a file called create-metaplex-collection.ts
, and add our imports:
Connect to devnet, load a user and Airdrop some NZT if needed:
Connect to Metaplex and Irys:
Add the data we want in for our Collection:
Upload the offchain metadata to irys:
Then actually make the collection:
Run the file with:
The output should look like this:
Congratulations! You’ve created a Metaplex Verified Collection. Check this out on Nexis Native Chain Explorer using the URL above. 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:
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:
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.
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 simila
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 Nexis Native Chain Explorer! Just like previously, if you have any issues, you should fix them yourself, but if needed the solution code is available.
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, Magic Eden, 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 fair launch platforms on the Digital Collectables page. 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.