2

I'm working on a front-end. I want to fetch all the NFT of a specific collection the user owns. I used to do:

const tokens = await metaplex
    .nfts()
    .findAllByOwner({ owner: provider.publicKey });

But I recently noticed this returned an empty array even though the wallet I'm using has the tokens. To bypass this issue, I started using getProgramAccounts like so to get it to work properly:

const mints = (
  await provider.connection.getParsedTokenAccountsByOwner(
    provider.publicKey,
    { programId: TOKEN_PROGRAM_ID }
  )
).value
  .filter((e) => e.account.data.parsed.info.tokenAmount.uiAmount === 1)
  .map((e) => new PublicKey(e.account.data.parsed.info.mint));
const accounts: (Nft | null)[] = (
  await Promise.all(
    mints.map(async (e) => {
      try {
        return (await metaplex
          .nfts()
          .findByMint({ mintAddress: e })) as any;
      } catch {
        return null;
      }
    })
  )
).filter(Boolean);

Is there an explanation for this? Is there a way to make the old expression work? It's much more compact and easier to read.

2
  • did you add a check if provider.publicKey is not equal to undefined ?
    – Pratik.js
    Nov 13, 2022 at 16:05
  • Yes I do and the other way of fetching data works. Nov 13, 2022 at 17:47

1 Answer 1

1

It turns out to be a provider error: I switched from using Alchemy provider to Helius and it now works as expected.

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.