简体   繁体   English

如何通过 Phantom 钱包集成使用 Vanilla JS 和 JSON-RPC 获取 Solana 帐户信息和/或 SOL 余额?

[英]How to get Solana Account Info and-or SOL Balance using Vanilla JS and JSON-RPC via Phantom wallet integration?

The following Vanilla JS example connects to and disconnects from the Solana blockchain via Phantom wallet.以下 Vanilla JS 示例通过 Phantom 钱包连接和断开 Solana 区块链。

It successfully connects and gets the public address.它成功连接并获得公共地址。

It fails when trying to use a JSON-RPC request to get the wallet balance and the account info.尝试使用 JSON-RPC 请求获取钱包余额和帐户信息时失败。

If someone could help sort this out, we'll have some basic examples for those of us who prefer to keep it Vanilla when possible.如果有人可以帮助解决这个问题,我们将为我们这些喜欢尽可能保留原版的人提供一些基本示例。

Connect Function:连接功能:

// Connect Phantom
function phantom_connect() {

  // Check for Solana & Phantom
  var provider = () => {
    if ("solana" in window) {
      var provider = window.solana;
      if (provider.isPhantom) {
        return provider;
      } else {
        return false;
      }
    }
    window.open("https://phantom.app", "_blank");
  };

  var phantom = provider();

  if (phantom !== false) {

    console.log("Phantom Wallet Found, Connecting..");

    try {

      // Connect to Solana
      var connect_wallet = phantom.connect();

      // After Connecting
      phantom.on("connect", () => {

        // Check Connection
        console.log("Phantom Connected: " + phantom.isConnected);

        // Get Wallet Address
        var wallet_address = phantom.publicKey.toString();
        console.log("Solana Wallet Address: " + wallet_address);


        // ********** THIS FAILS **********
        // Get Account Info
        var account = phantom.request({
          "jsonrpc": "2.0",
          "id": 1,
          "method": "getAccountInfo",
          "params": [wallet_address, {
            "encoding": "jsonParsed"
          }]
        });
        console.log("Solana Account Info:");
        console.log(account);
        // ********************************


        // ********** THIS FAILS **********
        // Get Wallet Balance
        var balance = phantom.request({
          "jsonrpc": "2.0",
          "id": 1,
          "method": "getBalance",
          "params": [wallet_address]
        });
        console.log("Solana Wallet Balance:");
        console.log(balance);
        // ********************************


      });
      //

    } catch (err) {
      console.log("Connection Cancelled!");
    }
  }

}

Disconnect Function:断开功能:

// Disconnect Phantom
function phantom_disconnect() {
  window.solana.request({
    method: "disconnect"
  });
  window.solana.on('disconnect', () => {
    console.log("Phantom Disconnected!");
  });
}

The console shows a -32603 error on both getBalance and getAccountInfo.控制台在 getBalance 和 getAccountInfo 上均显示 -32603 错误。

RPC Error: JsonRpcEngine: Response has no error or result for request:

It doesn't use JSON-RPC API, but I put below my code to get Solana (Phantom) Wallet Balance on Devnet .它不使用 JSON-RPC API,但我在代码下方放置了Devnet上的 Solana (Phantom) Wallet Balance。

provider = window.solana;
connection = new solanaWeb3.Connection(solanaWeb3.clusterApiUrl('devnet'), 'confirmed');
// After Connecting
connection.getBalance(provider.publicKey).then(function(value) { console.log(value); })

I used this method using connection.getAccountInfo and stored it in the state我使用 connection.getAccountInfo 使用此方法并将其存储在状态中

const [userSOLBalance, setSOLBalance] = useState<number>()
if (wallet.publicKey) {
  const SOL = connection.getAccountInfo(wallet.publicKey)
  SOL.then((res) => setSOLBalance(res.lamports / LAMPORTS_PER_SOL))
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 javascript async function 通过 json-rpc 请求获取 Phantom 钱包余额时出现问题? - Problem getting the balance of Phantom wallet with json-rpc request using javascript async function? Solana - 如何从我的 Phantom 钱包中获取余额? - Solana - How to get the balance from my Phantom wallet? 如何通过 Phantom 使用 web3js 正确传输 Solana SOL - How to properly transfer Solana SOL using web3js via Phantom 幻影钱包中的“TypeError:x.pubkey.toBase58 不是函数”...使用@solana/web3.js 将 sol 转移到 JavaScript 中的另一个帐户 - "TypeError: x.pubkey.toBase58 is not a function" in phantom wallet...transfer sol to another account in JavaScript with@solana/web3.js 如何使用 Web3.js 通过 Solana Pay 连接虚拟钱包? - How to connect phantom wallet by Solana Pay using Web3.js? 如何使用虚拟钱包和 solana web3js 转移 NFT spl-token - How to transfer NFT spl-token using phantom wallet and solana web3js 如何通过“@solana/web3.js”和“@solana/sol-wallet-adapter”传输自定义 SPL 令牌 - How to transfer custom SPL token by '@solana/web3.js' and '@solana/sol-wallet-adapter' 如何使用 Phantom Wallet 提供商创建帐户/签名者? - How to create account/signer using Phantom Wallet provider? Solana:使用 Js 从 NFT 代币获取 Candy Machine 信息 - Solana: Get Candy Machine info from a NFT token using Js Uncaught (in promise) ReferenceError: Buffer is not defined。 使用 Phantom Wallet、Solana 和 React 进行交易 - Uncaught (in promise) ReferenceError: Buffer is not defined. Using Phantom Wallet, Solana and React to make a transaction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM