-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabi.js
executable file
·68 lines (58 loc) · 2.84 KB
/
abi.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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const c = require('chalk');
const { FunctionFragment } = require('ethers');
const sqlite3 = require('sqlite3');
const { open } = require('sqlite');
const formath = hash => hash.slice(0, 4) + '..' + hash.slice(60);
const formatv = ver => ver.replace('commit.', '');
async function abi(db, hash, base) {
const { ContractName: name, CompilerVersion: version } = JSON.parse(fs.readFileSync(path.join(base, 'metadata.json'), 'utf8'));
process.stdout.write(`${c.magenta(formath(hash))} ${c.cyan(name)} ${formatv(version)} `);
const output = fs.readFileSync(path.join(base, 'output.jsonc'), 'utf8');
const m = output.match(/^\/\/(sol|json)\n/);
const { contracts } = JSON.parse(output.slice(m[0].length));
const sym = m[1];
await db.run('INSERT INTO contract_hashes(hash, name, version, source) VALUES (:hash, :name, :version, :source)', {
':hash': hash,
':name': name,
':version': version,
':source': sym,
});
let values = '';
for (const file in contracts) {
for (const contract in contracts[file]) {
const { abi } = contracts[file][contract];
const fns = abi.filter(m => m.type === 'function').map(m => FunctionFragment.from(m).format('sighash'));
for (const fn of fns) {
values += `${values === '' ? '' : ', ' }(:hash, '${file}', '${contract}', '${fn}')`;
}
}
}
await db.run('INSERT INTO contract_functions(hash, file, contract, sighash) VALUES ' + values, {
':hash': hash,
});
}
async function main() {
const config = require('./.config.js');
const db = await open({
filename: 'abi.sqlite',
driver: sqlite3.Database
});
await db.exec('CREATE TABLE IF NOT EXISTS contract_hashes (hash TEXT PRIMARY KEY ON CONFLICT REPLACE, name TEXT NOT NULL, version TEXT NOT NULL, source TEXT) STRICT');
await db.exec('CREATE TABLE IF NOT EXISTS contract_functions (hash TEXT, file TEXT, contract TEXT, sighash TEXT NOT NULL) STRICT');
await db.exec('CREATE VIEW IF NOT EXISTS sighashes AS SELECT sighash, COUNT(sighash) AS count FROM contract_functions GROUP BY sighash ORDER BY COUNT(sighash) DESC');
await db.exec('CREATE VIEW IF NOT EXISTS versions AS SELECT version, COUNT(version) AS count FROM contract_hashes GROUP BY version ORDER BY COUNT(version) DESC');
for (const prefix of fs.readdirSync(config.contracts)) {
for (const hash of fs.readdirSync(`${config.contracts}/${prefix}`)) {
try {
await abi(db, hash, `${config.contracts}/${prefix}/${hash}`);
console.info(`${c.green('\u2713')}`);
} catch (err) {
console.info(`${c.red(err.message + ' \u2A2F')}`);
}
}
}
}
main().catch(err => console.error(err));