Typescript
Token2022 mit Web3.js

Token2022 mit Web3.js

Die Permanent-Delegate-Erweiterung

Die PermanentDelegate Erweiterung ist eine Mint-Konto-Erweiterung, die einen permanenten Delegierten für alle Token der Mint ermöglicht, der in der Lage ist, jeden Token dieser Mint von jedem Token-Konto zu übertragen oder zu verbrennen.

Initialisierung des Mint-Kontos

Um die PermanentDelegate Erweiterung auf einem Mint Konto zu initialisieren, benötigen wir die initializePermanentDelegate() Funktion.

Hier ist, wie man eine Mint mit der Permanent-Delegate-Erweiterung erstellt:

ts
import {
    Keypair,
    SystemProgram,
    Transaction,
    sendAndConfirmTransaction,
} from '@solana/web3.js';
import {
    createInitializeMintInstruction,
    createInitializePermanentDelegateInstruction,
    getMintLen,
    ExtensionType,
    TOKEN_2022_PROGRAM_ID,
} from '@solana/spl-token';

const mint = Keypair.generate();

// Calculate the size needed for a Mint account with Permanent Delegate extension
const mintLen = getMintLen([ExtensionType.PermanentDelegate]);

// Calculate minimum lamports required for rent exemption
const lamports = await connection.getMinimumBalanceForRentExemption(mintLen);

// Create the account with the correct size and owner
const createAccountInstruction = SystemProgram.createAccount({
    fromPubkey: keypair.publicKey,
    newAccountPubkey: mint.publicKey,
    space: mintLen,
    lamports,
    programId: TOKEN_2022_PROGRAM_ID,
});

// Initialize the Permanent Delegate extension
const initializePermanentDelegate = createInitializePermanentDelegateInstruction(
    mint.publicKey,
    keypair.publicKey,
    TOKEN_2022_PROGRAM_ID,
);

// Initialize the mint itself
const initializeMintInstruction = createInitializeMintInstruction(
    mint.publicKey,
    6,
    keypair.publicKey,
    null,
    TOKEN_2022_PROGRAM_ID,
);

// Combine all instructions in the correct order
const transaction = new Transaction().add(
    createAccountInstruction,
    initializePermanentDelegate,
    initializeMintInstruction,
);

const signature = await sendAndConfirmTransaction(connection, transaction, [keypair, mint]);

console.log(`Mint created! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);

Schließen des Mint-Kontos

Wenn ein Mint Konto einen Bestand von null hat, kann der CloseMint Authority die Miete für dieses Konto zurückfordern, indem er die closeAccount Anweisung wie folgt verwendet:

ts
const closeMintInstruction = createCloseAccountInstruction(
    mint.publicKey,
    keypair.publicKey,
    keypair.publicKey,
    [],
    TOKEN_2022_PROGRAM_ID,
);

const transaction = new Transaction().add(closeMintInstruction);

const signature = await sendAndConfirmTransaction(connection, transaction, [keypair], {skipPreflight: false});

console.log(`Mint closed! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);

Berechtigte Aktionen

Im Gegensatz zu regulären Delegierten, die widerrufen werden können, ist diese Delegiertenautorität permanent und unveränderlich.

Das bedeutet, dass jede normale Aktion wie transfer(), burn(), approve() und freeze() jederzeit direkt von der Autorität ausgeführt werden kann, ohne dass die Signatur des eigentlichen Besitzers erforderlich ist.

Das bedeutet, dass wir einfach die normalen transferChecked(), burnChecked(), ... Anweisungen verwenden und die PermanentDelegate Autorität im authority Feld übergeben können.

Blueshift © 2025Commit: e573eab