Typescript
SPL Token với Web3.js

SPL Token với Web3.js

Instruction Freeze và Thaw

Freeze ngăn chặn tất cả các thao tác token trên một account cho đến khi nó được thaw. Chỉ freeze authority của mint mới có thể thực hiện thao tác này.

Hoàn toàn vô hiệu hóa transfer, approval, và burn và nó chỉ ảnh hưởng đến account bị đóng băng cụ thể

Thaw kích hoạt lại các thao tác token trên một account đã bị đóng băng trước đó. Chỉ freeze authority của mint mới có thể thaw account.

Khôi phục đầy đủ chức năng cho một account bị đóng băng và chỉ có thể được thực hiện bởi freeze authority của mint

Trước khi chúng ta có thể freeze và thaw bất kỳ token nào, chúng ta sẽ cần phải có:

  • Account Mint đã được khởi tạo mà chúng ta giữ quyền freezeAuthority
  • Account Token hoặc account Associated Token đã được khởi tạo mà chúng ta muốn freeze hoặc thaw

Instruction thô

Bằng cách chỉ sử dụng instruction "thô" mà không có bất kỳ abstraction nào, đây là cách freeze token sẽ trông như thế nào:

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:

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

Instruction được trừu tượng hóa

Đây là cách các instruction tương tự sẽ trông như thế nào được trừu tượng hóa với 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`)

Đây là cách các instruction tương tự sẽ trông như thế nào được trừu tượng hóa với instruction 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`)
Nội dung
Xem mã nguồn
Blueshift © 2025Commit: f7a03c2