workspace.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. import { isBrowser } from "./utils/common";
  7. let _populatedWorkspace = false;
  8. /**
  9. * The `workspace` namespace provides a convenience API to automatically
  10. * search for and deserialize [[Program]] objects defined by compiled IDLs
  11. * in an Anchor workspace.
  12. *
  13. * This API is for Node only.
  14. */
  15. const workspace = new Proxy({} as any, {
  16. get(workspaceCache: { [key: string]: Program }, programName: string) {
  17. if (isBrowser) {
  18. console.log("Workspaces aren't available in the browser");
  19. return undefined;
  20. }
  21. const fs = require("fs");
  22. const process = require("process");
  23. if (!_populatedWorkspace) {
  24. const path = require("path");
  25. let projectRoot = process.cwd();
  26. while (!fs.existsSync(path.join(projectRoot, "Anchor.toml"))) {
  27. const parentDir = path.dirname(projectRoot);
  28. if (parentDir === projectRoot) {
  29. projectRoot = undefined;
  30. }
  31. projectRoot = parentDir;
  32. }
  33. if (projectRoot === undefined) {
  34. throw new Error("Could not find workspace root.");
  35. }
  36. const idlFolder = `${projectRoot}/target/idl`;
  37. if (!fs.existsSync(idlFolder)) {
  38. throw new Error(
  39. `${idlFolder} doesn't exist. Did you use "anchor build"?`
  40. );
  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 | { address: string; idl?: string } },
  76. idlMap: Map<string, Idl>
  77. ) {
  78. Object.keys(overrideConfig).forEach((programName) => {
  79. const wsProgramName = camelCase(programName, { pascalCase: true });
  80. const entry = overrideConfig[programName];
  81. const overrideAddress = new PublicKey(
  82. typeof entry === "string" ? entry : entry.address
  83. );
  84. let idl = idlMap.get(programName);
  85. if (typeof entry !== "string" && entry.idl) {
  86. idl = JSON.parse(require("fs").readFileSync(entry.idl, "utf-8"));
  87. }
  88. workspaceCache[wsProgramName] = new Program(idl, overrideAddress);
  89. });
  90. }
  91. export default workspace;