@graphprotocol/graph-cli 0.22.2 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/examples/basic-event-handlers/yarn.lock +4 -4
  2. package/examples/example-subgraph/yarn.lock +4 -4
  3. package/examples/near/.gitkeep +0 -0
  4. package/manifest-schema.graphql +35 -2
  5. package/package.json +1 -1
  6. package/src/codegen/schema.js +1 -2
  7. package/src/codegen/template.js +9 -51
  8. package/src/codegen/typescript.js +7 -0
  9. package/src/command-helpers/compiler.js +6 -2
  10. package/src/command-helpers/data-sources.js +38 -0
  11. package/src/command-helpers/fs.js +8 -0
  12. package/src/commands/build.js +14 -0
  13. package/src/commands/codegen.js +8 -0
  14. package/src/commands/deploy.js +11 -8
  15. package/src/commands/init.js +5 -8
  16. package/src/commands/test.js +8 -3
  17. package/src/compiler/index.js +93 -76
  18. package/src/{abi.js → protocols/ethereum/abi.js} +0 -0
  19. package/src/{codegen → protocols/ethereum/codegen}/abi.js +47 -40
  20. package/src/{codegen → protocols/ethereum/codegen}/abi.test.js +1 -1
  21. package/src/protocols/ethereum/codegen/template.js +42 -0
  22. package/src/protocols/ethereum/subgraph.js +205 -0
  23. package/src/protocols/ethereum/type-generator.js +203 -0
  24. package/src/protocols/index.js +97 -0
  25. package/src/protocols/near/subgraph.js +42 -0
  26. package/src/scaffold.js +3 -2
  27. package/src/scaffold.test.js +1 -1
  28. package/src/subgraph.js +30 -252
  29. package/src/type-generator.js +31 -211
  30. package/src/validation/index.js +1 -0
  31. package/src/validation/manifest.js +73 -23
  32. package/src/validation/schema.js +6 -0
  33. package/subgraph-ethereum.yaml +134 -0
  34. package/subgraph-near.yaml +134 -0
  35. package/tests/cli/init/from-contract/abis/Contract.json +69 -0
  36. package/tests/cli/init/from-contract/package.json +16 -0
  37. package/tests/cli/init/from-contract/schema.graphql +6 -0
  38. package/tests/cli/init/from-contract/src/mapping.ts +49 -0
  39. package/tests/cli/init/from-contract/subgraph.yaml +26 -0
  40. package/tests/cli/validation/near-is-valid/mapping.ts +0 -0
  41. package/tests/cli/validation/near-is-valid/schema.graphql +8 -0
  42. package/tests/cli/validation/near-is-valid/subgraph.yaml +21 -0
  43. package/tests/cli/validation.test.js +8 -0
@@ -2,10 +2,10 @@
2
2
  # yarn lockfile v1
3
3
 
4
4
 
5
- "@graphprotocol/graph-ts@0.22.0":
6
- version "0.22.0"
7
- resolved "https://registry.yarnpkg.com/@graphprotocol/graph-ts/-/graph-ts-0.22.0.tgz#5280513d1c8a162077f82ce7f9a492bb5783d6f4"
8
- integrity sha512-kJIBL73xBxj0+NJdRABukAtcrc3Mb8jt31s0tCLPe6c57rQXEf7KR9oYrFdzE1ZsJCP6j+MX+A2+sj1Pj3aJtQ==
5
+ "@graphprotocol/graph-ts@0.22.1":
6
+ version "0.22.1"
7
+ resolved "https://registry.yarnpkg.com/@graphprotocol/graph-ts/-/graph-ts-0.22.1.tgz#3189b2495b33497280f617316cce68074d48e236"
8
+ integrity sha512-T5xrHN0tHJwd7ZnSTLhk5hAL3rCIp6rJ40kBCrETnv1mfK9hVyoojJK6VtBQXTbLsYtKe4SYjjD0cdOsAR9QiA==
9
9
  dependencies:
10
10
  assemblyscript "0.19.10"
11
11
 
@@ -2,10 +2,10 @@
2
2
  # yarn lockfile v1
3
3
 
4
4
 
5
- "@graphprotocol/graph-ts@0.22.0":
6
- version "0.22.0"
7
- resolved "https://registry.yarnpkg.com/@graphprotocol/graph-ts/-/graph-ts-0.22.0.tgz#5280513d1c8a162077f82ce7f9a492bb5783d6f4"
8
- integrity sha512-kJIBL73xBxj0+NJdRABukAtcrc3Mb8jt31s0tCLPe6c57rQXEf7KR9oYrFdzE1ZsJCP6j+MX+A2+sj1Pj3aJtQ==
5
+ "@graphprotocol/graph-ts@0.22.1":
6
+ version "0.22.1"
7
+ resolved "https://registry.yarnpkg.com/@graphprotocol/graph-ts/-/graph-ts-0.22.1.tgz#3189b2495b33497280f617316cce68074d48e236"
8
+ integrity sha512-T5xrHN0tHJwd7ZnSTLhk5hAL3rCIp6rJ40kBCrETnv1mfK9hVyoojJK6VtBQXTbLsYtKe4SYjjD0cdOsAR9QiA==
9
9
  dependencies:
