Bitcoin: How do I cash in Physical Bitcoins?
February 11, 2025Market Signals, Order Book, Liquidation
February 11, 2025
Verifying NFT Collection Membership in Solana using mpl_token_metadata
Are you working with Solana and need to verify whether an NFT belongs to a specific collection on the network? In this article, we will explore how to use mpl_token_metadata::state::Metadata to achieve this.
What is mpl_token_metadata::state::Metadata?
mpl_token_metadata::state::Metadata is a data structure that stores information about an NFT’s metadata, including its ownership and collection membership. In Solana, the mpl_token_metadata library provides a way to interact with minted tokens and verify their properties.
Checking if an NFT belongs to a specific collection
To check if an NFT belongs to a specific collection on Solana, you can use the mpl_token_metadata::state::Metadata data structure. Here’s an example of how to do this:
use mpl_token_metadata::{TokenId, TokenMetadata};
fn main() {
// Create a token metadata instance
let metadata = TokenMetadata::from_id_and_owner("some-token-id", "some-token-owner");
// Get the current block timestamp (this is used for pagination)
let now = BlockNumber::zero();
// Check if the token belongs to the "some-collection" collection
match metadata.state().collection() {
Some(collection) => println!("Token belongs to collection: {}", collection),
None => println!("Token does not belong to a specific collection"),
}
}
Using mpl_token_metadata::state::Metadata with spl-merkle-v2

One of the features of Solana’s spl-merkle-v2 consensus algorithm is that it uses a Merkle-based proof system to verify the ownership and collection membership of NFTs. To use spl-merkle-v2, you need to create a SplV2Proof instance, which contains a merkle root and other necessary metadata.
Here’s an example of how to use mpl_token_metadata::state::Metadata with spl-merkle-v2:
use mpl_token_metadata::{TokenId, TokenMetadata, SplV2Proof};
use spl_merkle_v2::SplV2ProofState;
fn main() {
// Create a token metadata instance
let metadata = TokenMetadata::from_id_and_owner("some-token-id", "some-token-owner");
// Get the current block timestamp (this is used for pagination)
let now = BlockNumber::zero();
// Check if the token belongs to the "some-collection" collection using spl-merkle-v2
match metadata.state().spl_2_proof_state() {
Some(proof) => {
println!("Token belongs to collection: {}", proof.collection());
}
None => println!("Token does not belong to a specific collection"),
}
}
Example Use Case
Suppose you have an NFT with the following metadata:
{
"id": "some-token-id",
"owner": "some-token-owner",
"collection_id": "some-collection-id"
}
To verify whether this token belongs to the “some-collection” collection, you can use the mpl_token_metadata::state::Metadata data structure:
use mpl_token_metadata::{TokenId, TokenMetadata};
fn main() {
// Create a token metadata instance
let metadata = TokenMetadata::from_id_and_owner("some-token-id", "some-token-owner");
// Get the current block timestamp (this is used for pagination)
let now = BlockNumber::zero();
match metadata.state().collection() {
Some(collection) => println!("Token belongs to collection: {}", collection),
None => println!("Token does not belong to a specific collection"),
}
}
By using mpl_token_metadata::state::Metadata with spl-merkle-v2, you can efficiently verify whether an NFT’s mint address belongs to a specific collection on Solana.
