Skip to content

Commit f3f703a

Browse files
committed
codegen: Move main TypeGenerator logic to protocols/ethereum folder
1 parent b9fc7ad commit f3f703a

File tree

16 files changed

+317
-211
lines changed

16 files changed

+317
-211
lines changed

src/codegen/abi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const immutable = require('immutable')
33
const tsCodegen = require('./typescript')
44
const typesCodegen = require('./types')
55
const util = require('./util')
6-
const ABI = require('../abi')
6+
const ABI = require('../protocols/ethereum/abi')
77

88
module.exports = class AbiCodeGenerator {
99
constructor(abi) {

src/codegen/abi.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fs = require('fs-extra')
22
const path = require('path')
33
const immutable = require('immutable')
44

5-
const ABI = require('../abi')
5+
const ABI = require('../protocols/ethereum/abi')
66
const ts = require('./typescript')
77
const AbiCodeGenerator = require('./abi')
88

src/commands/codegen.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ const chalk = require('chalk')
22
const path = require('path')
33

44
const TypeGenerator = require('../type-generator')
5+
const Protocol = require('../protocols')
56
const { fixParameters } = require('../command-helpers/gluegun')
7+
const { getDataSourcesAndTemplates } = require('../command-helpers/data-sources')
68
const { assertManifestApiVersion, assertGraphTsVersion } = require('../command-helpers/version')
79

810
const HELP = `
@@ -60,6 +62,7 @@ module.exports = {
6062
return
6163
}
6264

65+
let protocol
6366
try {
6467
// Checks to make sure codegen doesn't run against
6568
// older subgraphs (both apiVersion and graph-ts version).
@@ -69,6 +72,10 @@ module.exports = {
6972
// the wrong AssemblyScript version.
7073
await assertManifestApiVersion(manifest, '0.0.5')
7174
await assertGraphTsVersion(path.dirname(manifest), '0.22.0')
75+
76+
const dataSourcesAndTemplates = await getDataSourcesAndTemplates(manifest)
77+
78+
protocol = Protocol.fromDataSources(dataSourcesAndTemplates)
7279
} catch (e) {
7380
print.error(e.message)
7481
process.exitCode = 1
@@ -79,6 +86,7 @@ module.exports = {
7986
subgraphManifest: manifest,
8087
outputDir: outputDir,
8188
skipMigrations,
89+
protocol,
8290
})
8391

8492
// Watch working directory for file updates or additions, trigger

src/commands/init.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const { withSpinner, step } = require('../command-helpers/spinner')
1515
const { fixParameters } = require('../command-helpers/gluegun')
1616
const { chooseNodeUrl } = require('../command-helpers/node')
1717
const { abiEvents, generateScaffold, writeScaffold } = require('../scaffold')
18-
const ABI = require('../abi')
18+
const ABI = require('../protocols/ethereum/abi')
1919

2020
const networkChoices = [
2121
'mainnet',

src/compiler/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const toolbox = require('gluegun/toolbox')
99
const { step, withSpinner } = require('../command-helpers/spinner')
1010
const Subgraph = require('../subgraph')
1111
const Watcher = require('../watcher')
12-
const ABI = require('../abi')
12+
const ABI = require('../protocols/ethereum/abi')
1313
const { applyMigrations } = require('../migrations')
1414
const asc = require('./asc')
1515

src/abi.js renamed to src/protocols/ethereum/abi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fs = require('fs-extra')
22
const immutable = require('immutable')
33
const path = require('path')
44

5-
const AbiCodeGenerator = require('./codegen/abi')
5+
const AbiCodeGenerator = require('../../codegen/abi')
66

77
const TUPLE_ARRAY_PATTERN = /^tuple\[([0-9]*)\]$/
88

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
const fs = require('fs-extra')
2+
const path = require('path')
3+
const immutable = require('immutable')
4+
const prettier = require('prettier')
5+
const TypeGenerator = require('../../type-generator')
6+
const ABI = require('./abi')
7+
const { step, withSpinner } = require('../../command-helpers/spinner')
8+
9+
module.exports = class EthereumTypeGenerator {
10+
constructor(options = {}) {
11+
this.sourceDir = options.sourceDir
12+
this.outputDir = options.outputDir
13+
}
14+
15+
async loadABIs(subgraph) {
16+
return await withSpinner(
17+
'Load contract ABIs',
18+
'Failed to load contract ABIs',
19+
`Warnings while loading contract ABIs`,
20+
async spinner => {
21+
try {
22+
return subgraph
23+
.get('dataSources')
24+
.reduce(
25+
(abis, dataSource) =>
26+
dataSource
27+
.getIn(['mapping', 'abis'])
28+
.reduce(
29+
(abis, abi) =>
30+
abis.push(
31+
this._loadABI(
32+
dataSource,
33+
abi.get('name'),
34+
abi.get('file'),
35+
spinner,
36+
),
37+
),
38+
abis,
39+
),
40+
immutable.List(),
41+
)
42+
} catch (e) {
43+
throw Error(`Failed to load contract ABIs: ${e.message}`)
44+
}
45+
},
46+
)
47+
}
48+
49+
_loadABI(dataSource, name, maybeRelativePath, spinner) {
50+
try {
51+
if (this.sourceDir) {
52+
let absolutePath = path.resolve(this.sourceDir, maybeRelativePath)
53+
step(spinner, `Load contract ABI from`, TypeGenerator.displayPath(absolutePath))
54+
return { dataSource: dataSource, abi: ABI.load(name, absolutePath) }
55+
} else {
56+
return { dataSource: dataSource, abi: ABI.load(name, maybeRelativePath) }
57+
}
58+
} catch (e) {
59+
throw Error(`Failed to load contract ABI: ${e.message}`)
60+
}
61+
}
62+
63+
async loadDataSourceTemplateABIs(subgraph) {
64+
return await withSpinner(
65+
`Load data source template ABIs`,
66+
`Failed to load data source template ABIs`,
67+
`Warnings while loading data source template ABIs`,
68+
async spinner => {
69+
let abis = []
70+
for (let template of subgraph.get('templates', immutable.List())) {
71+
for (let abi of template.getIn(['mapping', 'abis'])) {
72+
abis.push(
73+
this._loadDataSourceTemplateABI(
74+
template,
75+
abi.get('name'),
76+
abi.get('file'),
77+
spinner,
78+
),
79+
)
80+
}
81+
}
82+
return abis
83+
},
84+
)
85+
}
86+
87+
_loadDataSourceTemplateABI(template, name, maybeRelativePath, spinner) {
88+
try {
89+
if (this.sourceDir) {
90+
let absolutePath = path.resolve(this.sourceDir, maybeRelativePath)
91+
step(
92+
spinner,
93+
`Load data source template ABI from`,
94+
TypeGenerator.displayPath(absolutePath),
95+
)
96+
return { template, abi: ABI.load(name, absolutePath) }
97+
} else {
98+
return { template, abi: ABI.load(name, maybeRelativePath) }
99+
}
100+
} catch (e) {
101+
throw Error(`Failed to load data source template ABI: ${e.message}`)
102+
}
103+
}
104+
105+
generateTypesForABIs(abis) {
106+
return withSpinner(
107+
`Generate types for contract ABIs`,
108+
`Failed to generate types for contract ABIs`,
109+
`Warnings while generating types for contract ABIs`,
110+
async spinner => {
111+
return await Promise.all(
112+
abis.map(async (abi, name) => await this._generateTypesForABI(abi, spinner)),
113+
)
114+
},
115+
)
116+
}
117+
118+
async _generateTypesForABI(abi, spinner) {
119+
try {
120+
step(
121+
spinner,
122+
`Generate types for contract ABI:`,
123+
`${abi.abi.name} (${TypeGenerator.displayPath(abi.abi.file)})`,
124+
)
125+
126+
let codeGenerator = abi.abi.codeGenerator()
127+
let code = prettier.format(
128+
[
129+
TypeGenerator.getGeneratedFileNote(),
130+
...codeGenerator.generateModuleImports(),
131+
...codeGenerator.generateTypes(),
132+
].join('\n'),
133+
{
134+
parser: 'typescript',
135+
},
136+
)
137+
138+
let outputFile = path.join(
139+
this.outputDir,
140+
abi.dataSource.get('name'),
141+
`${abi.abi.name}.ts`,
142+
)
143+
step(spinner, `Write types to`, TypeGenerator.displayPath(outputFile))
144+
await fs.mkdirs(path.dirname(outputFile))
145+
await fs.writeFile(outputFile, code)
146+
} catch (e) {
147+
throw Error(`Failed to generate types for contract ABI: ${e.message}`)
148+
}
149+
}
150+
151+
async generateTypesForDataSourceTemplateABIs(abis) {
152+
return await withSpinner(
153+
`Generate types for data source template ABIs`,
154+
`Failed to generate types for data source template ABIs`,
155+
`Warnings while generating types for data source template ABIs`,
156+
async spinner => {
157+
return await Promise.all(
158+
abis.map(
159+
async (abi, name) =>
160+
await this._generateTypesForDataSourceTemplateABI(abi, spinner),
161+
),
162+
)
163+
},
164+
)
165+
}
166+
167+
async _generateTypesForDataSourceTemplateABI(abi, spinner) {
168+
try {
169+
step(
170+
spinner,
171+
`Generate types for data source template ABI:`,
172+
`${abi.template.get('name')} > ${abi.abi.name} (${TypeGenerator.displayPath(
173+
abi.abi.file,
174+
)})`,
175+
)
176+
177+
let codeGenerator = abi.abi.codeGenerator()
178+
let code = prettier.format(
179+
[
180+
TypeGenerator.getGeneratedFileNote(),
181+
...codeGenerator.generateModuleImports(),
182+
...codeGenerator.generateTypes(),
183+
].join('\n'),
184+
{
185+
parser: 'typescript',
186+
},
187+
)
188+
189+
let outputFile = path.join(
190+
this.outputDir,
191+
'templates',
192+
abi.template.get('name'),
193+
`${abi.abi.name}.ts`,
194+
)
195+
step(spinner, `Write types to`, TypeGenerator.displayPath(outputFile))
196+
await fs.mkdirs(path.dirname(outputFile))
197+
await fs.writeFile(outputFile, code)
198+
} catch (e) {
199+
throw Error(`Failed to generate types for data source template ABI: ${e.message}`)
200+
}
201+
}
202+
}

src/protocols/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const EthereumTypeGenerator = require('./ethereum/type-generator')
2+
3+
module.exports = class Protocol {
4+
static fromDataSources(dataSourcesAndTemplates) {
5+
const firstDataSourceKind = dataSourcesAndTemplates[0].kind
6+
return new Protocol(firstDataSourceKind)
7+
}
8+
9+
constructor(name) {
10+
this.name = name
11+
}
12+
13+
hasABIs() {
14+
switch (this.name) {
15+
case 'ethereum':
16+
case 'ethereum/contract':
17+
return true
18+
case 'near':
19+
return false
20+
}
21+
}
22+
23+
getTypeGenerator(options) {
24+
switch (this.name) {
25+
case 'ethereum':
26+
case 'ethereum/contract':
27+
return new EthereumTypeGenerator(options)
28+
case 'near':
29+
return null
30+
}
31+
}
32+
}

src/scaffold.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const pkginfo = require('pkginfo')(module)
66
const { getSubgraphBasename } = require('./command-helpers/subgraph')
77
const { step } = require('./command-helpers/spinner')
88
const { ascTypeForEthereum, valueTypeForAsc } = require('./codegen/types')
9-
const ABI = require('./abi')
9+
const ABI = require('./protocols/ethereum/abi')
1010
const AbiCodeGenerator = require('./codegen/abi')
1111
const util = require('./codegen/util')
1212

src/scaffold.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const ABI = require('./abi')
1+
const ABI = require('./protocols/ethereum/abi')
22
const immutable = require('immutable')
33
const {
44
generateEventFieldAssignments,

0 commit comments

Comments
 (0)