logo
logo
Sign in

A Detailed Guide to NFT Minting on Solana using Metaplex API

avatar
Oodles Blockchain

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.”


Proof of History is a novel consensus methodology used by Solana that relies on time-stamping methods. The Solana network assigns a timestamp to each transaction, enabling the network to collectively confirm the transaction's validity 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. If not, let's create a second asynchronous function called uploadImage and call it before our primary function. The inputs for this function should be filePath and fileName. It should then return a promise that resolves to a string that contains the URI for the image that we uploaded.

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

By using the provided code samples and these instructions, you can generate your own NFTs on the Solana blockchain. 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.

collect
0
avatar
Oodles Blockchain
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more