简体中文
创建和管理 NFT 收藏的完全指南,使用 CreateKit
import { CollectionManager, b3Testnet } from '@b3dotfun/basemint' import { createPublicClient, createWalletClient, http } from 'viem' import { privateKeyToAccount } from 'viem/accounts' const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`) const publicClient = createPublicClient({ chain: b3Testnet, transport: http() }) const walletClient = createWalletClient({ chain: b3Testnet, transport: http(), account }) const collectionManager = new CollectionManager(publicClient) // 定义基础集合 const basicCollection = { name: "My Art Collection", symbol: "MAC", creator: account.address, gameOwner: account.address, description: "一系列数字艺术作品", image: "https://example.com/collection-image.png" } // 生成创建者签名 const creatorSignature = await collectionManager.generateCreatorSignature( walletClient, basicCollection )
import { parseEther } from 'viem' const advancedCollection = { // 必需 name: "Premium Gaming Items", symbol: "PGI", creator: account.address, gameOwner: "0x1234567890123456789012345678901234567890", // 不同的游戏所有者 // 供应量和定价 maxSupply: 5000n, mintPrice: parseEther("0.01"), // 0.01 ETH maxPerWallet: 5n, // 时间控制 startTime: BigInt(Math.floor(Date.now() / 1000) + 3600), // 1小时后开始 endTime: BigInt(Math.floor(Date.now() / 1000) + 86400 * 7), // 7天后结束 // 白名单配置 isWhitelistEnabled: true, whitelistMerkleRoot: "0x..." as `0x${string}`, // 元数据 description: "为高级玩家提供的独家游戏物品", image: "https://example.com/premium-collection.png", external_url: "https://mygame.com/premium-items", animation_url: "https://example.com/collection-animation.mp4", // 集合属性 attributes: [ { trait_type: "Category", value: "Gaming" }, { trait_type: "Rarity", value: "Premium" }, { trait_type: "Edition", value: "First" } ], // 技术 tokenStandard: "ERC1155" as const, chainId: 1993 }
const erc721Collection = { name: "Unique Art Pieces", symbol: "UAP", creator: account.address, gameOwner: account.address, tokenStandard: "ERC721" as const, maxSupply: 1000n, // 每个代币都是独一无二的 description: "独一无二的数字艺术作品" } // ERC721 铸造(数量始终为 1) const collection721 = collectionManager.createCollection( predictedAddress, "ERC721" ) await collection721.mint( walletClient, 1n, // 对于 ERC721 始终为 1 undefined, // 元数据 URI mintPrice, proof )
const collectionMetadata = { name: "My Collection", description: "一系列精彩的数字资产", image: "https://example.com/collection-image.png", external_url: "https://mywebsite.com/collection", // 市场背景和横幅 background_color: "ffffff", banner_image_url: "https://example.com/banner.png", // 集合属性 attributes: [ { trait_type: "Theme", value: "Fantasy" }, { trait_type: "Artist", value: "Digital Creator" } ] }
import { NFTMetadataManager, MediaType } from '@b3dotfun/basemint' // 为不同的媒体类型生成元数据 const artworkMetadata = NFTMetadataManager.generateNFTMetadata( collectionMetadata, MediaType.ARTWORK ) const model3dMetadata = NFTMetadataManager.generateNFTMetadata( collectionMetadata, MediaType.MODEL_3D ) const videoMetadata = NFTMetadataManager.generateNFTMetadata( collectionMetadata, MediaType.VIDEO ) // 转换为 JSON const metadataJson = NFTMetadataManager.generateJSON(artworkMetadata) console.log(metadataJson)
import { validateCollectionMetadata } from '@b3dotfun/basemint' try { // 验证集合元数据 const validation = validateCollectionMetadata(collectionMetadata) if (!validation.isValid) { console.error("验证错误:", validation.errors) return } console.log("✅ 集合元数据有效") // 继续生成签名 const signature = await collectionManager.generateCreatorSignature( walletClient, collectionMetadata ) } catch (error) { console.error("验证失败:", error) }
// 首先生成创建者签名 const creatorSignature = await collectionManager.generateCreatorSignature( walletClient, collectionMetadata ) // 预测集合地址 const predictedAddress = collectionManager.predictCollectionAddress( collectionMetadata, creatorSignature ) console.log(`集合将部署在:${predictedAddress}`) // 现在您可以在部署前使用此地址 // 用于市场集成、前端显示等。
const collection = collectionManager.createCollection( predictedAddress, "ERC721" ) // 检查集合是否已部署 const isDeployed = await collection.isDeployed() console.log(`已部署:${isDeployed}`) // 获取集合信息(仅在部署后有效) if (isDeployed) { const info = await collection.getCollectionInfo() console.log("集合信息:", { name: info.name, symbol: info.symbol, totalSupply: info.totalSupply.toString(), maxSupply: info.maxSupply.toString(), mintPrice: info.mintPrice.toString(), maxPerWallet: info.maxPerWallet.toString() }) }
// 部署后只能进行某些操作 // 检查当前铸造价格(如果实现了动态定价) const currentPrice = await collection.getCurrentMintPrice() // 检查当前是否可以铸造 const isMintingActive = await collection.isMintingActive() // 获取剩余供应量 const remainingSupply = await collection.getRemainingSupply() console.log({ currentPrice: currentPrice.toString(), isMintingActive, remainingSupply: remainingSupply.toString() })
const qualityCollection = { name: "Professional Art Collection", symbol: "PAC", creator: account.address, gameOwner: account.address, // 高质量描述 description: "精选的专业数字艺术作品集,展示当代主题和创新技术。", // 专业图像(最小 640x640px) image: "https://example.com/high-res-collection-image.png", // 全面的属性以提高可发现性 attributes: [ { trait_type: "Art Style", value: "Contemporary" }, { trait_type: "Medium", value: "Digital" }, { trait_type: "Artist Verification", value: "Verified" }, { trait_type: "Edition Type", value: "Limited" } ], // 外部链接以增加可信度 external_url: "https://professionalartist.com/collection" }
私钥管理
签名验证
访问控制
地址预测不匹配
无效的集合参数
签名生成失败