Given the address of a solana wallet, how can I get its SOL balance?
1 Answer
import { Connection, LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js';
const connection = new Connection("https://api.mainnet-beta.solana.com");
const walletKey = new PublicKey("D33XEQK2xnLBp6LSzpTR3PS1CL6nZyB5QEKpXQxbUVMx");
//The following returns the balance in LAMPORTS - https://docs.solana.com/terminology#lamport
const balance = await connection.getBalance(walletKey)
//So we convert it to SOL
const solBalance = balance / LAMPORTS_PER_SOL