Typescript
使用 Web3.js 的 SPL 代币

使用 Web3.js 的 SPL 代币

冻结和解冻指南

冻结会阻止账户上的所有代币操作,直到该账户被解冻。只有铸币的冻结权限持有者可以执行此操作。

完全禁用转账、授权和销毁操作,并且仅影响特定被冻结的账户

解冻会重新启用之前被冻结账户上的代币操作。只有铸币的冻结权限持有者可以解冻账户。

恢复被冻结账户的全部功能,并且只能由铸币的冻结权限持有者执行

在我们冻结和解冻任何代币之前,我们需要已经完成以下操作:

  • 初始化一个我们持有 freezeAuthorityMint 账户
  • 初始化一个我们想要冻结或解冻的 Token 账户或 Associated Token 账户

Raw Instruction

仅使用“原始”指令而不进行任何抽象,冻结代币的操作如下:

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`);

仅使用“原始”指令而不进行任何抽象,撤销代币的操作如下:

// 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

以下是通过 freezeAccount() 指令抽象后的操作:

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`)

以下是通过 thawAccount() 指令抽象后的操作:

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`)
Blueshift © 2025Commit: fd080b2