-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgenParserData.js
83 lines (74 loc) · 2.69 KB
/
genParserData.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
const fs = require('fs')
const path = require('path')
// const { utils: { Interface } } = require('ethers')
const { Interface, EventFragment } = require('@ethersproject/abi')
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const argv = yargs(hideBin(process.argv))
.default('abis', './abis')
.default('generated', './generated').argv
const abisDir = path.join(__dirname, argv.abis)
const generatedDir = path.join(__dirname, argv.generated)
const abiDirs = fs
.readdirSync(generatedDir)
.filter((f) => fs.statSync(path.join(generatedDir, f)).isDirectory())
const outFile = path.join(generatedDir, `parserData.ts`)
const processedEvents = []
console.log('Generating data for parser...')
console.log('Saving to', outFile)
fs.appendFileSync(
outFile,
`import { Bytes, TypedMap } from "@graphprotocol/graph-ts"\n` +
`export const parserMap = new TypedMap<Bytes, string[]>()\n`,
{ flag: 'w' }
)
let maps = ''
for (const abiName of abiDirs) {
const inFile = path.join(generatedDir, abiName, `${abiName}.ts`)
const abiFile = path.join(abisDir, `${abiName}.json`)
if (!fs.existsSync(inFile) || !fs.existsSync(abiFile)) {
throw new Error(`ABI or generated file for "${abiName} not found!`)
}
const genEventRegexp = /export class (\w+) extends ethereum\.Event/gi
const generatedEvents = Array.from(
fs.readFileSync(inFile, 'utf8').matchAll(genEventRegexp),
(m) => m[1]
)
// const abiInt = new Interface(JSON.parse(fs.readFileSync(abiFile, 'utf8')))
const abi = JSON.parse(fs.readFileSync(abiFile, 'utf8'))
const abiInt = new Interface(abi)
abiInt.fragments
.filter((f) => EventFragment.isEventFragment(f))
.forEach((event) => {
if (generatedEvents.includes(event.name)) {
const topicHash = abiInt.getEventTopic(event)
// skip repeated
if (processedEvents.findIndex((e) => e.topic === topicHash) === -1) {
let sameNameIdx = processedEvents.findIndex(
(e) => e.name === event.name
)
let rename = false
if (sameNameIdx !== -1) {
processedEvents[sameNameIdx].cast = true
rename = true
}
processedEvents.push({
topic: topicHash,
name: event.name,
file: abiName,
rename: rename,
types: event.inputs.map((i) => i.format('minimal')),
})
}
}
})
}
processedEvents.forEach((e) => {
let name = e.rename ? e.file + e.name : e.name
maps += `parserMap.set(Bytes.fromHexString("${e.topic}"), ["${name}",${e.types
.map((t) => `"${t}"`)
.join(',')}])\n`
})
fs.appendFileSync(outFile, maps)
console.log('Parser data generated successfully')
console.log()