Posted By : Rahul
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.
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.
Check It Out | Exploring Solana Blockchain Development for Enterprises
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");
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.
To execute the script, it's essential to define several variables:
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());
Suggested Read | A Quick Guide to NFT Development on Solana Blockchain
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}
]
};
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;
}
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;
}
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
Also, Discover | Advanced NFT Marketplace Development on Solana Blockchain
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.
Interested in developing NFTs on Solana, then connect with our NFT developers to get started.
November 8, 2024 at 08:20 pm
Your comment is awaiting moderation.