DEV Community

Cover image for Easy Solana Token Airdrops: A TypeScript Guide
Sumana
Sumana

Posted on • Updated on

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.

Top comments (3)

Collapse
 
aksel_dd921cd45ac7c121b46 profile image
Aksel

How do i make this program communicate with my wallet? for example Phantom. I understand the program but dont understand how to make the code communicate with my wallet? :)

Collapse
 
sumana10 profile image
Sumana

Copy your wallet address
Image description

paste itairdrop("<wallet address>", 1);

Collapse
 
aksel_dd921cd45ac7c121b46 profile image
Aksel

Do i have to pay tx fee per airdrop batch or is it per adress when coding this setup?