Solana umożliwia natychmiastowe, globalne transfery tokenów z opłatami poniżej 0,001 USD. Niezależnie od tego, czy tworzysz rozwiązania do przekazów międzynarodowych, wypłat wynagrodzeń czy operacji skarbowych, podstawowa płatność stablecoinem rozlicza się w mniej niż sekundę i kosztuje ułamek centa.
Jak to działa
Płatność przenosi stablecoiny z token account nadawcy na token account odbiorcy. Jeśli odbiorca otrzymuje ten token po raz pierwszy, jego token account może zostać utworzone w ramach tej samej transakcji.
Zobacz Jak działają płatności na Solanie, aby poznać podstawowe pojęcia dotyczące płatności.
Wtyczka token2022Program() z @solana-program/token-2022 rozszerza Twojego
klienta @solana/kit o helper client.token2022. Jego
metoda transferToATA automatycznie obsługuje wyprowadzanie ATA i budowanie
transakcji, co jest idealne do pojedynczych transferów płatności.
Poniższe kroki pokazują główny przebieg procesu. Pełny, gotowy do uruchomienia kod znajdziesz w sekcji Demo.
Utwórz Token Helper
Dodaj wtyczkę token2022Program() do swojego klienta za pomocą
.use(token2022Program()). Udostępnia to client.token2022, którego
instructions zapewniają metody do typowych operacji na tokenach.
Użyj token2022Program() dla mintów należących do programu Token-2022 lub
tokenProgram() z @solana-program/token dla mintów należących do oryginalnego
Token Program.
Wyślij płatność
Użyj transferToATA(), aby przesyłać tokeny między portfelami. Metoda
obsługuje:
- Rozwiązywanie ATA: Automatycznie wyprowadza Associated Token Accounts (ATA) dla nadawcy i odbiorcy. Jeśli associated token account odbiorcy nie istnieje, instrukcja utworzenia konta jest automatycznie dodawana do tej samej transakcji.
- Sprawdzane kwoty: Podaj
amountw jednostkach bazowych wraz zdecimalsminta. Transfer jest weryfikowany względem dziesiętnych minta on-chain (np. 250000 jednostek bazowych -> 0,25 tokena, jeśli mint ma 6 miejsc dziesiętnych) - Budowanie transakcji:
sendTransaction()buduje, podpisuje i wysyła transakcję, zwracając wynik, któregocontext.signaturezawiera sygnaturę transakcji
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();
Weryfikacja sald
Po zakończeniu transferu odczytaj salda tokenów z RPC. Wyprowadź ATA każdego
portfela za pomocą findAssociatedTokenPda(), a następnie wywołaj
getTokenAccountBalance(), aby pobrać saldo dla tego konta.
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?