Fetching and Updating Solana Wallet Balances

Introduction

This TypeScript tutorial guides you through fetching and updating wallet balances on the Solana blockchain. You’ll learn how to retrieve SOL balances programmatically and perform airdrops, enabling you to manage wallet funds wit…


This content originally appeared on DEV Community and was authored by Sumana

Introduction

This TypeScript tutorial guides you through fetching and updating wallet balances on the Solana blockchain. You'll learn how to retrieve SOL balances programmatically and perform airdrops, enabling you to manage wallet funds with ease.

Step 1: Setting Up Your TypeScript Project

First, set up your TypeScript project:

npm init -y
npm install --save-dev typescript
npx tsc --init

Step 2: Installing Solana Web3.js Library

Install the Solana Web3.js library:

npm install --save @solana/web3.js

Step 3: Fetching and Updating Wallet Balances

Create index.ts:

import { Connection, PublicKey, LAMPORTS_PER_SOL } from "@solana/web3.js";
import { airdrop } from "../airdrop";

export const showBalance = async (publicKey: PublicKey): Promise<number | null> => {
    const conn = new Connection("http://127.0.0.1:8899", "confirmed");
    const response = await conn.getAccountInfo(publicKey);

    if (response === null) {
        return null;
    }
    return response.lamports / LAMPORTS_PER_SOL;
};

(async () => {
    const publicKey = "Your Key";
    const balance = await showBalance(new PublicKey(publicKey));
    console.log(`The balance of the ${publicKey} is ${balance}`);

    await airdrop(publicKey, 5);

    const updatedBalance = await showBalance(new PublicKey(publicKey));
    console.log(`Updated balance of the ${publicKey} is ${updatedBalance}`);
})();

Step 4: Building and Executing Your Project

Build your project:

npm run build

Run your project:

node dist/index.js

Conclusion

You've successfully fetched and updated a Solana wallet's balance using TypeScript. Keep exploring Solana's capabilities to build innovative and secure applications🚀!

P.S. If you need help with airdropping SOL, check out our Airdrop Post for a detailed guide.


This content originally appeared on DEV Community and was authored by Sumana


Print Share Comment Cite Upload Translate Updates
APA

Sumana | Sciencx (2024-08-12T17:47:55+00:00) Fetching and Updating Solana Wallet Balances. Retrieved from https://www.scien.cx/2024/08/12/fetching-and-updating-solana-wallet-balances/

MLA
" » Fetching and Updating Solana Wallet Balances." Sumana | Sciencx - Monday August 12, 2024, https://www.scien.cx/2024/08/12/fetching-and-updating-solana-wallet-balances/
HARVARD
Sumana | Sciencx Monday August 12, 2024 » Fetching and Updating Solana Wallet Balances., viewed ,<https://www.scien.cx/2024/08/12/fetching-and-updating-solana-wallet-balances/>
VANCOUVER
Sumana | Sciencx - » Fetching and Updating Solana Wallet Balances. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/12/fetching-and-updating-solana-wallet-balances/
CHICAGO
" » Fetching and Updating Solana Wallet Balances." Sumana | Sciencx - Accessed . https://www.scien.cx/2024/08/12/fetching-and-updating-solana-wallet-balances/
IEEE
" » Fetching and Updating Solana Wallet Balances." Sumana | Sciencx [Online]. Available: https://www.scien.cx/2024/08/12/fetching-and-updating-solana-wallet-balances/. [Accessed: ]
rf:citation
» Fetching and Updating Solana Wallet Balances | Sumana | Sciencx | https://www.scien.cx/2024/08/12/fetching-and-updating-solana-wallet-balances/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.