cpi-guard.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { CpiGuard } from "../target/types/cpi_guard";
  4. import {
  5. ExtensionType,
  6. TOKEN_2022_PROGRAM_ID,
  7. createEnableCpiGuardInstruction,
  8. createInitializeAccountInstruction,
  9. createMint,
  10. disableCpiGuard,
  11. getAccountLen,
  12. mintTo,
  13. } from "@solana/spl-token";
  14. import {
  15. sendAndConfirmTransaction,
  16. SystemProgram,
  17. Transaction,
  18. } from "@solana/web3.js";
  19. describe("cpi-guard", () => {
  20. // Configure the client to use the local cluster.
  21. const provider = anchor.AnchorProvider.env();
  22. const connection = provider.connection;
  23. const wallet = provider.wallet as anchor.Wallet;
  24. anchor.setProvider(provider);
  25. const program = anchor.workspace.CpiGuard as Program<CpiGuard>;
  26. const mintKeypair = new anchor.web3.Keypair();
  27. const tokenKeypair = new anchor.web3.Keypair();
  28. it("Create Token Account with CpiGuard extension", async () => {
  29. await createMint(
  30. connection,
  31. wallet.payer, // Payer of the transaction and initialization fees
  32. wallet.publicKey, // Mint Authority
  33. null, // Optional Freeze Authority
  34. 2, // Decimals of Mint
  35. mintKeypair, // Optional keypair
  36. undefined, // Options for confirming the transaction
  37. TOKEN_2022_PROGRAM_ID // Token Extension Program ID
  38. );
  39. // Size of Token Account with extension
  40. const accountLen = getAccountLen([ExtensionType.CpiGuard]);
  41. // Minimum lamports required for Token Account
  42. const lamports = await connection.getMinimumBalanceForRentExemption(
  43. accountLen
  44. );
  45. // Instruction to invoke System Program to create new account
  46. const createAccountInstruction = SystemProgram.createAccount({
  47. fromPubkey: wallet.publicKey, // Account that will transfer lamports to created account
  48. newAccountPubkey: tokenKeypair.publicKey, // Address of the account to create
  49. space: accountLen, // Amount of bytes to allocate to the created account
  50. lamports, // Amount of lamports transferred to created account
  51. programId: TOKEN_2022_PROGRAM_ID, // Program assigned as owner of created account
  52. });
  53. // Instruction to initialize Token Account data
  54. const initializeAccountInstruction = createInitializeAccountInstruction(
  55. tokenKeypair.publicKey, // Token Account Address
  56. mintKeypair.publicKey, // Mint Account
  57. wallet.publicKey, // Token Account Owner
  58. TOKEN_2022_PROGRAM_ID // Token Extension Program ID
  59. );
  60. // Instruction to initialize the CpiGuard Extension
  61. const enableCpiGuiardInstruction = createEnableCpiGuardInstruction(
  62. tokenKeypair.publicKey,
  63. wallet.publicKey,
  64. [],
  65. TOKEN_2022_PROGRAM_ID
  66. );
  67. const transaction = new Transaction().add(
  68. createAccountInstruction,
  69. initializeAccountInstruction,
  70. enableCpiGuiardInstruction
  71. );
  72. const transactionSignature = await sendAndConfirmTransaction(
  73. connection,
  74. transaction,
  75. [wallet.payer, tokenKeypair] // Signers
  76. );
  77. await mintTo(
  78. connection,
  79. wallet.payer,
  80. mintKeypair.publicKey,
  81. tokenKeypair.publicKey,
  82. wallet.payer,
  83. 1,
  84. [],
  85. null,
  86. TOKEN_2022_PROGRAM_ID
  87. );
  88. console.log("Your transaction signature", transactionSignature);
  89. });
  90. it("Transfer, expect fail", async () => {
  91. try {
  92. await program.methods
  93. .cpiTransfer()
  94. .accounts({
  95. sender: wallet.publicKey,
  96. senderTokenAccount: tokenKeypair.publicKey,
  97. mintAccount: mintKeypair.publicKey,
  98. })
  99. .rpc({ skipPreflight: true });
  100. } catch (error) {
  101. console.log("\nExpect Error:", error.message);
  102. }
  103. });
  104. it("Disable CpiGuard", async () => {
  105. const transactionSignature = await disableCpiGuard(
  106. connection,
  107. wallet.payer,
  108. tokenKeypair.publicKey,
  109. wallet.publicKey
  110. );
  111. console.log("Your transaction signature", transactionSignature);
  112. });
  113. it("Transfer, expect success", async () => {
  114. const transactionSignature = await program.methods
  115. .cpiTransfer()
  116. .accounts({
  117. sender: wallet.publicKey,
  118. senderTokenAccount: tokenKeypair.publicKey,
  119. mintAccount: mintKeypair.publicKey,
  120. })
  121. .rpc({ skipPreflight: true });
  122. console.log("Your transaction signature", transactionSignature);
  123. });
  124. });