workspace.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import camelCase from "camelcase";
  2. import * as toml from "toml";
  3. import { PublicKey } from "@solana/web3.js";
  4. import { Program } from "./program";
  5. import { Idl } from "./idl";
  6. let _populatedWorkspace = false;
  7. /**
  8. * The `workspace` namespace provides a convenience API to automatically
  9. * search for and deserialize [[Program]] objects defined by compiled IDLs
  10. * in an Anchor workspace.
  11. *
  12. * This API is for Node only.
  13. */
  14. const workspace = new Proxy({} as any, {
  15. get(workspaceCache: { [key: string]: Program }, programName: string) {
  16. const fs = require("fs");
  17. const process = require("process");
  18. if (
  19. typeof window !== "undefined" &&
  20. !window.process?.hasOwnProperty("type")
  21. ) {
  22. // Workspaces are available in electron, but not in the browser, yet.
  23. return undefined;
  24. }
  25. if (!_populatedWorkspace) {
  26. const path = require("path");
  27. let projectRoot = process.cwd();
  28. while (!fs.existsSync(path.join(projectRoot, "Anchor.toml"))) {
  29. const parentDir = path.dirname(projectRoot);
  30. if (parentDir === projectRoot) {
  31. projectRoot = undefined;
  32. }
  33. projectRoot = parentDir;
  34. }
  35. if (projectRoot === undefined) {
  36. throw new Error("Could not find workspace root.");
  37. }
  38. const idlFolder = `${projectRoot}/target/idl`;
  39. if (!fs.existsSync(idlFolder)) {
  40. throw new Error(`${idlFolder} doesn't exist. Did you use "anchor build"?`);
  41. }
  42. const idlMap = new Map<string, Idl>();
  43. fs.readdirSync(idlFolder).forEach(file => {
  44. const filePath = `${idlFolder}/${file}`;
  45. const idlStr = fs.readFileSync(filePath);
  46. const idl = JSON.parse(idlStr);
  47. idlMap.set(idl.name, idl);
  48. const name = camelCase(idl.name, {pascalCase: true});
  49. if (idl.metadata && idl.metadata.address) {
  50. workspaceCache[name] = new Program(
  51. idl,
  52. new PublicKey(idl.metadata.address)
  53. );
  54. }
  55. });
  56. // Override the workspace programs if the user put them in the config.
  57. const anchorToml = toml.parse(
  58. fs.readFileSync(path.join(projectRoot, "Anchor.toml"), "utf-8")
  59. );
  60. const clusterId = anchorToml.provider.cluster;
  61. if (anchorToml.clusters && anchorToml.clusters[clusterId]) {
  62. attachWorkspaceOverride(
  63. workspaceCache,
  64. anchorToml.clusters[clusterId],
  65. idlMap
  66. );
  67. }
  68. _populatedWorkspace = true;
  69. }
  70. return workspaceCache[programName];
  71. },
  72. });
  73. function attachWorkspaceOverride(
  74. workspaceCache: { [key: string]: Program },
  75. overrideConfig: { [key: string]: string },
  76. idlMap: Map<string, Idl>
  77. ) {
  78. Object.keys(overrideConfig).forEach((programName) => {
  79. const wsProgramName = camelCase(programName, { pascalCase: true });
  80. const overrideAddress = new PublicKey(overrideConfig[programName]);
  81. workspaceCache[wsProgramName] = new Program(
  82. idlMap.get(programName),
  83. overrideAddress
  84. );
  85. });
  86. }
  87. export default workspace;