Solana cho phép chuyển token toàn cầu tức thì với phí dưới $0.001. Cho dù bạn đang xây dựng chuyển tiền xuyên biên giới, giải ngân lương, hay các hoạt động quản lý quỹ, một khoản thanh toán stablecoin cơ bản được thanh toán trong vòng chưa đầy một giây và chỉ tốn một phần nhỏ của một xu.
Cách hoạt động
Một khoản thanh toán chuyển stablecoin từ token account của người gửi đến token account của người nhận. Nếu người nhận đang nhận token này lần đầu tiên, token account của họ có thể được tạo như một phần của cùng giao dịch đó.
Xem How Payments Work on Solana để hiểu các khái niệm thanh toán cốt lõi.
Plugin token2022Program() từ @solana-program/token-2022 mở rộng client
@solana/kit của bạn với helper client.token2022.
Phương thức transferToATA của nó tự động xử lý việc dẫn xuất ATA và xây dựng
giao dịch, rất lý tưởng cho các lần chuyển thanh toán đơn lẻ.
Các bước dưới đây cho thấy luồng cốt lõi. Xem Demo để có mã hoàn chỉnh có thể chạy được.
Tạo Token Helper
Thêm plugin token2022Program() vào client của bạn bằng
.use(token2022Program()). Thao tác này sẽ hiển thị client.token2022, trong
đó instructions cung cấp các phương thức cho các thao tác token thông dụng.
Sử dụng token2022Program() cho các mint thuộc sở hữu của chương trình
Token-2022, hoặc tokenProgram() từ @solana-program/token cho các mint thuộc
sở hữu của Token Program gốc.
Gửi thanh toán
Sử dụng transferToATA() để chuyển token giữa các ví. Phương thức này xử lý:
- Phân giải ATA: Tự động dẫn xuất Associated Token Accounts (ATAs) cho người gửi và người nhận. Nếu ATA của người nhận chưa tồn tại, lệnh tạo tài khoản sẽ tự động được thêm vào cùng giao dịch đó.
- Số tiền đã kiểm tra: Truyền
amounttheo đơn vị cơ sở cùng vớidecimalscủa mint. Lượng chuyển được kiểm tra so với số thập phân của mint trên chuỗi (ví dụ: 250000 đơn vị cơ sở -> 0.25 token, nếu mint có 6 chữ số thập phân) - Xây dựng giao dịch:
sendTransaction()xây dựng, ký và gửi giao dịch, phân giải thành một kết quả màcontext.signaturecủa nó chứa chữ ký giao dịch
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();
Xác minh số dư
Sau khi quá trình chuyển hoàn tất, hãy đọc số dư token từ RPC. Dẫn xuất ATA của
mỗi ví bằng findAssociatedTokenPda(), sau đó gọi getTokenAccountBalance() để
lấy số dư cho tài khoản đó.
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();
Demo
// 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?