@graphprotocol/graph-cli 0.37.2-alpha-20221219142030-0cf2a37 → 0.37.2

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.
@@ -1,5 +1,6 @@
1
1
  const { withSpinner } = require('./spinner')
2
2
  const fetch = require('node-fetch')
3
+ const immutable = require('immutable')
3
4
 
4
5
  const loadAbiFromEtherscan = async (ABI, network, address) =>
5
6
  await withSpinner(
@@ -17,7 +18,7 @@ const loadAbiFromEtherscan = async (ABI, network, address) =>
17
18
  // a `result` field. The `status` is '0' in case of errors and '1' in
18
19
  // case of success
19
20
  if (json.status === '1') {
20
- return new ABI('Contract', undefined, JSON.parse(json.result))
21
+ return new ABI('Contract', undefined, immutable.fromJS(JSON.parse(json.result)))
21
22
  } else {
22
23
  throw new Error('ABI not found, try loading it from a local file')
23
24
  }
@@ -41,7 +42,7 @@ const loadAbiFromBlockScout = async (ABI, network, address) =>
41
42
  // a `result` field. The `status` is '0' in case of errors and '1' in
42
43
  // case of success
43
44
  if (json.status === '1') {
44
- return new ABI('Contract', undefined, JSON.parse(json.result))
45
+ return new ABI('Contract', undefined, immutable.fromJS(JSON.parse(json.result)))
45
46
  } else {
46
47
  throw new Error('ABI not found, try loading it from a local file')
47
48
  }
@@ -1,3 +1,4 @@
1
+ const immutable = require('immutable')
1
2
  const { loadManifest } = require('../migrations/util/load-manifest')
2
3
 
3
4
  // Loads manifest from file path and returns all:
@@ -12,15 +13,15 @@ const fromFilePath = async manifestPath => {
12
13
 
13
14
  const extractDataSourceByType = (manifest, dataSourceType, protocol) =>
14
15
  manifest
15
- .get(dataSourceType, [])
16
+ .get(dataSourceType, immutable.List())
16
17
  .reduce(
17
18
  (dataSources, dataSource, dataSourceIndex) =>
18
19
  protocol.isValidKindName(dataSource.get('kind'))
19
20
  ? dataSources.push(
20
- { path: [dataSourceType, dataSourceIndex], dataSource },
21
+ immutable.Map({ path: [dataSourceType, dataSourceIndex], dataSource }),
21
22
  )
22
23
  : dataSources,
23
- []
24
+ immutable.List(),
24
25
  )
25
26
 
26
27
  // Extracts data sources and templates from a immutable manifest data structure
@@ -9,6 +9,7 @@ const { generateEventIndexingHandlers } = require('../scaffold/mapping')
9
9
  const { generateEventType, abiEvents } = require('../scaffold/schema')
10
10
  const { generateTestsFiles } = require('../scaffold/tests')
11
11
  const { strings } = require('gluegun')
12
+ const { Map } = require('immutable')
12
13
 
13
14
  const generateDataSource = async (
14
15
  protocol,
@@ -19,22 +20,15 @@ const generateDataSource = async (
19
20
  ) => {
20
21
  const protocolManifest = protocol.getManifestScaffold()
21
22
 
22
- return {
23
- kind: protocol.name,
24
- name: contractName,
25
- network: network,
26
- source: yaml.parse(
27
- prettier.format(
28
- protocolManifest.source({ contract: contractAddress, contractName }),
29
- { parser: 'yaml' },
30
- ),
31
- ),
32
- mapping: yaml.parse(
33
- prettier.format(protocolManifest.mapping({ abi, contractName }), {
34
- parser: 'yaml',
35
- }),
36
- ),
37
- }
23
+ return Map.of(
24
+ 'kind', protocol.name,
25
+ 'name', contractName,
26
+ 'network', network,
27
+ 'source', yaml.parse(prettier.format(protocolManifest.source({contract: contractAddress, contractName}),
28
+ {parser: 'yaml'})),
29
+ 'mapping', yaml.parse(prettier.format(protocolManifest.mapping({abi, contractName}),
30
+ {parser: 'yaml'}))
31
+ ).asMutable()
38
32
  }
39
33
 
40
34
  const generateScaffold = async (
@@ -136,9 +130,11 @@ const writeMapping = async (abi, protocol, contractName, entities) => {
136
130
 
137
131
  const writeTestsFiles = async (abi, protocol, contractName) => {
138
132
  const hasEvents = protocol.hasEvents()
139
- const events = hasEvents ? abiEvents(abi).toJS() : []
133
+ const events = hasEvents
134
+ ? abiEvents(abi).toJS()
135
+ : []
140
136
 
141
- if (events.length > 0) {
137
+ if(events.length > 0) {
142
138
  // If a contract is added to a subgraph that has no tests folder
143
139
  await fs.ensureDir('./tests/')
144
140
 
@@ -1,4 +1,5 @@
1
1
  const chalk = require('chalk')
2
+ const immutable = require('immutable')
2
3
  const toolbox = require('gluegun/toolbox')
3
4
 
4
5
  const step = (spinner, subject, text) => {
@@ -1,16 +1,11 @@
1
1
  const chalk = require('chalk')
2
2
  const toolbox = require('gluegun/toolbox')
3
+ const immutable = require('immutable')
3
4
  const { withSpinner } = require('../command-helpers/spinner')
4
5
  const Subgraph = require('../subgraph')
5
6
  const Protocol = require('../protocols')
6
7
  const DataSourcesExtractor = require('../command-helpers/data-sources')
7
- const {
8
- generateDataSource,
9
- writeABI,
10
- writeSchema,
11
- writeMapping,
12
- writeTestsFiles,
13
- } = require('../command-helpers/scaffold')
8
+ const { generateDataSource, writeABI, writeSchema, writeMapping, writeTestsFiles } = require('../command-helpers/scaffold')
14
9
  const { loadAbiFromEtherscan, loadAbiFromBlockScout } = require('../command-helpers/abi')
15
10
  const EthereumABI = require('../protocols/ethereum/abi')
16
11
  const { fixParameters } = require('../command-helpers/gluegun')
@@ -41,7 +36,7 @@ module.exports = {
41
36
  h,
42
37
  help,
43
38
  mergeEntities,
44
- networkFile,
39
+ networkFile
45
40
  } = toolbox.parameters.options
46
41
 
47
42
  contractName = contractName || 'Contract'
@@ -59,8 +54,7 @@ module.exports = {
59
54
  }
60
55
 
61
56
  let address = toolbox.parameters.first || toolbox.parameters.array[0]
62
- let manifestPath =
63
- toolbox.parameters.second || toolbox.parameters.array[1] || './subgraph.yaml'
57
+ let manifestPath = toolbox.parameters.second || toolbox.parameters.array[1] || './subgraph.yaml'
64
58
 
65
59
  // Show help text if requested
66
60
  if (help || h) {
@@ -78,10 +72,7 @@ module.exports = {
78
72
  const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(manifestPath)
79
73
  let protocol = Protocol.fromDataSources(dataSourcesAndTemplates)
80
74
  let manifest = await Subgraph.load(manifestPath, { protocol })
81
- let network = manifest.result
82
- .get('dataSources')
83
- .get(0)
84
- .get('network')
75
+ let network = manifest.result.get('dataSources').get(0).get('network')
85
76
  let result = manifest.result.asMutable()
86
77
 
87
78
  let entities = getEntities(manifest)
@@ -105,27 +96,17 @@ module.exports = {
105
96
  }
106
97
  }
107
98
 
108
- let { collisionEntities, onlyCollisions, abiData } = updateEventNamesOnCollision(
109
- ethabi,
110
- entities,
111
- contractName,
112
- mergeEntities,
113
- )
99
+ let { collisionEntities, onlyCollisions, abiData } = updateEventNamesOnCollision(ethabi, entities, contractName, mergeEntities)
114
100
  ethabi.data = abiData
115
101
 
116
102
  await writeABI(ethabi, contractName)
117
- await writeSchema(ethabi, protocol, result.schema?.file, collisionEntities)
103
+ await writeSchema(ethabi, protocol, result.getIn(['schema', 'file']), collisionEntities)
118
104
  await writeMapping(ethabi, protocol, contractName, collisionEntities)
119
105
  await writeTestsFiles(ethabi, protocol, contractName)
120
106
 
121
107
  let dataSources = result.get('dataSources')
122
- let dataSource = await generateDataSource(
123
- protocol,
124
- contractName,
125
- network,
126
- address,
127
- ethabi,
128
- )
108
+ let dataSource = await generateDataSource(protocol,
109
+ contractName, network, address, ethabi)
129
110
 
130
111
  // Handle the collisions edge case by copying another data source yaml data
131
112
  if (mergeEntities && onlyCollisions) {
@@ -145,7 +126,7 @@ module.exports = {
145
126
  await Subgraph.write(result, manifestPath)
146
127
 
147
128
  // Update networks.json
148
- const networksFile = networkFile || './networks.json'
129
+ const networksFile = networkFile || "./networks.json"
149
130
  await updateNetworksFile(toolbox, network, contractName, address, networksFile)
150
131
 
151
132
  // Detect Yarn and/or NPM
@@ -165,26 +146,28 @@ module.exports = {
165
146
  'Warning during codegen',
166
147
  async spinner => {
167
148
  await system.run(yarn ? 'yarn codegen' : 'npm run codegen')
168
- },
149
+ }
169
150
  )
170
- },
151
+ }
171
152
  }
172
153
 
173
- const getEntities = manifest => {
174
- let dataSources = manifest.result.get('dataSources', [])
175
- let templates = manifest.result.get('templates', [])
154
+ const getEntities = (manifest) => {
155
+ let dataSources = manifest.result.get('dataSources', immutable.List())
156
+ let templates = manifest.result.get('templates', immutable.List())
176
157
 
177
158
  return dataSources
178
159
  .concat(templates)
179
- .map(dataSource => dataSource.mapping?.entities)
160
+ .map(dataSource => dataSource.getIn(['mapping', 'entities']))
180
161
  .flatten()
181
162
  }
182
163
 
183
- const getContractNames = manifest => {
184
- let dataSources = manifest.result.get('dataSources', [])
185
- let templates = manifest.result.get('templates', [])
164
+ const getContractNames = (manifest) => {
165
+ let dataSources = manifest.result.get('dataSources', immutable.List())
166
+ let templates = manifest.result.get('templates', immutable.List())
186
167
 
187
- return dataSources.concat(templates).map(dataSource => dataSource.get('name'))
168
+ return dataSources
169
+ .concat(templates)
170
+ .map(dataSource => dataSource.get('name'))
188
171
  }
189
172
 
190
173
  const updateEventNamesOnCollision = (ethabi, entities, contractName, mergeEntities) => {
@@ -196,7 +179,7 @@ const updateEventNamesOnCollision = (ethabi, entities, contractName, mergeEntiti
196
179
  for (let i = 0; i < abiData.size; i++) {
197
180
  let dataRow = abiData.get(i).asMutable()
198
181
 
199
- if (dataRow.get('type') === 'event') {
182
+ if (dataRow.get('type') === 'event'){
200
183
  if (entities.indexOf(dataRow.get('name')) !== -1) {
201
184
  if (entities.indexOf(`${contractName}${dataRow.get('name')}`) !== -1) {
202
185
  print.error(`Contract name ('${contractName}')
@@ -207,7 +190,7 @@ const updateEventNamesOnCollision = (ethabi, entities, contractName, mergeEntiti
207
190
 
208
191
  if (mergeEntities) {
209
192
  collisionEntities.push(dataRow.get('name'))
210
- abiData = abiData.delete(i)
193
+ abiData = abiData.asImmutable().delete(i) // needs to be immutable when deleting, yes you read that right - https://github.com/immutable-js/immutable-js/issues/1901
211
194
  i-- // deletion also shifts values to the left
212
195
  continue
213
196
  } else {
@@ -217,7 +200,7 @@ const updateEventNamesOnCollision = (ethabi, entities, contractName, mergeEntiti
217
200
  onlyCollisions = false
218
201
  }
219
202
  }
220
- abiData = abiData.set(i, dataRow)
203
+ abiData = abiData.asMutable().set(i, dataRow)
221
204
  }
222
205
 
223
206
  return { abiData, collisionEntities, onlyCollisions }
@@ -1,6 +1,7 @@
1
1
  const chalk = require('chalk')
2
2
  const crypto = require('crypto')
3
3
  const fs = require('fs-extra')
4
+ const immutable = require('immutable')
4
5
  const path = require('path')
5
6
  const yaml = require('js-yaml')
6
7
  const toolbox = require('gluegun/toolbox')
@@ -140,12 +141,12 @@ class Compiler {
140
141
  files.push(this.options.subgraphManifest)
141
142
 
142
143
  // Add all file paths specified in manifest
143
- files.push(path.resolve(subgraph.schema?.file))
144
+ files.push(path.resolve(subgraph.getIn(['schema', 'file'])))
144
145
  subgraph.get('dataSources').map(dataSource => {
145
- files.push(dataSource.mapping?.file)
146
+ files.push(dataSource.getIn(['mapping', 'file']))
146
147
  // Only watch ABI related files if the target protocol has support/need for them.
147
148
  if (this.protocol.hasABIs()) {
148
- dataSource.mapping?.abis.map(abi => {
149
+ dataSource.getIn(['mapping', 'abis']).map(abi => {
149
150
  files.push(abi.get('file'))
150
151
  })
151
152
  }
@@ -257,7 +258,7 @@ class Compiler {
257
258
  }
258
259
 
259
260
  try {
260
- let dataSourceName = dataSource.name
261
+ let dataSourceName = dataSource.getIn(['name'])
261
262
 
262
263
  let baseDir = this.sourceDir
263
264
  let absoluteMappingPath = path.resolve(baseDir, mappingPath)
@@ -551,7 +552,7 @@ class Compiler {
551
552
  updates.push({
552
553
  keyPath: ['schema', 'file'],
553
554
  value: await this._uploadFileToIPFS(
554
- subgraph.schema?.file,
555
+ subgraph.getIn(['schema', 'file']),
555
556
  uploadedFiles,
556
557
  spinner,
557
558
  ),
@@ -559,7 +560,7 @@ class Compiler {
559
560
 
560
561
  if (this.protocol.hasABIs()) {
561
562
  for (let [i, dataSource] of subgraph.get('dataSources').entries()) {
562
- for (let [j, abi] of dataSource.mapping?.abis.entries()) {
563
+ for (let [j, abi] of dataSource.getIn(['mapping', 'abis']).entries()) {
563
564
  updates.push({
564
565
  keyPath: ['dataSources', i, 'mapping', 'abis', j, 'file'],
565
566
  value: await this._uploadFileToIPFS(
@@ -578,7 +579,7 @@ class Compiler {
578
579
  updates.push({
579
580
  keyPath: ['dataSources', i, 'mapping', 'file'],
580
581
  value: await this._uploadFileToIPFS(
581
- dataSource.mapping?.file,
582
+ dataSource.getIn(['mapping', 'file']),
582
583
  uploadedFiles,
583
584
  spinner,
584
585
  ),
@@ -589,7 +590,7 @@ class Compiler {
589
590
  updates.push({
590
591
  keyPath: ['dataSources', i, 'source', 'package', 'file'],
591
592
  value: await this._uploadFileToIPFS(
592
- dataSource.source', 'package?.file,
593
+ dataSource.getIn(['source', 'package', 'file']),
593
594
  uploadedFiles,
594
595
  spinner,
595
596
  ),
@@ -597,9 +598,9 @@ class Compiler {
597
598
  }
598
599
  }
599
600
 
600
- for (let [i, template] of subgraph.get('templates', []).entries()) {
601
+ for (let [i, template] of subgraph.get('templates', immutable.List()).entries()) {
601
602
  if (this.protocol.hasABIs()) {
602
- for (let [j, abi] of template.mapping?.abis.entries()) {
603
+ for (let [j, abi] of template.getIn(['mapping', 'abis']).entries()) {
603
604
  updates.push({
604
605
  keyPath: ['templates', i, 'mapping', 'abis', j, 'file'],
605
606
  value: await this._uploadFileToIPFS(
@@ -614,7 +615,7 @@ class Compiler {
614
615
  updates.push({
615
616
  keyPath: ['templates', i, 'mapping', 'file'],
616
617
  value: await this._uploadFileToIPFS(
617
- template.mapping?.file,
618
+ template.getIn(['mapping', 'file']),
618
619
  uploadedFiles,
619
620
  spinner,
620
621
  ),
@@ -660,7 +661,7 @@ class Compiler {
660
661
  ' ..',
661
662
  `${hash}${alreadyUploaded ? ' (already uploaded)' : ''}`,
662
663
  )
663
- return { '/': `/ipfs/${hash}` }
664
+ return immutable.fromJS({ '/': `/ipfs/${hash}` })
664
665
  }
665
666
 
666
667
  async _uploadSubgraphDefinitionToIPFS(subgraph) {
@@ -1,3 +1,5 @@
1
+ const immutable = require('immutable')
2
+
1
3
  module.exports = class ArweaveSubgraph {
2
4
  constructor(options = {}) {
3
5
  this.manifest = options.manifest
@@ -6,10 +8,13 @@ module.exports = class ArweaveSubgraph {
6
8
  }
7
9
 
8
10
  validateManifest() {
9
- return []
11
+ return immutable.List()
10
12
  }
11
13
 
12
14
  handlerTypes() {
13
- return ['blockHandlers', 'transactionHandlers']
15
+ return immutable.List([
16
+ 'blockHandlers',
17
+ 'transactionHandlers',
18
+ ])
14
19
  }
15
20
  }
@@ -1,3 +1,5 @@
1
+ const immutable = require('immutable')
2
+
1
3
  module.exports = class CosmosSubgraph {
2
4
  constructor(options = {}) {
3
5
  this.manifest = options.manifest
@@ -6,10 +8,15 @@ module.exports = class CosmosSubgraph {
6
8
  }
7
9
 
8
10
  validateManifest() {
9
- return []
11
+ return immutable.List()
10
12
  }
11
13
 
12
14
  handlerTypes() {
13
- return ['blockHandlers', 'eventHandlers', 'transactionHandlers', 'messageHandlers']
15
+ return immutable.List([
16
+ 'blockHandlers',
17
+ 'eventHandlers',
18
+ 'transactionHandlers',
19
+ 'messageHandlers',
20
+ ])
14
21
  }
15
22
  }
@@ -1,4 +1,5 @@
1
1
  const fs = require('fs-extra')
2
+ const immutable = require('immutable')
2
3
  const path = require('path')
3
4
 
4
5
  const AbiCodeGenerator = require('./codegen/abi')
@@ -103,17 +104,17 @@ module.exports = class ABI {
103
104
  callFunctions() {
104
105
  // An entry is a function if its type is not set or if it is one of
105
106
  // 'constructor', 'function' or 'fallback'
106
- let functionTypes = new Set(['constructor', 'function', 'fallback'])
107
+ let functionTypes = immutable.Set(['constructor', 'function', 'fallback'])
107
108
  let functions = this.data.filter(
108
- entry => !entry.has('type') || functionTypes.has(entry.get('type')),
109
+ entry => !entry.has('type') || functionTypes.includes(entry.get('type')),
109
110
  )
110
111
 
111
112
  // A function is a call function if it is nonpayable, payable or
112
113
  // not constant
113
- let mutabilityTypes =new Set(['nonpayable', 'payable'])
114
+ let mutabilityTypes = immutable.Set(['nonpayable', 'payable'])
114
115
  return functions.filter(
115
116
  entry =>
116
- mutabilityTypes.has(entry.get('stateMutability')) ||
117
+ mutabilityTypes.includes(entry.get('stateMutability')) ||
117
118
  entry.get('constant') === false,
118
119
  )
119
120
  }
@@ -124,7 +125,7 @@ module.exports = class ABI {
124
125
  .map(entry => {
125
126
  const name = entry.get('name', '<default>')
126
127
  const inputs = entry
127
- .get('inputs', [])
128
+ .get('inputs', immutable.List())
128
129
  .map(input => buildSignatureParameter(input))
129
130
 
130
131
  return `${name}(${inputs.join(',')})`
@@ -154,6 +155,6 @@ module.exports = class ABI {
154
155
  throw Error(`No valid ABI in file: ${path.relative(process.cwd(), file)}`)
155
156
  }
156
157
 
157
- return new ABI(name, file, abi)
158
+ return new ABI(name, file, immutable.fromJS(abi))
158
159
  }
159
160
  }