Die Memo Transfer Extension
Die MemoTranfer Extension ist eine Token Konto-Erweiterung, die sicherstellt, dass alle eingehenden Überweisungen auf ein Token-Konto ein Memo enthalten, was eine verbesserte Transaktionsverfolgung und Benutzeridentifikation ermöglicht.
Initialisierung des Token-Kontos
Um die MemoTransfer Erweiterung auf einem Token Konto zu initialisieren, benötigen wir die enableRequiredMemoTransfers() Funktion.
Hier ist, wie man einen Token mit der Memo Transfer Extension erstellt:
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`);Token mit einem Memo übertragen
Um das Memo-Programm auf Solana zu verwenden, haben wir zwei mögliche Wege.
Wir können unsere eigene "rohe" Memo-Anweisung erstellen, wie hier:
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
}),Oder wir können das Memo-Programm SDK verwenden, nachdem wir das Paket heruntergeladen haben, wie hier:
npm i @solana/spl-memoIn unserem Beispiel werden wir die zweite Option verwenden und es wird so aussehen:
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`);Deaktivieren und Aktivieren des Memos
Wenn wir nicht erzwingen wollen, dass die Überweisung mit einem Memo erfolgt, können wir dies mit der disableRequiredMemoTransfer Funktion tun, und es würde so aussehen:
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`);Und wenn wir es wieder aktivieren möchten, können wir einfach die enableRequiredMemoTransfers() Funktion wie folgt verwenden:
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`);