Solana

2026年02月05日更新 19 人订阅
原价: ¥ 8.6 限时优惠
专栏简介

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 条评论

请先 登录 后评论