Creating Airdrops with Compressed Tokens
ZK Compression is the most efficient way to distribute your SPL tokens.
By the end of this guide, you'll have built a fully functioning, programmatic airdrop.
Airdropping SPL Tokens
The high-level overview is this:
Mint and send the to-be-airdropped SPL tokens to a wallet you control.
Create batches of instructions based on a list of recipients and amounts.
Build transactions from these instruction batches, then sign, send, and confirm them.
1. Install the SDK
npm install --save \
@lightprotocol/stateless.js \
@lightprotocol/compressed-token \
@solana/web3.js \
@solana/spl-token \
bs58 \
dotenv
2. Mint SPL tokens to yourself
import { Keypair } from '@solana/web3.js';
import { createRpc } from '@lightprotocol/stateless.js';
import {
createMint,
getOrCreateAssociatedTokenAccount,
mintTo,
} from "@solana/spl-token";
import { createTokenPool } from '@lightprotocol/compressed-token';
import bs58 from "bs58";
import dotenv from "dotenv";
dotenv.config();
// Set these values in your .env file
const RPC_ENDPOINT = process.env.RPC_ENDPOINT!;
const PAYER = Keypair.fromSecretKey(
bs58.decode(process.env.PAYER_KEYPAIR!)
);
// Create Rpc endpoint
const connection = createRpc(RPC_ENDPOINT);
(async() => {
/// Create an SPL mint
const mint = await createMint(
connection,
PAYER,
PAYER.publicKey,
null,
9
);
console.log(`create-mint success! address: ${mint}`);
/// Register mint for compression
const poolTxId = await createTokenPool(connection, PAYER, mint);
console.log(`createTokenPool success: ${poolTxId}`);
/// Create an associated SPL token account for the sender (PAYER)
const ata = await getOrCreateAssociatedTokenAccount(
connection,
PAYER,
mint,
PAYER.publicKey
);
console.log(`ATA: ${ata.address.toBase58()}`);
/// Mint SPL tokens to the sender
const mintToTxId = await mintTo(
connection,
PAYER,
mint,
ata.address,
PAYER.publicKey,
1e9 * 1e9 // 1b * decimals
);
console.log(`mint-to success! txId: ${mintToTxId}`);
})();
You now have a regular SPL token account owned by PAYER
that holds all minted tokens.
3. Distribute the tokens
Next, you want to distribute the SPL tokens from your distributor to all recipients.
A. Simple version
B. Optimized For large-scale airdrops
First, create a helper that takes recipients and amounts and returns batches of instructions:
Now, you can create the logic that signs and sends transactions in batches. For this, first add a helper method that refreshes Solana blockhashes in the background:
Then, add the helper that signs and sends the transactions using recent blockhashes.
Finally, put it all together in your main file:
Ensure that you have all the necessary .env
variables set up. You can now run your code and execute the airdrop!
Advanced: Decompress / Claim
Advanced Tips
Set priority fees dynamically for decompression. Learn more here.
Native Swap via Jup-API
If you have a custom FE, you can let users swap compressed tokens using the Jup-API. A reference implementation is available here.
Support
For additional support or questions, please refer to our documentation, or contact Swen or Mert on Telegram or via email.
Last updated
Was this helpful?