设置权限说明
更改 mint 或账户的权限。这允许转移所有权或更新特定的权限类型。
在我们设置任何代币或代币账户的权限之前,我们需要已经完成以下操作:
- 初始化一个我们持有
mintAuthority
或freezeAuthority
的Mint
账户 - 初始化一个我们拥有的
Token
账户或Associated Token
账户
原始指令
通过仅使用“原始”指令而不进行任何抽象,更改 Mint
或 Token
账户的权限将如下所示:
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`);
抽象指令
以下是使用 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`)