Skip to content

Commit 4383a7d

Browse files
authored
Merge pull request #760 from graphprotocol/otavio/near
NEAR support
2 parents 9b39958 + 312e780 commit 4383a7d

File tree

33 files changed

+1014
-644
lines changed

33 files changed

+1014
-644
lines changed

manifest-schema.graphql

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ type Schema {
66
file: File!
77
}
88

9-
union DataSource = EthereumContractDataSource
9+
union DataSource =
10+
EthereumContractDataSource |
11+
NearContractDataSource
12+
1013
union DataSourceTemplate = EthereumContractDataSourceTemplate
1114

1215
type EthereumContractDataSource {
@@ -17,14 +20,27 @@ type EthereumContractDataSource {
1720
mapping: EthereumContractMapping!
1821
}
1922

23+
type NearContractDataSource {
24+
kind: String!
25+
name: String!
26+
network: String
27+
source: NearContractSource!
28+
mapping: NearContractMapping!
29+
}
30+
2031
type EthereumContractSource {
2132
address: String
2233
abi: String!
2334
startBlock: BigInt
2435
}
2536

37+
type NearContractSource {
38+
account: String
39+
startBlock: BigInt
40+
}
41+
2642
type EthereumContractMapping {
27-
kind: String!
43+
kind: String
2844
apiVersion: String!
2945
language: String!
3046
file: File!
@@ -35,6 +51,16 @@ type EthereumContractMapping {
3551
eventHandlers: [EthereumContractEventHandler!]
3652
}
3753

54+
type NearContractMapping {
55+
kind: String # This will be removed, it isn't used / it doesn't mean anything
56+
apiVersion: String!
57+
language: String!
58+
file: File!
59+
entities: [String!]!
60+
blockHandlers: [NearBlockHandler!]
61+
receiptHandlers: [NearReceiptHandler!]
62+
}
63+
3864
type EthereumContractAbi {
3965
name: String!
4066
file: File!
@@ -49,11 +75,19 @@ type EthereumBlockFilter {
4975
kind: String!
5076
}
5177

78+
type NearBlockHandler {
79+
handler: String!
80+
}
81+
5282
type EthereumCallHandler {
5383
function: String!
5484
handler: String!
5585
}
5686

87+
type NearReceiptHandler {
88+
handler: String!
89+
}
90+
5791
type EthereumContractEventHandler {
5892
event: String!
5993
topic0: String

src/codegen/schema.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ module.exports = class SchemaCodeGenerator {
2323
// APIs
2424
'store',
2525

26-
// Basic Ethereum types
27-
'Address',
26+
// Basic Scalar types
2827
'Bytes',
2928
'BigInt',
3029
'BigDecimal',

src/codegen/template.js

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ const immutable = require('immutable')
33
const tsCodegen = require('./typescript')
44

55
module.exports = class DataSourceTemplateCodeGenerator {
6-
constructor(template) {
6+
constructor(template, protocol) {
77
this.template = template
8+
this.protocolTemplateCodeGen = protocol.getTemplateCodeGen(template)
89
}
910

1011
generateModuleImports() {
1112
return [
1213
tsCodegen.moduleImports(
13-
['Address', 'DataSourceTemplate', 'DataSourceContext'],
14+
[
15+
...this.protocolTemplateCodeGen.generateModuleImports(),
16+
'DataSourceTemplate',
17+
'DataSourceContext',
18+
],
1419
'@graphprotocol/graph-ts',
1520
),
1621
]
@@ -24,55 +29,8 @@ module.exports = class DataSourceTemplateCodeGenerator {
2429
let name = this.template.get('name')
2530

2631
let klass = tsCodegen.klass(name, { export: true, extends: 'DataSourceTemplate' })
27-
klass.addMethod(this._generateCreateMethod())
28-
klass.addMethod(this._generateCreateWithContextMethod())
32+
klass.addMethod(this.protocolTemplateCodeGen.generateCreateMethod())
33+
klass.addMethod(this.protocolTemplateCodeGen.generateCreateWithContextMethod())
2934
return klass
3035
}
31-
32-
_generateCreateMethod() {
33-
let name = this.template.get('name')
34-
let kind = this.template.get('kind')
35-
36-
switch (kind) {
37-
case 'ethereum/contract':
38-
return tsCodegen.staticMethod(
39-
'create',
40-
[tsCodegen.param('address', tsCodegen.namedType('Address'))],
41-
tsCodegen.namedType('void'),
42-
`
43-
DataSourceTemplate.create('${name}', [address.toHex()])
44-
`,
45-
)
46-
47-
default:
48-
throw new Error(
49-
`Data sources with kind != 'ethereum/contract' are not supported yet`,
50-
)
51-
}
52-
}
53-
54-
_generateCreateWithContextMethod() {
55-
let name = this.template.get('name')
56-
let kind = this.template.get('kind')
57-
58-
switch (kind) {
59-
case 'ethereum/contract':
60-
return tsCodegen.staticMethod(
61-
'createWithContext',
62-
[
63-
tsCodegen.param('address', tsCodegen.namedType('Address')),
64-
tsCodegen.param('context', tsCodegen.namedType('DataSourceContext')),
65-
],
66-
tsCodegen.namedType('void'),
67-
`
68-
DataSourceTemplate.createWithContext('${name}', [address.toHex()], context)
69-
`,
70-
)
71-
72-
default:
73-
throw new Error(
74-
`Data sources with kind != 'ethereum/contract' are not supported yet`,
75-
)
76-
}
77-
}
7836
}

src/codegen/typescript.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ const klassMember = (name, type) => new ClassMember(name, type)
179179
const nullableType = type => new NullableType(type)
180180
const moduleImports = (nameOrNames, module) => new ModuleImports(nameOrNames, module)
181181

182+
const GENERATED_FILE_NOTE = `
183+
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
184+
`
185+
182186
module.exports = {
183187
// Types
184188
NullableType,
@@ -194,4 +198,7 @@ module.exports = {
194198
param,
195199
nullableType,
196200
moduleImports,
201+
202+
// Utilities
203+
GENERATED_FILE_NOTE,
197204
}

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/command-helpers/data-sources.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const immutable = require('immutable')
2+
const { loadManifest } = require('../migrations/util/load-manifest')
3+
4+
// Loads manifest from file path and returns all:
5+
// - data sources
6+
// - templates
7+
// In a single list.
8+
const fromFilePath = async manifestPath => {
9+
const { dataSources = [], templates = [] } = await loadManifest(manifestPath)
10+
11+
return dataSources.concat(templates)
12+
}
13+
14+
const extractDataSourceByType = (manifest, dataSourceType, protocol) =>
15+
manifest
16+
.get(dataSourceType, immutable.List())
17+
.reduce(
18+
(dataSources, dataSource, dataSourceIndex) =>
19+
protocol.isValidKindName(dataSource.get('kind'))
20+
? dataSources.push(
21+
immutable.Map({ path: [dataSourceType, dataSourceIndex], dataSource }),
22+
)
23+
: dataSources,
24+
immutable.List(),
25+
)
26+
27+
// Extracts data sources and templates from a immutable manifest data structure
28+
const fromManifest = (manifest, protocol) => {
29+
const dataSources = extractDataSourceByType(manifest, 'dataSources', protocol)
30+
const templates = extractDataSourceByType(manifest, 'templates', protocol)
31+
32+
return dataSources.concat(templates)
33+
}
34+
35+
module.exports = {
36+
fromFilePath,
37+
fromManifest,
38+
}

src/command-helpers/fs.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const path = require('path')
2+
3+
const displayPath = p =>
4+
path.relative(process.cwd(), p)
5+
6+
module.exports = {
7+
displayPath,
8+
}

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 DataSourcesExtractor = 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 DataSourcesExtractor.fromFilePath(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/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 DataSourcesExtractor = 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 DataSourcesExtractor.fromFilePath(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/deploy.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ const { withSpinner } = require('../command-helpers/spinner')
1111
const { validateSubgraphName } = require('../command-helpers/subgraph')
1212
const { DEFAULT_IPFS_URL } = require('../command-helpers/ipfs')
1313
const { assertManifestApiVersion, assertGraphTsVersion } = require('../command-helpers/version')
14+
const DataSourcesExtractor = require('../command-helpers/data-sources')
1415
const { validateStudioNetwork } = require('../command-helpers/studio')
15-
const { loadManifest } = require('../migrations/util/load-manifest')
16+
const Protocol = require('../protocols')
1617

1718
const HELP = `
1819
${chalk.bold('graph deploy')} [options] ${chalk.bold('<subgraph-name>')} ${chalk.bold(
@@ -128,13 +129,9 @@ module.exports = {
128129
: filesystem.resolve('subgraph.yaml')
129130

130131
try {
131-
let manifestFile = await loadManifest(manifest)
132+
const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(manifest)
132133

133-
for (const { network } of manifestFile.dataSources || []) {
134-
validateStudioNetwork({ studio, product, network })
135-
}
136-
137-
for (const { network } of manifestFile.templates || []) {
134+
for (const { network } of dataSourcesAndTemplates) {
138135
validateStudioNetwork({ studio, product, network })
139136
}
140137
} catch (e) {
@@ -192,6 +189,7 @@ module.exports = {
192189
return
193190
}
194191

192+
let protocol
195193
try {
196194
// Checks to make sure deploy doesn't run against
197195
// older subgraphs (both apiVersion and graph-ts version).
@@ -201,6 +199,10 @@ module.exports = {
201199
// using the wrong AssemblyScript compiler.
202200
await assertManifestApiVersion(manifest, '0.0.5')
203201
await assertGraphTsVersion(path.dirname(manifest), '0.22.0')
202+
203+
const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(manifest)
204+
205+
protocol = Protocol.fromDataSources(dataSourcesAndTemplates)
204206
} catch (e) {
205207
print.error(e.message)
206208
process.exitCode = 1
@@ -215,7 +217,8 @@ module.exports = {
215217
outputDir,
216218
outputFormat: 'wasm',
217219
skipMigrations,
218-
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,
219222
})
220223

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

0 commit comments

Comments
 (0)