-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.mjs
57 lines (50 loc) · 1.5 KB
/
main.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
54
55
56
57
import { readdirSync, readFileSync, writeFileSync } from 'fs'
import { hrtime } from 'process'
import { Contract } from 'sevm';
const argv = process.argv;
if (argv.length < 5) {
console.log('Usage: node main.js MODE INPUT_DIR OUTPUT_FILE [SELECTORS_FILE]')
process.exit(1)
}
const mode = argv[2];
const indir = argv[3];
const outfile = argv[4];
const selectors = mode === 'mutability' ? JSON.parse(readFileSync(argv[5])) : {};
function timeit(fn) {
const start_ts = hrtime.bigint();
const r = fn();
const duration_ms = Number((hrtime.bigint() - start_ts) / 1000000n);
return [duration_ms, r]
}
function extract(code, mode, fname) {
const [duration_ms, contract] = timeit(() => {
try {
return new Contract(code);
} catch (e) {
// console.log(e);
}
});
if (mode === 'selectors') {
return [duration_ms, Object.keys(contract ? contract.functions : {})]
} else if (mode === 'mutability') {
return [duration_ms, Object.fromEntries(selectors[fname][1].map((s) => {
const fn = contract ? contract.functions[s] : undefined;
if (fn === undefined) {
return [s, 'selnotfound'];
} else {
return [s, fn.constant ? 'view' : (fn.payable ? 'payable' : 'nonpayable')];
}
}))];
} else {
throw 'unsupported mode';
}
}
const res = Object.fromEntries(
readdirSync(indir).map(
file => [
file,
extract(JSON.parse(readFileSync(`${indir}/${file}`))['code'], mode, file)
]
)
);
writeFileSync(outfile, JSON.stringify(res), 'utf8');