10
10
  assemblyscript "0.19.10"
11
11
 
File without changes
@@ -6,7 +6,10 @@ type Schema {
6
6
  file: File!
7
7
  }
8
8
 
9
- union DataSource = EthereumContractDataSource
9
+ union DataSource =
10
+ EthereumContractDataSource |
11
+ NearContractDataSource
12
+
10
13
  union DataSourceTemplate = EthereumContractDataSourceTemplate
11
14
 
12
15
  type EthereumContractDataSource {
@@ -17,14 +20,27 @@ type EthereumContractDataSource {
17
20
  mapping: EthereumContractMapping!
18
21
  }
19
22
 
23
+ type NearContractDataSource {
24
+ kind: String!
25
+ name: String!
26
+ network: String
27
+ source: NearContractSource!
28
+ mapping: NearContractMapping!
29
+ }
30
+
20
31
  type EthereumContractSource {
21
32
  address: String
22
33
  abi: String!
23
34
  startBlock: BigInt
24
35
  }
25
36
 
37
+ type NearContractSource {
38
+ account: String
39
+ startBlock: BigInt
40
+ }
41
+
26
42
  type EthereumContractMapping {
27
- kind: String!
43
+ kind: String
28
44
  apiVersion: String!
29
45
  language: String!
30
46
  file: File!
@@ -35,6 +51,15 @@ type EthereumContractMapping {
35
51
  eventHandlers: [EthereumContractEventHandler!]
36
52
  }
37
53
 
54
+ type NearContractMapping {
55
+ apiVersion: String!
56
+ language: String!
57
+ file: File!
58
+ entities: [String!]!
59
+ blockHandlers: [NearBlockHandler!]
60
+ receiptHandlers: [NearReceiptHandler!]
61
+ }
62
+
38
63
  type EthereumContractAbi {
39
64
  name: String!
40
65
  file: File!
@@ -49,11 +74,19 @@ type EthereumBlockFilter {
49
74
  kind: String!
50
75
  }
51
76
 
77
+ type NearBlockHandler {
78
+ handler: String!
79
+ }
80
+
52
81
  type EthereumCallHandler {
53
82
  function: String!
54
83
  handler: String!
55
84
  }
56
85
 
86
+ type NearReceiptHandler {
87
+ handler: String!
88
+ }
89
+
57
90
  type EthereumContractEventHandler {
58
91
  event: String!
59
92
  topic0: String
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphprotocol/graph-cli",
3
- "version": "0.22.2",
3
+ "version": "0.23.0",
4
4
  "description": "CLI for building for and deploying to The Graph",
5
5
  "dependencies": {
6
6
  "assemblyscript": "0.19.10",
@@ -23,8 +23,7 @@ module.exports = class SchemaCodeGenerator {
23
23
  // APIs
24
24
  'store',
25
25
 
26
- // Basic Ethereum types
27
- 'Address',
26
+ // Basic Scalar types
28
27
  'Bytes',
29
28
  'BigInt',
30
29
  'BigDecimal',
@@ -3,14 +3,19 @@ const immutable = require('immutable')
3
3
  const tsCodegen = require('./typescript')
4
4
 
5
5
  module.exports = class DataSourceTemplateCodeGenerator {
6
- constructor(template) {
6
+ constructor(template, protocol) {
7
7
  this.template = template
8
+ this.protocolTemplateCodeGen = protocol.getTemplateCodeGen(template)
8
9
  }
9
10
 
10
11
  generateModuleImports() {
11
12
  return [
12
13
  tsCodegen.moduleImports(
13
- ['Address', 'DataSourceTemplate', 'DataSourceContext'],
14
+ [
15
+ ...this.protocolTemplateCodeGen.generateModuleImports(),
16
+ 'DataSourceTemplate',
17
+ 'DataSourceContext',
18
+ ],
14
19
  '@graphprotocol/graph-ts',
15
20
  ),
16
21
  ]
@@ -24,55 +29,8 @@ module.exports = class DataSourceTemplateCodeGenerator {
24
29
  let name = this.template.get('name')
25
30
 
26
31
  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())
29
34
  return klass
30
35
  }
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
- }
78
36
  }
@@ -179,6 +179,10 @@ const klassMember = (name, type) => new ClassMember(name, type)
179
179
  const nullableType = type => new NullableType(type)
180
180
  const moduleImports = (nameOrNames, module) => new ModuleImports(nameOrNames, module)
181
181
 
182
+ const GENERATED_FILE_NOTE = `
183
+ // THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
184
+ `
185
+
182
186
  module.exports = {
183
187
  // Types
184
188
  NullableType,
@@ -194,4 +198,7 @@ module.exports = {
194
198
  param,
195
199
  nullableType,
196
200
  moduleImports,
201
+
202
+ // Utilities
203
+ GENERATED_FILE_NOTE,
197
204
  }
@@ -4,7 +4,10 @@ const toolbox = require('gluegun/toolbox')
4
4
  const Compiler = require('../compiler')
5
5
 
6
6
  // 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
+ ) => {
8
11
  // Parse the IPFS URL
9
12
  let url
10
13
  try {
@@ -31,7 +34,8 @@ The IPFS URL must be of the following format: http(s)://host[:port]/[path]`)
31
34
  outputDir: outputDir,
32
35
  outputFormat: outputFormat,
33
36
  skipMigrations,
34
- blockIpfsMethods
37
+ blockIpfsMethods,
38
+ protocol,
35
39
  })
36
40
  }
37
41
 
@@ -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
+ }
@@ -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
+ }
@@ -2,6 +2,8 @@ const chalk = require('chalk')
2
2
 
3
3
  const { createCompiler } = require('../command-helpers/compiler')
4
4
  const { fixParameters } = require('../command-helpers/gluegun')
5
+ const DataSourcesExtractor = require('../command-helpers/data-sources')
6
+ const Protocol = require('../protocols')
5
7
 
6
8
  const HELP = `
7
9
  ${chalk.bold('graph build')} [options] ${chalk.bold('[<subgraph-manifest>]')}
@@ -73,11 +75,23 @@ module.exports = {
73
75
  return
74
76
  }
75
77
 
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
+
76
89
  let compiler = createCompiler(manifest, {
77
90
  ipfs,
78
91
  outputDir,
79
92
  outputFormat,
80
93
  skipMigrations,
94
+ protocol,
81
95
  })
82
96
 
83
97
  // Exit with an error code if the compiler couldn't be created
@@ -2,7 +2,9 @@ const chalk = require('chalk')
2
2
  const path = require('path')
3
3
 
4
4
  const TypeGenerator = require('../type-generator')
5
+ const Protocol = require('../protocols')
5
6
  const { fixParameters } = require('../command-helpers/gluegun')
7
+ const DataSourcesExtractor = require('../command-helpers/data-sources')
6
8
  const { assertManifestApiVersion, assertGraphTsVersion } = require('../command-helpers/version')
7
9
 
8
10
  const HELP = `
@@ -60,6 +62,7 @@ module.exports = {
60
62
  return
61
63
  }
62
64
 
65
+ let protocol
63
66
  try {
64
67
  // Checks to make sure codegen doesn't run against
65
68
  // older subgraphs (both apiVersion and graph-ts version).
@@ -69,6 +72,10 @@ module.exports = {
69
72
  // the wrong AssemblyScript version.
70
73
  await assertManifestApiVersion(manifest, '0.0.5')
71
74
  await assertGraphTsVersion(path.dirname(manifest), '0.22.0')
75
+
76
+ const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(manifest)
77
+
78
+ protocol = Protocol.fromDataSources(dataSourcesAndTemplates)
72
79
  } catch (e) {
73
80
  print.error(e.message)
74
81
  process.exitCode = 1
@@ -79,6 +86,7 @@ module.exports = {
79
86
  subgraphManifest: manifest,
80
87
  outputDir: outputDir,
81
88
  skipMigrations,
89
+ protocol,
82
90
  })
83
91
 
84
92
  // Watch working directory for file updates or additions, trigger
@@ -11,8 +11,9 @@ const { withSpinner } = require('../command-helpers/spinner')
11
11
  const { validateSubgraphName } = require('../command-helpers/subgraph')
12
12
  const { DEFAULT_IPFS_URL } = require('../command-helpers/ipfs')
13
13
  const { assertManifestApiVersion, assertGraphTsVersion } = require('../command-helpers/version')
14
+ const DataSourcesExtractor = require('../command-helpers/data-sources')
14
15
  const { validateStudioNetwork } = require('../command-helpers/studio')
15
- const { loadManifest } = require('../migrations/util/load-manifest')
16
+ const Protocol = require('../protocols')
16
17
 
17
18
  const HELP = `
18
19
  ${chalk.bold('graph deploy')} [options] ${chalk.bold('<subgraph-name>')} ${chalk.bold(
@@ -128,13 +129,9 @@ module.exports = {
128
129
  : filesystem.resolve('subgraph.yaml')
129
130
 
130
131
  try {
131
- let manifestFile = await loadManifest(manifest)
132
+ const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(manifest)
132
133
 
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) {
138
135
  validateStudioNetwork({ studio, product, network })
139
136
  }
140
137
  } catch (e) {
@@ -192,6 +189,7 @@ module.exports = {
192
189
  return
193
190
  }
194
191
 
192
+ let protocol
195
193
  try {
196
194
  // Checks to make sure deploy doesn't run against
197
195
  // older subgraphs (both apiVersion and graph-ts version).
@@ -201,6 +199,10 @@ module.exports = {
201
199
  // using the wrong AssemblyScript compiler.
202
200
  await assertManifestApiVersion(manifest, '0.0.5')
203
201
  await assertGraphTsVersion(path.dirname(manifest), '0.22.0')
202
+
203
+ const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(manifest)
204
+
205
+ protocol = Protocol.fromDataSources(dataSourcesAndTemplates)
204
206
  } catch (e) {
205
207
  print.error(e.message)
206
208
  process.exitCode = 1
@@ -215,7 +217,8 @@ module.exports = {
215
217
  outputDir,
216
218
  outputFormat: 'wasm',
217
219
  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,
219
222
  })
220
223
 
221
224
  // Exit with an error code if the compiler couldn't be created
@@ -9,13 +9,14 @@ const {
9
9
  getSubgraphBasename,
10
10
  validateSubgraphName,
11
11
  } = require('../command-helpers/subgraph')
12
+ const DataSourcesExtractor = require('../command-helpers/data-sources')
12
13
  const { validateStudioNetwork } = require('../command-helpers/studio')
13
14
  const { withSpinner, step } = require('../command-helpers/spinner')
14
15
  const { fixParameters } = require('../command-helpers/gluegun')
15
16
  const { chooseNodeUrl } = require('../command-helpers/node')
16
- const { loadManifest } = require('../migrations/util/load-manifest')
17
17
  const { abiEvents, generateScaffold, writeScaffold } = require('../scaffold')
18
- const ABI = require('../abi')
18
+ // TODO: Use Protocol class to getABI
19
+ const ABI = require('../protocols/ethereum/abi')
19
20
 
20
21
  const networkChoices = [
21
22
  'mainnet',
@@ -613,13 +614,9 @@ const initSubgraphFromExample = async (
613
614
  try {
614
615
  // It doesn't matter if we changed the URL we clone the YAML,
615
616
  // we'll check it's network anyway. If it's a studio subgraph we're dealing with.
616
- let manifestFile = await loadManifest(path.join(directory, 'subgraph.yaml'))
617
+ const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(path.join(directory, 'subgraph.yaml'))
617
618
 
618
- for (const { network } of manifestFile.dataSources || []) {
619
- validateStudioNetwork({ studio, product, network })
620
- }
621
-
622
- for (const { network } of manifestFile.templates || []) {
619
+ for (const { network } of dataSourcesAndTemplates) {
623
620
  validateStudioNetwork({ studio, product, network })
624
621
  }
625
622
  } catch (e) {
@@ -2,6 +2,7 @@ const { Binary } = require('binary-install-raw')
2
2
  const os = require('os')
3
3
  const chalk = require('chalk')
4
4
  const fetch = require('node-fetch')
5
+ const semver = require('semver')
5
6
 
6
7
  const HELP = `
7
8
  ${chalk.bold('graph test')} ${chalk.dim('[options]')} ${chalk.bold('<datasource>')}
@@ -53,16 +54,20 @@ function getPlatform() {
53
54
  const type = os.type();
54
55
  const arch = os.arch();
55
56
  const release = os.release();
56
- const majorVersion = release.substr(0, release.indexOf('.'));
57
+ const cpuCore = os.cpus()[0];
58
+ const majorVersion = semver.major(release);
59
+ const isM1 = cpuCore.model.includes("Apple M1");
57
60
 
58
- if (arch === 'x64') {
61
+ if (arch === 'x64' || (arch === 'arm64' && isM1)) {
59
62
  if (type === 'Darwin') {
60
63
  if (majorVersion === '19') {
61
64
  return 'binary-macos-10.15';
62
65
  } else if (majorVersion === '18') {
63
66
  return 'binary-macos-10.14';
67
+ } else if (isM1) {
68
+ return 'binary-macos-11-m1';
64
69
  }
65
- return 'binary-macos-11'
70
+ return 'binary-macos-11';
66
71
  } else if (type === 'Linux') {
67
72
  if (majorVersion === '18') {
68
73
  return 'binary-linux-18';