@graphprotocol/graph-cli 0.28.1 → 0.29.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphprotocol/graph-cli",
3
- "version": "0.28.1",
3
+ "version": "0.29.0",
4
4
  "license": "(Apache-2.0 OR MIT)",
5
5
  "description": "CLI for building for and deploying to The Graph",
6
6
  "dependencies": {
@@ -0,0 +1,113 @@
1
+ const path = require('path')
2
+ const yaml = require('yaml')
3
+ const { step, withSpinner } = require('../command-helpers/spinner')
4
+
5
+ const updateSubgraphNetwork = async (toolbox, manifest, network, networksFile, identifierName) =>
6
+ await withSpinner(
7
+ `Update sources network`,
8
+ `Failed to update sources network`,
9
+ `Warnings while updating sources network`,
10
+ async spinner => {
11
+ let allNetworks
12
+
13
+ step(spinner, `Reading networks config`)
14
+ allNetworks = await toolbox.filesystem.read(networksFile, "json")
15
+ let networkConfig = allNetworks[network]
16
+
17
+ // Exit if the network passed with --network does not exits in networks.json
18
+ if(!networkConfig) {
19
+ throw new Error(`Network '${network}' was not found in '${networksFile}'`)
20
+ }
21
+
22
+ await toolbox.patching.update(manifest, content => {
23
+ let subgraph = yaml.parse(content)
24
+ let networkSources = Object.keys(networkConfig)
25
+ let subgraphSources = subgraph.dataSources.map(value => value.name);
26
+
27
+ // Update the dataSources network config
28
+ subgraph.dataSources = subgraph.dataSources.map(source => {
29
+ if (!networkSources.includes(source.name)) {
30
+ throw new Error(`'${source.name}' was not found in the '${network}' configuration, please update!`)
31
+ }
32
+
33
+ if (hasChanges(identifierName, network, networkConfig[source.name], source)) {
34
+ step(spinner, `Update '${source.name}' network configuration`)
35
+ source.network = network
36
+ source.source = source.source.abi ? { abi: source.source.abi } : {}
37
+ Object.assign(source.source, networkConfig[source.name])
38
+ } else {
39
+ step(spinner, `Skip '${source.name}': No changes to network configuration`)
40
+ }
41
+
42
+ return source
43
+ }
44
+ )
45
+
46
+ // All data sources shoud be on the same network,
47
+ // so we have to update the network of all templates too.
48
+ if(subgraph.templates) {
49
+ subgraph.templates = subgraph.templates.map(template => ({
50
+ ...template,
51
+ network,
52
+ }))
53
+ }
54
+
55
+ let unsusedSources = networkSources.filter(x => !subgraphSources.includes(x))
56
+
57
+ unsusedSources.forEach(source => {
58
+ step(spinner, `dataSource '${source}' from '${networksFile}' not found in ${manifest}`)
59
+ })
60
+
61
+ let yaml_doc = new yaml.Document()
62
+ yaml_doc.contents = subgraph
63
+ return yaml_doc.toString()
64
+ })
65
+ })
66
+
67
+ const initNetworksConfig = async(toolbox, directory, identifierName) =>
68
+ await withSpinner(
69
+ `Initialize networks config`,
70
+ `Failed to initialize networks config`,
71
+ `Warnings while initializing networks config`,
72
+ async spinner => {
73
+ let subgraphStr = await toolbox.filesystem.read(path.join(directory, 'subgraph.yaml'))
74
+ let subgraph = yaml.parse(subgraphStr)
75
+
76
+ const networks = subgraph.dataSources.reduce((acc, source) =>
77
+ Object.assign(
78
+ acc,
79
+ {
80
+ [source.network]: {
81
+ [source.name]: {
82
+ [identifierName]: source.source.address,
83
+ startBlock: source.source.startBlock,
84
+ },
85
+ },
86
+ }
87
+ )
88
+ , {})
89
+
90
+ await toolbox.filesystem.write(`${directory}/networks.json`, networks)
91
+
92
+ return true
93
+ },
94
+ )
95
+
96
+ // Checks if any network attribute has been changed
97
+ function hasChanges(identifierName, network, networkConfig, dataSource) {
98
+ let networkChanged = dataSource.network !== network
99
+
100
+ // Return directly if the network is different
101
+ if (networkChanged) return networkChanged
102
+
103
+ let addressChanged = networkConfig[identifierName] !== dataSource.source[identifierName]
104
+
105
+ let startBlockChanged = networkConfig.startBlock !== dataSource.source.startBlock
106
+
107
+ return networkChanged || addressChanged || startBlockChanged
108
+ }
109
+
110
+ module.exports = {
111
+ updateSubgraphNetwork,
112
+ initNetworksConfig
113
+ }
@@ -0,0 +1,78 @@
1
+ const { initNetworksConfig, updateSubgraphNetwork } = require('../command-helpers/network')
2
+ const toolbox = require('gluegun/toolbox')
3
+ const yaml = require('yaml')
4
+
5
+ describe('initNetworksConfig', () => {
6
+ beforeAll(async () => {
7
+ await initNetworksConfig(toolbox, './examples/example-subgraph', 'address')
8
+ })
9
+ afterAll(async () => {
10
+ await toolbox.filesystem.remove('./examples/example-subgraph/networks.json')
11
+ })
12
+
13
+ test('generates networks.json from subgraph.yaml', () => {
14
+ expect(toolbox.filesystem.exists('./examples/example-subgraph/networks.json')).toBe('file')
15
+ })
16
+
17
+ test('Populates the networks.json file with the data from subgraph.yaml', async () => {
18
+ let networksStr = await toolbox.filesystem.read('./examples/example-subgraph/networks.json')
19
+ let networks = JSON.parse(networksStr)
20
+
21
+ let expected = {
22
+ mainnet: {
23
+ ExampleSubgraph: { address: '0x22843e74c59580b3eaf6c233fa67d8b7c561a835' }
24
+ }
25
+ }
26
+
27
+ expect(networks).toStrictEqual(expected)
28
+ })
29
+ })
30
+
31
+ describe('updateSubgraphNetwork', () => {
32
+ beforeAll(async () => {
33
+ let content = {
34
+ optimism: {
35
+ ExampleSubgraph: { address: '0x12345...' }
36
+ }
37
+ }
38
+
39
+ await toolbox.filesystem.write('./examples/example-subgraph/networks.json', content)
40
+ await toolbox.filesystem.copy('./examples/example-subgraph/subgraph.yaml', './examples/example-subgraph/subgraph_copy.yaml')
41
+ })
42
+
43
+ afterAll(async () => {
44
+ await toolbox.filesystem.remove('./examples/example-subgraph/networks.json')
45
+ await toolbox.filesystem.remove('./examples/example-subgraph/subgraph_copy.yaml')
46
+ })
47
+
48
+ test('Updates subgraph.yaml', async () => {
49
+ let manifest = './examples/example-subgraph/subgraph_copy.yaml'
50
+ let networksFie = './examples/example-subgraph/networks.json'
51
+ let subgraph = await toolbox.filesystem.read(manifest)
52
+ let subgraphObj = yaml.parse(subgraph)
53
+
54
+ let network = subgraphObj.dataSources[0].network
55
+ let address = subgraphObj.dataSources[0].source.address
56
+
57
+ expect(network).toBe('mainnet')
58
+ expect(address).toBe('0x22843e74c59580b3eaf6c233fa67d8b7c561a835')
59
+
60
+ await updateSubgraphNetwork(
61
+ toolbox,
62
+ manifest,
63
+ 'optimism',
64
+ networksFie,
65
+ 'address'
66
+ )
67
+
68
+ subgraph = await toolbox.filesystem.read(manifest)
69
+ subgraphObj = yaml.parse(subgraph)
70
+
71
+ network = subgraphObj.dataSources[0].network
72
+ address = subgraphObj.dataSources[0].source.address
73
+
74
+ expect(network).toBe('optimism')
75
+ expect(address).toBe('0x12345...')
76
+ })
77
+
78
+ })
@@ -2,6 +2,7 @@ const chalk = require('chalk')
2
2
 
3
3
  const { createCompiler } = require('../command-helpers/compiler')
4
4
  const { fixParameters } = require('../command-helpers/gluegun')
5
+ const { updateSubgraphNetwork } = require('../command-helpers/network')
5
6
  const DataSourcesExtractor = require('../command-helpers/data-sources')
6
7
  const Protocol = require('../protocols')
7
8
 
@@ -16,13 +17,15 @@ Options:
16
17
  -t, --output-format <format> Output format for mappings (wasm, wast) (default: wasm)
17
18
  --skip-migrations Skip subgraph migrations (default: false)
18
19
  -w, --watch Regenerate types when subgraph files change (default: false)
20
+ --network <name> Network to use from networks.json
21
+ --network-file <path> Networks file (default: "./networks.json")
19
22
  `
20
23
 
21
24
  module.exports = {
22
25
  description: 'Builds a subgraph and (optionally) uploads it to IPFS',
23
26
  run: async toolbox => {
24
27
  // Obtain tools
25
- let { filesystem, print, system } = toolbox
28
+ let { filesystem, patching, print, system } = toolbox
26
29
 
27
30
  // Parse CLI parameters
28
31
  let {
@@ -37,6 +40,8 @@ module.exports = {
37
40
  t,
38
41
  w,
39
42
  watch,
43
+ network,
44
+ networkFile
40
45
  } = toolbox.parameters.options
41
46
 
42
47
  // Support both short and long option variants
@@ -68,6 +73,10 @@ module.exports = {
68
73
  manifest !== undefined && manifest !== ''
69
74
  ? manifest
70
75
  : filesystem.resolve('subgraph.yaml')
76
+ networkFile =
77
+ networkFile !== undefined && networkFile !== ''
78
+ ? networkFile
79
+ : filesystem.resolve("networks.json")
71
80
 
72
81
  // Show help text if requested
73
82
  if (help) {
@@ -86,6 +95,17 @@ module.exports = {
86
95
  return
87
96
  }
88
97
 
98
+ if (network && filesystem.exists(networkFile) !== "file") {
99
+ print.error(`Network file '${networkFile}' does not exists or is not a file!`)
100
+ process.exitCode = 1
101
+ return
102
+ }
103
+
104
+ if (network) {
105
+ let identifierName = protocol.getContract().identifierName()
106
+ await updateSubgraphNetwork(toolbox, manifest, network, networkFile, identifierName)
107
+ }
108
+
89
109
  let compiler = createCompiler(manifest, {
90
110
  ipfs,
91
111
  outputDir,
@@ -4,6 +4,7 @@ const immutable = require('immutable')
4
4
  const os = require('os')
5
5
  const path = require('path')
6
6
  const toolbox = require('gluegun/toolbox')
7
+ const yaml = require('yaml')
7
8
 
8
9
  const {
9
10
  getSubgraphBasename,
@@ -11,6 +12,7 @@ const {
11
12
  } = require('../command-helpers/subgraph')
12
13
  const DataSourcesExtractor = require('../command-helpers/data-sources')
13
14
  const { validateStudioNetwork } = require('../command-helpers/studio')
15
+ const { initNetworksConfig } = require('../command-helpers/network')
14
16
  const { withSpinner, step } = require('../command-helpers/spinner')
15
17
  const { fixParameters } = require('../command-helpers/gluegun')
16
18
  const { chooseNodeUrl } = require('../command-helpers/node')
@@ -132,7 +134,7 @@ const processInitForm = async (
132
134
  return `${e.message}
133
135
 
134
136
  Examples:
135
-
137
+
136
138
  $ graph init ${os.userInfo().username}/${name}
137
139
  $ graph init ${name} --allow-simple-name`
138
140
  }
@@ -288,7 +290,7 @@ const getEtherscanLikeAPIUrl = (network) => {
288
290
  case "optimism-kovan": return `https://api-kovan-optimistic.etherscan.io/api`;
289
291
  default: return `https://api-${network}.etherscan.io/api`;
290
292
  }
