Skip to main content

How to Send Tokens

Use the Token Program to transfer SPL Tokens. In order to send a SPL token, you need to know its SPL token account address. You can both get the address and send tokens with the following example.

import {
Connection,
clusterApiUrl,
Keypair,
LAMPORTS_PER_SOL,
Transaction,
sendAndConfirmTransaction,
} from "@solana/web3.js";
import {
createMint,
getOrCreateAssociatedTokenAccount,
mintTo,
createTransferInstruction,
} from "@solana/spl-token";

(async () => {
// Connect to cluster
const connection = new Connection(clusterApiUrl("devnet"), "confirmed");

// Generate a new wallet keypair and airdrop SOL
const fromWallet = Keypair.generate();
const fromAirdropSignature = await connection.requestAirdrop(
fromWallet.publicKey,
LAMPORTS_PER_SOL,
);
// Wait for airdrop confirmation
await connection.confirmTransaction(fromAirdropSignature);

// Generate a new wallet to receive newly minted token
const toWallet = Keypair.generate();

// Create new token mint
const mint = await createMint(
connection,
fromWallet,
fromWallet.publicKey,
null,
9,
);

// Get the token account of the fromWallet Solana address, if it does not exist, create it
const fromTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
fromWallet,
mint,
fromWallet.publicKey,
);

//get the token account of the toWallet Solana address, if it does not exist, create it
const toTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
fromWallet,
mint,
toWallet.publicKey,
);

// Minting 1 new token to the "fromTokenAccount" account we just returned/created
await mintTo(
connection,
fromWallet,
mint,
fromTokenAccount.address,
fromWallet.publicKey,
1000000000, // it's 1 token, but in lamports
[],
);

// Add token transfer instructions to transaction
const transaction = new Transaction().add(
createTransferInstruction(
fromTokenAccount.address,
toTokenAccount.address,
fromWallet.publicKey,
1,
),
);

// Sign transaction, broadcast, and confirm
await sendAndConfirmTransaction(connection, transaction, [fromWallet]);
})();