default-account-state.ts 3.2 KB

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