Skip to content

Commit 666e544

Browse files
committed
subgraph: Remove circular dependency
1 parent 8f6b69d commit 666e544

File tree

6 files changed

+74
-67
lines changed

6 files changed

+74
-67
lines changed

src/command-helpers/data-sources.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
1+
const immutable = require('immutable')
12
const { loadManifest } = require('../migrations/util/load-manifest')
23

4+
// From file path
35
const getDataSourcesAndTemplates = async manifestPath => {
46
const { dataSources = [], templates = [] } = await loadManifest(manifestPath)
57

68
return dataSources.concat(templates)
79
}
810

11+
// From in memory immutable data structure
12+
const collectDataSources = (manifest, protocolName) =>
13+
manifest
14+
.get('dataSources')
15+
.reduce(
16+
(dataSources, dataSource, dataSourceIndex) =>
17+
dataSource.get('kind') === protocolName
18+
? dataSources.push(
19+
immutable.Map({ path: ['dataSources', dataSourceIndex], dataSource }),
20+
)
21+
: dataSources,
22+
immutable.List(),
23+
)
24+
25+
const collectDataSourceTemplates = (manifest, protocolName) =>
26+
manifest.get('templates', immutable.List()).reduce(
27+
(templates, template, templateIndex) =>
28+
template.get('kind') === protocolName
29+
? templates.push(
30+
immutable.Map({
31+
path: ['templates', templateIndex],
32+
dataSource: template,
33+
}),
34+
)
35+
: templates,
36+
immutable.List(),
37+
)
38+
939
module.exports = {
1040
getDataSourcesAndTemplates,
41+
collectDataSources,
42+
collectDataSourceTemplates,
1143
}

src/protocols/ethereum/subgraph.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const immutable = require('immutable')
22
const ABI = require('./abi')
3-
const Subgraph = require('../../subgraph')
3+
const { collectDataSources, collectDataSourceTemplates } = require('../../command-helpers/data-sources')
4+
const { validateContractAddresses } = require('../../validation')
45

