Chương trình memo của Solana cho phép bạn đính kèm số hóa đơn, ID đơn hàng hoặc tham chiếu tùy chỉnh vào bất kỳ khoản thanh toán nào. Các memo này được ghi lại vĩnh viễn trên chuỗi và hiển thị trong nhật ký giao dịch, giúp dễ dàng đối chiếu thanh toán với hệ thống nội bộ của bạn.
Cách hoạt động
Chương trình memo ghi văn bản vào nhật ký của giao dịch. Các nhật ký này được lập chỉ mục bởi các trình khám phá và nhà cung cấp RPC, giúp memo có thể tìm kiếm được để đối chiếu thanh toán.
Xem Cách thanh toán hoạt động trên Solana để hiểu các khái niệm thanh toán cốt lõi.
Thêm memo yêu cầu xây dựng các lệnh trực tiếp, điều này cho bạn quyền kiểm soát những gì được bao gồm trong giao dịch.
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.
Import chương trình memo
Import getAddMemoInstruction từ @solana-program/memo để tạo các lệnh memo.
Tạo lệnh chuyển khoản
Tạo lệnh chuyển token chỉ định ATA nguồn, ATA đích, quyền hạn (người ký), và số lượng theo đơn vị cơ bản.
Tạo lệnh memo
Tạo một lệnh memo với một thông điệp. Thông điệp này sẽ hiển thị trong nhật ký chương trình của giao dịch.
Gửi giao dịch với memo
Kết hợp các lệnh chuyển khoản và memo trong một giao dịch duy nhất.
Xem ghi chú trong nhật ký giao dịch
Sau khi giao dịch được xác nhận, hãy truy xuất nó để xem ghi chú trong nhật ký. Ghi chú sẽ xuất hiện dưới dạng thông báo nhật ký từ chương trình Memo.
"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// =============================================================================
Is this page helpful?