Skip to content

Commit 71aa957

Browse files
committed
compiler: Put ABI related updates behind protocol check
1 parent 2c2b4e6 commit 71aa957

File tree

4 files changed

+112
-72
lines changed

4 files changed

+112
-72
lines changed

src/command-helpers/compiler.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ const toolbox = require('gluegun/toolbox')
44
const Compiler = require('../compiler')
55

66
// Helper function to construct a subgraph compiler
7-
const createCompiler = (manifest, { ipfs, outputDir, outputFormat, skipMigrations, blockIpfsMethods }) => {
7+
const createCompiler = (
8+
manifest,
9+
{ ipfs, outputDir, outputFormat, skipMigrations, blockIpfsMethods, protocol }
10+
) => {
811
// Parse the IPFS URL
912
let url
1013
try {
@@ -31,7 +34,8 @@ The IPFS URL must be of the following format: http(s)://host[:port]/[path]`)
3134
outputDir: outputDir,
3235
outputFormat: outputFormat,
3336
skipMigrations,
34-
blockIpfsMethods
37+
blockIpfsMethods,
38+
protocol,
3539
})
3640
}
3741

src/commands/build.js

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

33
const { createCompiler } = require('../command-helpers/compiler')
44
const { fixParameters } = require('../command-helpers/gluegun')
5+
const { getDataSourcesAndTemplates } = require('../command-helpers/data-sources')
6+
const Protocol = require('../protocols')
57

