Easy Solana Token Airdrops: A TypeScript Guide

sumana10

Sumana

Posted on August 10, 2024

Easy Solana Token Airdrops: A TypeScript Guide

Introduction:

This TypeScript tutorial simplifies Solana token distribution. Learn to effortlessly airdrop Solana tokens programmatically, making token distribution on the Solana blockchain a breeze.

Starting a Test Validator

solana-test-validator
Enter fullscreen mode Exit fullscreen mode

Step 1: Setting Up Your TypeScript Project
Initialize a TypeScript project:

npm init -y
npm install --save-dev typescript
npx tsc --init
Enter fullscreen mode Exit fullscreen mode

Step 2: Installing Solana Web3.js Library
Install the library:

npm install --save @solana/web3.js
Enter fullscreen mode Exit fullscreen mode

Step 3: Crafting the Airdrop Functionality
Create index.ts:

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

export const airdrop = async (address: PublicKey | string, amount: number) => {
    const publicKey = new PublicKey(address);
    const connection = new Connection("http://127.0.0.1:8899", "confirmed");
    const signature = await connection.requestAirdrop(publicKey, amount * LAMPORTS_PER_SOL);
    await connection.confirmTransaction(signature);
}

airdrop("<public key>", 1);
Enter fullscreen mode Exit fullscreen mode

Step 4: Building and Executing Your Project
Build:

npm run build
Enter fullscreen mode Exit fullscreen mode

Run:

node dist/index.js
Enter fullscreen mode Exit fullscreen mode

Conclusion:
You've learned to airdrop Solana tokens programmatically👩🏼‍💻! Explore further and leverage Solana's capabilities for innovative blockchain applications.

💖 💪 🙅 🚩
sumana10
Sumana

Posted on August 10, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related