non-transferable.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import * as anchor from '@coral-xyz/anchor';
  2. import type { Program } from '@coral-xyz/anchor';
  3. import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID, getOrCreateAssociatedTokenAccount, mintTo, transfer } from '@solana/spl-token';
  4. import type { NonTransferable } from '../target/types/non_transferable';
  5. describe('non-transferable', () => {
  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.NonTransferable as Program<NonTransferable>;
  11. const mintKeypair = new anchor.web3.Keypair();
  12. const recipient = new anchor.web3.Keypair();
  13. it('Create Mint with NonTransferable extension', async () => {
  14. const transactionSignature = await program.methods
  15. .initialize()
  16. .accounts({ mintAccount: mintKeypair.publicKey })
  17. .signers([mintKeypair])
  18. .rpc({ skipPreflight: true });
  19. console.log('Your transaction signature', transactionSignature);
  20. });
  21. it('Attempt Token Transfer', async () => {
  22. const amount = 1;
  23. const sourceTokenAccount = await getOrCreateAssociatedTokenAccount(
  24. connection,
  25. wallet.payer, // Transaction fee payer
  26. mintKeypair.publicKey, // Mint
  27. wallet.publicKey, // Owner
  28. false, // Allow owner off curve
  29. null, // Commitment
  30. null, // Confirm options
  31. TOKEN_2022_PROGRAM_ID, // Token Extension Program ID
  32. ASSOCIATED_TOKEN_PROGRAM_ID, // Associated Token Program ID
  33. );
  34. const destinationTokenAccount = await getOrCreateAssociatedTokenAccount(
  35. connection,
  36. wallet.payer, // Transaction fee payer
  37. mintKeypair.publicKey, // Mint
  38. recipient.publicKey, // Owner
  39. false, // Allow owner off curve
  40. null, // Commitment
  41. null, // Confirm options
  42. TOKEN_2022_PROGRAM_ID, // Token Extension Program ID
  43. ASSOCIATED_TOKEN_PROGRAM_ID, // Associated Token Program ID
  44. );
  45. await mintTo(
  46. connection,
  47. wallet.payer, // Transaction fee payer
  48. mintKeypair.publicKey, // Mint
  49. sourceTokenAccount.address, // Mint to
  50. wallet.payer, // Mint authority
  51. amount, // Amount
  52. [], // Additional signers
  53. null, // Commitment
  54. TOKEN_2022_PROGRAM_ID, // Token Extension Program ID
  55. );
  56. try {
  57. // Attempt to Transfer tokens, expect error
  58. await transfer(
  59. connection,
  60. wallet.payer, // Transaction fee payer
  61. sourceTokenAccount.address, // Transfer from
  62. destinationTokenAccount.address, // Transfer to
  63. wallet.publicKey, // Source Token Account owner
  64. amount, // Amount
  65. undefined, // Additional signers
  66. undefined, // Confirmation options
  67. TOKEN_2022_PROGRAM_ID, // Token Extension Program ID
  68. );
  69. } catch (error) {
  70. console.log('\nExpect Error:', error.logs);
  71. }
  72. });
  73. });