68
const HELP = `
79
${chalk.bold('graph build')} [options] ${chalk.bold('[<subgraph-manifest>]')}
@@ -73,11 +75,23 @@ module.exports = {
7375
return
7476
}
7577

78+
let protocol
79+
try {
80+
const dataSourcesAndTemplates = await getDataSourcesAndTemplates(manifest)
81+
82+
protocol = Protocol.fromDataSources(dataSourcesAndTemplates)
83+
} catch (e) {
84+
print.error(e.message)
85+
process.exitCode = 1
86+
return
87+
}
88+
7689
let compiler = createCompiler(manifest, {
7790
ipfs,
7891
outputDir,
7992
outputFormat,
8093
skipMigrations,
94+
protocol,
8195
})
8296

8397
// Exit with an error code if the compiler couldn't be created

src/commands/deploy.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const { DEFAULT_IPFS_URL } = require('../command-helpers/ipfs')
1313
const { assertManifestApiVersion, assertGraphTsVersion } = require('../command-helpers/version')
1414
const { getDataSourcesAndTemplates } = require('../command-helpers/data-sources')
1515
const { validateStudioNetwork } = require('../command-helpers/studio')
16+
const Protocol = require('../protocols')
1617

1718
const HELP = `
1819
${chalk.bold('graph deploy')} [options] ${chalk.bold('<subgraph-name>')} ${chalk.bold(
@@ -188,6 +189,7 @@ module.exports = {
188189
return
189190
}
190191

192+
let protocol
191193
try {
192194
// Checks to make sure deploy doesn't run against
193195
// older subgraphs (both apiVersion and graph-ts version).
@@ -197,6 +199,10 @@ module.exports = {
197199
// using the wrong AssemblyScript compiler.
198200
await assertManifestApiVersion(manifest, '0.0.5')
199201
await assertGraphTsVersion(path.dirname(manifest), '0.22.0')
202+
203+
const dataSourcesAndTemplates = await getDataSourcesAndTemplates(manifest)
204+
205+
protocol = Protocol.fromDataSources(dataSourcesAndTemplates)
200206
} catch (e) {
201207
print.error(e.message)
202208
process.exitCode = 1
@@ -211,7 +217,8 @@ module.exports = {
211217
outputDir,
212218
outputFormat: 'wasm',
213219
skipMigrations,
214-
blockIpfsMethods: isStudio // Network does not support publishing subgraphs with IPFS methods
220+
blockIpfsMethods: isStudio, // Network does not support publishing subgraphs with IPFS methods
221+
protocol,
215222
})
216223

217224
// Exit with an error code if the compiler couldn't be created

src/compiler/index.js

Lines changed: 84 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class Compiler {
5252

5353
this.globalsFile = path.join(globalsLib, globalsFile)
5454

55+
this.protocol = this.options.protocol
56+
5557
process.on('uncaughtException', function(e) {
5658
toolbox.print.error(`UNCAUGHT EXCEPTION: ${e}`)
5759
})
@@ -132,9 +134,12 @@ class Compiler {
132134
files.push(path.resolve(subgraph.getIn(['schema', 'file'])))
133135
subgraph.get('dataSources').map(dataSource => {
134136
files.push(dataSource.getIn(['mapping', 'file']))
135-
dataSource.getIn(['mapping', 'abis']).map(abi => {
136-
files.push(abi.get('file'))
137-
})
137+
// Only watch ABI related files if the target protocol has support/need for them.
138+
if (this.protocol.hasABIs()) {
139+
dataSource.getIn(['mapping', 'abis']).map(abi => {
140+
files.push(abi.get('file'))
141+
})
142+
}
138143
})
139144

140145
// Make paths absolute
@@ -403,46 +408,53 @@ class Compiler {
403408
})
404409

405410
// Copy data source files and update their paths
406-
subgraph = subgraph.update('dataSources', dataSources => {
407-
return dataSources.map(dataSource =>
408-
dataSource
409-
// Write data source ABIs to the output directory
410-
.updateIn(['mapping', 'abis'], abis =>
411-
abis.map(abi =>
412-
abi.update('file', abiFile => {
413-
abiFile = path.resolve(this.sourceDir, abiFile)
414-
let abiData = ABI.load(abi.get('name'), abiFile)
415-
return path.relative(
416-
this.options.outputDir,
417-
this._writeSubgraphFile(
418-
abiFile,
419-
JSON.stringify(abiData.data.toJS(), null, 2),
420-
this.sourceDir,
421-
this.subgraphDir(this.options.outputDir, dataSource),
422-
spinner,
423-
),
424-
)
425-
}),
426-
),
427-
)
428-
429-
// The mapping file is already being written to the output
430-
// directory by the AssemblyScript compiler
431-
.updateIn(['mapping', 'file'], mappingFile =>
432-
path.relative(
433-
this.options.outputDir,
434-
path.resolve(this.sourceDir, mappingFile),
435-
),
411+
subgraph = subgraph.update('dataSources', dataSources =>
412+
dataSources.map(dataSource => {
413+
let updatedDataSource = dataSource
414+
415+
if (this.protocol.hasABIs()) {
416+
updatedDataSource = updatedDataSource
417+
// Write data source ABIs to the output directory
418+
.updateIn(['mapping', 'abis'], abis =>
419+
abis.map(abi =>
420+
abi.update('file', abiFile => {
421+
abiFile = path.resolve(this.sourceDir, abiFile)
422+
let abiData = ABI.load(abi.get('name'), abiFile)
423+
return path.relative(
424+
this.options.outputDir,
425+
this._writeSubgraphFile(
426+
abiFile,
427+
JSON.stringify(abiData.data.toJS(), null, 2),
428+
this.sourceDir,
429+
this.subgraphDir(this.options.outputDir, dataSource),
430+
spinner,
431+
),
432+
)
433+
}),
434+
),
435+
)
436+
}
437+
438+
// The mapping file is already being written to the output
439+
// directory by the AssemblyScript compiler
440+
return updatedDataSource.updateIn(['mapping', 'file'], mappingFile =>
441+
path.relative(
442+
this.options.outputDir,
443+
path.resolve(this.sourceDir, mappingFile),
436444
),
437-
)
438-
})
445+
)
446+
})
447+
)
439448

440449
// Copy template files and update their paths
441-
subgraph = subgraph.update('templates', templates => {
442-
return templates === undefined
450+
subgraph = subgraph.update('templates', templates =>
451+
templates === undefined
443452
? templates
444-
: templates.map(template =>
445-
template
453+
: templates.map(template => {
454+
let updatedTemplate = template
455+
456+
if (this.protocol.hasABIs()) {
457+
updatedTemplate = updatedTemplate
446458
// Write template ABIs to the output directory
447459
.updateIn(['mapping', 'abis'], abis =>
448460
abis.map(abi =>
@@ -462,17 +474,18 @@ class Compiler {
462474
}),
463475
),
464476
)
477+
}
465478

466-
// The mapping file is already being written to the output
467-
// directory by the AssemblyScript compiler
468-
.updateIn(['mapping', 'file'], mappingFile =>
469-
path.relative(
470-
this.options.outputDir,
471-
path.resolve(this.sourceDir, mappingFile),
472-
),
473-
),
479+
// The mapping file is already being written to the output
480+
// directory by the AssemblyScript compiler
481+
return updatedTemplate.updateIn(['mapping', 'file'], mappingFile =>
482+
path.relative(
483+
this.options.outputDir,
484+
path.resolve(this.sourceDir, mappingFile),
485+
),
474486
)
475-
})
487+
})
488+
)
476489

477490
// Write the subgraph manifest itself
478491
let outputFilename = path.join(this.options.outputDir, 'subgraph.yaml')
@@ -506,17 +519,18 @@ class Compiler {
506519
),
507520
})
508521

509-
// Upload the ABIs of all data sources to IPFS
510-
for (let [i, dataSource] of subgraph.get('dataSources').entries()) {
511-
for (let [j, abi] of dataSource.getIn(['mapping', 'abis']).entries()) {
512-
updates.push({
513-
keyPath: ['dataSources', i, 'mapping', 'abis', j, 'file'],
514-
value: await this._uploadFileToIPFS(
515-
abi.get('file'),
516-
uploadedFiles,
517-
spinner,
518-
),
519-
})
522+
if (this.protocol.hasABIs()) {
523+
for (let [i, dataSource] of subgraph.get('dataSources').entries()) {
524+
for (let [j, abi] of dataSource.getIn(['mapping', 'abis']).entries()) {
525+
updates.push({
526+
keyPath: ['dataSources', i, 'mapping', 'abis', j, 'file'],
527+
value: await this._uploadFileToIPFS(
528+
abi.get('file'),
529+
uploadedFiles,
530+
spinner,
531+
),
532+
})
533+
}
520534
}
521535
}
522536

@@ -532,17 +546,18 @@ class Compiler {
532546
})
533547
}
534548

535-
// Upload the mapping and ABIs of all data source templates
536549
for (let [i, template] of subgraph.get('templates', immutable.List()).entries()) {
537-
for (let [j, abi] of template.getIn(['mapping', 'abis']).entries()) {
538-
updates.push({
539-
keyPath: ['templates', i, 'mapping', 'abis', j, 'file'],
540-
value: await this._uploadFileToIPFS(
541-
abi.get('file'),
542-
uploadedFiles,
543-
spinner,
544-
),
545-
})
550+
if (this.protocol.hasABIs()) {
551+
for (let [j, abi] of template.getIn(['mapping', 'abis']).entries()) {
552+
updates.push({
553+
keyPath: ['templates', i, 'mapping', 'abis', j, 'file'],
554+
value: await this._uploadFileToIPFS(
555+
abi.get('file'),
556+
uploadedFiles,
557+
spinner,
558+
),
559+
})
560+
}
546561
}
547562

548563
updates.push({

0 commit comments

Comments
 (0)