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.

1// To install all the required packages use the following command
2// npm install @metaplex-foundation/mpl-toolbox @metaplex-foundation/umi @metaplex-foundation/umi-bundle-defaults
3import {
4 createTokenIfMissing,
5 findAssociatedTokenPda,
6 transferTokens,
7} from '@metaplex-foundation/mpl-toolbox';
8import {
9 keypairIdentity,
10 publicKey,
11 transactionBuilder,
12} from '@metaplex-foundation/umi';
13import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
14import { readFileSync } from 'fs';
15
16// Initialize Umi with Devnet endpoint
17 const umi = createUmi('https://api.devnet.solana.com')
18
19 // Load your wallet/keypair
20 const wallet = '<your wallet file path>'
21 const secretKey = JSON.parse(readFileSync(wallet, 'utf-8'))
22 const keypair = umi.eddsa.createKeypairFromSecretKey(new Uint8Array(secretKey))
23 umi.use(keypairIdentity(keypair))
24
25 // Your token mint address and destination wallet
26 const mintAddress = publicKey('<your token mint address>')
27 const destinationAddress = publicKey('<destination wallet address>')
28
29// Find the source token account (your account)
30 const sourceTokenAccount = findAssociatedTokenPda(umi, {
31 mint: mintAddress,
32 owner: umi.identity.publicKey,
33 })
34
35 // Find the destination token account
36 const destinationTokenAccount = findAssociatedTokenPda(umi, {
37 mint: mintAddress,
38 owner: destinationAddress,
39 })
40
41 // Create the destination token account if it doesn't exist
42 transactionBuilder()
43 .add(createTokenIfMissing(umi, {
44 mint: mintAddress,
45 owner: destinationAddress,
46 }))
47 // Transfer 100 tokens
48 .add(
49 transferTokens(umi, {
50 source: sourceTokenAccount,
51 destination: destinationTokenAccount,
52 amount: 100,
53 }))
54 .sendAndConfirm(umi)
55
56console.log('Transferred 100 tokens')
57 console.log('From:', sourceTokenAccount)
58 console.log('To:', destinationTokenAccount)

Parameters

Customize these parameters for your transfer:

ParameterDescription
mintAddressThe token mint address
destinationAddressRecipient wallet address
amountNumber of tokens to transfer

How It Works

The transfer process involves four steps:

  1. Find source token account - Locate your token account using findAssociatedTokenPda
  2. Find destination token account - Locate the recipient's token account
  3. Create destination token account if needed - Use createTokenIfMissing to ensure the recipient has a token account
  4. 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.