Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

token-2022: Add memo-transfer examples #6668

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions docs/src/token-2022/extensions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -909,13 +909,17 @@ import {
Transaction,
LAMPORTS_PER_SOL,
} from '@solana/web3.js';
import { createMemoInstruction } from '@solana/spl-memo';
import {
createAssociatedTokenAccount,
createMint,
createEnableRequiredMemoTransfersInstruction,
createInitializeAccountInstruction,
createTransferInstruction,
disableRequiredMemoTransfers,
enableRequiredMemoTransfers,
getAccountLen,
mintTo,
ExtensionType,
TOKEN_2022_PROGRAM_ID,
} from '@solana/spl-token';
Expand Down Expand Up @@ -993,6 +997,43 @@ Signature: 5MnWtrhMK32zkbacDMwBNft48VAUpr4EoRM87hkT9AFYvPgPEU7V7ERV6gdfb3kASri4w
</TabItem>
</Tabs>

#### Example: Transferring with a memo

When transferring into an account with required transfer memos, you must include
a memo instruction before the transfer.

<Tabs className="unique-tabs" groupId="language-selection">
<TabItem value="cli" label="CLI" default>

```console
$ spl-token transfer EbPBt3XkCb9trcV4c8fidhrvoeURbDbW87Acustzyi8N 10 4Uzz67txwYbfYpF8r5UGEMYJwhPAYQ5eFUY89KTYc2bL --with-memo "memo text"
Signature: 5a9X8JrWzwZqb3iMonfUfSZbisQ57aEmW5cFntWGYRv2UZx8ACkMineBEQRHwLMzYHeyFDEHMXu8zqAMv5tm4u1g
```

</TabItem>
<TabItem value="jsx" label="JS">

```jsx
const sourceTokenAccount = await createAssociatedTokenAccount(
connection,
payer,
mint,
payer.publicKey,
undefined,
TOKEN_2022_PROGRAM_ID
);
await mintTo(connection, payer, mint, sourceTokenAccount, mintAuthority, 100, [], undefined, TOKEN_2022_PROGRAM_ID);

const transferTransaction = new Transaction().add(
createMemoInstruction('Hello, memo-transfer!', [payer.publicKey]),
createTransferInstruction(sourceTokenAccount, destination, payer.publicKey, 100, [], TOKEN_2022_PROGRAM_ID)
);
await sendAndConfirmTransaction(connection, transferTransaction, [payer], undefined);
```

</TabItem>
</Tabs>

### Reallocate

In the previous example, astute readers of the JavaScript code may have noticed
Expand Down
20 changes: 20 additions & 0 deletions token/js/examples/memoTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import {
Transaction,
LAMPORTS_PER_SOL,
} from '@solana/web3.js';
import { createMemoInstruction } from '@solana/spl-memo';
import {
createAssociatedTokenAccount,
createMint,
createEnableRequiredMemoTransfersInstruction,
createInitializeAccountInstruction,
createTransferInstruction,
disableRequiredMemoTransfers,
enableRequiredMemoTransfers,
getAccountLen,
mintTo,
ExtensionType,
TOKEN_2022_PROGRAM_ID,
} from '../src';
Expand Down Expand Up @@ -61,4 +65,20 @@ import {
await disableRequiredMemoTransfers(connection, payer, destination, owner, [], undefined, TOKEN_2022_PROGRAM_ID);

await enableRequiredMemoTransfers(connection, payer, destination, owner, [], undefined, TOKEN_2022_PROGRAM_ID);

const sourceTokenAccount = await createAssociatedTokenAccount(
connection,
payer,
mint,
payer.publicKey,
undefined,
TOKEN_2022_PROGRAM_ID
);
await mintTo(connection, payer, mint, sourceTokenAccount, mintAuthority, 100, [], undefined, TOKEN_2022_PROGRAM_ID);

const transferTransaction = new Transaction().add(
createMemoInstruction('Hello, memo-transfer!', [payer.publicKey]),
createTransferInstruction(sourceTokenAccount, destination, payer.publicKey, 100, [], TOKEN_2022_PROGRAM_ID)
);
await sendAndConfirmTransaction(connection, transferTransaction, [payer], undefined);
})();