record.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import assert from "assert";
  2. import { splRecordProgram } from "@project-serum/spl-record";
  3. import { Keypair, PublicKey } from "@solana/web3.js";
  4. import { BN } from "@project-serum/anchor";
  5. import { SPL_RECORD_PROGRAM_ID } from "../constants";
  6. import {
  7. confirmTx,
  8. getProvider,
  9. loadKp,
  10. sendAndConfirmTx,
  11. test,
  12. } from "../utils";
  13. export async function recordTests() {
  14. const provider = await getProvider();
  15. const program = splRecordProgram({
  16. provider,
  17. programId: SPL_RECORD_PROGRAM_ID,
  18. });
  19. const kp = await loadKp();
  20. const RECORD_DATA = new Uint8Array(8).fill(1);
  21. const newAuthorityKp = new Keypair();
  22. let recordPk: PublicKey;
  23. async function initialize() {
  24. const recordKp = new Keypair();
  25. recordPk = recordKp.publicKey;
  26. const createRecordAccountIx =
  27. await program.account.recordData.createInstruction(recordKp);
  28. const initIx = await program.methods
  29. .initialize()
  30. .accounts({
  31. recordAccount: recordKp.publicKey,
  32. authority: kp.publicKey,
  33. })
  34. .instruction();
  35. await sendAndConfirmTx([createRecordAccountIx, initIx], [kp, recordKp]);
  36. }
  37. async function write() {
  38. await program.methods
  39. .write(new BN(0), RECORD_DATA)
  40. .accounts({
  41. recordAccount: recordPk,
  42. signer: kp.publicKey,
  43. })
  44. .rpc();
  45. }
  46. async function setAuthority() {
  47. await program.methods
  48. .setAuthority()
  49. .accounts({
  50. recordAccount: recordPk,
  51. signer: kp.publicKey,
  52. newAuthority: newAuthorityKp.publicKey,
  53. })
  54. .rpc();
  55. try {
  56. await write();
  57. throw new Error("Authority did not update.");
  58. } catch {}
  59. }
  60. async function fetchRecordDataAccount() {
  61. const record = await program.account.recordData.fetch(recordPk);
  62. assert(record.authority.equals(newAuthorityKp.publicKey));
  63. assert(record.data.bytes.every((b, i) => b === RECORD_DATA[i]));
  64. }
  65. async function closeAccount() {
  66. const txHash = await program.methods
  67. .closeAccount()
  68. .accounts({
  69. recordAccount: recordPk,
  70. signer: newAuthorityKp.publicKey,
  71. receiver: kp.publicKey,
  72. })
  73. .signers([newAuthorityKp])
  74. .rpc();
  75. await confirmTx(txHash);
  76. try {
  77. await program.account.recordData.fetch(recordPk);
  78. throw new Error("Record account did not close.");
  79. } catch {}
  80. }
  81. await test(initialize);
  82. await test(write);
  83. await test(setAuthority);
  84. await test(fetchRecordDataAccount);
  85. await test(closeAccount);
  86. }