immutable-owner.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { ImmutableOwner } from "../target/types/immutable_owner";
  4. import {
  5. AuthorityType,
  6. TOKEN_2022_PROGRAM_ID,
  7. createMint,
  8. setAuthority,
  9. } from "@solana/spl-token";
  10. describe("immutable-owner", () => {
  11. // Configure the client to use the local cluster.
  12. const provider = anchor.AnchorProvider.env();
  13. const connection = provider.connection;
  14. const wallet = provider.wallet as anchor.Wallet;
  15. anchor.setProvider(provider);
  16. const program = anchor.workspace.ImmutableOwner as Program<ImmutableOwner>;
  17. const tokenKeypair = new anchor.web3.Keypair();
  18. it("Create Token Account with ImmutableOwner extension", async () => {
  19. const mint = await createMint(
  20. connection,
  21. wallet.payer, // Payer of the transaction and initialization fees
  22. wallet.publicKey, // Mint Authority
  23. null, // Optional Freeze Authority
  24. 2, // Decimals of Mint
  25. undefined, // Optional keypair
  26. undefined, // Options for confirming the transaction
  27. TOKEN_2022_PROGRAM_ID // Token Extension Program ID
  28. );
  29. const transactionSignature = await program.methods
  30. .initialize()
  31. .accounts({
  32. mintAccount: mint,
  33. tokenAccount: tokenKeypair.publicKey,
  34. })
  35. .signers([tokenKeypair])
  36. .rpc({ skipPreflight: true });
  37. console.log("Your transaction signature", transactionSignature);
  38. });
  39. it("Attempt to change token account owner, expect fail", async () => {
  40. try {
  41. await setAuthority(
  42. connection, // Connection to use
  43. wallet.payer, // Payer of the transaction fee
  44. tokenKeypair.publicKey, // Token Account
  45. wallet.publicKey, // Owner of the Token Account
  46. AuthorityType.AccountOwner, // Type of Authority
  47. new anchor.web3.Keypair().publicKey, // Random address as new account Owner
  48. undefined, // Additional signers
  49. undefined, // Confirmation options
  50. TOKEN_2022_PROGRAM_ID // Token Extension Program ID
  51. );
  52. } catch (error) {
  53. console.log("\nExpect Error:", error.logs);
  54. }
  55. });
  56. });