5

I'm using the Solana dApp Scaffold to build a basic app.

How do I check if the connected wallet has X NFT?

1
  • 1
    by X NFT do you mean if the wallet has a specific NFT from a collection? Or any NFT from a particular collection?
    – C.OG
    Commented Aug 8, 2022 at 7:43

1 Answer 1

2

Solution 1

You can use solana/web3.js. get all token accounts by owner then check if the token account with a certain mint has a balance of 1.

Solution 2

Get all NFTs then check if there's the NFT you are looking for.

import { Metaplex, keypairIdentity } from "@metaplex-foundation/js";
import { Connection, clusterApiUrl, Keypair, PublicKey } from "@solana/web3.js";

(async () => {
  const connection = new Connection(clusterApiUrl("mainnet-beta"), "confirmed");
  const keypair = Keypair.generate();

  const metaplex = new Metaplex(connection);
  metaplex.use(keypairIdentity(keypair));

  const owner = new PublicKey("2R4bHmSBHkHAskerTHE6GE1Fxbn31kaD5gHqpsPySVd7");
  const allNFTs = await metaplex.nfts().findAllByOwner(owner).run();

  console.log(allNFTs);
})();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.