Płatność z notatką

Program memo Solany pozwala dołączać numery faktur, identyfikatory zamówień lub własne odniesienia do każdej płatności. Te notatki są trwale zapisywane on-chain i widoczne w logach transakcji, co ułatwia powiązanie płatności z Twoimi systemami wewnętrznymi.

Jak to działa

Program Memo zapisuje tekst w logach transakcji. Logi te są indeksowane przez explorery i dostawców RPC, dzięki czemu notatki można wyszukiwać podczas uzgadniania płatności.

Zobacz Jak działają płatności w Solanie, aby poznać podstawowe pojęcia dotyczące płatności.

Dodanie notatki wymaga samodzielnego budowania instrukcji, co daje Ci kontrolę nad tym, co zostanie uwzględnione w transakcji.

Poniższe kroki pokazują główny przebieg. Zobacz Demo, aby uzyskać kompletny, gotowy do uruchomienia kod.

Import Memo Program

Zaimportuj getAddMemoInstruction z @solana-program/memo, aby tworzyć instrukcje memo.

Create Transfer Instruction

Utwórz instrukcję transferu tokenów, określając źródłowe ATA, docelowe ATA, autoryzację (podpisującego) oraz kwotę w jednostkach bazowych.

Create Memo Instruction

Utwórz instrukcję memo z wiadomością. Ta wiadomość będzie widoczna w logach programu danej transakcji.

Send Transaction with Memo

Połącz instrukcje transferu i memo w jednej transakcji.

Wyświetlanie memo w logach transakcji

Po potwierdzeniu transakcji pobierz ją, aby zobaczyć memo w logach. Memo pojawi się jako komunikat logu z programu Memo.

Example Transaction Logs
"Program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb invoke [1]",
"Program log: Instruction: Transfer",
"Program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb consumed 1682 of 200000 compute units",
"Program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb success",
"Program MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr invoke [1]",
'Program log: Memo (len 46): "Payment for services rendered - Invoice #12345"',
"Program MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr consumed 18097 of 198318 compute units",
"Program MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr success",
"Program ComputeBudget111111111111111111111111111111 invoke [1]",
"Program ComputeBudget111111111111111111111111111111 success"

Import Memo Program

Zaimportuj getAddMemoInstruction z @solana-program/memo, aby tworzyć instrukcje memo.

Create Transfer Instruction

Utwórz instrukcję transferu tokenów, określając źródłowe ATA, docelowe ATA, autoryzację (podpisującego) oraz kwotę w jednostkach bazowych.

Create Memo Instruction

Utwórz instrukcję memo z wiadomością. Ta wiadomość będzie widoczna w logach programu danej transakcji.

Send Transaction with Memo

Połącz instrukcje transferu i memo w jednej transakcji.

Wyświetlanie memo w logach transakcji

Po potwierdzeniu transakcji pobierz ją, aby zobaczyć memo w logach. Memo pojawi się jako komunikat logu z programu Memo.

Example Transaction Logs
"Program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb invoke [1]",
"Program log: Instruction: Transfer",
"Program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb consumed 1682 of 200000 compute units",
"Program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb success",
"Program MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr invoke [1]",
'Program log: Memo (len 46): "Payment for services rendered - Invoice #12345"',
"Program MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr consumed 18097 of 198318 compute units",
"Program MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr success",
"Program ComputeBudget111111111111111111111111111111 invoke [1]",
"Program ComputeBudget111111111111111111111111111111 success"
Payment with Memo
import { getAddMemoInstruction } from "@solana-program/memo";

Demo

Demo
// Generate keypairs for sender and recipient
const 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 tokens
const { client, mint } = await demoSetup(sender, recipient);
console.log("\nMint Address:", mint.address);
// Derive the Associated Token Accounts addresses (ATAs) for sender and recipient
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
});
console.log("Sender Token Account:", senderAta.toString());
console.log("Recipient Token Account:", recipientAta.toString());
// =============================================================================
// Token Payment with Memo Demo
// =============================================================================
// Create instruction to transfer tokens from sender to recipient
// Transferring 250,000 base units = 0.25 tokens (with 6 decimals)
const transferInstruction = getTransferInstruction({
source: senderAta,
destination: recipientAta,
authority: sender.address,
amount: 250_000n // 0.25 tokens
});
// Create instruction to add a memo to the transaction
const memoInstruction = getAddMemoInstruction({
memo: "Payment for services rendered - Invoice #12345"
});
// Prepare and send transaction with both transfer and memo using @solana/client
const signature = await client.transaction.prepareAndSend({
authority: sender,
instructions: [transferInstruction, memoInstruction],
version: 0
});
console.log("\n=== Token Payment with Memo Complete ===");
console.log("Transaction Signature:", signature.toString());
// Fetch final token account balances using @solana/client SPL token helper
const splToken = client.splToken({
mint: mint.address,
tokenProgram: "auto"
});
const 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);
// Fetch transaction details to view the memo in the logs
const transaction = await client.runtime.rpc
.getTransaction(signature, {
encoding: "jsonParsed",
maxSupportedTransactionVersion: 0
})
.send();
console.log("\nTransaction logs with Memo:");
console.log(transaction?.meta?.logMessages);
// =============================================================================
// Demo Setup Helper Function
// =============================================================================
Console
Click to execute the code.

Is this page helpful?

Spis treści

Edytuj stronę

Zarządzane przez

© 2026 Solana Foundation.
Wszelkie prawa zastrzeżone.
Bądź na bieżąco