/
system-types.js
90 lines (88 loc) · 4.09 KB
/
system-types.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handler = exports.builder = exports.desc = exports.command = void 0;
const fs_1 = __importDefault(require("fs"));
const glob_1 = __importDefault(require("glob"));
const utils_1 = require("../utils");
const IDregex = new RegExp(/(?<=uint256 constant ID = uint256\(keccak256\(")(.*)(?="\))/);
exports.command = "system-types";
exports.desc = "Generates system type file. Note: assumes ABIs of all systems in ./abi and typechain generated types in ./types/ethers-contracts";
const builder = (yargs) => yargs.options({
include: { type: "array" },
exclude: { type: "array" },
out: { type: "string" },
});
exports.builder = builder;
const handler = () => __awaiter(void 0, void 0, void 0, function* () {
const wd = process.cwd();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let abis = [];
let systems = [];
let ids = [];
let typePaths = [];
const systemsPath = `${wd}/src/systems/*.sol`;
const [resolve, , promise] = (0, utils_1.deferred)();
(0, glob_1.default)(systemsPath, {}, (_, matches) => {
systems = matches.map((path) => {
const fragments = path.split("/");
return fragments[fragments.length - 1].split(".sol")[0];
});
ids = matches
.map((path) => fs_1.default.readFileSync(path).toString())
.map((source, index) => {
const regexResult = IDregex.exec(source);
const id = regexResult && regexResult[0];
if (!id) {
console.log("Source:", source);
console.log("ID:", id);
console.log("Regex:", IDregex, regexResult);
throw new Error("No ID found for" +
matches[index] +
". Make sure your system source file includes a ID definition (uint256 constant ID = uint256(keccak256(<ID>));)");
}
return id;
});
abis = systems.map((system) => `../abi/${system}.json`);
typePaths = systems.map((system) => `./ethers-contracts/${system}.ts`);
resolve();
});
// Make the callback synchronous
yield promise;
console.log("Matches", systems);
console.log("Solidity", ids);
console.log("Type paths", typePaths);
console.log("ABIs", abis);
const SystemTypes = `// Autogenerated using mud system-types
${typePaths.map((path, index) => `import { ${systems[index]} } from "${path.replace(".ts", "")}";`).join("\n")}
export type SystemTypes = {
${systems.map((system, index) => ` "${ids[index]}": ${system};`).join("\n")}
};
`;
const SystemAbis = `// Autogenerated using mud system-types
${abis.map((path, index) => `import { abi as ${systems[index]} } from "${path}";`).join("\n")}
export const SystemAbis = {
${systems.map((system, index) => ` "${ids[index]}": ${system},`).join("\n")}
};
`;
console.log("Generating files:");
console.log("SystemTypes.ts", SystemTypes);
console.log("SystemAbis.ts", SystemAbis);
fs_1.default.writeFileSync("./types/SystemTypes.ts", SystemTypes);
fs_1.default.writeFileSync("./types/SystemAbis.ts", SystemAbis);
// fs.writeFileSync(out, JSON.stringify({ abi }));
// console.log(`Created diamond abi at ${out}`);
// process.exit(0);
});
exports.handler = handler;