Solana

2026年01月26日更新 17 人订阅
原价: ¥ 8.6 限时优惠
专栏简介 Solana 开发学习之Solana 基础知识 Solana 开发学习之通过RPC与Solana交互 Web3与Solana实操指南:如何签名与发送交易 Web3 新玩法:Solana Nonce Account 让你交易无忧 Web3 快上手:Solana 造你的链上名片 Web3 开发实战:用 Anchor 打造 Solana 猜数游戏 @solana/web3.js 2.0:Solana 转账全流程解析 玩转 Web3 Solana:从零到代币开发 Web3 开发入门:Solana CLI 配置与本地验证器实战 Web3 Eclipse 开发环境搭建与资产跨链桥接实战指南 用 Gill 库解锁 Web3:Solana 代币转账实战 Web3开发:用Rust实现Solana SOL转账教程 用 Rust 开发 Solana:解锁 Web3 交易费用计算 Web3开发入门:Solana账户创建与Rust实践全攻略 Web3 实战:用 Anchor 打造 Solana 智能合约全流程 Solana Web3 快速入门:创建并获取钱包账户的完整指南 Web3 开发实操:用 Anchor 在 Solana 创建代币 Mint Account 从零到 Web3:使用 @solana/kit 快速查询 Solana 账户余额 快速上手 Web3:用 @solana/kit 在 Solana 上创建钱包并查询余额 Web3实战:使用Anchor与Rust开发和调用Solana智能合约 Web3实战:Solana CPI全解析,从Anchor封装到PDA转账 用 Rust 在 Solana 上打造你的专属代币:从零到一的 Web3 实践 探索Solana SDK实战:Web3开发的双路径与轻量模块化 手把手教你用 Solana Token-2022 创建支持元数据的区块链代币 Solana 开发实战:Rust 客户端调用链上程序全流程 Solana 开发进阶:在 Devnet 上实现链上程序部署、调用与更新 Solana 开发进阶:链上事件到链下解析全攻略 从零打造Solana空投工具库:Rust开发实战指南 从零开始:用 Rust 开发 Solana 链上 Token 元数据查询工具 Solana 智能合约终极部署指南:从入门到主网,定制你的专属靓号 Program ID 【Solana 开发实战】轻松搞定链上 IDL:从上传到获取全解析 Solana 投票 DApp 开发实战:从合约到部署的完整指南 Surfpool:Solana 上的 Anvil,本地开发闪电般⚡️ 【Solana实操】64字节私钥文件解析难题:用三种姿势安全获取钱包地址 Solana 密钥实战:一文搞懂私钥、公钥、PDA 的底层关系与 CLI 操作 Solana 地址进阶:从 TS/JS 到 Rust SDK V3,完全掌握公钥与 PDA 的底层逻辑 Solana 开发者笔记:PDA 与账户操作的10个关键要点 拒绝“版本代差”:基于 Solana SDK V3 的「链上动态存储器」工业级实现 从零到 Devnet:Solana Anchor Vault 个人金库开发全流程实操 Anchor 中一个隐蔽但致命的坑:Accounts 顺序导致 AccountNotInitialized 从核心逻辑到上链部署:Solana Anchor 托管程序实战全记录 Solana 开发实战:使用 @solana/web3.js 与 Bun 铸造首个 SPL 代币 Solana 开发实战:使用 @solana/kit (v2) 发行 SPL 代币全流程

Solana 开发实战:使用 @solana/web3.js 与 Bun 铸造首个 SPL 代币

Solana开发实战:使用@solana/web3.js与Bun铸造首个SPL代币在Solana的世界里,发行代币(MintToken)往往是开发者迈向DeFi开发的第一步,也是理解链上账户模型的最佳实践。很多人习惯使用图形化界面发币,但作为开发者,掌握如何通过代码与脚本

Solana 开发实战:使用 @solana/web3.js 与 Bun 铸造首个 SPL 代币

在 Solana 的世界里,发行代币(Mint Token)往往是开发者迈向 DeFi 开发的第一步,也是理解链上账户模型的最佳实践。

很多人习惯使用图形化界面发币,但作为开发者,掌握如何通过代码与脚本来控制这一过程才是核心竞争力。 如何在代码中创建一个“铸币厂”?什么是 Mint 账户与 ATA 账户的绑定关系?如何确保创建和铸造在同一个交易块中原子化完成?

本文将抛开复杂的理论概念,直接进入代码实操。我们将基于本地开发环境,使用 TypeScript 和 @solana/web3.js,编写一个自动化的脚本,体验从“一无所有”到“千万资产”到账的完整开发流程。并且使用 Bun 不仅启动速度极快,而且原生支持 TypeScript,省去了复杂的编译配置。

打开你的终端,让我们开始铸造。

实操

使用 @solana/web3.js 铸造 SPL Token

创建并切换到项目目录

