Set Authority Instructions
Changes the authority of a mint or account. This allows transferring ownership or updating specific authority types.
Before we can mint any token, we'll need to already have:
- Initialized a
Mint
account which we hold themintAuthority
orfreezeAuthority
- Initialized a
Token
account orAssociated Token
account that we own
Raw Instruction
By using just "raw" instruction without any abstraction, this is how changing the authority of a Mint
or Token
account would look like:
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`);
Abstracted Instruction
This is how the same instructions would look like abstracted away with the setAuthority()
instruction:
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`)