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
Step 1: Setting Up Your TypeScript Project
Initialize a TypeScript project:
npm init -y
npm install --save-dev typescript
npx tsc --init
Step 2: Installing Solana Web3.js Library
Install the library:
npm install --save @solana/web3.js
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);
Step 4: Building and Executing Your Project
Build:
npm run build
Run:
node dist/index.js
Conclusion:
You've learned to airdrop Solana tokens programmatically👩🏼‍💻! Explore further and leverage Solana's capabilities for innovative blockchain applications.
Top comments (3)
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? :)
Copy your wallet address
paste it
airdrop("<wallet address>", 1);
Do i have to pay tx fee per airdrop batch or is it per adress when coding this setup?