From 03a9a3920f17f45f5563b6779750fe58feea3ac1 Mon Sep 17 00:00:00 2001 From: sashass1315 Date: Wed, 8 Oct 2025 19:11:49 +0300 Subject: [PATCH] feat(cast): cache Etherscan sources under Foundry cache with temp fallback --- crates/cast/src/cmd/storage.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/crates/cast/src/cmd/storage.rs b/crates/cast/src/cmd/storage.rs index eb8eaa2cc6616..6c92d0b8379a5 100644 --- a/crates/cast/src/cmd/storage.rs +++ b/crates/cast/src/cmd/storage.rs @@ -159,11 +159,22 @@ impl StorageArgs { eyre::bail!("Contract at provided address is not a valid Solidity contract") } - // Create a new temp project - // TODO: Cache instead of using a temp directory: metadata from Etherscan won't change - let root = tempfile::tempdir()?; - let root_path = root.path(); - let mut project = etherscan_project(metadata, root_path)?; + // Create or reuse a persistent cache for Etherscan sources; fall back to a temp dir + let root_path = if let Some(cache_root) = + foundry_config::Config::foundry_etherscan_chain_cache_dir(chain) + { + let sources_root = cache_root.join("sources"); + let contract_root = sources_root.join(format!("{address}")); + if let Err(err) = std::fs::create_dir_all(&contract_root) { + sh_warn!("Could not create etherscan cache dir, falling back to temp: {err}")?; + tempfile::tempdir()?.path().to_path_buf() + } else { + contract_root + } + } else { + tempfile::tempdir()?.path().to_path_buf() + }; + let mut project = etherscan_project(metadata, &root_path)?; add_storage_layout_output(&mut project); // Decide on compiler to use (user override -> metadata -> autodetect) @@ -214,9 +225,6 @@ impl StorageArgs { artifact }; - // Clear temp directory - root.close()?; - fetch_and_print_storage(provider, address, block, artifact).await } }