javascriptsolanasolana-web3jsanchor-solana

How can I get all token information in my Solana wallet?


I tried to get all token information in my wallet using solana/web3 but I can't get it.

I need token address, token amount, token price, token name, token symbol at least.

How can I get this data?

This is my current code that gets the token mint address and amount.

const getTokenFromTokenAccount = async (tokenAccount) => {
  const Ta = new PublicKey(tokenAccount);
  const AccInfo = await connection.getParsedAccountInfo(Ta, {
    commitment: "confirmed",
  });

  const splData = AccInfo.value?.data;
  let splToken = "";
  if (splData && "parsed" in splData) {
    const parsed = splData.parsed;
    splToken = parsed.info.mint;
    amount = parsed.info.tokenAmount.uiAmount;

    return { mint: splToken, amount: amount };
  }
};

const getAllTokenInfo = async () => {
  const wallet = new PublicKey("pkrQznmnts6q3vDfbDwuaj5JPqGmjiC6gJfx4xcAv5n");
  const tokenProgramId = new PublicKey(
    "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" // SPL TOKEN - TOKEN_PROGRAM_ID
  );
  const token2022ProgramId = new PublicKey(
    "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb" // TOKEN 2022 - TOKEN_PROGRAM_ID
  );

  let tokenAccounts = [];

  const splTokenAccounts = await connection.getTokenAccountsByOwner(wallet, {
    programId: tokenProgramId,
  });
  const token2022Accounts = await connection.getTokenAccountsByOwner(wallet, {
    programId: token2022ProgramId,
  });

  tokenAccounts = [...splTokenAccounts.value, ...token2022Accounts.value];

  let tokenInfoArray = [];
  for (let i = 0; i < tokenAccounts.length; i++) {
    const tokenInfo = await getTokenFromTokenAccount(
      tokenAccounts[i].pubkey.toBase58()
    );
    tokenInfoArray.push(tokenInfo);
  }
  return tokenInfoArray;
};

Solution

  • In general, you can easily get all token list and information in your wallet using your Birdeye API.

    Please check this:

    const axios = require("axios");
    const dotenv = require("dotenv");
    
    dotenv.config();
    
    const BIRDEYE_API_KEY = process.env.BIRDEYE_API_KEY;
    
    async function getTokenOverview(tokenAddress) {
      const res = await axios.get(
        "https://public-api.birdeye.so/defi/token_overview",
        {
          params: { address: tokenAddress },
          headers: {
            accept: "application/json",
            "x-chain": "solana",
            "x-api-key": BIRDEYE_API_KEY,
          },
        }
      );
      if (res.data?.success) {
        return res.data?.data;
      } else {
        return null;
      }
    }
    
    
    async function getWalletTokenList(walletAddress) {
      const res = await axios.get(
        "https://public-api.birdeye.so/v1/wallet/token_list",
        {
          params: { wallet: walletAddress },
          headers: {
            accept: "application/json",
            "x-chain": "solana",
            "x-api-key": BIRDEYE_API_KEY,
          },
        }
      );
      if (res.data?.success) {
        return res.data?.data;
      } else {
        return null;
      }
    }
    
    module.exports = {
      getTokenOverview,
      getWalletTokenList,
    }
    

    Also, tokenPrice varies slightly from DEX to DEX.

    I hope this helps you.