Solanaは、0.001ドル未満の手数料で即座にグローバルなトークン転送を可能にします。国境を越えた送金、給与支払い、または財務業務を構築する場合でも、基本的なステーブルコイン支払いは1秒未満で決済され、コストはわずか数分の1セントです。
仕組み
支払いは、送信者のトークンアカウントから受信者のトークンアカウントにステーブルコインを移動します。受信者がこのトークンを初めて受け取る場合、そのトークンアカウントは同じトランザクションの一部として作成できます。
コア支払いの概念については、Solanaでの支払いの仕組みを参照してください。
@solana/clientのsplTokenヘルパーは、ATAの導出、小数点変換、トランザクション構築を自動的に処理します。これは単一の支払い転送に最適です。
以下の手順はコアフローを示しています。完全な実行可能コードについては、デモを参照してください。
トークンヘルパーを作成する
ミントアドレスを指定してclient.splToken()を呼び出すことで、splToken()ヘルパーを設定します。ヘルパーは一般的なトークン操作のメソッドを提供します。
tokenProgram: "auto"を設定すると、ミントアカウントがTokenプログラムまたはToken-2022プログラムのどちらによって所有されているかを自動的に検出します。
支払いを送信する
sendTransfer()を使用して、ウォレット間でトークンを転送します。このメソッドは以下を処理します:
- ATA解決: 送信者と受信者の関連トークンアカウント(ATA)を自動的に導出します。受信者のATAが存在しない場合、アカウントを作成する命令が同じトランザクションに自動的に追加されます。
- 小数点変換: トークン量を受け入れ、ミントの小数点に基づいて基本単位に自動的に変換します(例: ミントが6桁の小数点を持つ場合、0.25トークン -> 250000基本単位)
- トランザクション構築: 転送命令を含むトランザクションを作成、署名、送信します
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?