Deposit tokens
Token Deposit Process
Depositing tokens into an airdrop vault requires setting up the PDA's token account and transferring tokens from your wallet. This is a two-instruction process:
- Create the vault's ATA (if it doesn't exist)
- Transfer tokens to the vault
1. Find Required PDAs
import { findAssociatedTokenPda, TOKEN_PROGRAM_ADDRESS } from "@solana-program/token";
// Find the airdrop's vault
const [vault, vaultBump] = await findAssociatedTokenPda({
owner: airdropPda,
mint: mint,
tokenProgram: TOKEN_PROGRAM_ADDRESS,
});
// Find your source token account
const [sourceTokenAccount] = await findAssociatedTokenPda({
owner: client.wallet.address,
mint: mint,
tokenProgram: TOKEN_PROGRAM_ADDRESS,
});
Locate the token accounts for both your wallet and the airdrop vault
2. Create Vault ATA (If Needed)
import { getCreateAssociatedTokenIdempotentInstructionAsync } from "@solana-program/token";
const vaultATACreateInst = await getCreateAssociatedTokenIdempotentInstructionAsync({
ata: vault,
mint: mint,
payer: client.wallet,
owner: airdropPda,
});
Idempotent operation that only creates if account doesn't exist
3. Get Deposit Instruction
import { getDepositTokensInstruction } from "@dropsy/airdrop";
const instruction = getDepositTokensInstruction({
sourceTokenAccount, // wallet ATA for the mint
vault, // airdrop ATA for the mint
mint, // mint address
airdrop: airdropPda, //airdrop pda address
tokenProgram: TOKEN_PROGRAM_ADDRESS, // or token 2022 address
authority: client.wallet, // the airdrop authority
amount: 500000n, // Amount in token base units (not decimals)
});
Build the instruction to move tokens into the airdrop vault
4. Build Transaction
const transactionMessage = await createTransactionMessageFromInstructions(
client.rpc,
client.wallet,
[vaultATACreateInst, instruction] // Order matters - create before transfer
);
const signedTransaction = await signTransactionMessageWithSigners(transactionMessage);
Create a single Transaction from both instructions
Send and Confirm Transaction
await client.sendAndConfirmTransaction(signedTransaction, { commitment: "confirmed" });
const signature = getSignatureFromTransaction(signedTransaction);
console.log("signature:", signature);
Submits the signed transaction to the network and waits for confirmation.
Critical Details
- Token Decimals: The
amount
must be in base units (e.g., for 6-decimal token: 1 token = 1,000,000 units) - ATA Creation: The idempotent creation ensures the transaction won't fail if the ATA already exists
- Order Matters: Always put the ATA creation instruction before the transfer
Verification
After deposit, verify the vault balance matches your expected amount:
const vaultBalance = await client.rpc.getTokenAccountBalance(vault).send();
console.log("Vault balance:", vaultBalance.value.amount);