291
- }
293
+ }
292
294
 
293
295
  const loadAbiFromEtherscan = async (ABI, network, address) =>
294
296
  await withSpinner(
@@ -356,7 +358,7 @@ module.exports = {
356
358
 
357
359
  node = node || g
358
360
  ;({ node, allowSimpleName } = chooseNodeUrl({ product, studio, node, allowSimpleName }))
359
-
361
+
360
362
  if (fromContract && fromExample) {
361
363
  print.error(`Only one of --from-example and --from-contract can be used at a time.`)
362
364
  process.exitCode = 1
@@ -686,6 +688,12 @@ const initSubgraphFromExample = async (
686
688
  return
687
689
  }
688
690
 
691
+ let networkConf = await initNetworksConfig(toolbox, directory, "address")
692
+ if (networkConf !== true) {
693
+ process.exitCode = 1
694
+ return
695
+ }
696
+
689
697
  // Update package.json to match the subgraph name
690
698
  let prepared = await withSpinner(
691
699
  `Update subgraph name and commands in package.json`,
@@ -827,6 +835,13 @@ const initSubgraphFromContract = async (
827
835
  return
828
836
  }
829
837
 
838
+ let identifierName = protocolInstance.getContract().identifierName()
839
+ let networkConf = await initNetworksConfig(toolbox, directory, identifierName)
840
+ if (networkConf !== true) {
841
+ process.exitCode = 1
842
+ return
843
+ }
844
+
830
845
  // Initialize a fresh Git repository
831
846
  let repo = await initRepository(toolbox, directory)
832
847
  if (repo !== true) {
@@ -4,6 +4,8 @@
4
4
  Write subgraph to directory
5
5
  - Create subgraph scaffold
6
6
  ✔ Create subgraph scaffold
7
+ - Initialize networks config
8
+ ✔ Initialize networks config
7
9
  - Initialize subgraph repository
8
10
  ✔ Initialize subgraph repository
9
11
  - Install dependencies with yarn
@@ -4,6 +4,8 @@
4
4
  Write subgraph to directory
5
5
  - Create subgraph scaffold
6
6
  ✔ Create subgraph scaffold
7
+ - Initialize networks config
8
+ ✔ Initialize networks config
7
9
  - Initialize subgraph repository
8
10
  ✔ Initialize subgraph repository
9
11
  - Install dependencies with yarn
@@ -4,6 +4,8 @@
4
4
  Write subgraph to directory
5
5
  - Create subgraph scaffold
6
6
  ✔ Create subgraph scaffold
7
+ - Initialize networks config
8
+ ✔ Initialize networks config
7
9
  - Initialize subgraph repository
8
10
  ✔ Initialize subgraph repository
9
11
  - Install dependencies with yarn
@@ -6,6 +6,8 @@
6
6
  Write subgraph to directory
7
7
  - Create subgraph scaffold
8
8
  ✔ Create subgraph scaffold
9
+ - Initialize networks config
10
+ ✔ Initialize networks config
9
11
  - Initialize subgraph repository
10
12
  ✔ Initialize subgraph repository
11
13
  - Install dependencies with yarn
@@ -1,5 +1,7 @@
1
1
  - Cloning example subgraph
2
2
  ✔ Cloning example subgraph
3
+ - Initialize networks config
4
+ ✔ Initialize networks config
3
5
  - Update subgraph name and commands in package.json
4
6
  ✔ Update subgraph name and commands in package.json
5
7
  - Initialize subgraph repository
@@ -4,6 +4,8 @@
4
4
  Write subgraph to directory
5
5
  - Create subgraph scaffold
6
6
  ✔ Create subgraph scaffold
7
+ - Initialize networks config
8
+ ✔ Initialize networks config
7
9
  - Initialize subgraph repository
8
10
  ✔ Initialize subgraph repository
9
11
  - Install dependencies with yarn