Freeze and Thaw Instructions
Freeze prevents all token operations on an account until it's thawed. Only the mint's freeze authority can perform this operation.
Thaw re-enables token operations on a previously frozen account. Only the mint's freeze authority can thaw accounts.
Before we can freeze and thaw any token, we'll need to already have:
- Initialized a
Mint
account which we hold thefreezeAuthority
- Initialized a
Token
account orAssociated Token
account that we want to freeze or thaw
Raw Instruction
By using just "raw" instruction without any abstraction, this is how freezing a token would look like:
ts
const tokenAccount = Keypair.generate();
const tokenAccount = await getAssociatedTokenAddress(
mint.publicKey,
tokenAccount.publicKey,
);
// Create ATA creation instruction
const createAtaInstruction = createAssociatedTokenAccountIdempotentInstruction(
keypair.publicKey, // payer
tokenAccount, // associated token account address
destination.publicKey, // owner
mint.publicKey, // mint
);
// Freeze an ATA
const freezeInstruction = createFreezeAccountInstruction(
tokenAccount // account
mint // mint
keypair.publickey // authority
);
const transaction = new Transaction().add(
createAtaInstruction,
freezeInstruction,
);
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair]);
console.log(`Token accounts created and frozen! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);
By using just "raw" instruction without any abstraction, this is how revoke a token would look like:
ts
// Thaw an ATA
const thawInstruction = createThawAccountInstruction(
tokenAccount // account
mint // mint
keypair.publickey // authority
);
const transaction = new Transaction().add(thawInstruction);
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair]);
console.log(`Token account thawed! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);
Abstracted Instruction
This is how the same instructions would look like abstracted away with the freezeAccount()
instruction:
ts
const tokenAccount = Keypair.generate();
const ata = await getOrCreateAssociatedTokenAccount(
connection,
keypair,
mint,
tokenAccount.publicKey
);
console.log(`This is your ATA: ${ata.address}!`)
let tx = await freezeAccount(
connection,
keypair,
tokenAccount // account
mint // mint
keypair.publickey // authority
);
console.log(`Token Account succesfully Frozen!. Transaction Here: https://explorer.solana.com/tx/${tx}?cluster=devnet`)
This is how the same instructions would look like abstracted away with the thawAccount()
instruction:
ts
let tx = await thawAccount(
connection,
keypair,
tokenAccount // account
mint // mint
keypair.publickey // authority
);
console.log(`Token Account succesfully Thawed!. Transaction Here: https://explorer.solana.com/tx/${tx}?cluster=devnet`)