Cnft

CNFT is an Elixir library for handling Compressed NFT (cNFT) transactions on the Solana blockchain. It provides a native interface through Rustler for efficient creation, minting, and transfer of compressed NFTs using the Metaplex Bubblegum protocol.

Installation

Add cnft to your list of dependencies in mix.exs:

def deps do
  [
    {:cnft, "~> 0.1.6"}
  ]
end

Prerequisites

Usage

Creating a Tree Config

The first step in working with compressed NFTs is creating a Merkle tree configuration:

# Configure your Solana connection
rpc_client = "https://api.mainnet-beta.solana.com"  # Or your preferred RPC endpoint

# Configure the payer account (must be funded)
payer = "Base58 encoded key or [bytes_array_of_private_key]"

# Configure tree parameters
# https://developers.metaplex.com/bubblegum/create-trees
max_depth = 14
max_buffer_size = 64

# Create the tree
{signature, tree_address} = CNFT.create_tree_transaction(
  rpc_client,
  payer,
  max_depth,
  max_buffer_size
)

Minting a New Compressed NFT

Once you have a tree configured, you can mint compressed NFTs:

# Tree address from the creation step
tree = "treePubkeyHere123456789abcdef"

# NFT recipient
owner = "recipientPubkeyHere123456789"

# NFT metadata
name = "My Awesome NFT"
symbol = "AWESOME"
uri = "https://arweave.net/yourMetadataJson"  # Should point to JSON matching Metaplex standard
seller_fee_basis_points = 500  # 5% royalty (500 basis points = 5%)
is_mutable = true  # Can metadata be updated later?
nonce = 0  # Uniquely identifies this mint, increment for each subsequent mint

# mint transaction
{signature, asset_id} = CNFT.mint_v1(
  rpc_client,
  tree,
  owner,
  payer,
  name,
  symbol,
  uri,
  seller_fee_basis_points,
  is_mutable,
  nonce
)

Transferring a Compressed NFT

Transfer an existing compressed NFT to a new owner:

receiver = "RECEIVER_PUBLIC_KEY" # The account that will receive the NFT

asset_id = "assetpublickeyaddress" # Asset ID from minting step
transfer_sign = CNFT.transfer(rpc_client, asset, owner, payer, receiver)

Fetching Asset Details

Returns the information of a compressed asset.

asset_id = "assetIdToLookup"
asset_details = CNFT.get_asset(rpc_client, asset_id)

Fetch Asset Proof

Returns the merkle tree proof information for a compressed asset.

asset_id = "assetIdToLookup"
asset_prrof = CNFT.get_proof(rpc_client, asset_id)

Best Practices

Demo

  1. create tree transaction
  2. mint cnft transaction
  3. transfer cnft transaction
Screenshot 2025-03-12 at 5 02 32 AM

Development

  1. Clone the repository
git clone https://github.com/thrishank/cnfts-elixir

cd cnfts-elixir
  1. Install dependencies with mix deps.get
mix deps.get
  1. Ensure Rust is installed for native compilation
mix compile
  1. update the varaible in test/cnfts_test.exs
@rpc_client ""
@private_key ""
@owner_key ""
@receiver_key ""
  1. Run tests
mix test

License

This project is licensed under the MIT License.