【问题标题】:Solana - How to get token balance for a foreign account?Solana - 如何获得外国账户的代币余额?
【发布时间】:2021-12-06 17:02:04
【问题描述】:

在 Solana 中,您可以通过 CLI 获得自己的平衡

$ spl-token accounts

但是,如果我有账户ID或他的公钥,我如何获得外国账户的代币余额?当我使用 solana explorer 时,我可以在搜索外国账户 ID 时看到我需要的信息,然后单击 Tokens 选项卡(“History”旁边): https://explorer.solana.com/address/DNuqHBGxzm96VLkLWCUctjYW9CX68DBY6jQ1cVuYP2Ai/tokens?cluster=devnet

所以如果浏览器网站可以做到,每个人都可以,区块链上的所有信息都是公开的,对吧?

【问题讨论】:

    标签: blockchain solana


    【解决方案1】:

    没错,确实一切都是公开的,所以如果你想得到别人账户的余额,你可以简单地使用getBalance如果是SOL(https://docs.solana.com/developing/clients/jsonrpc-api#getbalance)或getTokenAccountBalance如果是SPL Token账户( https://docs.solana.com/developing/clients/jsonrpc-api#gettokenaccountbalance)。

    【讨论】:

      【解决方案2】:

      在 Jon Cinque 的回答之后,以下代码为 SOL 取得了平衡(可能对其他人或未来的我有帮助):

      const web3 = require("@solana/web3.js");
      const { Keypair, Transaction, SystemProgram, LAMPORTS_PER_SOL, sendAndConfirmTransaction, clusterApiUrl } = require("@solana/web3.js");
      
      let secretKey = Uint8Array.from([233, 65, ... (rest of my secret)]);
      
      let fromKeypair = Keypair.fromSecretKey(secretKey);
      
      let connection = new web3.Connection(clusterApiUrl('devnet'));
      
      (async () => {
      
          const balance = await connection.getBalance(
              fromKeypair.publicKey
          );
          console.log(balance)
      })()
      

      输出:7912350560 这是正确的,因为我在该帐户中有 7.912350560 SOL。

      但是对于 SPL 令牌它还没有工作......

      【讨论】:

        猜你喜欢
        • 2021-07-22
        • 1970-01-01
        • 2022-11-14
        • 2021-12-10
        • 1970-01-01
        • 2022-11-10
        • 1970-01-01
        • 2011-12-09
        • 2022-11-16
        相关资源
        最近更新 更多