compileAnchorIdls.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const fs = require("fs");
  2. const SRC_IDL = __dirname + "/../../../solana/idl";
  3. const DST_IDL = __dirname + "/../src/anchor-idl";
  4. const TS = __dirname + "/../src/solana/types";
  5. const programs = {
  6. "wormhole.json": "Wormhole",
  7. "token_bridge.json": "TokenBridge",
  8. "nft_bridge.json": "NftBridge",
  9. };
  10. function main() {
  11. if (!fs.existsSync(DST_IDL)) {
  12. fs.mkdirSync(DST_IDL);
  13. }
  14. if (!fs.existsSync(TS)) {
  15. fs.mkdirSync(TS);
  16. }
  17. for (const basename of fs.readdirSync(SRC_IDL)) {
  18. const idl = DST_IDL + "/" + basename;
  19. fs.copyFileSync(SRC_IDL + "/" + basename, idl);
  20. const targetTypescript = TS + "/" + snakeToCamel(basename).replace("json", "ts");
  21. const programType = programs[basename];
  22. fs.writeFileSync(targetTypescript, `export type ${programType} = `);
  23. fs.appendFileSync(targetTypescript, fs.readFileSync(idl));
  24. }
  25. }
  26. const snakeToCamel = str =>
  27. str.toLowerCase().replace(/([-_][a-z])/g, group =>
  28. group
  29. .toUpperCase()
  30. .replace('-', '')
  31. .replace('_', '')
  32. );
  33. main();