Memo 傳輸擴展
MemoTranfer 擴展是一個 Token 賬戶擴展,強制要求所有進入該代幣賬戶的轉賬都包含備忘錄,從而促進更高效的交易追蹤和用戶識別。
初始化代幣賬戶
要在 Token 賬戶上初始化 MemoTransfer 擴展,我們需要使用 enableRequiredMemoTransfers() 函數。
以下是如何創建帶有 Memo 傳輸擴展的代幣:
ts
import { Keypair, SystemProgram, Transaction, sendAndConfirmTransaction } from "@solana/web3.js";
import {
createInitializeAccountInstruction,
createEnableRequiredMemoTransfersInstruction,
getAccountLen,
ExtensionType,
TOKEN_2022_PROGRAM_ID,
} from "@solana/spl-token";
const tokenAccount = Keypair.generate();
// Calculate the size needed for a Token account with Memo Transfer extension
const accountLen = getAccountLen([ExtensionType.MemoTransfer]);
// 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`,
);Expand
[43 more lines]
帶備忘錄的代幣轉賬
在 Solana 上使用 Memo 程式,我們有兩種可行的路徑。
構建我們自己的「原始」備忘錄指令,如下所示:
ts
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
}),或者我們可以下載 Memo 程式 SDK 後使用它,如下所示:
text
npm i @solana/spl-memo在我們的例子中,我們將使用第二種選項,結果如下:
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`,
);Expand
[7 more lines]
禁用和啟用備忘錄
如果我們不想強制轉賬附帶備忘錄,可以使用 disableRequiredMemoTransfer 函數,操作如下:
ts
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`,
);Expand
[16 more lines]
如果我們想重新啟用它,只需使用 enableRequiredMemoTransfers() 函數,如下所示:
ts
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`,
);Expand
[16 more lines]