Minting Compressed NFTs using the Solana Suite

Minting Compressed NFTs using the Solana Suite

·

4 min read

I am fukaoi, developing a Solana client SDK called Solana Suite. In this post, I want to use Solana Suite to mint a hot topic in the Solana community, Compressed NFTs (cNFTs).

What is Compressed NFT (cNFT)?

Compared to traditional NFTs, cNFTs have a different method of data storage. In regular NFTs, most of the metadata is stored on-chain. However, due to the expensive storage fees on Solana, cNFTs adopt an off-chain storage approach. When storing off-chain, the hash value is saved in an on-chain Merkle tree, significantly reducing costs. This approach also improves searchability.

What is a Collection?

A Collection is a mechanism for grouping Solana NFTs. Taking the example of the famous NFT project Mad Lads, there are two types of NFTs: those for sale and Collection NFTs. Collection NFTs represent the group, and the NFTs for sale belong to that collection.

In regular NFTs, creating a Collection was optional, but with cNFTs, specifying a Collection has become mandatory. When minting cNFTs, it is required to belong to some Collection.

Let's implement it!

I'd like to explain the implementation of cNFT using Solana Suite. We'll be using the devnet for Solana's network. (* Solana Suite defaults to devnet.)

Installation of the cNFT module:

$ npm i @solana-suite/compressed-nft

Creating an account (Keypair) for issuing cNFTs:

import { Account } from '@solana-suite/compressed-nft';

const owner = Account.Keypair.create();

/*
 Keypair {
  secret: 'CsfK6FvY5D6Pcq12a2Rb1T7wZvk7DMHdeRdJTyn3MViF2hWiaUmSN4eYqa37cpUX66daCyaZhQEyTeuBM6dDkpb',
  pubkey: 'HUqoYvBzfKpth5Wx8EaUz83n4XwoBrmMQdCRZVxTxCqH'
}
*/

Since the owner account we created doesn't have enough SOL for fees, I'd like to airdrop some SOL for use on the devnet.

Installing the Airdrop module:

$ npm i @solana-suite/airdrop

Requesting 1 SOL for the owner account:

import { Airdrop } from '@solana-suite/airdrop';

await Airdrop.request(owner.pubkey);

Creating a space to store cNFTs:

Using a Merkle tree, we create a space on-chain. Since we are minting only a small number of cNFTs this time, we generate a space that can store only 8 items. The signing is done with the owner's private key.

The available space sizes are:

8, 16,000, 1,00,000, 16,700,000, 67,000,000, 1,000,000,000

import { CompressedNft} from '@solana-suite/compressed-nft';

const spaceInst = await CompressedNft.createSpace(owner.secret, 8);

Minting NFT for the Collection:

This time, the Collection NFT we are creating will be minted with the following information. Since the owner is also the owner of the collection, signing is done with the owner's private key.

Parameter nameValue
nameAnimals Collection
symbolANIC
filePath./animals.jpeg
 const collectionInst = await CompressedNft.mintCollection(
    owner.secret,
    {
      filePath: './animals.jpeg',
      name: 'Animals Collection',
      symbol: 'ANIC',
    }
  );

Submitting to the Solana Network:

Solana Suite allows you to control the timing of submitting to the Solana Network. Since we haven't submitted yet when calling createSpace() and mintCollection(), it is necessary to call the submit function.

await [spaceInst, collectionInst].submit();

By passing in an array, you can consolidate the number of transactions into one. While it's just a small improvement, it contributes to reducing transaction fees.

  • Here is the Solcan URL for the actually generated Collection NFT.

  • As explained earlier, creating the "Space" and "Collection NFT" is a one-time process.

Minting cNFTs:

Now that the Space and Collection NFT are ready, let's generate the desired cNFT. cNFTs can have metadata settings similar to Collection NFTs. Here too, signing is done with the owner's private key.

Parameter NameValue
nameLion
symbolANIC-LION
filePath./lion.jpeg
 const mintInst = await CompressedNft.mint(
    owner.secret,
    {
      filePath: 'linon.jpeg',
      name: 'Lion',
      symbol: 'ANIC-LION',
    },
    space,          // The space account included in spaceInst
    mintCollection, // The collection account included in collectionInst
}

await mintInst.submit();
  • This is the XRAY URL for the Lion NFT actually generated.

    (*Since Solscan does not yet support cNFTs, we will use an explorer that supports them.)

  • Here is the complete source code

Summary

How was it? Wasn't it quite straightforward? Solana Suite is an SDK designed to enable web engineers to quickly develop Web3 services. Therefore, it is designed to be user-friendly even for those without knowledge of Solana. Solana is an excellent blockchain, and I would be delighted if you could take this opportunity to casually explore it using Solana Suite.

Bonus:

Although not directly related to cNFTs, Solana Suite's responses are implemented using the Result Type, a pattern commonly used in functional programming languages like Rust. Handling responses from functions can be done as follows. Additionally, exceptions occurring internally are converted to Result Type using Try().

const res = await mintInst.submit();
if (res.isOk) {
    // success
} else if (res.isErr) {
    // error
}
const res = await mintInst.submit();
res.unwrap();
await mintInst.submit().match(
    (value: string) => // success,
    (error: Error)  => // error,
);