L'Extension Default Account State
L'extension DefaultAccountState
(état par défaut du compte) est une extension de compte de Mint
qui permet de geler par défaut tous les comptes de Token
nouvellement créés pour un mint précis. La Freeze Authority
du mint peut ensuite débloquer (dégeler) ces comptes de Token
afin qu'ils puissent être utilisés.
Initialisation du Compte de Mint
Pour initialiser l'extension DefaultAccountState
sur un compte de Mint
nous allons avoir besoin de la fonction initializeDefaultAccountState()
.
Voici comment créer un compte de mint avec l'extension DefaultAccountState
:
import {
Keypair,
SystemProgram,
Transaction,
sendAndConfirmTransaction,
} from '@solana/web3.js';
import {
createInitializeMintInstruction,
createInitializeDefaultAccountStateInstruction,
getMintLen,
ExtensionType,
TOKEN_2022_PROGRAM_ID,
} from '@solana/spl-token';
const mint = Keypair.generate();
// Calculate the size needed for a Mint account with Default Account State extension
const mintLen = getMintLen([ExtensionType.DefaultAccountState]);
// 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 Default Account State extension
const initializeDefaultAccountState = createInitializeDefaultAccountStateInstruction(
mint.publicKey,
AccountState.Frozen,
TOKEN_2022_PROGRAM_ID,
);
// Initialize the mint itself
const initializeMintInstruction = createInitializeMintInstruction(
mint.publicKey,
6,
keypair.publicKey,
keypair.publicKey, // freeze authority is MANDATORY
TOKEN_2022_PROGRAM_ID,
);
// Combine all instructions in the correct order
const transaction = new Transaction().add(
createAccountInstruction,
initializeDefaultAccountState,
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`);
Déblocage d'un compte de Jeton
L'extension DefaultAccountState
signifie que tous les comptes de Token
initialisés seront frozen
par défaut.
Cela signifie qu'il n'est pas possible d'effectuer des opérations de Mint
, Transfer
ou pratiquement toute autre opération avec ces comptes de Token
si nous ne les débloquons (dégelons) pas.
Nous pouvons facilement débloquer un compte de Token
à l'aide de l'instruction thawAccount
comme ceci :
const ata = await getAssociatedTokenAddress(
mint.publicKey,
keypair.publicKey,
false,
TOKEN_2022_PROGRAM_ID
);
const createAtaInstruction = createAssociatedTokenAccountIdempotentInstruction(
keypair.publicKey, // payer
ata, // associated token account address
keypair.publicKey, // owner
mint.publicKey, // mint
TOKEN_2022_PROGRAM_ID
)
const thawAtaInstruction = createThawAccountInstruction(
ata,
mint.publicKey,
keypair.publicKey,
undefined,
TOKEN_2022_PROGRAM_ID,
);
const transaction = new Transaction().add(
createAtaInstruction,
thawAtaInstruction,
);
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair], {skipPreflight: true});
console.log(`Token accounts created and thawed! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);
Modification du Default Account State
Une fois que nous n'avons plus besoin d'exercer un contrôle important sur la distribution des jetons et que nous souhaitons permettre à tout le monde de les échanger librement, nous pouvons modifier l'état par défaut du compte à l'aide de l'instruction updateDefaultAccountState
comme ceci :
const updateDefaultAccountStateInstruction = createUpdateDefaultAccountStateInstruction(
mint.publicKey,
AccountState.Initialized,
keypair.publicKey,
undefined,
TOKEN_2022_PROGRAM_ID,
);
const transaction = new Transaction().add(updateDefaultAccountStateInstruction);
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair]);
console.log(`Default account state changed! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);