Solana 实现了即时、全球范围的代币转账,手续费低于 $0.001。无论你是在构建跨境汇款、工资发放还是资金管理,基础稳定币支付都能在一秒内结算,成本仅为一分钱的一小部分。
工作原理
支付会将稳定币从发送方的 token account 转入接收方的 token account。如果接收方首次接收该代币,其 token account 可以在同一笔交易中自动创建。
参见 Solana 上的支付原理 了解核心支付概念。
来自 @solana-program/token-2022 的 token2022Program() 插件,为您的
@solana/kit 客户端扩展了 client.token2022
辅助工具。其 transferToATA
方法可自动处理 ATA 派生和交易构建,非常适合单笔支付转账。
以下步骤展示了核心流程。完整可运行代码请参见 演示。
创建 Token 辅助工具
使用 .use(token2022Program()) 将 token2022Program()
插件添加到您的客户端。这将暴露 client.token2022,其 instructions
提供了常见 token 操作的方法。
对于由 Token-2022 程序拥有的铸币,请使用 token2022Program();对于由原始 Token
Program 拥有的铸币,请使用来自 @solana-program/token 的 tokenProgram()。
发送支付
使用 transferToATA() 在钱包之间转移代币。该方法处理以下事项:
- ATA 解析:自动为发送方和接收方派生 Associated Token Accounts(ATA)。若接收方的 ATA 不存在,则创建该账户的指令将自动添加到同一笔交易中。
- 金额校验:以基本单位传入
amount,同时传入铸币的decimals。转账将在链上根据铸币的小数位数进行校验(例如,若铸币有 6 位小数,250000 基本单位 -> 0.25 个代币)。 - 交易构建:
sendTransaction()负责构建、签名并发送交易,返回一个结果,其context.signature字段保存交易签名。
const client = createClient().use(signer(sender)).use(solanaLocalRpc()).use(token2022Program());const result = await client.token2022.instructions.transferToATA({mint: mint.address,authority: sender,recipient: recipient.address,amount: 250_000n,decimals: 6}).sendTransaction();
验证余额
转账完成后,从 RPC 读取代币余额。使用 findAssociatedTokenPda()
派生每个钱包的 ATA,然后调用 getTokenAccountBalance() 获取该账户的余额。
const client = createClient().use(signer(sender)).use(solanaLocalRpc()).use(token2022Program());const result = await client.token2022.instructions.transferToATA({mint: mint.address,authority: sender,recipient: recipient.address,amount: 250_000n,decimals: 6}).sendTransaction();const [senderAta] = await findAssociatedTokenPda({mint: mint.address,owner: sender.address,tokenProgram: TOKEN_2022_PROGRAM_ADDRESS});const [recipientAta] = await findAssociatedTokenPda({mint: mint.address,owner: recipient.address,tokenProgram: TOKEN_2022_PROGRAM_ADDRESS});const senderBalance = await client.rpc.getTokenAccountBalance(senderAta).send();const recipientBalance = await client.rpc.getTokenAccountBalance(recipientAta).send();
演示
// Generate signers for sender and recipientconst sender = await generateKeyPairSigner();const recipient = await generateKeyPairSigner();console.log("Sender Address:", sender.address);console.log("Recipient Address:", recipient.address);// Demo Setup: Create client, mint account, and fund the sender with initial tokensconst { client, mint } = await demoSetup(sender);console.log("\nMint Address:", mint.address);// =============================================================================// Basic Token Payment Demo// =============================================================================// Transfer tokens from sender to recipient. The source and destination ATAs are// derived automatically, and the recipient's ATA is created if it does not exist.const result = await client.token2022.instructions.transferToATA({mint: mint.address,authority: sender,recipient: recipient.address,amount: 250_000n, // 0.25 tokens (base units for a mint with 6 decimals)decimals: 6}).sendTransaction();console.log("\n=== Token Payment Complete ===");console.log("Transaction Signature:", result.context.signature);// Derive the sender and recipient ATAs to read their balancesconst [senderAta] = await findAssociatedTokenPda({mint: mint.address,owner: sender.address,tokenProgram: TOKEN_2022_PROGRAM_ADDRESS});const [recipientAta] = await findAssociatedTokenPda({mint: mint.address,owner: recipient.address,tokenProgram: TOKEN_2022_PROGRAM_ADDRESS});// Fetch final token account balances via RPCconst senderBalance = await client.rpc.getTokenAccountBalance(senderAta).send();const recipientBalance = await client.rpc.getTokenAccountBalance(recipientAta).send();console.log("\nSender Token Account Balance:",senderBalance.value.uiAmountString);console.log("Recipient Token Account Balance:",recipientBalance.value.uiAmountString);// =============================================================================// Demo Setup Helper Function// =============================================================================
Is this page helpful?