DEV Community

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

Posted on • Edited on

4 1

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.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

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?

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay