[英]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.