Set Authority

import { Transaction } from "@solana/web3.js";
import { CONNECTION, TEST_MINT, FEE_PAYER } from "../../../helper/const";
import { TOKEN_PROGRAM_ID, Token } from "@solana/spl-token";

// you can set/update your account's auth
// there are 4 types => 'MintTokens' | 'FreezeAccount' | 'AccountOwner' | 'CloseAccount'
// MintTokens and FreezeAccount are for mint account
// AccountOwner and CloseAccount are for token account

async function main() {
  let tx = new Transaction();
  tx.add(
    Token.createSetAuthorityInstruction(
      TOKEN_PROGRAM_ID, // always token program.
      TEST_MINT, // mint acocunt or token account
      null, // new auth, if you want to turn off the auth, just pass null
      "MintTokens", // authority type, there are 4 types => 'MintTokens' | 'FreezeAccount' | 'AccountOwner' | 'CloseAccount'
      FEE_PAYER.publicKey, // original auth
      []
    )
  );
  tx.feePayer = FEE_PAYER.publicKey;
  console.log(`txhash: ${await CONNECTION.sendTransaction(tx, [FEE_PAYER])}`);
}

main().then(
  () => process.exit(),
  (err) => {
    console.error(err);
    process.exit(-1);
  }
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Last Updated: 9/23/2022, 6:56:02 PM
Contributors: Yihau Chen