-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
87 lines (58 loc) · 2.42 KB
/
main.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
84
85
86
87
const YAML = require('yaml')
const path = require('path')
const fs = require('fs')
const {filterPublicExternalFunctions} = require('./ast_parser')
const functions = filterPublicExternalFunctions('ast.json')
let abi = ""
var project = process.argv[2]
const capitalize = str => {
return str.charAt(0).toUpperCase() + str.slice(1);
}
const processYAML = () => {
const subgraph_path = path.join('contracts', project, 'subgraph.yaml')
console.log("reading subgraph yaml from", subgraph_path)
const sg_yaml = fs.readFileSync(subgraph_path, 'utf8')
const sg = YAML.parse(sg_yaml)
delete sg.dataSources[0].mapping.eventHandlers
sg.dataSources[0].mapping.callHandlers = []
abi = sg.dataSources[0].mapping.abis[0].name
functions.forEach( fn => {
const paramTypes = fn.parameters.map(p => p.type)
const types = paramTypes.join(',')
const ch = {
function: `${fn.function_name}(${types})`,
handler: `handle${capitalize(fn.function_name)}`
}
sg.dataSources[0].mapping.callHandlers.push(ch)
})
fs.writeFileSync(subgraph_path, YAML.stringify(sg))
}
const template = "export function handle<FN_NAME_UC> (call: <FN_NAME_UC>Call): void {paramHandler(call, \"<FN_NAME_LC>\")}"
function applyTemplate(){
const templateFile = fs.readFileSync('template.ts', 'utf8')
const appliedTemplates = functions.map(fn => {
const function_name_uppercase = `${capitalize(fn.function_name)}`
const function_name_lowercase = `${fn.function_name}`
const replaced = template
.replaceAll("<FN_NAME_LC>", function_name_lowercase)
.replaceAll("<FN_NAME_UC>", function_name_uppercase)
return replaced
})
const callObjects = functions.map(fn => {
return `${capitalize(fn.function_name)}Call`
})
const joinedCallObjects = callObjects.join(',\n')
const joinedTemplates = appliedTemplates.join('\n')
const newFile = templateFile
.replaceAll("<ABI>", abi)
.replaceAll("<HANDLERS>", joinedTemplates)
.replaceAll("<CALL_OBJECTS>", joinedCallObjects)
// console.log(newFile)
fs.writeFileSync(path.join('contracts', project, `src/${project.toLowerCase()}.ts`), newFile)
}
function copyTSTemplate(){
fs.copyFileSync('./schema.graphql', path.join('contracts', project, 'schema.graphql'))
}
processYAML()
copyTSTemplate()
applyTemplate()