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

Commit fc27b85

Browse files
authored
token-2022: Add memo-transfer examples (#6668)
1 parent 1b9d7f0 commit fc27b85

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

docs/src/token-2022/extensions.mdx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,13 +909,17 @@ import {
909909
Transaction,
910910
LAMPORTS_PER_SOL,
911911
} from '@solana/web3.js';
912+
import { createMemoInstruction } from '@solana/spl-memo';
912913
import {
914+
createAssociatedTokenAccount,
913915
createMint,
914916
createEnableRequiredMemoTransfersInstruction,
915917
createInitializeAccountInstruction,
918+
createTransferInstruction,
916919
disableRequiredMemoTransfers,
917920
enableRequiredMemoTransfers,
918921
getAccountLen,
922+
mintTo,
919923
ExtensionType,
920924
TOKEN_2022_PROGRAM_ID,
921925
} from '@solana/spl-token';
@@ -993,6 +997,43 @@ Signature: 5MnWtrhMK32zkbacDMwBNft48VAUpr4EoRM87hkT9AFYvPgPEU7V7ERV6gdfb3kASri4w
993997
</TabItem>
994998
</Tabs>
995999

1000+
#### Example: Transferring with a memo
1001+
1002+
When transferring into an account with required transfer memos, you must include
1003+
a memo instruction before the transfer.
1004+
1005+
<Tabs className="unique-tabs" groupId="language-selection">
1006+
<TabItem value="cli" label="CLI" default>
1007+
1008+
```console
1009+
$ spl-token transfer EbPBt3XkCb9trcV4c8fidhrvoeURbDbW87Acustzyi8N 10 4Uzz67txwYbfYpF8r5UGEMYJwhPAYQ5eFUY89KTYc2bL --with-memo "memo text"
1010+
Signature: 5a9X8JrWzwZqb3iMonfUfSZbisQ57aEmW5cFntWGYRv2UZx8ACkMineBEQRHwLMzYHeyFDEHMXu8zqAMv5tm4u1g
1011+
```
1012+
1013+
</TabItem>
1014+
<TabItem value="jsx" label="JS">
1015+
1016+
```jsx
1017+
const sourceTokenAccount = await createAssociatedTokenAccount(
1018+
connection,
1019+
payer,
1020+
mint,
1021+
payer.publicKey,
1022+
undefined,
1023+
TOKEN_2022_PROGRAM_ID
1024+
);
1025+
await mintTo(connection, payer, mint, sourceTokenAccount, mintAuthority, 100, [], undefined, TOKEN_2022_PROGRAM_ID);
1026+
1027+
const transferTransaction = new Transaction().add(
1028+
createMemoInstruction('Hello, memo-transfer!', [payer.publicKey]),
1029+
createTransferInstruction(sourceTokenAccount, destination, payer.publicKey, 100, [], TOKEN_2022_PROGRAM_ID)
1030+
);
1031+
await sendAndConfirmTransaction(connection, transferTransaction, [payer], undefined);
1032+
```
1033+
1034+
</TabItem>
1035+
</Tabs>
1036+
9961037
### Reallocate
9971038

9981039
In the previous example, astute readers of the JavaScript code may have noticed

token/js/examples/memoTransfer.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ import {
77
Transaction,
88
LAMPORTS_PER_SOL,
99
} from '@solana/web3.js';
10+
import { createMemoInstruction } from '@solana/spl-memo';
1011
import {
12+
createAssociatedTokenAccount,
1113
createMint,
1214
createEnableRequiredMemoTransfersInstruction,
1315
createInitializeAccountInstruction,
16+
createTransferInstruction,
1417
disableRequiredMemoTransfers,
1518
enableRequiredMemoTransfers,
1619
getAccountLen,
20+
mintTo,
1721
ExtensionType,
1822
TOKEN_2022_PROGRAM_ID,
1923
} from '../src';
@@ -61,4 +65,20 @@ import {
6165
await disableRequiredMemoTransfers(connection, payer, destination, owner, [], undefined, TOKEN_2022_PROGRAM_ID);
6266

6367
await enableRequiredMemoTransfers(connection, payer, destination, owner, [], undefined, TOKEN_2022_PROGRAM_ID);
68+
69+
const sourceTokenAccount = await createAssociatedTokenAccount(
70+
connection,
71+
payer,
72+
mint,
73+
payer.publicKey,
74+
undefined,
75+
TOKEN_2022_PROGRAM_ID
76+
);
77+
await mintTo(connection, payer, mint, sourceTokenAccount, mintAuthority, 100, [], undefined, TOKEN_2022_PROGRAM_ID);
78+
79+
const transferTransaction = new Transaction().add(
80+
createMemoInstruction('Hello, memo-transfer!', [payer.publicKey]),
81+
createTransferInstruction(sourceTokenAccount, destination, payer.publicKey, 100, [], TOKEN_2022_PROGRAM_ID)
82+
);
83+
await sendAndConfirmTransaction(connection, transferTransaction, [payer], undefined);
6484
})();

0 commit comments

Comments
 (0)