9. NFT 铸造完整流程

NFT 铸造完整流程

掌握了 Collection 和环境配置后,我们现在可以编写完整的铸造脚本了。我们将分别展示 Token Metadata (旧标准)Metaplex Core (新标准) 的铸造流程。


1. Token Metadata 标准铸造 (Legacy)

在旧标准中,铸造一个 NFT 是一个涉及多个步骤和多个账户的复杂过程。

核心步骤

  1. Create Mint: 创建 SPL Mint 账户。
  2. Create Metadata: 创建 Metadata PDA,写入 URI 和名称。
  3. Create Master Edition: 创建 Master Edition PDA,锁定供应量。
  4. Verify Collection: 调用 verify 指令将 NFT 归入集合。

代码实现

typescript
import { createNft, verifyCollectionV1, findMetadataPda } from '@metaplex-foundation/mpl-token-metadata'; import { generateSigner, percentAmount, publicKey } from '@metaplex-foundation/umi'; async function mintLegacyNft(umi, collectionMintAddr) { // 1. 生成新的 Mint Keypair const nftMint = generateSigner(umi); console.log('正在铸造 Legacy NFT:', nftMint.publicKey); // 2. 铸造 (createNft 宏会自动处理 Mint, Metadata, Master Edition 的创建) await createNft(umi, { mint: nftMint, name: "My Legacy NFT", uri: "https://arweave.net/metadata.json", sellerFeeBasisPoints: percentAmount(5), // 5% 版税 collection: { key: publicKey(collectionMintAddr), verified: false, // 初始状态:未验证 }, }).sendAndConfirm(umi); // 3. 验证集合 (这是独立的一步交易) // 必须由 Collection Authority 签名 await verifyCollectionV1(umi, { metadata: findMetadataPda(umi, { mint: nftMint.publicKey }), collectionMint: publicKey(collectionMintAddr), authority: umi.identity, }).sendAndConfirm(umi); console.log('✅ NFT 已铸造并验证归属!'); }

2. Metaplex Core 标准铸造 (Next Gen)

Core 标准将流程极度简化。因为采用了单一账户模型,我们只需要创建一个 Asset 账户。

核心步骤

  1. Create Asset: 创建 Asset 账户,直接在参数中指定 Collection。
  2. Done: 是的,就这一步。Collection 归属在创建时自动完成验证(前提是签名者也是 Collection 的 Authority)。

代码实现

typescript
import { createV1, createPlugin, ruleSet } from '@metaplex-foundation/mpl-core'; import { generateSigner, publicKey } from '@metaplex-foundation/umi'; async function mintCoreNft(umi, collectionAddr) { const assetSigner = generateSigner(umi); console.log('正在铸造 Core NFT:', assetSigner.publicKey); // 构建插件 (可选) const plugins = [ { plugin: createPlugin({ type: 'Royalties', data: { basisPoints: 500, creators: [{ address: umi.identity.publicKey, percentage: 100 }], ruleSet: ruleSet('None'), }, }), authority: { type: 'UpdateAuthority' }, } ]; // 创建 Asset // 如果当前签名者也是 Collection 的 Authority,它会自动被验证 await createV1(umi, { asset: assetSigner, name: 'My Core NFT', uri: 'https://arweave.net/metadata.json', collection: publicKey(collectionAddr), // 直接指定集合 plugins, }).sendAndConfirm(umi); console.log('✅ Core NFT 已创建!'); }

3. 批量铸造策略 (Batch Minting)

在实际项目中(如 10k PFP 发售),我们需要批量铸造。

Candy Machine (糖果机)

对于大型公售,手动循环铸造效率太低且容易出错。Metaplex 提供了 Candy Machine 程序。

  • 用户支付 SOL,Candy Machine 自动铸造并分发 NFT。
  • 支持白名单、阶段销售、随机盲盒等功能。
  • Core Candy Machine 已经发布,支持 Core 标准资产的铸造。

自定义脚本

对于空投或后端发起的批量铸造:

  1. 使用 Promise.all 并发发送交易(注意 RPC速率限制)。
  2. 在循环中加入适当的 sleep 延迟。
  3. 保存铸造成功的 Mint 地址列表,以便失败重试。
typescript
async function batchMint(umi, collection, items) { const results = []; for (let i = 0; i < items.length; i++) { console.log(`Minting ${i + 1}/${items.length}...`); // 执行铸造逻辑... await mintCoreNft(umi, collection); // 简单的速率控制 await new Promise(r => setTimeout(r, 200)); } }
JSPlayground
EDITOR ACTIVE
Initializing JS Environment...

铸造流程对比

Mint
Metadata
Edition
Status: Ready
Cost Est: ~0.015 SOL