mcd solana_forge # mkdir solana_forge && cd solana_forge  
/Users/qiaopengjun/Code/Solana/solana_forge

项目初始化

bun init

✓ Select a project template: Blank

 + .gitignore
 + .cursor/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc
 + index.ts
 + tsconfig.json (for editor autocomplete)
 + README.md

To get started, run:

    bun run index.ts

bun install v1.2.17 (282dda62)

+ typescript@5.9.3
+ @types/bun@1.3.6

5 packages installed [5.64s]

安装依赖

bun add @solana/web3.js    
bun add @solana/spl-token    

铸造 SPL Token 脚本实现

使用 web3.js 铸造一个 SPL Token


/** Mint an SPL Token
 *
 *
 * Goal:
 *   Mint an SPL token in a single transaction using Web3.js and the SPL Token library.
 *
 * Objectives:
 *   1. Create an SPL mint account.
 *   2. Initialize the mint with 6 decimals and your public key (feePayer) as the mint and freeze authorities.
 *   3. Create an associated token account for your public key (feePayer) to hold the minted tokens.
 *   4. Mint 21,000,000 tokens to your associated token account.
 *   5. Sign and send the transaction.
 */

import {
    Keypair,
    Connection,
    sendAndConfirmTransaction,
    SystemProgram,
    Transaction,
} from "@solana/web3.js"

import {
    createAssociatedTokenAccountInstruction,
    createInitializeMint2Instruction,
    createMintToCheckedInstruction,
    MINT_SIZE,
    getMinimumBalanceForRentExemptMint,
    TOKEN_PROGRAM_ID,
    getAssociatedTokenAddressSync,
    ASSOCIATED_TOKEN_PROGRAM_ID
} from "@solana/spl-token"

import { readFileSync } from "node:fs"

// 1. 定义路径(可以从环境变量读取,或者直接写死)
const WALLET_PATH = process.env.WALLET_PATH || "/Users/qiaopengjun/.config/solana/id.json"

// 2. 读取并解析 JSON 文件
// Solana 的 id.json 格式是一个包含 64 个数字的数组 [12, 43, ...]
const secretKeyString = readFileSync(WALLET_PATH, "utf-8")
const secretKey = Uint8Array.from(JSON.parse(secretKeyString))

// 3. 生成 Keypair
// Import our keypair from the wallet file
const feePayer = Keypair.fromSecretKey(secretKey)

console.log(`✅ 已从路径加载钱包: ${feePayer.publicKey.toBase58()}`)

const endpoint = process.env.RPC_ENDPOINT || "https://api.devnet.solana.com"

//Create a connection to the RPC endpoint
const connection = new Connection(
    endpoint,
    "confirmed"
)

// Entry point of your TypeScript code (we will call this)
async function main() {
    try {

        // Generate a new keypair for the mint account
        const mint = Keypair.generate()

        const mintRent = await getMinimumBalanceForRentExemptMint(connection)

        // START HERE

        // Create the mint account
        const createAccountIx = SystemProgram.createAccount({
            fromPubkey: feePayer.publicKey,
            newAccountPubkey: mint.publicKey,
            space: MINT_SIZE,
            lamports: mintRent,
            programId: TOKEN_PROGRAM_ID
        })

        // Initialize the mint account
        // Set decimals to 6, and the mint and freeze authorities to the fee payer (you).
        const decimals = 6
        const initializeMintIx = createInitializeMint2Instruction(
            mint.publicKey,
            decimals, // decimals
            feePayer.publicKey, // mint authority
            feePayer.publicKey, // freeze authority
            TOKEN_PROGRAM_ID
        )

        // Create the associated token account
        const associatedTokenAccount = getAssociatedTokenAddressSync(
            mint.publicKey,
            feePayer.publicKey,
            false,
            TOKEN_PROGRAM_ID,
            ASSOCIATED_TOKEN_PROGRAM_ID
        )
        const createAssociatedTokenAccountIx = createAssociatedTokenAccountInstruction(
            feePayer.publicKey, // payer
            associatedTokenAccount, // ATA
            feePayer.publicKey, // owner
            mint.publicKey,
            TOKEN_PROGRAM_ID,
            ASSOCIATED_TOKEN_PROGRAM_ID
        )

        // Mint 21,000,000 tokens to the associated token account
        const mintAmount = BigInt(21_000_000) * BigInt(10 ** decimals)
        const mintToCheckedIx = createMintToCheckedInstruction(
            mint.publicKey,
            associatedTokenAccount,
            feePayer.publicKey, // mint authority
            mintAmount,
            decimals,
            [],
            TOKEN_PROGRAM_ID
        )

        const recentBlockhash = await connection.getLatestBlockhash()

        const transaction = new Transaction({
            feePayer: feePayer.publicKey,...

剩余50%的内容订阅专栏后可查看

点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论