Typescript
Token2022 mit Web3.js

Token2022 mit Web3.js

Die Immutable Owner Erweiterung

Die ImmutableOwner Erweiterung ist eine Token Konto-Erweiterung, die jegliche Änderungen an der Eigentümerschaft des Token Kontos verhindert. Dies schützt Konten vor unbefugtem Zugriff und Übertragungsversuchen.

Alle Token Extensions Program ATAs haben standardmäßig unveränderliche Eigentümer aktiviert

Initialisierung des Token-Kontos

Um die ImmutableOwner Erweiterung auf einem Token Konto zu initialisieren, benötigen wir die initializeImmutableOwner() Funktion.

Hier ist, wie man einen Token mit der Immutable Owner Erweiterung erstellt:

ts
import {
    Keypair,
    SystemProgram,
    Transaction,
    sendAndConfirmTransaction,
} from '@solana/web3.js';
import {
    createInitializeAccountInstruction,
    createInitializeImmutableOwnerInstruction,
    getAccountLen,
    ExtensionType,
    TOKEN_2022_PROGRAM_ID,
} from '@solana/spl-token';

const tokenAccount = Keypair.generate();

// Calculate the size needed for a Token account with Immutable Owner extension
const accountLen = getAccountLen([ExtensionType.ImmutableOwner]);

// 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 Immutable Owner extension
const initializeImmutableOwnerInstruction = createInitializeImmutableOwnerInstruction(
    tokenAccount.publicKey,
    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,
    initializeImmutableOwnerInstruction,
    initializeAccountInstruction,
);

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`);

Wie bereits erwähnt, wenn wir diese Erweiterung zu einem Associated Token Konto hinzufügen möchten, initialisieren wir es einfach mit dem Token2022 Programm.

Blueshift © 2025Commit: e573eab