The MemoTransfer
extension enforces that every incoming transfer to a Token
Account is accompanied by a memo instruction.
This memo instruction records a message in the transaction's program logs. This
feature is particularly useful for adding context to transactions, making it
easier to understand their purpose when reviewing the transaction logs later.
In this guide, we'll walk through an example of using Solana Playground. Here is the final script.
Getting Started
Start by opening this Solana Playground link with the following starter code.
If it is your first time using Solana Playground, you'll first need to create a Playground Wallet and fund the wallet with devnet SOL.
If you do not have a Playground wallet, you may see a type error within the
editor on all declarations of pg.wallet.publicKey
. This type error will clear
after you create a Playground wallet.
To get devnet SOL, run the solana airdrop
command in the Playground's
terminal, or visit this devnet faucet.
Once you've created and funded the Playground wallet, click the "Run" button to run the starter code.
Add Dependencies
Let's start by setting up our script. We'll be using the @solana/web3.js
,
@solana/spl-token
, and @solana/spl-memo
libraries.
Replace the starter code with the following:
Mint Setup
We'll first need to create a new Mint Account before we can create Token Accounts.
Memo Transfer Token Account
Next, let's build a transaction to enable the MemoTransfer
extension for a new
Token Account.
First, let's generate a new keypair to use as the address of the Token Account.
Next, let's determine the size of the new Token Account and calculate the minimum lamports needed for rent exemption.
With Token Extensions, the size of the Token Account will vary based on the extensions enabled.
Build Instructions
Next, let's build the set of instructions to:
- Create a new account
- Initialize the Token Account data
- Enable the
MemoTransfer
extension
First, build the instruction to invoke the System Program to create an account and assign ownership to the Token Extensions Program.
Next, build the instruction to initialize the Token Account data.
Lastly, build the instruction to enable the MemoTransfer
extension for the
Token Account.
Send Transaction
Next, let's add the instructions to a new transaction and send it to the
network. This will create a Token Account with the MemoTransfer
extension
enabled.
Run the script by clicking the Run
button. You can then inspect the
transaction details on SolanaFM.
Create and Fund Token Account
Next, let's set up another Token Account to demonstrate the functionality of the
MemoTransfer
extension.
First, create a sourceTokenAccount
owned by the Playground wallet.
Next, mint 2 tokens to the sourceTokenAccount
to fund it.
Transfer and Memo Instruction
Next, let's prepare the token transfer and memo instructions.
First, build the instruction to transfer tokens from the sourceTokenAccount
to
the tokenAccount
which has the MemoTransfer
extension enabled.
Next, build the memo instruction. The message will be included in the program logs of the transaction the instruction is added to.
Alternatively, you can create the instruction using the @solana/spl-memo
library:
Attempt Transfer without Memo
To demonstrate the functionality of the MemoTransfer
extension, let's first
attempt to send a token transfer without a memo.
Run the script by clicking the Run
button. You can then inspect the error in
the Playground terminal. You should see a message similar to the following:
Transfer with Memo
Next, send a token transfer with the memo instruction included on the transaction.
Run the script by clicking the Run
button. You can then inspect the
transaction details on SolanaFM.
Enable and Disable Memo Transfer
The MemoTransfer
extension can also be freely enabled or disabled by the Token
Account owner.
To enable the MemoTransfer
extension, use the enableRequiredMemoTransfers
instruction.
To disable the MemoTransfer
extension, use the disableRequiredMemoTransfers
instruction.
Once the MemoTransfer
extension is disabled, transactions to transfer tokens
without a memo instruction will complete successfully.
Run the script by clicking the Run
button. You can then inspect the
transaction details on SolanaFM.
Conclusion
The MemoTransfer
extension ensures every incoming transfer to a Token Account
includes a memo. By requiring a memo instruction with each transfer, a message
is recorded in the transaction's program logs. This feature is especially useful
for understanding the purpose of transactions when reviewing logs at a later
time.