Skip to content

Commit

Permalink
token-2022: Add memo-transfer examples (#6668)
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque committed May 7, 2024
1 parent 1b9d7f0 commit fc27b85
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
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);
})();

0 comments on commit fc27b85

Please sign in to comment.