checkingAccounts.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import {
  2. Account,
  3. Pubkey,
  4. type Result,
  5. Signer,
  6. u64,
  7. } from "@solanaturbine/poseidon";
  8. export default class CheckingAccounts {
  9. // Check 1: The program ID check is automatically handled by Anchor
  10. static PROGRAM_ID = new Pubkey(
  11. "8MWRHcfRvyUJpou8nD5oG7DmZ2Bmg99qBP8q5fZ5xJpg"
  12. );
  13. // Initialize user_account Instruction
  14. initialize(
  15. // ACCOUNTS
  16. payer: Signer, // Check: Signer account verification
  17. user_account: UserAccountState,
  18. data: u64
  19. ): Result {
  20. // CONTEXT
  21. // Check 2: Account initialization state is handled by Anchor's init constraint
  22. user_account.derive(["program"]).init();
  23. user_account.user_data = data;
  24. user_account.authority = payer.key;
  25. }
  26. // Update user_account Instruction
  27. update(
  28. // ACCOUNTS
  29. authority: Signer,
  30. user_account: UserAccountState,
  31. new_data: u64
  32. ): Result {
  33. // CONTEXT
  34. // Check 3: Ensures PDA matches the expected seeds
  35. // Check 4: Validates that the stored authority matches the signer
  36. user_account.derive(["program"]).has([authority]).constraints([]);
  37. user_account.user_data = new_data;
  38. }
  39. }
  40. // STATE
  41. export interface UserAccountState extends Account {
  42. user_data: u64;
  43. authority: Pubkey;
  44. }