发布完了上一节提到的NFT合约之后,我们尝试使用nodejs脚本来mint出一个nft
const solanaWeb3 = require('@solana/web3.js');
const splToken = require('@solana/spl-token');
// 连接到Solana网络
const connection = new solanaWeb3.Connection('https://api.devnet.solana.com', 'confirmed');
// 钱包密钥对
const walletPrivateKey = new Uint8Array([/* 钱包私钥 */]);
const walletKeyPair = solanaWeb3.Keypair.fromSecretKey(walletPrivateKey);
// 发送mint请求
async function mintNFT() {
// NFT合约地址
const nftMintAddress = new solanaWeb3.PublicKey(/* NFT合约地址 */);
// 发行者地址
const mintAuthority = walletKeyPair.publicKey;
// 获取NFT合约的元数据
const nftMintInfo = await connection.getAccountInfo(nftMintAddress);
if (nftMintInfo === null) {
console.error('NFT合约地址无效');
return;
}
// 解析NFT合约元数据
const nftMintData = splToken.MintLayout.decode(nftMintInfo.data);
// 创建新的NFT Token账户地址
const nftTokenAddress = await splToken.Token.createMintToInstruction(
splToken.TOKEN_PROGRAM_ID, // Token程序ID
nftMintAddress, // NFT合约地址
walletKeyPair.publicKey, // 接收NFT Token的地址
mintAuthority, // 发行者地址
[], // 授权账户
1 // 发行的NFT Token数量
);
// 发送交易以将NFT Token分配给接收地址
const transaction = new solanaWeb3.Transaction().add(nftTokenAddress);
const signature = await solanaWeb3.sendAndConfirmTransaction(
connection, // Solana网络连接
transaction, // 交易
[walletKeyPair] // 钱包密钥对
);
console.log(`NFT Token创建成功,交易签名为:${signature}`);
}
// 执行mint请求
mintNFT();