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.
Hàm trợ giúp splToken từ
@solana/client tự động xử lý việc
suy ra ATA, chuyển đổi số thập phân và xây dựng giao dịch. Điều này 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
Cấu hình hàm trợ giúp splToken() bằng cách gọi client.splToken() với địa chỉ
mint. Hàm trợ giúp cung cấp các phương thức cho các thao tác token phổ biến.
Đặt tokenProgram: "auto" tự động phát hiện xem mint account có thuộc sở hữu
của chương trình Token hay Token-2022.
Gửi thanh toán
Sử dụng sendTransfer() để chuyển token giữa các ví. Phương thức này xử lý:
- Phân giải ATA: Tự động suy ra Associated Token Accounts (ATA) cho người gửi và người nhận. Nếu ATA của người nhận không 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.
- Chuyển đổi số thập phân: Chấp nhận số lượng token và tự động chuyển đổi sang đơn vị cơ sở dựa trên số thập phân của mint (ví dụ: 0.25 token -> 250000 đơn vị cơ sở, nếu mint có 6 số thập phân)
- Xây dựng giao dịch: Tạo, ký và gửi giao dịch với lệnh chuyển
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});
Xác minh số dư
Sau khi hoàn tất chuyển khoản, sử dụng fetchBalance() để xác minh số dư token.
Phương thức này nhận địa chỉ ví và tự động suy ra ATA tương ứng để lấy số dư.
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// =============================================================================
Is this page helpful?