7

I'm working on a web app that can connect to Phantom Wallet. I've established the connection and have successfully retrieved the wallet's public key. The problem is, I can't seem to find any solution to get the account balance.

For reference, I wanted to display the account balance just like how solanart.io displays it.

Note that I've gone through all related docs (Solana/web3.js, Solana JSON RPC API etc). Please guide me as I'm still new to JSON RPC API.

For a heads up, I'm using vanilla js.

try {
      const resp = window.solana.request({
        method: "getAccountTokenBalance",
        params: [
          id, //wallet's public key
          {
            encoding: "base58",
          },
        ],
      });
      console.log(resp);
    } catch(err) {
      // error message
    }
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
Afa12
  • 73
  • 1
  • 4
  • 1
    https://solana-labs.github.io/solana-web3.js/classes/Connection.html#getBalance this should help. – munanadi Oct 11 '21 at 10:49

3 Answers3

2
import { useWallet } from '@solana/wallet-adapter-react'
import { LAMPORTS_PER_SOL,clusterApiUrl } from '@solana/web3.js'
import * as anchor from '@project-serum/anchor'


const wallet = useWallet()
const SOLANA_HOST = clusterApiUrl("devnet")
const connection = new anchor.web3.Connection(SOLANA_HOST)

let lamportBalance
if (wallet?.publicKey) {
        const balance = await connection.getBalance(wallet.publicKey)
        lamportBalance=(balance / LAMPORTS_PER_SOL)
      }
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
1

The RPC method that you're using does not exist. You'll want to use getBalance to get the SOL on the wallet: https://docs.solana.com/developing/clients/jsonrpc-api#getbalance

To get all of the non-SOL token balances owned by that wallet, you'll have to use getTokenAccountsByOwner using that wallet id: https://docs.solana.com/developing/clients/jsonrpc-api#gettokenaccountsbyowner

Jon C
  • 7,019
  • 10
  • 17
  • 2
    Thanks Jon for pointing this out. I've changed my code structure and used the getBalance. Finally, it works! – Afa12 Oct 15 '21 at 02:07
  • 2
    What did you change your construct to? Can you please provide a working Vanilla example? I have a connection, and I can get the address, but when I run this line to get a balance I get a -32603 error. var balance = window.solana.request({ "jsonrpc": "2.0", "id": 1, "method": "getBalance", "params": [wallet_address] }); – AIRAD LABS Oct 18 '21 at 00:22
  • I'm getting the wallet_address from: var wallet_address = window.solana.publicKey.toString(); – AIRAD LABS Oct 18 '21 at 00:27
  • https://stackoverflow.com/questions/69609838/how-to-get-solana-account-info-and-or-sol-balance-using-vanilla-js-and-json-rpc – AIRAD LABS Oct 18 '21 at 01:26
  • 1
    How can you get metadata on nft – Vince Oct 26 '21 at 23:10
0

This method works for me to get SOL Balance

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