program.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { PublicKey } from '@solana/web3.js';
  2. import { RpcFactory } from './rpc';
  3. import { Idl } from './idl';
  4. import Coder from './coder';
  5. import { Rpcs, Ixs, Accounts } from './rpc';
  6. /**
  7. * Program is the IDL deserialized representation of a Solana program.
  8. */
  9. export class Program {
  10. /**
  11. * Address of the program.
  12. */
  13. readonly programId: PublicKey;
  14. /**
  15. * IDL describing this program's interface.
  16. */
  17. readonly idl: Idl;
  18. /**
  19. * Async functions to invoke instructions against a Solana priogram running
  20. * on a cluster.
  21. */
  22. readonly rpc: Rpcs;
  23. /**
  24. * Async functions to fetch deserialized program accounts from a cluster.
  25. */
  26. readonly account: Accounts;
  27. /**
  28. * Functions to build `TransactionInstruction` objects.
  29. */
  30. readonly instruction: Ixs;
  31. public constructor(idl: Idl, programId: PublicKey) {
  32. this.idl = idl;
  33. this.programId = programId;
  34. // Build the serializer.
  35. const coder = new Coder(idl);
  36. // Build the dynamic RPC functions.
  37. const [rpcs, ixs, accounts] = RpcFactory.build(idl, coder, programId);
  38. this.rpc = rpcs;
  39. this.instruction = ixs;
  40. this.account = accounts;
  41. }
  42. }