This article outlines the process of developing and minting NFTs (non-fungible tokens) on the Solana blockchain using Metaplex’s SDK. It covers key steps such as setting up prerequisites, connecting to Solana, creating a wallet, declaring variables, defining NFT characteristics, and executing the minting process. It provides a comprehensive overview of the process for interested developers or businesses looking to engage in NFT creation with Solana development.
What is Solana
Solana is a high-performance blockchain network designed for a wide range of use cases, including payments, gaming, NFTs, and banking. It stands out as a platform built for extensive use, with transparency, connectivity, and decentralization at its core. In addition to its impressive scalability, Solana boasts several other exceptional features, such as “Proof of History.”
Solana uses a unique consensus method called Proof of History, which depends on time-stamping techniques. Every transaction in the Solana network is given a timestamp, which allows the network as a whole to quickly verify the transaction as valid in a matter of milliseconds.
Setup for Mint NFT
Prerequisites
- First, Make sure that node js is installed in your system to check if nodejs is installed or not run “node -v” if it shows some version example-”v18.16.0″. It means that nodejs is installed otherwise you can install nodejs from (https://nodejs.org/en) based on your OS.
- It’s essential to include the Solana Web3 and SPL Token libraries. Furthermore, we’ll be incorporating Metaplex’s JS SDK and MPL Token Metadata libraries. To initiate this, please enter the following command in your terminal
Connect Solana
npm install @solana/web3.js @metaplex-foundation/js
const { Connection, Keypair, PublicKey } = require("@solana/web3.js");
const { Metaplex, keypairIdentity, bundlrStorage, toMetaplexFile, toBigNumber } = require("@metaplex-foundation/js");
Create a Wallet
It’s crucial to generate a Solana File System Wallet, with the resulting keypair documented in a keypair.json file. Additionally, ensure the wallet receives airdropped SOL. This can be accomplished either through the Solana CLI.
Variables Declarations
To execute the script, it’s essential to define several variables:
- Your source wallet, which is represented by a keypair derived from your secret key.
- An instance of Metaplex.
- A CONFIG file that serves as a storage container for information related to the NFT we are set to mint.
const fs = require('fs');
const secret = require('./keypair.json');
const WALLET = Keypair.fromSecretKey(new Uint8Array(secret));
const NODE_RPC = 'https://rpc.ankr.com/solana_devnet';
const SOLANA_CONNECTION = new Connection(NODE_RPC);
const METAPLEX = Metaplex.make(SOLANA_CONNECTION)
.use(keypairIdentity(WALLET))
.use(bundlrStorage());
NFT Characteristics
Let’s instantiate a CONFIG object encapsulating specific metadata for our NFT. Establish a new constant, CONFIG, and include the following attributes:
const CONFIG = {
uploadPath: 'images/',
imgFileName: 'hello.jpeg',
imgType: 'image/jpeg',
imgName: 'Rahul Maurya',
description: 'it is a Tree !',
attributes: [
{trait_type: 'hair', value: 'Black'},
{trait_type: 'company', value: 'oodles'},
{trait_type: 'color', value: 'white'}
],
sellerFeeBasisPoints: 100,
symbol: 'OO',
creators: [
{address: WALLET.publicKey, share: 100}
]
};
Upload Image
It’s crucial to upload the image designated for our NFT to a decentralized storage platform. This is imperative as we’ll be passing the URI of the NFT image into the metadata. If you already have an image hosted with a URI, define it in your CONFIG file and proceed to step 2. Otherwise, let’s establish a new asynchronous function named uploadImage before our main function. This function should take a filePath and fileName as parameters and return a promise resolving to a string, indicating the URI pointing to our uploaded image.
async function uploadImage(filePath,fileName){
console.log(`Step 1 - Uploading Image`);
const imgBuffer = fs.readFileSync(filePath+fileName);
const imgMetaplexFile = toMetaplexFile(imgBuffer,fileName);
const imgUri = await METAPLEX.storage().upload(imgMetaplexFile);
console.log(` Image URI:`,imgUri);
return imgUri;
}
Upload Metadata
The metadata plays a crucial role in defining the uniqueness of your NFT, encompassing the image, defining traits, assigning it to a collection, and more. Metaplex simplifies the metadata uploading process through a single call to nfts().uploadMetadata(). To begin, let’s craft a new function named uploadMetadata. This function should accept five parameters: imgUri, imgType, nftName, description, and attributes.
async function uploadMetadata(imgUri, imgType ,nftName, description, attributes){
const { uri } = await METAPLEX
.nfts()
.uploadMetadata({
name: nftName,
description: description,
image: imgUri,
attributes: attributes,
properties: {
files: [
{
type: imgType,
uri: imgUri,
},
]
}
});
console.log(' Metadata URI:',uri);
return uri;
}
Mint NFTs
Minting our NFT becomes a straightforward process with a single method call: nfts().create(). However, in this final step, distinct from the metadata upload, we must provide some metadata that will be directly stored on the Solana chain.
async function mintNft(metadataUri, name, sellerFee, symbol, creators) {
const { nft } = await METAPLEX
.nfts()
.create({
uri: metadataUri,
name: name,
sellerFeeBasisPoints: sellerFee,
symbol: symbol,
creators: creators,
isMutable: false,
},
{ commitment: "finalized" });
console.log(`Minted NFT: https://explorer.solana.com/address/${nft.address}?cluster=devnet`);
}
async function main() {// Step 1 - upload Images
const imgUri = await uploadImage(CONFIG.uploadPath,CONFIG.imgFileName);
//Step 2 - Upload Metadata
const metadataUri = await uploadMetadata(imgUri,CONFIG.imgType,CONFIG.imgName, CONFIG.description, CONFIG.attributes);
//Step 3 - Mint NFT
mintNft(metadataUri,CONFIG.imgName,CONFIG.sellerFeeBasisPoints,CONFIG.symbol,CONFIG.creators);
}main();
Now, you Feel free to explore your minted NFT by clicking on the following link:
Minted NFT: https://explorer.solana.com/address/${nft.address}?cluster=devnet
Overview
You can create your own NFTs on the Solana blockchain by following these instructions and incorporating the included code samples. To ensure a smooth NFT creation process, make sure that each step is well-tested and validated.
References
Interested in developing NFTs on Solana, then connect with our NFT developers to get started.