Розширення стану облікового запису за замовчуванням
Розширення DefaultAccountState
є розширенням Mint, яке дозволяє всім новоствореним обліковим записам Token
для цього конкретного mint бути замороженими за замовчуванням. Потім Freeze Authority
mint може розморозити ці облікові записи Token
, щоб вони стали придатними для використання.
Initializing the Mint Account
Щоб ініціалізувати розширення DefaultAccountState
на обліковому записі Mint
, нам знадобиться функція initializeDefaultAccountState()
.
Ось як створити mint з розширенням Default Account State:
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`);
Thawing the Token Account
Наявність розширення DefaultAccountState
означає, що всі облікові записи Token
, які ініціалізуються, будуть frozen
за замовчуванням.
Це означає, що неможливо Mint
, Transfer
або взагалі робити будь-що з цими обліковими записами Token
, якщо ми їх не розморозимо.
Ми можемо легко розморозити обліковий запис Token
за допомогою інструкції thawAccount
таким чином:
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`);
Changing the Default Account State
Коли нам більше не потрібен посилений контроль над розповсюдженням токенів і ми хочемо дозволити всім вільно торгувати нашим токеном, ми можемо змінити стан облікового запису за замовчуванням за допомогою інструкції updateDefaultAccountState
таким чином:
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`);