Given the address of a solana wallet, how can I get its SOL balance?


1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.