checking-accounts.ts 1.9 KB

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