Solana CookbookTransactions

How to Add a Memo to a Transaction

Any transaction can add a message making use of the SPL memo program.

import {
airdropFactory,
appendTransactionMessageInstructions,
createSolanaClient,
createTransaction,
generateKeyPairSigner,
getSignatureFromTransaction,
lamports,
pipe,
setTransactionMessageFeePayerSigner,
setTransactionMessageLifetimeUsingBlockhash,
signTransactionMessageWithSigners
} from "gill";
import { getAddMemoInstruction } from "gill/programs";
const { rpc, rpcSubscriptions, sendAndConfirmTransaction } = createSolanaClient(
{
urlOrMoniker: "localnet"
}
);
const sender = await generateKeyPairSigner();
const LAMPORTS_PER_SOL = 1_000_000_000n;
await airdropFactory({ rpc, rpcSubscriptions })({
recipientAddress: sender.address,
lamports: lamports(LAMPORTS_PER_SOL), // 1 SOL
commitment: "confirmed"
});
const memoInstruction = getAddMemoInstruction({
memo: "Hello, world!"
});
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
const tx = createTransaction({
feePayer: sender,
version: "legacy",
instructions: [memoInstruction],
latestBlockhash
});
const signedTransaction = await signTransactionMessageWithSigners(tx);
const transactionSignature = getSignatureFromTransaction(signedTransaction);
console.log("Transaction Signature:", transactionSignature);
Console
Click to execute the code.

Is this page helpful?

How to Add a Memo to a Transaction | Solana