Skip to content

Commit

Permalink
fix: resolve more chain alias variants (#6905)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Jan 26, 2024
1 parent a6350ea commit 8ba6d56
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion crates/config/src/etherscan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
resolve::{interpolate, UnresolvedEnvVarError, RE_PLACEHOLDER},
Chain, Config, NamedChain,
};
use inflector::Inflector;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::{
collections::BTreeMap,
Expand Down Expand Up @@ -172,7 +173,19 @@ impl EtherscanConfig {
let (chain, alias) = match (chain, alias) {
// fill one with the other
(Some(chain), None) => (Some(chain), Some(chain.to_string())),
(None, Some(alias)) => (alias.parse().ok(), Some(alias.into())),
(None, Some(alias)) => {
// alloy chain is parsed as kebab case
(
alias.to_kebab_case().parse().ok().or_else(|| {
// if this didn't work try to parse as json because the deserialize impl
// supports more aliases
serde_json::from_str::<NamedChain>(&format!("\"{alias}\""))
.map(Into::into)
.ok()
}),
Some(alias.into()),
)
}
// leave as is
(Some(chain), Some(alias)) => (Some(chain), Some(alias.into())),
(None, None) => (None, None),
Expand Down Expand Up @@ -441,4 +454,35 @@ mod tests {

std::env::remove_var(env);
}

#[test]
fn resolve_etherscan_alias_config() {
let mut configs = EtherscanConfigs::default();
configs.insert(
"blast_sepolia".to_string(),
EtherscanConfig {
chain: None,
url: Some("https://api.etherscan.io/api".to_string()),
key: EtherscanApiKey::Key("ABCDEFG".to_string()),
},
);

let mut resolved = configs.clone().resolved();
let config = resolved.remove("blast_sepolia").unwrap().unwrap();
assert_eq!(config.chain, Some(Chain::blast_sepolia()));
}

#[test]
fn resolve_etherscan_alias() {
let config = EtherscanConfig {
chain: None,
url: Some("https://api.etherscan.io/api".to_string()),
key: EtherscanApiKey::Key("ABCDEFG".to_string()),
};
let resolved = config.clone().resolve(Some("base_sepolia")).unwrap();
assert_eq!(resolved.chain, Some(Chain::base_sepolia()));

let resolved = config.resolve(Some("base-sepolia")).unwrap();
assert_eq!(resolved.chain, Some(Chain::base_sepolia()));
}
}

0 comments on commit 8ba6d56

Please sign in to comment.