generateAbi.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const fs = require("fs");
  2. const solc = require("solc");
  3. // Assuming each contract is in the file with the same name.
  4. var contracts = [
  5. "IPyth",
  6. "IPythEvents",
  7. "AbstractPyth",
  8. "MockPyth",
  9. "PythErrors",
  10. ];
  11. var sources = {};
  12. var outputSelection = {};
  13. for (let contract of contracts) {
  14. const contractFile = `${contract}.sol`;
  15. sources[contractFile] = {
  16. content: fs.readFileSync(contractFile).toString(),
  17. };
  18. outputSelection[contractFile] = {};
  19. outputSelection[contractFile][contract] = ["abi"];
  20. }
  21. var input = {
  22. language: "Solidity",
  23. sources,
  24. settings: {
  25. outputSelection,
  26. },
  27. };
  28. function findImports(path) {
  29. return {
  30. contents: fs.readFileSync(path).toString(),
  31. };
  32. }
  33. const output = JSON.parse(
  34. solc.compile(JSON.stringify(input), { import: findImports })
  35. );
  36. if (!fs.existsSync("abis")) {
  37. fs.mkdirSync("abis");
  38. }
  39. for (let contract of contracts) {
  40. const contractFile = `${contract}.sol`;
  41. const abi = output.contracts[contractFile][contract].abi;
  42. fs.writeFileSync(
  43. `abis/${contract}.json`,
  44. JSON.stringify(abi, null, 2) + "\n"
  45. );
  46. }