Introduction to Metaplex
Introduction to Metaplex
Metaplex is a suite of tools designed to simplify the creation and distribution of NFTs (Non-Fungible Tokens) on the Solana blockchain. With Metaplex, developers can create NFTs with their API and SDK.
SDK
The SDK is used by developers building with Metaplex. It has a simple API that focuses on popular use cases and allows for easy integration with third-party plugins. To use the SDK, you will need to set up a Metaplex instance, which serves as the entry point for accessing the Metaplex SDK APIs. This instance accepts a connection used to communicate with the cluster and can be customized by specifying an "Identity Driver" and a "Storage Driver".
Identity Driver
The Identity Driver is effectively a keypair that can be used to sign transactions, which is required when creating an NFT. The Storage Driver is used to specify the storage service you want to use for uploading assets. By default, Metaplex uses the bundlrStorage driver, which uploads assets to Arweave, a permanent and decentralized storage service.
Identity Drivers use Keypairs, which are covered extensively in Assignment 2
Sample Instance
import {
Metaplex,
keypairIdentity,
bundlrStorage,
} from "@metaplex-foundation/js";
import { Connection, clusterApiUrl, Keypair } from "@solana/web3.js";
const connection = new Connection(clusterApiUrl("devnet"));
const wallet = Keypair.generate();
const metaplex = Metaplex.make(connection)
.use(keypairIdentity(wallet))
.use(
bundlrStorage({
address: "<https://devnet.bundlr.network>",
providerUrl: "<https://api.devnet.solana.com>",
timeout: 4000,
}),
);
Uploading Assets for NFTs
Before you can create an NFT, you need to prepare and upload any assets you plan to associate with the NFT. Most NFTs have an image associated with them, but this could be any other type of file. To upload an image, you will need to convert it to a buffer, convert it to the Metaplex format, and finally upload 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.
const buffer = fs.readFileSync("/path/to/image.png");
const file = toMetaplexFile(buffer, "image.png");
const imageUri = await metaplex.storage().upload(file);
Metadata
After uploading the image, you will need to upload the off-chain JSON metadata. This metadata includes things like the image URI as well as additional information such as the name and description of the NFT. You should follow the NFT standard to ensure compatibility with wallets, programs, and applications. The Metaplex SDK provides a method for uploading the metadata, called uploadMetadata, which accepts a metadata object and returns a URI that points to the uploaded metadata.
const { uri } = await metaplex.nfts().uploadMetadata({
name: "My NFT",
description: "My description",
image: imageUri,
});
Token Metadata
The Token Metadata program is an essential part of the Metaplex platform. The program standardizes the process of attaching metadata to SPL Tokens. When creating an NFT with Metaplex, the Token Metadata program creates a 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 determinist
Creating NFTs
Finally, you can 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 use the Identity Driver to sign the transaction and the metadata URI to associate the NFT with the metadata. The method returns the token ID of the newly created NFT.
Updating NFTs
The Metaplex SDK also provides methods for updating NFTs. To update an NFT, you will need to upload new metadata, using the same uploadMetadata method as before. You can then use the update method to update the NFT on the network. This method accepts the token ID of the NFT to be updated and the URI of the new metadata.
Last updated