index.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { Idl } from "../../idl.js";
  2. import { BorshInstructionCoder } from "./instruction.js";
  3. import { BorshAccountsCoder } from "./accounts.js";
  4. import { BorshEventCoder } from "./event.js";
  5. import { BorshStateCoder } from "./state.js";
  6. import { Coder } from "../index.js";
  7. export { BorshInstructionCoder } from "./instruction.js";
  8. export { BorshAccountsCoder, BorshAccountHeader } from "./accounts.js";
  9. export { BorshEventCoder, eventDiscriminator } from "./event.js";
  10. export { BorshStateCoder, stateDiscriminator } from "./state.js";
  11. /**
  12. * BorshCoder is the default Coder for Anchor programs implementing the
  13. * borsh based serialization interface.
  14. */
  15. export class BorshCoder<A extends string = string> implements Coder {
  16. /**
  17. * Instruction coder.
  18. */
  19. readonly instruction: BorshInstructionCoder;
  20. /**
  21. * Account coder.
  22. */
  23. readonly accounts: BorshAccountsCoder<A>;
  24. /**
  25. * Coder for state structs.
  26. */
  27. readonly state: BorshStateCoder;
  28. /**
  29. * Coder for events.
  30. */
  31. readonly events: BorshEventCoder;
  32. constructor(idl: Idl) {
  33. this.instruction = new BorshInstructionCoder(idl);
  34. this.accounts = new BorshAccountsCoder(idl);
  35. this.events = new BorshEventCoder(idl);
  36. if (idl.state) {
  37. this.state = new BorshStateCoder(idl);
  38. }
  39. }
  40. }