default-account-state.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { DefaultAccountState } from "../target/types/default_account_state";
  4. import {
  5. TOKEN_2022_PROGRAM_ID,
  6. mintTo,
  7. createAccount,
  8. } from "@solana/spl-token";
  9. describe("default-account-state", () => {
  10. const provider = anchor.AnchorProvider.env();
  11. const connection = provider.connection;
  12. const wallet = provider.wallet as anchor.Wallet;
  13. anchor.setProvider(provider);
  14. const program = anchor.workspace
  15. .DefaultAccountState as Program<DefaultAccountState>;
  16. const mintKeypair = new anchor.web3.Keypair();
  17. it("Create Mint with DefaultAccountState extension", async () => {
  18. const transactionSignature = await program.methods
  19. .initialize()
  20. .accounts({ mintAccount: mintKeypair.publicKey })
  21. .signers([mintKeypair])
  22. .rpc({ skipPreflight: true });
  23. console.log("Your transaction signature", transactionSignature);
  24. });
  25. it("Attempt Mint Token, expect fail", async () => {
  26. const amount = 1;
  27. // Create a token account, default state is frozen
  28. const tokenAccount = await createAccount(
  29. connection,
  30. wallet.payer, // Payer to create Token Account
  31. mintKeypair.publicKey, // Mint Account address
  32. wallet.payer.publicKey, // Token Account owner
  33. new anchor.web3.Keypair(), // Optional keypair
  34. undefined, // Confirmation options
  35. TOKEN_2022_PROGRAM_ID // Token Extension Program ID
  36. );
  37. try {
  38. // Attempt to mint tokens, expect error
  39. await mintTo(
  40. connection,
  41. wallet.payer, // Transaction fee payer
  42. mintKeypair.publicKey, // Mint
  43. tokenAccount, // Mint to
  44. wallet.payer, // Mint authority
  45. amount, // Amount
  46. [], // Additional signers
  47. null, // Commitment
  48. TOKEN_2022_PROGRAM_ID // Token Extension Program ID
  49. );
  50. } catch (error) {
  51. console.log("\nExpect Error:", error.logs);
  52. }
  53. });
  54. it("Update DefaultAccountState", async () => {
  55. // Update the default state to initialized (not frozen)
  56. const transactionSignature = await program.methods
  57. .updateDefaultState({ initialized: {} })
  58. .accounts({ mintAccount: mintKeypair.publicKey })
  59. .rpc({ skipPreflight: true });
  60. console.log("Your transaction signature", transactionSignature);
  61. });
  62. it("Attempt Mint Token, expect success", async () => {
  63. const amount = 1;
  64. // Create a token account, default state is initialized (not frozen)
  65. const tokenAccount = await createAccount(
  66. connection,
  67. wallet.payer, // Payer to create Token Account
  68. mintKeypair.publicKey, // Mint Account address
  69. wallet.payer.publicKey, // Token Account owner
  70. new anchor.web3.Keypair(), // Optional keypair
  71. undefined, // Confirmation options
  72. TOKEN_2022_PROGRAM_ID // Token Extension Program ID
  73. );
  74. await mintTo(
  75. connection,
  76. wallet.payer, // Transaction fee payer
  77. mintKeypair.publicKey, // Mint
  78. tokenAccount, // Mint to
  79. wallet.payer, // Mint authority
  80. amount, // Amount
  81. [], // Additional signers
  82. null, // Commitment
  83. TOKEN_2022_PROGRAM_ID // Token Extension Program ID
  84. );
  85. });
  86. });