Typescript
Token2022 avec Web3.js

Token2022 avec Web3.js

L'extension de délégué permanent

L'extension PermanentDelegate est une extension de compte Mint qui permet à un délégué permanent pour tous les jetons de la monnaie d'être capable de transférer ou de brûler n'importe quel jeton de cette monnaie, depuis n'importe quel compte de jeton.

Initializing the Mint Account

Pour initialiser l'extension PermanentDelegate sur un compte Mint, nous aurons besoin de la fonction initializePermanentDelegate().

Voici comment créer une monnaie avec l'extension de délégué permanent :

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

Closing the Mint Account

Si un compte Mint a une offre de zéro, le CloseMint Authority peut récupérer le loyer sur ce compte en utilisant l'instruction closeAccount comme ceci :

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

Permissioned Actions

Contrairement aux délégués ordinaires qui peuvent être révoqués, cette autorité de délégué est permanente et immuable.

Cela signifie que chaque action normale comme transfer(), burn(), approve() et freeze() peut être exécutée quand nécessaire directement par l'autorité sans avoir besoin de la signature du propriétaire réel.

Cela signifie que nous pouvons simplement utiliser les instructions normales transferChecked(), burnChecked(), ... et transmettre l'autorité PermanentDelegate dans le champ authority.

Blueshift © 2025Commit: e573eab