56
module.exports = class EthereumSubgraph {
67
constructor(options = {}) {
@@ -16,8 +17,8 @@ module.exports = class EthereumSubgraph {
1617
}
1718

1819
validateAbis() {
19-
let dataSources = Subgraph.collectDataSources(this.manifest, 'ethereum/contract')
20-
let dataSourceTemplates = Subgraph.collectDataSourceTemplates(this.manifest, 'ethereum/contract')
20+
let dataSources = collectDataSources(this.manifest, 'ethereum/contract')
21+
let dataSourceTemplates = collectDataSourceTemplates(this.manifest, 'ethereum/contract')
2122

2223
return dataSources.concat(dataSourceTemplates).reduce(
2324
(errors, dataSourceOrTemplate) =>
@@ -74,7 +75,7 @@ ${abiNames
7475
validateContractAddresses() {
7576
const ethereumAddressPattern = /^(0x)?[0-9a-fA-F]{40}$/
7677

77-
return Subgraph.validateContractAddresses(
78+
return validateContractAddresses(
7879
this.manifest,
7980
'ethereum/contract',
8081
address => ethereumAddressPattern.test(address),
@@ -83,8 +84,8 @@ ${abiNames
8384
}
8485

8586
validateEvents() {
86-
let dataSources = Subgraph.collectDataSources(this.manifest, 'ethereum/contract')
87-
let dataSourceTemplates = Subgraph.collectDataSourceTemplates(this.manifest, 'ethereum/contract')
87+
let dataSources = collectDataSources(this.manifest, 'ethereum/contract')
88+
let dataSourceTemplates = collectDataSourceTemplates(this.manifest, 'ethereum/contract')
8889

8990
return dataSources
9091
.concat(dataSourceTemplates)

src/protocols/near/subgraph.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const immutable = require('immutable')
2-
const Subgraph = require('../../subgraph')
2+
const { validateContractAddresses } = require('../../validation')
33

44
module.exports = class NearSubgraph {
55
constructor(options = {}) {
@@ -20,7 +20,7 @@ module.exports = class NearSubgraph {
2020
accountId.length <= MAXIMUM_ACCOUNT_ID_LENGTH
2121
const nearAccountIdPattern = /^(([a-z\d]+[\-_])*[a-z\d]+\.)*([a-z\d]+[\-_])*[a-z\d]+$/
2222

23-
return Subgraph.validateContractAddresses(
23+
return validateContractAddresses(
2424
this.manifest,
2525
'near',
2626
accountId => validateLength(accountId) && nearAccountIdPattern.test(accountId),

src/subgraph.js

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -87,64 +87,6 @@ module.exports = class Subgraph {
8787
}
8888
}
8989

90-
static collectDataSources(manifest, protocolName) {
91-
return manifest
92-
.get('dataSources')
93-
.reduce(
94-
(dataSources, dataSource, dataSourceIndex) =>
95-
dataSource.get('kind') === protocolName
96-
? dataSources.push(
97-
immutable.Map({ path: ['dataSources', dataSourceIndex], dataSource }),
98-
)
99-
: dataSources,
100-
immutable.List(),
101-
)
102-
}
103-
104-
static collectDataSourceTemplates(manifest, protocolName) {
105-
return manifest.get('templates', immutable.List()).reduce(
106-
(templates, template, templateIndex) =>
107-
template.get('kind') === protocolName
108-
? templates.push(
109-
immutable.Map({
110-
path: ['templates', templateIndex],
111-
dataSource: template,
112-
}),
113-
)
114-
: templates,
115-
immutable.List(),
116-
)
117-
}
118-
119-
static validateContractAddresses(manifest, protocolName, validator, errorMessage) {
120-
return manifest
121-
.get('dataSources')
122-
.filter(dataSource => dataSource.get('kind') === protocolName)
123-
.reduce((errors, dataSource, dataSourceIndex) => {
124-
let path = ['dataSources', dataSourceIndex, 'source', 'address']
125-
126-
// No need to validate if the source has no contract address
127-
if (!dataSource.get('source').has('address')) {
128-
return errors
129-
}
130-
131-
let address = dataSource.getIn(['source', 'address'])
132-
133-
// Validate whether the address is valid
134-
if (validator(address)) {
135-
return errors
136-
} else {
137-
return errors.push(
138-
immutable.fromJS({
139-
path,
140-
message: `\
141-
Contract address is invalid: ${address}${errorMessage ? `\n${errorMessage}` : ''}`,
142-
}),
143-
)
144-
}
145-
}, immutable.List())
146-
}
147-
14890
static validateRepository(manifest, { resolveFile }) {
14991
return manifest.get('repository') !==
15092
'https://github.com/graphprotocol/example-subgraph'

src/validation/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
22
validateSchema: require('./schema').validateSchema,
33
validateManifest: require('./manifest').validateManifest,
4+
validateContractAddresses: require('./manifest').validateContractAddresses,
45
}

src/validation/manifest.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,35 @@ const validateManifest = (value, type, schema, { resolveFile }) => {
290290
return validateDataSourceNetworks(value)
291291
}
292292

293-
module.exports = { validateManifest }
293+
const validateContractAddresses = (manifest, protocolName, validator, errorMessage) =>
294+
manifest
295+
.get('dataSources')
296+
.filter(dataSource => dataSource.get('kind') === protocolName)
297+
.reduce((errors, dataSource, dataSourceIndex) => {
298+
let path = ['dataSources', dataSourceIndex, 'source', 'address']
299+
300+
// No need to validate if the source has no contract address
301+
if (!dataSource.get('source').has('address')) {
302+
return errors
303+
}
304+
305+
let address = dataSource.getIn(['source', 'address'])
306+
307+
// Validate whether the address is valid
308+
if (validator(address)) {
309+
return errors
310+
} else {
311+
return errors.push(
312+
immutable.fromJS({
313+
path,
314+
message: `\
315+
Contract address is invalid: ${address}${errorMessage ? `\n${errorMessage}` : ''}`,
316+
}),
317+
)
318+
}
319+
}, immutable.List())
320+
321+
module.exports = {
322+
validateManifest,
323+
validateContractAddresses,
324+
}

0 commit comments

Comments
 (0)