admin管理员组文章数量:1025322
The following Vanilla JS example connects to and disconnects from the Solana blockchain via Phantom wallet.
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.
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(";, "_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.
RPC Error: JsonRpcEngine: Response has no error or result for request:
The following Vanilla JS example connects to and disconnects from the Solana blockchain via Phantom wallet.
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.
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.
Share Improve this question edited Oct 21, 2022 at 15:45 TylerH 21.1k79 gold badges79 silver badges114 bronze badges asked Oct 18, 2021 at 1:25 DapperDapper 1361 gold badge4 silver badges13 bronze badgesRPC Error: JsonRpcEngine: Response has no error or result for request:
2 Answers
Reset to default 4It doesn't use JSON-RPC API, but I put below my code to get Solana (Phantom) Wallet Balance on Devnet
.
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
const [userSOLBalance, setSOLBalance] = useState<number>()
if (wallet.publicKey) {
const SOL = connection.getAccountInfo(wallet.publicKey)
SOL.then((res) => setSOLBalance(res.lamports / LAMPORTS_PER_SOL))
}
The following Vanilla JS example connects to and disconnects from the Solana blockchain via Phantom wallet.
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.
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(";, "_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.
RPC Error: JsonRpcEngine: Response has no error or result for request:
The following Vanilla JS example connects to and disconnects from the Solana blockchain via Phantom wallet.
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.
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.
Share Improve this question edited Oct 21, 2022 at 15:45 TylerH 21.1k79 gold badges79 silver badges114 bronze badges asked Oct 18, 2021 at 1:25 DapperDapper 1361 gold badge4 silver badges13 bronze badgesRPC Error: JsonRpcEngine: Response has no error or result for request:
2 Answers
Reset to default 4It doesn't use JSON-RPC API, but I put below my code to get Solana (Phantom) Wallet Balance on Devnet
.
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
const [userSOLBalance, setSOLBalance] = useState<number>()
if (wallet.publicKey) {
const SOL = connection.getAccountInfo(wallet.publicKey)
SOL.then((res) => setSOLBalance(res.lamports / LAMPORTS_PER_SOL))
}
本文标签:
版权声明:本文标题:javascript - How to get Solana Account Info and-or SOL Balance using Vanilla JS and JSON-RPC via Phantom wallet integration? - S 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745620487a2159542.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论