在Solana发行元数据代币(纯JS完成)
<!--StartFragment-->
教学视频链接:
欢迎添加我的联系方式:Rico_ruilabs
<!--EndFragment-->
<!--StartFragment-->
是Javascript的运行环境,到官网下载安装即可 https\://nodejs.org/
新建文件夹,初始化项目
npm init -y
然后使用 npm install 指令安装以下各库
看该文档体会Solana Token、NFT是怎么构建的
https\://developers.metaplex.com/token-metadata
import { percentAmount, generateSigner, signerIdentity, createSignerFromKeypair } from '@metaplex-foundation/umi'import { TokenStandard, createAndMint ,mplTokenMetadata} from '@metaplex-foundation/mpl-token-metadata'import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'import { Keypair } from "@solana/web3.js"import { readFileSync } from 'fs'
const umi = createUmi('your_devnet_rpc');
这里的节点推荐使白嫖 QiuckNode,用过都说好,访问 quicknode.com,一步步注册即可,免费套餐跑测试代码都用
使用上一节在本机创建的 id.json 私钥文件,实例化用户钱包
const userKeypair = Keypair.fromSecretKey( Buffer.from(JSON.parse(readFileSync('./id.json', "utf-8"))))
此时获取的 _keypair 有两个成员,publicKey 与 secretKey
使用其 secretKey ,生成 UMI 签名者(Signer)
const userWallet = umi.eddsa.createKeypairFromSecretKey(userKeypair.secretKey);const userWalletSigner = createSignerFromKeypair(umi, userWallet);
将您的令牌信息在线化,我推荐使用 pinata
https\://www.pinata.cloud/
注册登录,来到仪表盘
TokenInfo.json
{ "name": "Per aspera ad astra", "symbol": "PAA", "description": "「循此苦旅,以達星辰」", "image": "https://white-historical-basilisk-887.mypinata.cloud/ipfs/QmS7kMQG1gHptsd4NAEVYDhMJ7SGa23hpJ63RYh69FqPGk"}
各字段分别为 名称、简称、介绍、头像的在线链接
const metadata = { name: "Per aspera ad astra", symbol: "PAA", uri: "https://white-historical-basilisk-887.mypinata.cloud/ipfs/QmVd6xVRqg9sJQP1zkUVizZ7jah6zD7j6fSPn9F7MRjZMo",}
// 生成 Mint 签名者const mint = generateSigner(umi);// 使用 use 方法配置 UMI 的签名者和元数据模块umi.use(signerIdentity(userWalletSigner)) .use(mplTokenMetadata())
createAndMint(umi, { mint, authority: umi.identity, name: metadata.name, symbol: metadata.symbol, uri: metadata.uri, sellerFeeBasisPoints: percentAmount(0), decimals: 9, amount: 666_000000000, tokenOwner: userWallet.publicKey, tokenStandard: TokenStandard.Fungible, }).sendAndConfirm(umi).then(() => { console.log("Successfully minted tokens (", mint.publicKey, ")");})
重要字段的含义为:
执行 index.js 代码
node .\index.js
得到带有元数据的新令牌
EpnTRJksPN5GicrhpLDHhW67dArQuYPnf8B1rWx7u96k
区块浏览器验证
// 引入各模块
import { percentAmount, generateSigner, signerIdentity, createSignerFromKeypair } from '@metaplex-foundation/umi'
import { TokenStandard, createAndMint ,mplTokenMetadata} from '@metaplex-foundation/mpl-token-metadata'
import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
import { Keypair } from "@solana/web3.js"
import { readFileSync } from 'fs'
// 连接到 Solana devnet
const umi = createUmi('https://hidden-purple-arrow.solana-devnet.quiknode.pro/206956a59552f9a87a253f7172eb7f58c9246801/')
// 使用 id.json 本地的私钥文件,实例化钱包
const userKeypair = Keypair.fromSecretKey(
Buffer.from(JSON.parse(readFileSync('./id.json', "utf-8")))
)
// 获取签名者
const userWallet = umi.eddsa.createKeypairFromSecretKey(userKeypair.secretKey);
const userWalletSigner = createSignerFromKeypair(umi, userWallet);
// 构建元数据
const metadata = {
name: "Per aspera ad astra",
symbol: "PAA",
uri: "https://white-historical-basilisk-887.mypinata.cloud/ipfs/QmVd6xVRqg9sJQP1zkUVizZ7jah6zD7j6fSPn9F7MRjZMo",
}
// 生成 Mint 签名者
const mint = generateSigner(umi);
// 使用 use 方法配置 UMI 的签名者和元数据模块
umi.use(signerIdentity(userWalletSigner))
.use(mplTokenMetadata())
// 打包发送 令牌铸造的交易
createAndMint(umi, {
mint,
authority: umi.identity,
name: metadata.name,
symbol: metadata.symbol,
uri: metadata.uri,
sellerFeeBasisPoints: percentAmount(0),
decimals: 9,
amount: 666_000000000,
tokenOwner: userWallet.publicKey,
tokenStandard: TokenStandard.Fungible,
}).sendAndConfirm(umi).then(() => {
console.log("Successfully minted tokens (", mint.publicKey, ")");
})
<!--EndFragment-->
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!