1

From freeCodeCampWeb3's Solana curriculum, build-a-university-nft-certification project:

"getMintAccounts should return all mint accounts owned by the provided payer parameter"

export async function getMintAccounts({ payer }) {
  const mintAccounts = await connection.getParsedTokenAccountsByOwner(
    payer.publicKey,
    {
      mint,
    },
  );

  console.log(mintAccounts);

  return mintAccounts.value.map(({ account }) => account);
}

For somewhat reason it returns an empty array. Performing a JSONRPC call using method of getProgramAccounts using the TOKEN_PROGRAM_ID shows an account with a pubkey value corresponding to the newly created mint.

1 Answer 1

4
import { Connection, GetProgramAccountsFilter } from "@solana/web3.js";
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";

const rpcEndpoint = 'https://example.solana-mainnet.quiknode.pro/000000/';
const solanaConnection = new Connection(rpcEndpoint);

const walletToQuery = 'YOUR_PUBLIC_KEY'; //example: vines1vzrYbzLMRdu58ou5XTby4qAqVRLmqo36NKPTg

async function getTokenAccounts(wallet: string, solanaConnection: Connection) {
    const filters:GetProgramAccountsFilter[] = [
        {
          dataSize: 165,    //size of account (bytes)
        },
        {
          memcmp: {
            offset: 32,     //location of our query in the account (bytes)
            bytes: wallet,  //our search criteria, a base58 encoded string
          },            
        }];
    const accounts = await solanaConnection.getParsedProgramAccounts(
        TOKEN_PROGRAM_ID, //new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
        {filters: filters}
    );
    console.log(`Found ${accounts.length} token account(s) for wallet ${wallet}.`);
    accounts.forEach((account, i) => {
        //Parse the account data
        const parsedAccountInfo:any = account.account.data;
        const mintAddress:string = parsedAccountInfo["parsed"]["info"]["mint"];
        const tokenBalance: number = parsedAccountInfo["parsed"]["info"]["tokenAmount"]["uiAmount"];
        //Log results
        console.log(`Token Account No. ${i + 1}: ${account.pubkey.toString()}`);
        console.log(`--Token Mint: ${mintAddress}`);
        console.log(`--Token Balance: ${tokenBalance}`);
    });
}
getTokenAccounts(walletToQuery,solanaConnection);

source: https://www.quicknode.com/guides/solana-development/spl-tokens/how-to-get-all-tokens-held-by-a-wallet-in-solana

After that if you want get metadata you can use fetchDigitalAsset Check here: https://developers.metaplex.com/token-metadata/fetch

1
  • Still getting an empty array ``` export async function createMintAccount({ payer }) { mint = await createMint(connection, payer, payer.publicKey, payer.publicKey, 0); console.log('Mint: ', mint.toBase58()); return mint; } ``` If it helps, this is the function that creates a mint account
    – Woods
    Commented Jun 2 at 6:28

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.