checking-accounts.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { CheckingAccounts } from "../target/types/checking_accounts";
  4. import {
  5. SystemProgram,
  6. Transaction,
  7. sendAndConfirmTransaction,
  8. } from "@solana/web3.js";
  9. describe("checking-accounts", () => {
  10. // Configure the client to use the local cluster.
  11. const provider = anchor.AnchorProvider.env();
  12. anchor.setProvider(provider);
  13. // Generate a new random keypair for the data account.
  14. const dataAccount = anchor.web3.Keypair.generate();
  15. // Generate a new keypair to represent the account we will change.
  16. const accountToChange = anchor.web3.Keypair.generate();
  17. // Generate a new keypair to represent the account we will create.
  18. const accountToCreate = anchor.web3.Keypair.generate();
  19. const wallet = provider.wallet as anchor.Wallet;
  20. const connection = provider.connection;
  21. const program = anchor.workspace
  22. .CheckingAccounts as Program<CheckingAccounts>;
  23. it("Is initialized!", async () => {
  24. // Create the new dataAccount, this is an account required by Solang even though we don't use it
  25. const tx = await program.methods
  26. .new()
  27. .accounts({ dataAccount: dataAccount.publicKey })
  28. .signers([dataAccount])
  29. .rpc({ skipPreflight: true });
  30. console.log("Your transaction signature", tx);
  31. });
  32. it("Create an account owned by our program", async () => {
  33. // Create the new account owned by our program by directly calling the system program
  34. let ix = SystemProgram.createAccount({
  35. fromPubkey: wallet.publicKey,
  36. newAccountPubkey: accountToChange.publicKey,
  37. lamports: await connection.getMinimumBalanceForRentExemption(0),
  38. space: 0,
  39. programId: program.programId, // Our program
  40. });
  41. await sendAndConfirmTransaction(connection, new Transaction().add(ix), [
  42. wallet.payer,
  43. accountToChange,
  44. ]);
  45. });
  46. it("Check Accounts", async () => {
  47. // Invoke the checkAccounts instruction on our program, passing in the account we want to "check"
  48. const tx = await program.methods
  49. .checkAccounts(accountToChange.publicKey, accountToCreate.publicKey)
  50. .accounts({ dataAccount: dataAccount.publicKey })
  51. .remainingAccounts([
  52. {
  53. pubkey: accountToChange.publicKey,
  54. isWritable: true,
  55. isSigner: false,
  56. },
  57. {
  58. pubkey: accountToCreate.publicKey,
  59. isWritable: true,
  60. isSigner: true,
  61. },
  62. ])
  63. .signers([accountToCreate])
  64. .rpc({ skipPreflight: true });
  65. console.log("Your transaction signature", tx);
  66. });
  67. });