NFT Collection Membership in Solana using MPL_TOKEN_METADATA
Do you work with Solana and you have to check if the NFT belongs to a specific network collection? In this article, we will examine how to achieve this on MPL_TOKEN_METADATA :: STATE :: Metadata “.
What is MPL_TOKEN_METADATA :: STATE :: Metadata “?
MPL_TOKEN_METADATA:: STATE :: Metadata “is a data structure that stores information about NFT metadata, including its property and membership in the collection. At the Solana Library, MPL_Token_metadata, is a way to interact with minted chips and check their properties.
Checking whether NFT belongs to a specific collection
You can use the MPL_TOKEN_METADATA :: STATE :: Metadata data structure to check that NFT belongs to the Solana collection. Here’s an example of how to do it:
`Rūdys
Use MPL_TOKEN_METADATA :: {tokenid, tokenemetadata};
FN Main () {
// Create a copy of the token metadata
Let Metadata = Tockey :: from_id_and_owner (“Some-token-ID”, “Some-token owner”);
// Get the current block time tag (this is used for page)
Let’s now = Blocknumber :: Zero ();
// Check that the token belongs to the collection “some collection”
compliance metadata.state (). Collection () {
Some (collection) => println! (“Token belongs to the collection: {}, collection),
No => Println! (“The word does not belong to the specific collection”),
}
}
`
Using MPL_TOKEN_METADATA :: STATE :: Metadata “withSPL-MERLLE-V2
**
One of the properties of Solana SPL-Meerkle-V2 consensus algorithm is that it uses a Merkle-based evidence system to verify NFT ownership and collection membership. To use SPL-Merkle-V2, you need to create a SPLV2PROON copy containing Merkle roots and other necessary metadata.
Here’s an example of how to use MPL_TOKEN_METADATA :: STATE :: Metadata with SPL-Merkle-V2:
`Rūdys
use mpl_token_metadata :: {tokenid, tokenmetadata, SPLV2 APROPOR};
Use SPL_merkle_V2 :: SPLV2PORPSTATE;
FN Main () {
// Create a copy of the token metadata
Let Metadata = Tockey :: from_id_and_owner (“Some-token-ID”, “Some-token owner”);
// Get the current block time tag (this is used for page)
Let’s now = Blocknumber :: Zero ();
// Verify that the token belongs to the collection “some collection” using SPL-Merler-V2
compliance metadata.state (). Spl_2_poropore_state () {
Some (evidence) => {
Println! (“The word belongs to the collection: {}”, proof.collection ());
}
No => Println! (“The word does not belong to the specific collection”),
}
}
`
Example Use case
Suppose you have NFT with these metadata:
`Json
{
“ID”: “Some-token-ID,”
“Owner”: “Some-TEOK Owner”,
Collection_id: Some of the Collection-ID
}
`
You can use MPL_TOKEN_METADATA to check that this access key belongs to the collection of something collection :: :: :: Metadata “data structure:
`Rūdys
Use MPL_TOKEN_METADATA :: {tokenid, tokenemetadata};
FN Main () {
// Create the Metadata Metadata specimens
Let Metadata = Tockey :: from_id_and_owner (“Some-token-ID”, “Some-token owner”);
// Get the current block time tag (this is used for page)
Let’s now = Blocknumber :: Zero ();
compliance metadata.state (). Collection () {
Some (collection) => println! (“Token belongs to the collection: {}, collection),
No => Println! (Token does not belong to a specific collection “),
}
}
`
With the MPL_TOKEN_METADATA :: STATE :: Metadata with SPL-Merkle-V2, you can effectively check that the NFT mint address is owned by a specific Solana collection.