Getting Started
Transfer Fungible Tokens
Transfer fungible tokens (SPL tokens) between wallets on the Solana blockchain.
Transfer Tokens
In the following section you can find a full code example and the Parameters that you might have to change. You can learn more about token transfer details in the Token Metadata program pages.
// To install all the required packages use the following command
// npm install @metaplex-foundation/mpl-toolbox @metaplex-foundation/umi @metaplex-foundation/umi-bundle-defaults
import {
createTokenIfMissing,
findAssociatedTokenPda,
transferTokens,
} from '@metaplex-foundation/mpl-toolbox';
import {
keypairIdentity,
publicKey,
transactionBuilder,
} from '@metaplex-foundation/umi';
import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
import { readFileSync } from 'fs';
// Initialize Umi with Devnet endpoint
const umi = createUmi('https://api.devnet.solana.com')
// Load your wallet/keypair
const wallet = '<your wallet file path>'
const secretKey = JSON.parse(readFileSync(wallet, 'utf-8'))
const keypair = umi.eddsa.createKeypairFromSecretKey(new Uint8Array(secretKey))
umi.use(keypairIdentity(keypair))
// Your token mint address and destination wallet
const mintAddress = publicKey('<your token mint address>')
const destinationAddress = publicKey('<destination wallet address>')
// Find the source token account (your account)
const sourceTokenAccount = findAssociatedTokenPda(umi, {
mint: mintAddress,
owner: umi.identity.publicKey,
})
// Find the destination token account
const destinationTokenAccount = findAssociatedTokenPda(umi, {
mint: mintAddress,
owner: destinationAddress,
})
// Create the destination token account if it doesn't exist
transactionBuilder()
.add(createTokenIfMissing(umi, {
mint: mintAddress,
owner: destinationAddress,
}))
// Transfer 100 tokens
.add(
transferTokens(umi, {
source: sourceTokenAccount,
destination: destinationTokenAccount,
amount: 100,
}))
.sendAndConfirm(umi)
console.log('Transferred 100 tokens')
console.log('From:', sourceTokenAccount)
console.log('To:', destinationTokenAccount)
Parameters
Customize these parameters for your transfer:
| Parameter | Description |
|---|---|
mintAddress | The token mint address |
destinationAddress | Recipient wallet address |
amount | Number of tokens to transfer |
How It Works
The transfer process involves four steps:
- Find source token account - Locate your token account using
findAssociatedTokenPda - Find destination token account - Locate the recipient's token account
- Create destination token account if needed - Use
createTokenIfMissingto ensure the recipient has a token account - Transfer tokens - Execute the transfer with
transferTokens
Token Accounts
Each wallet has an Associated Token Account (ATA) for each type of token they hold. The findAssociatedTokenPda function derives the address of these accounts based on the wallet address and token mint.
The createTokenIfMissing function automatically creates the token account if it doesn't exist yet, or does nothing if it already exists. This ensures the transfer will always succeed.