0

我正在尝试从虚拟钱包中获取 SOL 余额。钱包里有 > 0 SOL,所以当我打电话时,我知道有问题。这是我的代码:

(async () => {
            const connection = new solanaWeb3.Connection(
                solanaWeb3.clusterApiUrl('devnet'),
                'confirmed',
            );

            if (phantom && phantom.publicKey !== null) {
                const balance = await connection.getBalance(phantom.publicKey)
                console.log(balance);
            }
})();

我究竟做错了什么?

4

1 回答 1

0

您的问题看起来可能与分配给 phantom.publicKey 本身而不是您的代码有关。用下面定义的公钥替换 phantom.public 密钥是可行的。

const solanaWeb3 = require('@solana/web3.js');

const public_key=new solanaWeb3.PublicKey("your Solana public address with non-0 balance");

(async () => {
            const connection = new solanaWeb3.Connection(
                solanaWeb3.clusterApiUrl('devnet'),
                'confirmed',
            );

            if (public_key !== null) {
                const balance = await connection.getBalance(public_key);
                console.log(balance)
            }
})();

于 2022-01-14T04:25:36.113 回答