我和幻影钱包有联系。
我期望phantom_balance()
返回包含余额的一个或多个数据。
json-rpc
格式中有错误吗?
我在catch()
上得到了下面的catch()
故障。
Phantom - RPC Error: JsonRpcEngine: Response has no error or result for request:
{
"jsonrpc": "2.0",
"id": 1161935556,
"method": "getBalance",
"params": [
"iQKBP3tSX6g3j6pS6g3LuzhoqTQKBP3tSX6g33tSX6g3j6pS6g"
]
}
这是js
// get wallet provider, phantom
const get_provider = () => {
if ("solana" in window) {
const provider = window.solana;
if (provider.isPhantom) {
return provider;
}
}
window.open("https://phantom.app/", "_blank");
};
// async get wallet balance
async function phantom_balance() {
// alt window.solana
const phantom = get_provider();
console.log("Still Connected: " + phantom.isConnected);
if (phantom.isConnected !== false) {
const wallet_address = phantom.publicKey.toString();
console.log("Getting Balance For: " + wallet_address);
// json-rpc request (ERROR!)
const balance = await phantom.request({
method: "getBalance",
params: [wallet_address]
})
.then(function(data) {
console.log(data);
}).catch(function(e) {
console.log(e);
});
}
}
我还尝试了以下操作,并得到了一个错误,说明phantom.getBalance()
不是一个函数。
const balance = await phantom.getBalance(wallet_address)
.then(function(data) {
console.log(data);
}).catch(function(e) {
console.log(e);
});
我目前没有将任何<script>
包含在文档的头上。根据幻影文档,它们将window.solana
注入文档(这也公开了RPC接口)。也许我还需要web3来访问window.solana.getBalance(wallet_address)
和/或RPC方法window.solana.request(method: "getBalance")
?
https://stackoverflow.com/questions/69638508
复制相似问题