Typescript
Token2022 avec Web3.js

Token2022 avec Web3.js

L'Extension Mint Close Authority

L'extension MintCloseAuthority (Autorité de fermeture du mint) est une extension de compte de Mint qui permet à l'autorité de fermer et de récupérer la rente d'un compte de Mint donc l'offre actuelle est de 0.

Cette extension est utile pour nettoyer les mints inutilisés et récupérer les SOL qui ont été utilisés pour rendre le compte exempt de rente. Le mint ne peut être fermée que lorsqu'il n'y a plus de jetons en circulation.

Initialisation du Compte de Mint

Pour initialiser l'extension MintCloseAuthority sur un compte de Mint nous allons avoir besoin de la fonction initializeMintCloseAuthority().

Voici comment créer un compte de mint avec l'extension MintCloseAuthority :

ts
import {
    Keypair,
    SystemProgram,
    Transaction,
    sendAndConfirmTransaction,
} from '@solana/web3.js';
import {
    createInitializeMintInstruction,
    createInitializeMintCloseAuthorityInstruction,
    getMintLen,
    ExtensionType,
    TOKEN_2022_PROGRAM_ID,
} from '@solana/spl-token';
 
const mint = Keypair.generate();
 
// Calculate the size needed for a Mint account with Mint Close Authority extension
const mintLen = getMintLen([ExtensionType.MintCloseAuthority]);
 
// 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 Mint Close Authority extension
const initializeMintCloseAuthority = createInitializeMintCloseAuthorityInstruction(
    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,
    initializeMintCloseAuthority,
    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`);

Fermeture du Compte de Mint

Si un compte de Mint a un solde nul, la CloseMint Authority peut récupérer la rente du 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`);
Blueshift © 2025Commit: 6d01265