-
Notifications
You must be signed in to change notification settings - Fork 9
/
ercs.mjs
executable file
·53 lines (47 loc) · 1.73 KB
/
ercs.mjs
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
#!/usr/bin/env node
/* eslint-env node */
import { basename } from 'path';
import { EventFragment, FunctionFragment } from 'ethers';
import { readFileSync } from 'fs';
import solc from 'solc';
function main() {
const input = {
language: 'Solidity',
sources: { 'ercs.sol': { content: readFileSync('./scripts/ercs.sol', 'utf-8') } },
settings: { outputSelection: { '*': { '*': ['abi'] } } },
};
const output = solc.compile(JSON.stringify(input));
/** @type {import('solc').SolcOutput} */
const { errors, contracts } = JSON.parse(output);
if (errors) {
console.error(errors);
process.exit(1);
}
/** @typedef {{[hash: string]: string}} Table */
/** @type {{[name: string]: {selectors: string[], topics: string[], functions: Table, events: Table}}} */
const src = {};
for (const [name, { abi }] of Object.entries(contracts['ercs.sol'])) {
/** @type {Table} */
const functions = {};
/** @type {Table} */
const events = {};
for (const member of abi) {
if (member.type === 'function') {
const fn = FunctionFragment.from(member);
functions[fn.selector.substring(2, 10)] = fn.format('full');
} else if (member.type === 'event') {
const ev = EventFragment.from(member);
events[ev.topicHash.substring(2)] = ev.format('full');
}
}
src[name] = {
selectors: Object.keys(functions),
topics: Object.keys(events),
functions,
events,
};
}
console.info(`// Autogenerated by \`${basename(import.meta.url)}\`. DO NOT MODIFY`);
console.info('export default', src, ';');
}
main();