9

Given a specific public key of a wallet, how do I get all NFTs that wallet owns?

1
  • Both of the solutions provided above doesn't work. First solution always return the NFTs of the same wallet address, no matter which wallet you provide. Second solution the package just can't resolve a solana address thus throwing an error every time. Probably an issue with webpack5, but webpack5 is mandatory to use metaplex so can't downgrade. Does anyone has a fix or a better solution.
    – 2095377
    Nov 7, 2022 at 20:10

2 Answers 2

10

Given a wallet address, you can use findAllByOwner() from @metaplex-foundation/js to get all the NFTs that wallet owns.

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);
})();
3

You can use the @nfteyez/sol-rayz package to parse NFTs on Solana. This package is a good alternative to @metaplex-foundation/js

import {
    resolveToWalletAddress,
    getParsedNftAccountsByOwner,
} from "@nfteyez/sol-rayz";

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

  const publicAddress = await resolveToWalletAddress({
    text: new PublicKey("BtrH3c2zVjYdFcVXkv4CCPvTMEFesWjs6yy4xxdtPF2u"),
    connection,
  }));

  const NFTs = await getParsedNftAccountsByOwner({
    publicAddress,
    connection,
  })


  console.log(NFTs);
})();

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.