Skip to content

Commit 14c26a3

Browse files
committed
gnd: Replace --network with --network-file and update networks.json in add command
- Replace the --network flag with --network-file flag (default: networks.json) to match graph-cli's add command interface. The network is now always determined from the existing manifest's first data source. - After adding a new data source, automatically update networks.json with the new contract's address and start block using update_networks_file().
1 parent 126b1d7 commit 14c26a3

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

gnd/src/commands/add.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use clap::Parser;
1111
use inflector::Inflector;
1212
use serde_json::Value as JsonValue;
1313

14+
use crate::config::networks::update_networks_file;
1415
use crate::formatter::format_typescript;
1516
use crate::output::{step, Step};
1617
use crate::scaffold::manifest::{extract_events_from_abi, EventInfo};
@@ -40,9 +41,9 @@ pub struct AddOpt {
4041
#[clap(long)]
4142
pub merge_entities: bool,
4243

43-
/// Network the contract is deployed to
44-
#[clap(long)]
45-
pub network: Option<String>,
44+
/// Path to the networks.json file
45+
#[clap(long, default_value = "networks.json")]
46+
pub network_file: PathBuf,
4647

4748
/// Block number to start indexing from
4849
#[clap(long)]
@@ -79,19 +80,14 @@ pub async fn run_add(opt: AddOpt) -> Result<()> {
7980
let manifest: serde_yaml::Value = serde_yaml::from_str(&manifest_content)
8081
.with_context(|| format!("Failed to parse manifest: {}", opt.manifest.display()))?;
8182

82-
// Get network from manifest or flag
83-
let network = opt
84-
.network
85-
.clone()
86-
.or_else(|| {
87-
manifest
88-
.get("dataSources")
89-
.and_then(|ds| ds.as_sequence())
90-
.and_then(|seq| seq.first())
91-
.and_then(|first| first.get("network"))
92-
.and_then(|n| n.as_str())
93-
.map(String::from)
94-
})
83+
// Get network from manifest's first data source
84+
let network = manifest
85+
.get("dataSources")
86+
.and_then(|ds| ds.as_sequence())
87+
.and_then(|seq| seq.first())
88+
.and_then(|first| first.get("network"))
89+
.and_then(|n| n.as_str())
90+
.map(String::from)
9591
.unwrap_or_else(|| "mainnet".to_string());
9692

9793
// Fetch or load ABI
@@ -133,6 +129,20 @@ pub async fn run_add(opt: AddOpt) -> Result<()> {
133129
&events,
134130
)?;
135131

132+
// Update networks.json
133+
let networks_path = project_dir.join(&opt.network_file);
134+
update_networks_file(
135+
&networks_path,
136+
&network,
137+
&contract_name,
138+
&opt.address,
139+
start_block,
140+
)?;
141+
step(
142+
Step::Write,
143+
&format!("Updated {}", opt.network_file.display()),
144+
);
145+
136146
step(Step::Done, &format!("Added data source: {}", contract_name));
137147

138148
println!();
@@ -595,7 +605,7 @@ mod tests {
595605
abi: None,
596606
contract_name: None,
597607
merge_entities: false,
598-
network: None,
608+
network_file: PathBuf::from("networks.json"),
599609
start_block: None,
600610
};
601611

@@ -748,7 +758,7 @@ mod tests {
748758
abi: None,
749759
contract_name: None,
750760
merge_entities: false,
751-
network: None,
761+
network_file: PathBuf::from("networks.json"),
752762
start_block: None,
753763
};
754764

0 commit comments

Comments
 (0)