Typescript
Token2022 with Web3.js

Token2022 with Web3.js

The Memo Transfer Extension

The MemoTranfer Extension is a Token account extension that enforces that all incoming transfers to a token account include a memo, facilitating enhanced transaction tracking and user identification.

Initializing the Token Account

To initialie the MemoTransfer extension on a Token account we're going to need the enableRequiredMemoTransfers() function.

Here's how to create a token with the Immutable Owner extension:

import {
    Keypair,
    SystemProgram,
    Transaction,
    sendAndConfirmTransaction,
} from '@solana/web3.js';
import {
    createInitializeAccountInstruction,
    createInitializeImmutableOwnerInstruction,
    getAccountLen,
    ExtensionType,
    TOKEN_2022_PROGRAM_ID,
} from '@solana/spl-token';
 
const tokenAccount = Keypair.generate();
 
// Calculate the size needed for a Token account with Transfer Fee extension
const accountLen = getAccountLen([ExtensionType.ImmutableOwner]);
 
// Calculate minimum lamports required for rent exemption
const lamports = await connection.getMinimumBalanceForRentExemption(accountLen);
 
// Create the account with the correct size and owner
const createAccountInstruction = SystemProgram.createAccount({
    fromPubkey: keypair.publicKey,
    newAccountPubkey: tokenAccount.publicKey,
    space: accountLen,
    lamports,
    programId: TOKEN_2022_PROGRAM_ID,
});
 
// Initialize the Memo Transfer extension
const enableMemoTransferInstruction = createEnableRequiredMemoTransfersInstruction(
    destinationTokenAccount.publicKey,
    destinationKeypair.publicKey,
    undefined,
    TOKEN_2022_PROGRAM_ID,
);
 
// Initialize the Token account itself
const initializeAccountInstruction = createInitializeAccountInstruction(
    tokenAccount.publicKey,
    mint.publicKey,
    keypair.publicKey,
    TOKEN_2022_PROGRAM_ID,
);
 
const transaction = new Transaction().add(
    createAccountInstruction,
    initializeAccountInstruction,
    enableMemoTransferInstruction,
);
 
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair, tokenAccount], {skipPreflight: false});
 
console.log(`Token accounts created! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);

Transferring Token with a Memo

To use the Memo program on Solana we have to viable path.

Building our own "raw" memo instruction like this:

const message = "Hello, Solana";
 
new TransactionInstruction({
    keys: [{ pubkey: keypair.publicKey, isSigner: true, isWritable: true }],
    data: Buffer.from(message, "utf-8"), // Memo message. In this case it is "Hello, Solana"
    programId: new PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"), // Memo program that validates keys and memo message
}),
 
Or we can use the Memo program SDK after downloading the package like this: 
 

npm i @solana/spl-memo


In our example we're going to use the second option and it will look like this:

```ts
const memoInstruction = createMemoInstruction(
    "Hello, world!",
    [keypair.publicKey],
);

const transferInstruction = createTransferCheckedInstruction(
    tokenAccount,
    mint.publicKey,
    destinationTokenAccount.publicKey,
    keypair.publicKey,
    BigInt(100e6),
    6,
    undefined,
    TOKEN_2022_PROGRAM_ID,
);

const transferTransaction = new Transaction().add(
    memoInstruction,
    transferInstruction,
);

const transferSignature = await sendAndConfirmTransaction(connection, transferTransaction, [keypair]);

console.log(`Tokens transferred with memo! Check out your TX here: https://explorer.solana.com/tx/${transferSignature}?cluster=devnet`);

Disabling and Enabling the Memo

If we don't want to enforce the transfer to come with a memo, we can do so using the disableRequiredMemoTransfer function and it would look like so:

const disableRequiredMemoTransfersInstruction = createDisableRequiredMemoTransfersInstruction(
    destinationTokenAccount,
    destinationKeypair.publicKey,
    undefined,
    TOKEN_2022_PROGRAM_ID,
);
 
const transferInstruction = createTransferCheckedInstruction(
    tokenAccount,
    mint.publicKey,
    destinationTokenAccount,
    keypair.publicKey,
    BigInt(100e6),
    6,
    undefined,
    TOKEN_2022_PROGRAM_ID,
);
 
const transaction = new Transaction().add(
    disableRequiredMemoTransfersInstruction,
    transferInstruction,
);
 
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair, destinationKeypair]);
 
console.log(`Tokens transferred and account state changed! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);

And if we want to enable it back we can just use the enableRequiredMemoTransfers() function like this:

const enableRequiredMemoTransfersInstruction = createEnableRequiredMemoTransfersInstruction(
    destinationTokenAccount,
    destinationKeypair.publicKey,
    undefined,
    TOKEN_2022_PROGRAM_ID,
);
 
const transferInstruction = createTransferCheckedInstruction(
    tokenAccount,
    mint.publicKey,
    destinationTokenAccount,
    keypair.publicKey,
    BigInt(100e6),
    6,
    undefined,
    TOKEN_2022_PROGRAM_ID,
);
 
const transaction = new Transaction().add(
    enableRequiredMemoTransfersInstruction,
    transferInstruction,
);
 
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair, destinationKeypair]);
 
console.log(`Tokens transferred and account state changed! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);
Contents
View Source
Blueshift © 2025Commit: dd6c76d