solana-web3.js Invalid param: unrecognized Token program id

2.5k Views Asked by At

I am trying to get all NFT's from a solana wallet. I am using Connection.getTokenAccountsByOwner method in the solana-web3.js API.

Error reads: Error: failed to get token accounts owned by account <wallet key>: Invalid param: unrecognized Token program id

Reproduce:

async function getCollection(pk){
   try {
        //connect to solana cluster
        var connection = new web3.Connection(
          web3.clusterApiUrl('devnet'),
          'confirmed',
        );
        //get token accounts from wallet
        const nfts = await connection.getTokenAccountsByOwner(pk, {programId: pk})
        console.log(nfts);
   } catch (error) {
        console.log("Error getting NFT:" + error);
      }
}

I do not understand exactly what the 'filter' parameter {programId: publicKey} is for. I want to access all NFT's in the account, not just ones I know the mint or program ID of.

I believe this is possible. Any help would be greatly appreciated!

1

There are 1 best solutions below

1
On

I have resolved my issue. The 'programId' needs to be the solana token program id.

import * as web3 from '@solana/web3.js';
import {TOKEN_PROGRAM_ID} from '@solana/spl-token'

//Each account contains a different token type. 
//NFT's will have only one token in account (I believe)
const accounts = await connection.getTokenAccountsByOwner(pk, {programId: TOKEN_PROGRAM_ID})

Best I can tell, from here you take each NFT's mint address and search it with metaplex API to get metadata.

Helpful: https://yihau.github.io/solana-web3-demo/advanced/token/get-all-token-account-by-owner.html