Next, let's fetch the balance of a specific wallet address. Add the following function to your index.js
file:
async function getWalletBalance(walletAddress) {
try {
const publicKey = new solanaWeb3.PublicKey(walletAddress);
const balance = await connection.getBalance(publicKey);
console.log(`Wallet Balance: ${balance / solanaWeb3.LAMPORTS_PER_SOL} SOL`);
} catch (error) {
console.error('Error fetching wallet balance:', error);
}
}
// Replace with your Solana wallet address
const walletAddress = 'YourWalletAddressHere';
getWalletBalance(walletAddress);