Solana 实现了即时、全球范围的代币转账,手续费低于 $0.001。无论你是在构建跨境汇款、工资发放还是资金管理,基础稳定币支付都能在一秒内结算,成本仅为一分钱的一小部分。
工作原理
支付会将稳定币从发送方的 token account 转入接收方的 token account。如果接收方首次接收该代币,其 token account 可以在同一笔交易中自动创建。
参见 Solana 上的支付原理 了解核心支付概念。
splToken 辅助工具来自
@solana/client,可自动处理 ATA 推导、小数转换和交易构建,非常适合单笔支付转账。
以下步骤展示了核心流程。完整可运行代码请参见 演示。
创建 Token 辅助工具
通过调用 client.splToken() 并传入 mint 地址,配置 splToken()
辅助工具。该工具提供常用代币操作方法。
设置 tokenProgram: "auto" 时会自动检测 mint
account 是否由 Token 或 Token-2022 程序管理。
发送支付
使用 sendTransfer() 在钱包之间转账代币。该方法会自动处理:
- ATA 解析:自动推导发送方和接收方的 Associated Token Account(ATA)。如果接收方的 ATA 不存在,创建账户的指令会自动加入同一笔交易。
- 小数转换:支持代币金额输入,并根据 mint 的小数位自动转换为基础单位(如 0.25 代币 -> 250000 基础单位,若 mint 有 6 位小数)
- 交易构建:创建、签名并发送包含转账指令的交易
Basic Payment
const client = createClient({endpoint: "http://localhost:8899",websocketEndpoint: "ws://localhost:8900",commitment: "confirmed"});const splToken = client.splToken({mint: mint.address,tokenProgram: "auto"});const signature = await splToken.sendTransfer({amount: 0.25,authority: sender,destinationOwner: recipient.address});
验证余额
转账完成后,使用 fetchBalance()
来验证代币余额。该方法接收一个钱包地址,并会自动推导出对应的 ATA 以获取余额。
Basic Payment
const client = createClient({endpoint: "http://localhost:8899",websocketEndpoint: "ws://localhost:8900",commitment: "confirmed"});const splToken = client.splToken({mint: mint.address,tokenProgram: "auto"});const signature = await splToken.sendTransfer({amount: 0.25,authority: sender,destinationOwner: recipient.address});const senderBalance = await splToken.fetchBalance(sender.address);const recipientBalance = await splToken.fetchBalance(recipient.address);
演示
Demo
// Generate keypairs for sender and recipientconst sender = (await generateKeypair()).signer;const recipient = (await generateKeypair()).signer;console.log("Sender Address:", sender.address);console.log("Recipient Address:", recipient.address);// Demo Setup: Create client, mint account, token accounts, and fund with initial tokensconst { client, mint } = await demoSetup(sender, recipient);console.log("\nMint Address:", mint.address);// =============================================================================// Basic Token Payment Demo// =============================================================================// Create splToken helper for this mint using @solana/clientconst splToken = client.splToken({mint: mint.address,tokenProgram: "auto"});// Transfer tokens from sender to recipient (ATA and decimals handled automatically)const signature = await splToken.sendTransfer({amount: 0.25,authority: sender,destinationOwner: recipient.address});console.log("\n=== Token Payment Complete ===");console.log("Transaction Signature:", signature.toString());// Fetch final token account balances using splToken helperconst senderBalance = await splToken.fetchBalance(sender.address);const recipientBalance = await splToken.fetchBalance(recipient.address);console.log("\nSender Token Account Balance:", senderBalance);console.log("Recipient Token Account Balance:", recipientBalance);// =============================================================================// Demo Setup Helper Function// =============================================================================
Console
Click to execute the code.
Is this page helpful?