Solana 的 memo 程序允许你为任何支付附加发票编号、订单 ID 或自定义参考信息。这些备注会永久记录在链上,并在交易日志中可见,方便你将支付与内部系统进行匹配。
工作原理
Memo 程序会将文本写入交易日志。这些日志会被区块浏览器和 RPC 服务商索引,使备注内容可以被搜索,便于支付对账。
参见 Solana 支付原理 了解核心支付概念。
添加备注需要直接构建指令,这样你可以完全控制交易中包含的内容。
以下步骤展示了核心流程。完整可运行代码请参见 演示。
导入 Memo 程序
从 getAddMemoInstruction 导入 @solana-program/memo,用于创建 memo 指令。
创建转账指令
创建代币转账指令,指定源 ATA、目标 ATA、授权人(签名者)以及以基础单位计的金额。
创建 Memo 指令
创建带有消息的 memo 指令。该消息将在交易的程序日志中可见。
发送带备注的交易
将转账指令和 memo 指令合并到同一笔交易中。
在交易日志中查看 Memo
交易确认后,获取该交易即可在日志中查看 Memo。Memo 会作为 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"
演示
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);// Derive the Associated Token Accounts addresses (ATAs) for sender and recipientconst [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 transactionconst memoInstruction = getAddMemoInstruction({memo: "Payment for services rendered - Invoice #12345"});// Prepare and send transaction with both transfer and memo using @solana/clientconst 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 helperconst 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 logsconst 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?