Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion manifest-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ type Schema {
}

union DataSource = EthereumContractDataSource
union DataSourceTemplate = EthereumContractDataSourceTemplate

type EthereumContractDataSource {
kind: String!
name: String!
network: String
source: EthereumContractSource!
mapping: EthereumContractMapping!
templates: [EthereumContractDataSourceTemplate!]
}

type EthereumContractSource {
Expand Down Expand Up @@ -64,6 +64,7 @@ type SubgraphManifest {
description: String
repository: String
dataSources: [DataSource!]!
templates: [DataSourceTemplate!]
}

type EthereumContractDataSourceTemplate {
Expand Down
177 changes: 85 additions & 92 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,29 +169,22 @@ class Compiler {
async spinner => {
subgraph = subgraph.update('dataSources', dataSources =>
dataSources.map(dataSource =>
dataSource
.updateIn(['mapping', 'file'], mappingPath =>
this._compileDataSourceMapping(dataSource, mappingPath, spinner),
)
.update(
'templates',
templates =>
templates === undefined
? templates
: templates.map(template =>
template.updateIn(['mapping', 'file'], mappingPath =>
this._compileDataSourceTemplateMapping(
dataSource,
template,
mappingPath,
spinner,
),
),
),
),
dataSource.updateIn(['mapping', 'file'], mappingPath =>
this._compileDataSourceMapping(dataSource, mappingPath, spinner),
),
),
)

subgraph = subgraph.update('templates', templates =>
templates === undefined
? templates
: templates.map(template =>
template.updateIn(['mapping', 'file'], mappingPath =>
this._compileTemplateMapping(template, mappingPath, spinner),
),
),
)

return subgraph
},
)
Expand Down Expand Up @@ -230,7 +223,17 @@ class Compiler {
global = path.relative(baseDir, global)

asc.main(
[inputFile, global, '--baseDir', baseDir, '--lib', libs, '--outFile', outputFile, '--optimize'],
[
inputFile,
global,
'--baseDir',
baseDir,
'--lib',
libs,
'--outFile',
outputFile,
'--optimize',
],
{
stdout: process.stdout,
stderr: process.stdout,
Expand All @@ -247,13 +250,12 @@ class Compiler {
}
}

_compileDataSourceTemplateMapping(dataSource, template, mappingPath, spinner) {
_compileTemplateMapping(template, mappingPath, spinner) {
try {
let dataSourceName = dataSource.get('name')
let templateName = template.get('name')

let outFile = path.join(
this.subgraphDir(this.options.outputDir, dataSource),
this.options.outputDir,
'templates',
templateName,
this.options.outputFormat == 'wasm'
Expand All @@ -264,7 +266,7 @@ class Compiler {
step(
spinner,
'Compile data source template:',
`${dataSourceName} > ${templateName} => ${this.displayPath(outFile)}`,
`${templateName} => ${this.displayPath(outFile)}`,
)

let baseDir = this.sourceDir
Expand All @@ -283,7 +285,17 @@ class Compiler {
global = path.relative(baseDir, global)

asc.main(
[inputFile, global, '--baseDir', baseDir, '--lib', libs, '--outFile', outputFile, '--optimize'],
[
inputFile,
global,
'--baseDir',
baseDir,
'--lib',
libs,
'--outFile',
outputFile,
'--optimize',
],
{
stdout: process.stdout,
stderr: process.stdout,
Expand Down Expand Up @@ -350,51 +362,43 @@ class Compiler {
// directory by the AssemblyScript compiler
.updateIn(['mapping', 'file'], mappingFile =>
path.relative(this.options.outputDir, mappingFile),
)

.update(
'templates',
templates =>
templates === undefined
? templates
: templates.map(template =>
template
// Write data source template ABIs to the output directory
.updateIn(['mapping', 'abis'], abis =>
abis.map(abi =>
abi.update('file', abiFile => {
let abiData = ABI.load(abi.get('name'), abiFile)
return path.relative(
this.options.outputDir,
this._writeSubgraphFile(
abiFile,
JSON.stringify(abiData.data.toJS(), null, 2),
this.sourceDir,
path.join(
this.subgraphDir(
this.options.outputDir,
dataSource,
),
'templates',
template.get('name'),
),
spinner,
),
)
}),
),
)

// The mapping file is already being written to the output
// directory by the AssemblyScript compiler
.updateIn(['mapping', 'file'], mappingFile =>
path.relative(this.options.outputDir, mappingFile),
),
),
),
)
})

// Copy template files and update their paths
subgraph = subgraph.update('templates', templates => {
return templates === undefined
? templates
: templates.map(template =>
template
// Write template ABIs to the output directory
.updateIn(['mapping', 'abis'], abis =>
abis.map(abi =>
abi.update('file', abiFile => {
let abiData = ABI.load(abi.get('name'), abiFile)
return path.relative(
this.options.outputDir,
this._writeSubgraphFile(
abiFile,
JSON.stringify(abiData.data.toJS(), null, 2),
this.sourceDir,
this.subgraphDir(this.options.outputDir, template),
spinner,
),
)
}),
),
)

// The mapping file is already being written to the output
// directory by the AssemblyScript compiler
.updateIn(['mapping', 'file'], mappingFile =>
path.relative(this.options.outputDir, mappingFile),
),
)
})

// Write the subgraph manifest itself
let outputFilename = path.join(this.options.outputDir, 'subgraph.yaml')
step(spinner, 'Write subgraph manifest', this.displayPath(outputFilename))
Expand Down Expand Up @@ -453,38 +457,27 @@ class Compiler {
})
}

// Upload the ABIs of all data source templates to IPFS
for (let [i, dataSource] of subgraph.get('dataSources').entries()) {
for (let [j, template] of dataSource
.get('templates', immutable.List())
.entries()) {
for (let [k, abi] of template.getIn(['mapping', 'abis']).entries()) {
updates.push({
keyPath: ['dataSources', i, 'templates', j, 'mapping', 'abis', k, 'file'],
value: await this._uploadFileToIPFS(
abi.get('file'),
uploadedFiles,
spinner,
),
})
}
}
}

// Upload all template mappings
for (let [i, dataSource] of subgraph.get('dataSources').entries()) {
for (let [j, template] of dataSource
.get('templates', immutable.List())
.entries()) {
// Upload the mapping and ABIs of all data source templates
for (let [i, template] of subgraph.get('templates', immutable.List()).entries()) {
for (let [j, abi] of template.getIn(['mapping', 'abis']).entries()) {
updates.push({
keyPath: ['dataSources', i, 'templates', j, 'mapping', 'file'],
keyPath: ['templates', i, 'mapping', 'abis', j, 'file'],
value: await this._uploadFileToIPFS(
template.getIn(['mapping', 'file']),
abi.get('file'),
uploadedFiles,
spinner,
),
})
}

updates.push({
keyPath: ['templates', i, 'mapping', 'file'],
value: await this._uploadFileToIPFS(
template.getIn(['mapping', 'file']),
uploadedFiles,
spinner,
),
})
}

// Apply all updates to the subgraph
Expand Down
1 change: 1 addition & 0 deletions src/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { withSpinner, step } = require('./command-helpers/spinner')
const MIGRATIONS = [
require('./migrations/mapping_api_version_0_0_1'),
require('./migrations/mapping_api_version_0_0_2'),
require('./migrations/spec_version_0_0_2'),
]

const applyMigrations = async options =>
Expand Down
35 changes: 35 additions & 0 deletions src/migrations/spec_version_0_0_2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const fs = require('fs-extra')
const toolbox = require('gluegun/toolbox')
const yaml = require('js-yaml')

// Spec version to 0.0.2 uses top level templates. graph-cli no longer supports
// 0.0.1 which used nested templates.
module.exports = {
name: 'Bump mapping specVersion from 0.0.1 to 0.0.2',
predicate: async ({ sourceDir, manifestFile }) => {
let manifest = yaml.safeLoad(fs.readFileSync(manifestFile, 'utf-8'))
return (
manifest &&
typeof manifest === 'object' &&
manifest.specVersion &&
manifest.specVersion === '0.0.1'
)
},
apply: async ({ manifestFile }) => {
await toolbox.patching.replace(
manifestFile,
'specVersion: 0.0.1',
'specVersion: 0.0.2',
)
await toolbox.patching.replace(
manifestFile,
"specVersion: '0.0.1'",
"specVersion: '0.0.2'",
)
await toolbox.patching.replace(
manifestFile,
'specVersion: "0.0.1"',
'specVersion: "0.0.2"',
)
},
}
25 changes: 10 additions & 15 deletions src/subgraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ let { strOptions } = require('yaml/types')
let graphql = require('graphql/language')
let validation = require('./validation')
let ABI = require('./abi')
const toolbox = require('gluegun/toolbox')

const throwCombinedError = (filename, errors) => {
throw new Error(
Expand Down Expand Up @@ -98,20 +97,16 @@ module.exports = class Subgraph {
}

static collectDataSourceTemplates(manifest) {
return manifest.get('dataSources').reduce(
(templates, dataSource, dataSourceIndex) =>
dataSource.get('templates', immutable.List()).reduce(
(templates, template, templateIndex) =>
template.get('kind') === 'ethereum/contract'
? templates.push(
immutable.Map({
path: ['dataSources', dataSourceIndex, 'templates', templateIndex],
dataSource: template,
}),
)
: templates,
templates,
),
return manifest.get('templates', immutable.List()).reduce(
(templates, template, templateIndex) =>
template.get('kind') === 'ethereum/contract'
? templates.push(
immutable.Map({
path: ['templates', templateIndex],
dataSource: template,
}),
)
: templates,
immutable.List(),
)
}
Expand Down
Loading