immutable-owner.ts 2.1 KB

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