index.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import camelCase from "camelcase";
  2. import { PublicKey } from "@solana/web3.js";
  3. import Coder from "../../coder";
  4. import Provider from "../../provider";
  5. import { Idl, IdlInstruction } from "../../idl";
  6. import StateFactory, { StateClient } from "./state";
  7. import InstructionFactory, { InstructionNamespace } from "./instruction";
  8. import TransactionFactory, { TransactionNamespace } from "./transaction";
  9. import RpcFactory, { RpcNamespace } from "./rpc";
  10. import AccountFactory, { AccountNamespace } from "./account";
  11. import SimulateFactory, { SimulateNamespace } from "./simulate";
  12. import { parseIdlErrors } from "../common";
  13. import { AllInstructions } from "./types";
  14. // Re-exports.
  15. export { StateClient } from "./state";
  16. export { InstructionNamespace, InstructionFn } from "./instruction";
  17. export { TransactionNamespace, TransactionFn } from "./transaction";
  18. export { RpcNamespace, RpcFn } from "./rpc";
  19. export { AccountNamespace, AccountClient, ProgramAccount } from "./account";
  20. export { SimulateNamespace, SimulateFn } from "./simulate";
  21. export { IdlAccounts, IdlTypes } from "./types";
  22. export default class NamespaceFactory {
  23. /**
  24. * Generates all namespaces for a given program.
  25. */
  26. public static build<IDL extends Idl>(
  27. idl: IDL,
  28. coder: Coder,
  29. programId: PublicKey,
  30. provider: Provider
  31. ): [
  32. RpcNamespace<IDL>,
  33. InstructionNamespace<IDL>,
  34. TransactionNamespace<IDL>,
  35. AccountNamespace<IDL>,
  36. SimulateNamespace<IDL>,
  37. StateClient<IDL> | undefined
  38. ] {
  39. const rpc: RpcNamespace = {};
  40. const instruction: InstructionNamespace = {};
  41. const transaction: TransactionNamespace = {};
  42. const simulate: SimulateNamespace = {};
  43. const idlErrors = parseIdlErrors(idl);
  44. const state = StateFactory.build(idl, coder, programId, provider);
  45. idl.instructions.forEach(<I extends AllInstructions<IDL>>(idlIx: I) => {
  46. const ixItem = InstructionFactory.build<IDL, I>(
  47. idlIx,
  48. (ixName, ix) => coder.instruction.encode(ixName, ix),
  49. programId
  50. );
  51. const txItem = TransactionFactory.build(idlIx, ixItem);
  52. const rpcItem = RpcFactory.build(idlIx, txItem, idlErrors, provider);
  53. const simulateItem = SimulateFactory.build(
  54. idlIx,
  55. txItem,
  56. idlErrors,
  57. provider,
  58. coder,
  59. programId,
  60. idl
  61. );
  62. const name = camelCase(idlIx.name);
  63. instruction[name] = ixItem;
  64. transaction[name] = txItem;
  65. rpc[name] = rpcItem;
  66. simulate[name] = simulateItem;
  67. });
  68. const account: AccountNamespace<IDL> = idl.accounts
  69. ? AccountFactory.build(idl, coder, programId, provider)
  70. : ({} as AccountNamespace<IDL>);
  71. return [
  72. rpc as RpcNamespace<IDL>,
  73. instruction as InstructionNamespace<IDL>,
  74. transaction as TransactionNamespace<IDL>,
  75. account,
  76. simulate as SimulateNamespace<IDL>,
  77. state,
  78. ];
  79. }
  80. }