-
Notifications
You must be signed in to change notification settings - Fork 93
/
main.ts
59 lines (50 loc) · 1.76 KB
/
main.ts
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
import { readdirSync, readFileSync, existsSync, writeFileSync, mkdirSync } from 'fs'
import { resolve, extname } from 'path'
import { abi } from 'thor-devkit';
const baseDir = resolve(__dirname, 'ABIs')
const distDir = resolve(__dirname, 'dist')
const qDir = resolve(distDir, 'q')
const cDir = resolve(distDir, 'c')
const entries = readdirSync(baseDir, { withFileTypes: true })
const fsOpt = { encoding: 'utf8' }
mkdirSync(qDir, { recursive: true })
mkdirSync(cDir, { recursive: true })
writeFileSync(
resolve(distDir, 'contracts.json'),
JSON.stringify(entries.map(v => {
if (!v.isFile() || v.name.startsWith('.')) {
return
}
const str = readFileSync(resolve(baseDir, v.name), fsOpt)
const abiJSONArray = JSON.parse(str)
if (!Array.isArray(abiJSONArray)) {
throw new Error('ABI expected array')
}
const contractName = v.name.slice(0, -extname(v.name).length)
abiJSONArray.forEach(abiJSON => save(abiJSON, contractName))
return v.name
})),
fsOpt)
function save(jsonABI: any, contractName: string) {
jsonABI = { ...jsonABI, $contractName: contractName }
let sig = ''
if (jsonABI.type === 'event') {
const ev = new abi.Event(jsonABI)
sig = ev.signature
} else {
if (!jsonABI.inputs) {
// fallback / constructor
return
}
const fn = new abi.Function(jsonABI)
sig = fn.signature
}
const path = resolve(qDir, sig + '.json')
if (existsSync(path)) {
const exist = JSON.parse(readFileSync(path, fsOpt)) as any[]
exist.push(jsonABI)
writeFileSync(path, JSON.stringify(exist), fsOpt)
} else {
writeFileSync(path, JSON.stringify([jsonABI]), fsOpt)
}
}