最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Solana check all spl token balances of a wallet - Stack Overflow

programmeradmin4浏览0评论

I'm using solana json rpc api to check a wallet's token balance from my javascript app. I have used the function for it like this

const getTokenBalance = async (walletAddress, tokenMintAddress) => {
  const response = await axios({
    url: ``,
    method: "post",
    headers: { "Content-Type": "application/json" },
    data: {
      jsonrpc: "2.0",
      id: 1,
      method: "getTokenAccountsByOwner",
      params: [
        walletAddress,
        {
          mint: tokenMintAddress,
        },
        {
          encoding: "jsonParsed",
        },
      ],
    },
  });
  if (
    Array.isArray(response?.data?.result?.value) &&
    response?.data?.result?.value?.length > 0 &&
    response?.data?.result?.value[0]?.account?.data?.parsed?.info?.tokenAmount
      ?.amount > 0
  ) {
    return (
      Number(
        response?.data?.result?.value[0]?.account?.data?.parsed?.info
          ?.tokenAmount?.amount
      ) / 1000000000
    );
  } else {
    return 0;
  }
};

However I want to get all the token balance by one call instead of asking a token balance by giving a mint address for every token out there which makes my api respond like 10 minutes, is there any friendly way to do that?

I saw Covalent api can do it for checking ethereum wallet balance, wonder how they can do it

I'm using solana json rpc api to check a wallet's token balance from my javascript app. I have used the function for it like this

const getTokenBalance = async (walletAddress, tokenMintAddress) => {
  const response = await axios({
    url: `https://api.mainnet-beta.solana.com`,
    method: "post",
    headers: { "Content-Type": "application/json" },
    data: {
      jsonrpc: "2.0",
      id: 1,
      method: "getTokenAccountsByOwner",
      params: [
        walletAddress,
        {
          mint: tokenMintAddress,
        },
        {
          encoding: "jsonParsed",
        },
      ],
    },
  });
  if (
    Array.isArray(response?.data?.result?.value) &&
    response?.data?.result?.value?.length > 0 &&
    response?.data?.result?.value[0]?.account?.data?.parsed?.info?.tokenAmount
      ?.amount > 0
  ) {
    return (
      Number(
        response?.data?.result?.value[0]?.account?.data?.parsed?.info
          ?.tokenAmount?.amount
      ) / 1000000000
    );
  } else {
    return 0;
  }
};

However I want to get all the token balance by one call instead of asking a token balance by giving a mint address for every token out there which makes my api respond like 10 minutes, is there any friendly way to do that?

I saw Covalent api can do it for checking ethereum wallet balance, wonder how they can do it

Share Improve this question asked Oct 24, 2021 at 19:39 onuriltanonuriltan 3,9085 gold badges27 silver badges39 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 11

Most of the standard RPC accept batch requests, you should be able to send an array of all the requests you want, note that response will be an array as well.

// For example

const response = await axios({
    url: `https://api.mainnet-beta.solana.com`,
    method: "post",
    headers: { "Content-Type": "application/json" },
    data: [
        {
          jsonrpc: "2.0",
          id: 1,
          method: "getTokenAccountsByOwner",
          params: [
            walletAddress,
            {
              mint: tokenMintAddress,
            },
            {
              encoding: "jsonParsed",
            },
          ],
        },
        {
          jsonrpc: "2.0",
          id: 1,
          method: "getTokenAccountsByOwner",
          params: [
            walletAddress2,
            {
              mint: tokenMintAddress2,
            },
            {
              encoding: "jsonParsed",
            },
          ],
        },
    ]
});

As all tokens (that follow the standard) are "childs" of the Token Program you can get all with one RPC call :

curl https://api.mainnet-beta.solana.com -X POST -H "Content-Type: 
application/json" -d ' {                                                                                
    "jsonrpc":"2.0",
    "method":"getTokenAccountsByOwner",
    "params": [
       "walletAddress",
      {
        "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
      }, 
      {
        "encoding": "jsonParsed"
      }
    ],
    "id":4
  } 

There are actully 4 ways to fetch balance of tokens

  1. Solana CLI
  2. Solana-Web3.js (Worked for me) using getOrCreateAssociatedTokenAccount(connection, provider, mintPublicKey, provider.publicKey)
  3. cURL
  4. Rust

All method details are mentioned in the below link.

https://www.quicknode.com/guides/solana-development/getting-started/how-to-look-up-the-balance-of-a-solana-wallet

If u need to get a balance of ur own token u can used the json rpc api with the follow post.

    curl https://api.devnet.solana.com/ -X POST -H "Content-Type: application/json" -d '
  {
    "jsonrpc": "2.0",
    "id": 1,
    "method": "getTokenAccountsByOwner",
    "params": [
      "uja3w9XG1g6DQSVT6YASK99FVmdVwXoHVoQEgtEJdLv",
      {
        "mint": "7TMzmUe9NknkeS3Nxcx6esocgyj8WdKyEMny9myDGDYJ"
      },
      {
        "encoding": "jsonParsed"
      }
    ]
  }
'
发布评论

评论列表(0)

  1. 暂无评论