快速入门
创建同质化代币
Last updated November 25, 2025
使用Token Metadata程序在Solana上创建带元数据的同质化代币。
学习内容
本指南将向您展示如何创建和铸造具有以下要素的同质化代币:
- 自定义名称、符号和元数据
- 代币图像和描述
- 可配置的小数位数(可分割性)
- 初始代币供应量
创建代币
以下代码是一个完整可运行的示例。可自定义的参数如下所示。有关代币创建的更多详情,请参阅Token Metadata程序页面。
1// npm install @metaplex-foundation/mpl-token-metadata @metaplex-foundation/mpl-toolbox @metaplex-foundation/umi @metaplex-foundation/umi-bundle-defaults
2import {
3 createFungible,
4 mplTokenMetadata,
5} from '@metaplex-foundation/mpl-token-metadata'
6import {
7 createTokenIfMissing,
8 findAssociatedTokenPda,
9 mintTokensTo,
10} from '@metaplex-foundation/mpl-toolbox'
11import {
12 generateSigner,
13 keypairIdentity,
14 percentAmount,
15 some,
16} from '@metaplex-foundation/umi'
17import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
18import { readFileSync } from 'fs'
19
20// Initialize Umi with your RPC endpoint
21const umi = createUmi('https://api.devnet.solana.com').use(mplTokenMetadata())
22
23// Load your wallet keypair
24const wallet = '<your wallet file path>'
25const secretKey = JSON.parse(readFileSync(wallet, 'utf-8'))
26const keypair = umi.eddsa.createKeypairFromSecretKey(new Uint8Array(secretKey))
27umi.use(keypairIdentity(keypair))
28
29// Generate a new mint account
30const mint = generateSigner(umi)
31
32// Step 1: Create the fungible token with metadata
33await createFungible(umi, {
34 mint,
35 name: 'My Fungible Token',
36 symbol: 'MFT',
37 uri: 'https://example.com/my-token-metadata.json',
38 sellerFeeBasisPoints: percentAmount(0),
39 decimals: some(9),
40}).sendAndConfirm(umi)
41
42// Step 2: Mint initial supply to your wallet
43await createTokenIfMissing(umi, {
44 mint: mint.publicKey,
45 owner: umi.identity.publicKey,
46})
47 .add(
48 mintTokensTo(umi, {
49 mint: mint.publicKey,
50 token: findAssociatedTokenPda(umi, {
51 mint: mint.publicKey,
52 owner: umi.identity.publicKey,
53 }),
54 amount: 1_000_000_000_000_000, // 1,000,000 tokens with 9 decimals
55 })
56 )
57 .sendAndConfirm(umi)
58
59console.log('Token created:', mint.publicKey)
60console.log('Metadata and mint account initialized')
61console.log('Initial supply minted to:', umi.identity.publicKey)
1import { generateKeyPairSigner } from '@solana/kit';
2import { createFungible } from '@metaplex-foundation/mpl-token-metadata-kit';
3
4// Assuming rpc, rpcSubscriptions, and sendAndConfirmInstructions are set up
5// See getting-started for full setup
6
7const mint = await generateKeyPairSigner();
8const authority = await generateKeyPairSigner(); // Your wallet
9
10// Create a fungible token with metadata and mint initial supply
11const createAndMintIx = await createFungible({
12 mint,
13 authority,
14 payer: authority,
15 name: 'My Fungible Token',
16 symbol: 'MFT',
17 uri: 'https://example.com/my-token-metadata.json',
18 sellerFeeBasisPoints: 0,
19 decimals: 9,
20 tokenOwner: authority.address,
21 amount: 1_000_000_000_000_000n, // 1,000,000 tokens with 9 decimals
22});
23
24// Send the instruction (createFungible returns a single combined instruction)
25await sendAndConfirm({
26 instructions: [createAndMintIx],
27 payer: authority,
28});
29
30console.log('Fungible token created:', mint.address);
31console.log('Initial supply minted to:', authority.address);
1# Create a Fungible Token using the Metaplex CLI
2
3# Interactive wizard mode (recommended for beginners)
4mplx toolbox token create --wizard
5
6# Basic token creation (required: --name, --symbol, --mint-amount)
7mplx toolbox token create \
8 --name "My Token" \
9 --symbol "MYT" \
10 --mint-amount 1000000
11
12# Full token creation with all options
13mplx toolbox token create \
14 --name "My Token" \
15 --symbol "MYT" \
16 --description "A fungible token on Solana" \
17 --image ./token-image.png \
18 --decimals 9 \
19 --mint-amount 1000000000000000
20
21# Note: mint-amount is in smallest units
22# With --decimals 9, to mint 1,000,000 tokens: --mint-amount 1000000000000000
23# With --decimals 0 (default), to mint 1,000,000 tokens: --mint-amount 1000000
参数
根据您的代币自定义以下参数:
| 参数 | 描述 |
|---|---|
name | 代币名称(最多32个字符) |
symbol | 代币缩写(最多6个字符) |
uri | 链接到链下元数据JSON |
sellerFeeBasisPoints | 版税百分比(550 = 5.5%) |
decimals | 小数位数(some(9)是标准) |
amount | 要铸造的代币数量 |
元数据和图像
uri必须指向至少包含以下信息的JSON文件。更多详情可在Token Metadata标准页面找到。您需要将JSON和图像URL上传到可公开访问的位置。我们建议使用Web3存储提供商,如Arweave。如果您想在代码中完成此操作,请按照此指南操作。
{
"name": "My Fungible Token",
"symbol": "MFT",
"description": "A fungible token on Solana",
"image": "https://arweave.net/tx-hash"
}
