The Default Account State Extension
The DefaultAccountState
extension is a Mint extension that allows all newly created Token
accounts for that specific mint to be frozen by default. The Freeze Authority
of the mint can then thaw (unfreeze) these Token
accounts so they can become usable.
Initializing the Mint Account
To initialie the DefaultAccountState
extension on a Mint
account we're going to need the initializeDefaultAccountState()
function.
Here's how to create a mint with the Default Account State extension:
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 Transfer Fee 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,
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`);
Remember that the
Freeze Authority
on theMint
is mandatory for this extension or we'll not be able to thaw anyToken
account at creation.
Thawing the Token Account
Having the DefaultAccountState
extension means that all Token
account that gets initialized, are going to be frozen
by default.
This means that it's not possible to Mint
, Transfer
or pretty much do anything with those Token
account if we don't thaw (unfreeze) them.
We can easily thaw a Token
account using the thawAccount
instruction like this:
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
Once we don't need to have greater control over token distribution and we want to let everybody freely trade or token, we can change the default account state using the updateDefaultAccountState
instruction like so:
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`);