Solana创建Fungible Token遇Non-base58错误及参数传递问题
解决Metaplex createCreateMetadataAccountV2Instruction 参数类型不匹配问题
正确传递参数的方式
当前版本的@metaplex-foundation/mpl-token-metadata中,createCreateMetadataAccountV2Instruction要求传入两个核心参数:一个包含所有必要账户的对象,以及元数据配置参数对象。具体步骤如下:
- 生成元数据账户PDA
先通过findProgramAddressSync生成元数据账户的公钥,这是关联代币mint的必需账户:
import { PublicKey } from "@solana/web3.js"; import { MPL_TOKEN_METADATA_PROGRAM_ID } from "@metaplex-foundation/mpl-token-metadata"; const [metadataPda] = PublicKey.findProgramAddressSync( [Buffer.from("metadata"), MPL_TOKEN_METADATA_PROGRAM_ID.toBuffer(), mintAccount.toBuffer()], MPL_TOKEN_METADATA_PROGRAM_ID );
- 构建账户参数对象
按照createMetadataAccountV2InstructionAccounts类型要求,组装所有需要的账户公钥:
const accounts = { metadata: metadataPda, mint: mintAccount, // 你的代币mint账户公钥 mintAuthority: wallet.publicKey, // 铸币权限账户 payer: wallet.publicKey, // 支付交易费用的账户 updateAuthority: wallet.publicKey, // 元数据更新权限账户 };
- 构建元数据配置参数
用CreateMetadataAccountArgsV2类型定义代币的元数据信息:
import { CreateMetadataAccountArgsV2 } from "@metaplex-foundation/mpl-token-metadata"; const metadataArgs: CreateMetadataAccountArgsV2 = { data: { name: "你的代币名称", symbol: "代币符号", uri: "元数据JSON文件的URL", sellerFeeBasisPoints: 0, // 版税比例(千分比) creators: null, // 可选,设置创作者信息 collection: null, // 可选,关联集合 uses: null, // 可选,设置代币用途 }, isMutable: true, // 是否允许修改元数据 };
- 创建指令并添加到交易
最后将两个参数传入指令函数,再添加到交易中:
import { createCreateMetadataAccountV2Instruction } from "@metaplex-foundation/mpl-token-metadata"; import { Transaction } from "@solana/web3.js"; const createMetadataInstruction = createCreateMetadataAccountV2Instruction(accounts, metadataArgs); const transaction = new Transaction().add(createMetadataInstruction);
教程代码出错的原因
直接复制教程代码报错,核心原因是库版本迭代导致API结构变更:
- 你参考的教程基于旧版本的
mpl-token-metadata,当时的createCreateMetadataAccountV2Instruction可能允许直接传入分散的公钥参数,而非封装后的账户对象; - 新版本的库对参数结构做了规范化调整,要求将所有账户统一放入一个对象中传递,因此旧代码会触发TypeScript类型校验错误。
内容的提问来源于stack exchange,提问作者Alchemist
相关产品推荐
相关产品推荐

