checking-accounts.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 { Keypair, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";
  5. import { BN } from "bn.js";
  6. describe("checking-accounts", () => {
  7. // Configure the client to use the local cluster.
  8. const provider = anchor.AnchorProvider.env();
  9. anchor.setProvider(provider);
  10. const program = anchor.workspace
  11. .CheckingAccounts as Program<CheckingAccounts>;
  12. // Generate new user keypairs for testing
  13. const user = Keypair.generate();
  14. let userAccount: PublicKey;
  15. let userAccountBump: number;
  16. before(async () => {
  17. const latestBlockHash = await provider.connection.getLatestBlockhash();
  18. // Airdrop 1 SOL to the user
  19. const airdropUser = await provider.connection.requestAirdrop(
  20. user.publicKey,
  21. 1 * LAMPORTS_PER_SOL
  22. );
  23. await provider.connection.confirmTransaction({
  24. blockhash: latestBlockHash.blockhash,
  25. lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
  26. signature: airdropUser,
  27. });
  28. // Derive PDA for the user account
  29. [userAccount, userAccountBump] = await PublicKey.findProgramAddressSync(
  30. [Buffer.from("program")],
  31. program.programId
  32. );
  33. });
  34. it("Initialize User Account", async () => {
  35. // Initialize user account instruction invoked from the program
  36. await program.methods
  37. .initialize(new BN(1))
  38. .accountsPartial({
  39. payer: user.publicKey, // User's publickey
  40. userAccount, // PDA of the user account
  41. })
  42. .signers([user])
  43. .rpc();
  44. });
  45. it("Updates the User Account data", async () => {
  46. // Update user account instruction invoked from the program
  47. await program.methods
  48. .update(new BN(2))
  49. .accountsPartial({
  50. authority: user.publicKey, // Authority of the user account
  51. userAccount, // PDA of the user account
  52. })
  53. .signers([user])
  54. .rpc();
  55. });
  56. });