non-transferable.ts 2.8 KB

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