cpi-guard.ts 4.1 KB

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