index.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 { BorshTypesCoder } from "./types.js";
  7. import { Coder } from "../index.js";
  8. export { BorshInstructionCoder } from "./instruction.js";
  9. export { BorshAccountsCoder, ACCOUNT_DISCRIMINATOR_SIZE } from "./accounts.js";
  10. export { BorshEventCoder, eventDiscriminator } from "./event.js";
  11. export { BorshStateCoder, stateDiscriminator } from "./state.js";
  12. /**
  13. * BorshCoder is the default Coder for Anchor programs implementing the
  14. * borsh based serialization interface.
  15. */
  16. export class BorshCoder<A extends string = string, T extends string = string>
  17. implements Coder
  18. {
  19. /**
  20. * Instruction coder.
  21. */
  22. readonly instruction: BorshInstructionCoder;
  23. /**
  24. * Account coder.
  25. */
  26. readonly accounts: BorshAccountsCoder<A>;
  27. /**
  28. * Coder for state structs.
  29. */
  30. readonly state: BorshStateCoder;
  31. /**
  32. * Coder for events.
  33. */
  34. readonly events: BorshEventCoder;
  35. /**
  36. * Coder for user-defined types.
  37. */
  38. readonly types: BorshTypesCoder<T>;
  39. constructor(idl: Idl) {
  40. this.instruction = new BorshInstructionCoder(idl);
  41. this.accounts = new BorshAccountsCoder(idl);
  42. this.events = new BorshEventCoder(idl);
  43. if (idl.state) {
  44. this.state = new BorshStateCoder(idl);
  45. }
  46. this.types = new BorshTypesCoder(idl);
  47. }
  48. }