From CLI Commands to My First Solana Transfer Tool
From CLI Commands to My First Solana Transfer Tool
从命令行指令到我的第一个 Solana 转账工具
By this point in the #100DaysOfSolana course, I had already explored a few different aspects of Solana, not just the CLI. But when I first started experimenting, I was mainly running one‑off CLI commands on devnet: solana transfer <recipient> <amount>. It worked, but it felt awkward. Each time I wanted to send SOL I had to type in the recipient and the amount again, with no balance checks or helpful feedback — just bare commands.
在 #100DaysOfSolana 课程进行到这一阶段时,我已经探索了 Solana 的多个方面,而不仅仅是命令行界面(CLI)。但当我刚开始尝试时,我主要是在 devnet(开发网)上运行一次性的 CLI 命令:solana transfer <recipient> <amount>。虽然能用,但感觉很笨拙。每次我想发送 SOL 时,都必须重新输入接收者地址和金额,没有任何余额检查或有用的反馈——仅仅是原始的命令。
On Day 17, the course pushed me to go further: instead of repeating raw CLI commands, I built my own Node.js script. Now I can run: node transfer.mjs <recipient> 0.05. The recipient and amount are passed in as arguments, and the script handles the rest — checking balances, sending the transaction, and even giving me a link to Solana Explorer. That was the moment things started to click. I wasn’t just typing commands anymore, I was building a tool I could reuse.
在第 17 天,课程推动我更进一步:我不再重复输入原始的 CLI 命令,而是编写了自己的 Node.js 脚本。现在我可以运行:node transfer.mjs <recipient> 0.05。接收者和金额作为参数传入,脚本会自动处理剩下的工作——检查余额、发送交易,甚至还会给我一个 Solana Explorer 的链接。那一刻,一切都豁然开朗了。我不再只是敲击命令,而是在构建一个可以重复使用的工具。
The Struggle
挣扎
At first, I thought it would be straightforward. But I quickly ran into issues: Node v12 didn’t support modern syntax like ?? and ?.. Even after upgrading, I hit the dreaded error: Transfer failed: No random values implementation could be found. Debugging felt endless. I was learning Web3 concepts while also fighting with Web2 runtime quirks.
起初,我以为这会很简单。但我很快遇到了问题:Node v12 不支持像 ?? 和 ?. 这样的现代语法。即使升级后,我又遇到了那个令人头疼的错误:Transfer failed: No random values implementation could be found(转账失败:找不到随机值实现)。调试过程感觉永无止境。我既要学习 Web3 的概念,又要与 Web2 运行时的各种怪癖作斗争。
The Breakthrough
突破
The breakthrough came when I realized the Solana kit I was using expected browser‑like APIs. Node didn’t expose them globally. So I patched my environment:
当我意识到我使用的 Solana 开发套件需要类似浏览器的 API,而 Node 没有在全局范围内暴露这些 API 时,突破口出现了。于是我修补了我的环境:
import { webcrypto } from "node:crypto";
globalThis.crypto = webcrypto;
// Polyfill CustomEvent for Node.js < 18.7
if (typeof CustomEvent === "undefined") {
globalThis.CustomEvent = class CustomEvent extends Event {
constructor(type, options = {}) {
super(type, options);
this.detail = options.detail ?? null;
}
};
}
Suddenly, the randomness error disappeared. My script connected, checked balances, built transactions, and confirmed them with a link to Solana Explorer.
突然间,随机性错误消失了。我的脚本成功连接、检查余额、构建交易,并通过 Solana Explorer 的链接确认了交易。
The Result
成果
Now I have a reusable CLI transfer tool that:
- Accepts recipient + amount as arguments
- Checks my balance before sending
- Reports the result with a signature and Explorer link
- Shows my updated balance after the transfer
现在我拥有了一个可复用的 CLI 转账工具,它可以:
- 接受接收者和金额作为参数
- 在发送前检查我的余额
- 通过签名和 Explorer 链接报告结果
- 在转账后显示我更新后的余额
What started as a one‑off CLI experiment became a proper utility — something I can reuse, extend, and share.
最初只是一个一次性的 CLI 实验,现在变成了一个实用的工具——我可以重复使用、扩展并分享它。
My Takeaway
我的心得
Web3 isn’t magic. It’s just APIs, runtimes, and debugging like any other stack. The difference is that every bug you fix teaches you something deeper about how blockchains work. If you’re a web or mobile developer curious about Solana, start small. Wrap a CLI command in code. Debug the errors. Before you know it, you’ll have built your own tool — and you’ll understand Solana a lot better. I went from typing raw commands to building my first Solana transfer tool. And the best part? It feels like the beginning of something bigger.
Web3 并不是魔法。它和其他技术栈一样,无非就是 API、运行时和调试。不同之处在于,你修复的每一个 Bug 都会让你对区块链的运作方式有更深刻的理解。如果你是一名对 Solana 好奇的 Web 或移动端开发者,请从小处着手。用代码封装一个 CLI 命令,调试错误。不知不觉中,你就会构建出自己的工具,并且会更深入地理解 Solana。我从敲击原始命令转变为构建出我的第一个 Solana 转账工具。最棒的是什么?这感觉像是开启了更宏大篇章的序幕。