I'm using the Solana dApp Scaffold to build a basic app.
How do I check if the connected wallet has X NFT?
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);
})();
X
NFT do you mean if the wallet has a specific NFT from a collection? Or any NFT from a particular collection?