-
Notifications
You must be signed in to change notification settings - Fork 229
Add network config file #848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
evaporei
merged 23 commits into
graphprotocol:main
from
dimitrovmaksim:add-network-config-file
Mar 22, 2022
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
d011d66
Add some initial logic for the networking file
dimitrovmaksim 8d63378
Move logic into a command-helper
dimitrovmaksim 04c00a6
Add spinner
dimitrovmaksim ba2c33b
Code cleanup
dimitrovmaksim 753fc85
Initialize networks.json whne running init command
dimitrovmaksim 1f43cf2
Update tests
dimitrovmaksim 1cdb285
Add await on filesystem read
dimitrovmaksim b9cbdfb
Rename networks.js to networks.js. Rename flag and var to singular. E…
dimitrovmaksim 46359bb
Merge branch 'main' into add-network-config-file
dimitrovmaksim 7c1b5bb
Throw error if subgraph.yaml datasource is not specified in a network…
dimitrovmaksim 4f521b0
Add .DS_Store to .gitignore
dimitrovmaksim 593037c
Add tests
dimitrovmaksim c7c04d4
Throw error if networkFile path is not file
dimitrovmaksim 0d99416
Add back accidentally deleted line
dimitrovmaksim eaa51d1
Rename some variables. Update message
dimitrovmaksim 887807f
Remove jsonIndent option
dimitrovmaksim c945e76
Merge branch 'main' into add-network-config-file
dimitrovmaksim 10dd401
Address comments
dimitrovmaksim 0b03a19
Merge branch 'main' into add-network-config-file
dimitrovmaksim 9c479ec
Use current datasources protocol to determine if network uses account…
dimitrovmaksim 0247eda
Pass only the indentifierName to updateSubgraphNetwork
dimitrovmaksim f138af6
Update tests
dimitrovmaksim f5f5b7d
Fix arguments
dimitrovmaksim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
const path = require('path') | ||
const yaml = require('yaml') | ||
const { step, withSpinner } = require('../command-helpers/spinner') | ||
|
||
const updateSubgraphNetwork = async (toolbox, manifest, network, networksFile, identifierName) => | ||
await withSpinner( | ||
`Update sources network`, | ||
`Failed to update sources network`, | ||
`Warnings while updating sources network`, | ||
async spinner => { | ||
let allNetworks | ||
|
||
step(spinner, `Reading networks config`) | ||
allNetworks = await toolbox.filesystem.read(networksFile, "json") | ||
let networkConfig = allNetworks[network] | ||
|
||
// Exit if the network passed with --network does not exits in networks.json | ||
if(!networkConfig) { | ||
throw new Error(`Network '${network}' was not found in '${networksFile}'`) | ||
} | ||
|
||
await toolbox.patching.update(manifest, content => { | ||
let subgraph = yaml.parse(content) | ||
let networkSources = Object.keys(networkConfig) | ||
let subgraphSources = subgraph.dataSources.map(value => value.name); | ||
|
||
// Update the dataSources network config | ||
subgraph.dataSources = subgraph.dataSources.map(source => { | ||
if (!networkSources.includes(source.name)) { | ||
throw new Error(`'${source.name}' was not found in the '${network}' configuration, please update!`) | ||
} | ||
|
||
if (hasChanges(identifierName, network, networkConfig[source.name], source)) { | ||
step(spinner, `Update '${source.name}' network configuration`) | ||
source.network = network | ||
source.source = source.source.abi ? { abi: source.source.abi } : {} | ||
Object.assign(source.source, networkConfig[source.name]) | ||
} else { | ||
step(spinner, `Skip '${source.name}': No changes to network configuration`) | ||
} | ||
|
||
return source | ||
} | ||
) | ||
|
||
// All data sources shoud be on the same network, | ||
// so we have to update the network of all templates too. | ||
if(subgraph.templates) { | ||
subgraph.templates = subgraph.templates.map(template => ({ | ||
...template, | ||
network, | ||
})) | ||
} | ||
|
||
let unsusedSources = networkSources.filter(x => !subgraphSources.includes(x)) | ||
|
||
unsusedSources.forEach(source => { | ||
step(spinner, `dataSource '${source}' from '${networksFile}' not found in ${manifest}`) | ||
}) | ||
|
||
let yaml_doc = new yaml.Document() | ||
yaml_doc.contents = subgraph | ||
return yaml_doc.toString() | ||
}) | ||
}) | ||
|
||
const initNetworksConfig = async(toolbox, directory, identifierName) => | ||
await withSpinner( | ||
`Initialize networks config`, | ||
`Failed to initialize networks config`, | ||
`Warnings while initializing networks config`, | ||
async spinner => { | ||
let subgraphStr = await toolbox.filesystem.read(path.join(directory, 'subgraph.yaml')) | ||
let subgraph = yaml.parse(subgraphStr) | ||
|
||
const networks = subgraph.dataSources.reduce((acc, source) => | ||
Object.assign( | ||
acc, | ||
{ | ||
[source.network]: { | ||
[source.name]: { | ||
[identifierName]: source.source.address, | ||
startBlock: source.source.startBlock, | ||
}, | ||
}, | ||
} | ||
) | ||
, {}) | ||
|
||
await toolbox.filesystem.write(`${directory}/networks.json`, networks) | ||
|
||
return true | ||
}, | ||
) | ||
|
||
// Checks if any network attribute has been changed | ||
function hasChanges(identifierName, network, networkConfig, dataSource) { | ||
let networkChanged = dataSource.network !== network | ||
|
||
// Return directly if the network is different | ||
if (networkChanged) return networkChanged | ||
|
||
let addressChanged = networkConfig[identifierName] !== dataSource.source[identifierName] | ||
|
||
let startBlockChanged = networkConfig.startBlock !== dataSource.source.startBlock | ||
|
||
return networkChanged || addressChanged || startBlockChanged | ||
} | ||
|
||
module.exports = { | ||
updateSubgraphNetwork, | ||
initNetworksConfig | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const { initNetworksConfig, updateSubgraphNetwork } = require('../command-helpers/network') | ||
const toolbox = require('gluegun/toolbox') | ||
const yaml = require('yaml') | ||
|
||
describe('initNetworksConfig', () => { | ||
beforeAll(async () => { | ||
await initNetworksConfig(toolbox, './examples/example-subgraph', 'address') | ||
}) | ||
afterAll(async () => { | ||
await toolbox.filesystem.remove('./examples/example-subgraph/networks.json') | ||
}) | ||
|
||
test('generates networks.json from subgraph.yaml', () => { | ||
expect(toolbox.filesystem.exists('./examples/example-subgraph/networks.json')).toBe('file') | ||
}) | ||
|
||
test('Populates the networks.json file with the data from subgraph.yaml', async () => { | ||
let networksStr = await toolbox.filesystem.read('./examples/example-subgraph/networks.json') | ||
let networks = JSON.parse(networksStr) | ||
|
||
let expected = { | ||
mainnet: { | ||
ExampleSubgraph: { address: '0x22843e74c59580b3eaf6c233fa67d8b7c561a835' } | ||
} | ||
} | ||
|
||
expect(networks).toStrictEqual(expected) | ||
}) | ||
}) | ||
|
||
describe('updateSubgraphNetwork', () => { | ||
beforeAll(async () => { | ||
let content = { | ||
optimism: { | ||
ExampleSubgraph: { address: '0x12345...' } | ||
} | ||
} | ||
|
||
await toolbox.filesystem.write('./examples/example-subgraph/networks.json', content) | ||
await toolbox.filesystem.copy('./examples/example-subgraph/subgraph.yaml', './examples/example-subgraph/subgraph_copy.yaml') | ||
}) | ||
|
||
afterAll(async () => { | ||
await toolbox.filesystem.remove('./examples/example-subgraph/networks.json') | ||
await toolbox.filesystem.remove('./examples/example-subgraph/subgraph_copy.yaml') | ||
}) | ||
|
||
test('Updates subgraph.yaml', async () => { | ||
let manifest = './examples/example-subgraph/subgraph_copy.yaml' | ||
let networksFie = './examples/example-subgraph/networks.json' | ||
let subgraph = await toolbox.filesystem.read(manifest) | ||
let subgraphObj = yaml.parse(subgraph) | ||
|
||
let network = subgraphObj.dataSources[0].network | ||
let address = subgraphObj.dataSources[0].source.address | ||
|
||
expect(network).toBe('mainnet') | ||
expect(address).toBe('0x22843e74c59580b3eaf6c233fa67d8b7c561a835') | ||
|
||
await updateSubgraphNetwork( | ||
toolbox, | ||
manifest, | ||
'optimism', | ||
networksFie, | ||
'address' | ||
) | ||
|
||
subgraph = await toolbox.filesystem.read(manifest) | ||
subgraphObj = yaml.parse(subgraph) | ||
|
||
network = subgraphObj.dataSources[0].network | ||
address = subgraphObj.dataSources[0].source.address | ||
|
||
expect(network).toBe('optimism') | ||
expect(address).toBe('0x12345...') | ||
}) | ||
|
||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ const chalk = require('chalk') | |
|
||
const { createCompiler } = require('../command-helpers/compiler') | ||
const { fixParameters } = require('../command-helpers/gluegun') | ||
const { updateSubgraphNetwork } = require('../command-helpers/network') | ||
const DataSourcesExtractor = require('../command-helpers/data-sources') | ||
const Protocol = require('../protocols') | ||
|
||
|
@@ -16,13 +17,15 @@ Options: | |
-t, --output-format <format> Output format for mappings (wasm, wast) (default: wasm) | ||
--skip-migrations Skip subgraph migrations (default: false) | ||
-w, --watch Regenerate types when subgraph files change (default: false) | ||
--network <name> Network to use from networks.json | ||
--network-file <path> Networks file (default: "./networks.json") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is missing a space before |
||
` | ||
|
||
module.exports = { | ||
description: 'Builds a subgraph and (optionally) uploads it to IPFS', | ||
run: async toolbox => { | ||
// Obtain tools | ||
let { filesystem, print, system } = toolbox | ||
let { filesystem, patching, print, system } = toolbox | ||
|
||
// Parse CLI parameters | ||
let { | ||
|
@@ -37,6 +40,8 @@ module.exports = { | |
t, | ||
w, | ||
watch, | ||
network, | ||
networkFile | ||
} = toolbox.parameters.options | ||
|
||
// Support both short and long option variants | ||
|
@@ -68,6 +73,10 @@ module.exports = { | |
manifest !== undefined && manifest !== '' | ||
? manifest | ||
: filesystem.resolve('subgraph.yaml') | ||
networkFile = | ||
networkFile !== undefined && networkFile !== '' | ||
? networkFile | ||
: filesystem.resolve("networks.json") | ||
|
||
// Show help text if requested | ||
if (help) { | ||
|
@@ -86,6 +95,17 @@ module.exports = { | |
return | ||
} | ||
|
||
if (network && filesystem.exists(networkFile) !== "file") { | ||
print.error(`Network file '${networkFile}' does not exists or is not a file!`) | ||
process.exitCode = 1 | ||
return | ||
} | ||
|
||
if (network) { | ||
let identifierName = protocol.getContract().identifierName() | ||
await updateSubgraphNetwork(toolbox, manifest, network, networkFile, identifierName) | ||
} | ||
|
||
let compiler = createCompiler(manifest, { | ||
ipfs, | ||
outputDir, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.