Typescript
SPL Token với Web3.js

SPL Token với Web3.js

Instruction Set Authority

Thay đổi authority của mint hoặc account. Điều này cho phép chuyển giao quyền sở hữu hoặc cập nhật các loại authority cụ thể.

Trước khi chúng ta có thể set authority đối với bất kỳ token hoặc token account nào, chúng ta sẽ cần phải có:

  • Account Mint đã khởi tạo mà chúng ta giữ mintAuthority hoặc freezeAuthority
  • Account Token hoặc account Associated Token đã khởi tạo mà chúng ta sở hữu

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 thay đổi authority của account Mint hoặc Token sẽ trông như thế nào:

const changeTokenAuthorityInstruction = createSetAuthorityInstruction(
    tokenAccount // account
    keypair.publickey // current authority
    AuthorityType.AccountOwner
    newAuthority.publiKey // new authority
);
 
const changeMintFreezeAuthorityInstruction = createSetAuthorityInstruction(
    mint // account
    keypair.publickey // current authority
    AuthorityType.FreezeAccount
    newAuthority.publiKey // new authority
);
 
const changeMintAuthorityInstruction = createSetAuthorityInstruction(
    mint // account
    keypair.publickey // current authority
    AuthorityType.MintTokens
    newAuthority.publiKey // new authority
);
 
const transaction = new Transaction().add(
    changeTokenAuthorityInstruction,
    changeMintFreezeAuthorityInstruction,
    changeMintAuthorityInstruction
);
 
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair]);
 
console.log(`Token and Mint authority changed! 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 setAuthority():

let tx = await setAuthority(
    connection,
    keypair,
    tokenAccount // account
    keypair.publickey // current authority
    AuthorityType.AccountOwner
    newAuthority.publiKey // new authority
);
 
console.log(`Token Account Authority Changed!. Transaction Here: https://explorer.solana.com/tx/${tx}?cluster=devnet`)
 
let tx = await setAuthority(
    connection,
    keypair,
    mint // account
    keypair.publickey // current authority
    AuthorityType.FreezeAccount
    newAuthority.publiKey // new authority
);
 
console.log(`Mint Freeze Authority Changed!. Transaction Here: https://explorer.solana.com/tx/${tx}?cluster=devnet`)
 
let tx = await setAuthority(
    connection,
    keypair,
    mint // account
    keypair.publickey // current authority
    AuthorityType.MintTokens
    newAuthority.publiKey // new authority
);
 
console.log(`Mint Authority Changed!. Transaction Here: https://explorer.solana.com/tx/${tx}?cluster=devnet`)
Nội dung
Xem mã nguồn
Blueshift © 2025Commit: f7a03c2