快速入门
转移同质化代币
Last updated November 25, 2025
在Solana区块链上的钱包之间转移同质化代币(SPL代币)。
转移代币
在以下部分,您可以看到完整的代码示例以及需要更改的参数。有关代币转移的更多详情,请参阅Token Metadata程序页面。
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)
1# Transfer Tokens using the Metaplex CLI
2
3# Usage: mplx toolbox token transfer <MINT_ADDRESS> <AMOUNT> <DESTINATION>
4mplx toolbox token transfer <MINT_ADDRESS> <AMOUNT> <DESTINATION_ADDRESS>
5
6# Example: Transfer 100 tokens (0 decimals)
7mplx toolbox token transfer 7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU 100 9WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM
8
9# Example: Transfer 100 tokens (9 decimals)
10# Amount is in smallest units: 100 * 10^9 = 100000000000
11mplx toolbox token transfer 7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU 100000000000 9WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM
12
13# Note: If the destination doesn't have a token account, it will be created automatically
参数
根据您的转移需求自定义以下参数:
| 参数 | 描述 |
|---|---|
mintAddress | 代币铸币地址 |
destinationAddress | 接收者的钱包地址 |
amount | 要转移的代币数量 |
工作原理
转移过程涉及4个步骤:
- 查找源代币账户 - 使用
findAssociatedTokenPda定位您的代币账户 - 查找目标代币账户 - 定位接收者的代币账户
- 如有需要创建目标代币账户 - 使用
createTokenIfMissing确保接收者有代币账户 - 转移代币 - 使用
transferTokens执行转移
代币账户
每个钱包为其持有的每种代币类型都有一个关联代币账户(ATA)。findAssociatedTokenPda函数根据钱包地址和代币铸币地址派生这些账户的地址。
createTokenIfMissing函数会自动创建代币账户(如果尚不存在),如果已存在则不执行任何操作。这确保了转移始终成功。
