Skip to content

Commit 00cd692

Browse files
committed
gnd: Eliminate duplicate contract fetching in interactive init mode
When running `gnd init` interactively with a contract address, the contract info was fetched twice: once in InitForm::run_interactive() to get defaults for prompts, and again in init_from_contract() to generate the scaffold. Store the fetched ContractInfo in InitForm and pass it through to init_from_contract() to skip the redundant network request.
1 parent 9b12c31 commit 00cd692

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

gnd/src/commands/init.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ pub async fn run_init(opt: InitOpt) -> Result<()> {
146146
};
147147

148148
match source {
149-
ScaffoldSource::Contract => init_from_contract(&opt).await,
149+
ScaffoldSource::Contract => init_from_contract(&opt, None).await,
150150
ScaffoldSource::Example => init_from_example(&opt),
151151
ScaffoldSource::Subgraph => init_from_subgraph(&opt).await,
152152
}
@@ -232,7 +232,7 @@ async fn run_interactive(opt: InitOpt) -> Result<()> {
232232
skip_git: opt.skip_git,
233233
..Default::default()
234234
};
235-
init_from_contract(&contract_opt).await
235+
init_from_contract(&contract_opt, form.contract_info).await
236236
}
237237
}
238238
}
@@ -244,7 +244,10 @@ enum ScaffoldSource {
244244
}
245245

246246
/// Initialize a subgraph from a contract address.
247-
async fn init_from_contract(opt: &InitOpt) -> Result<()> {
247+
///
248+
/// If `prefetched` is provided, it will be used instead of fetching contract info again.
249+
/// This avoids duplicate network requests when running in interactive mode.
250+
async fn init_from_contract(opt: &InitOpt, prefetched: Option<ContractInfo>) -> Result<()> {
248251
let address = opt
249252
.from_contract
250253
.as_ref()
@@ -260,14 +263,14 @@ async fn init_from_contract(opt: &InitOpt) -> Result<()> {
260263

261264
let network = opt.network.as_deref().unwrap_or("mainnet");
262265

263-
step(
264-
Step::Load,
265-
&format!("Fetching contract info from {} on {}", address, network),
266-
);
267-
268266
let contract_info = {
269267
// Load ABI from file if provided
270268
if let Some(abi_path) = &opt.abi {
269+
step(
270+
Step::Load,
271+
&format!("Loading contract ABI from {}", abi_path.display()),
272+
);
273+
271274
let abi_str = fs::read_to_string(abi_path)
272275
.with_context(|| format!("Failed to read ABI file: {}", abi_path.display()))?;
273276
let abi: serde_json::Value = serde_json::from_str(&abi_str)
@@ -294,8 +297,16 @@ async fn init_from_contract(opt: &InitOpt) -> Result<()> {
294297
name,
295298
start_block,
296299
}
300+
} else if let Some(info) = prefetched {
301+
// Use pre-fetched contract info from interactive prompts
302+
info
297303
} else {
298304
// Fetch ABI from Etherscan/Sourcify
305+
step(
306+
Step::Load,
307+
&format!("Fetching contract info from {} on {}", address, network),
308+
);
309+
299310
let service = ContractService::load()
300311
.await
301312
.context("Failed to load contract service")?;

gnd/src/prompt.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,16 @@ pub struct InitForm {
395395
pub source_type: SourceType,
396396
pub contract_address: Option<String>,
397397
pub contract_name: String,
398+
/// User-confirmed start block. May differ from the fetched default in
399+
/// `contract_info` if the user modified it when prompted.
398400
pub start_block: Option<u64>,
399401
pub index_events: bool,
400402
pub abi_path: Option<String>,
403+
/// Cached contract info fetched during interactive prompts, to avoid
404+
/// re-fetching. Primarily used for the ABI; the `start_block` here is
405+
/// the original fetched value before user confirmation (use
406+
/// `self.start_block` for the final value).
407+
pub contract_info: Option<ContractInfo>,
401408
}
402409

403410
impl InitForm {
@@ -454,6 +461,7 @@ impl InitForm {
454461
start_block: None,
455462
index_events: false,
456463
abi_path: None,
464+
contract_info: None,
457465
});
458466
}
459467

@@ -544,6 +552,7 @@ impl InitForm {
544552
start_block,
545553
index_events,
546554
abi_path,
555+
contract_info: fetched_info,
547556
})
548557
}
549558
}

0 commit comments

Comments
 (0)