Skip to content

Commit

Permalink
update docs and set v1 as the default
Browse files Browse the repository at this point in the history
  • Loading branch information
iainnash committed Jan 22, 2025
1 parent 35057a6 commit fae108b
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions crates/block-explorers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,20 @@ pub mod verify;

pub(crate) type Result<T, E = EtherscanError> = std::result::Result<T, E>;

/// The Etherscan.io API version 1 - classic verifier, one API per chain, 2 - new multichain verifier
#[derive(Clone, Debug, PartialEq, Copy)]
pub enum EtherscanApiVersion {
V1,
V2,
}

/// Default implementation for EtherscanApi V1
impl Default for EtherscanApiVersion {
fn default() -> Self {
EtherscanApiVersion::V1
}
}

/// The Etherscan.io API client.
#[derive(Clone, Debug)]
pub struct Client {
Expand Down Expand Up @@ -107,6 +115,7 @@ impl Client {
Client::builder().with_api_key(api_key).chain(chain)?.build()
}

/// Create a new client with the correct endpoint with the chain and chosen API v2 version
pub fn new_v2_from_env(chain: Chain) -> Result<Self> {
let api_key = std::env::var("ETHERSCAN_API_KEY")?;
Client::builder()
Expand Down Expand Up @@ -300,8 +309,8 @@ pub struct ClientBuilder {
api_key: Option<String>,
/// Etherscan API endpoint like <https://api(-chain).etherscan.io/api>
etherscan_api_url: Option<Url>,
/// Etherscan API version (v2 is new verifier version)
etherscan_api_version: Option<EtherscanApiVersion>,
/// Etherscan API version (v2 is new verifier version, v1 is the default)
etherscan_api_version: EtherscanApiVersion,
/// Etherscan base endpoint like <https://etherscan.io>
etherscan_url: Option<Url>,
/// Path to where ABI files should be cached
Expand Down Expand Up @@ -334,7 +343,7 @@ impl ClientBuilder {
.ok_or_else(|| EtherscanError::ChainNotSupported(chain))?;

// V2 etherscan default API urls are different – this handles that case.
let etherscan_api_url = if self.etherscan_api_version == Some(EtherscanApiVersion::V2) {
let etherscan_api_url = if self.etherscan_api_version == EtherscanApiVersion::V2 {
let chain_id = chain.id();
Url::parse(&format!("https://api.etherscan.io/v2/api?chainid={}", chain_id))
.map_err(|_| EtherscanError::Builder("Bad URL Parse".into()))?
Expand All @@ -347,7 +356,7 @@ impl ClientBuilder {

/// Configures the etherscan api version
pub fn with_api_version(mut self, api_version: EtherscanApiVersion) -> Self {
self.etherscan_api_version = Some(api_version);
self.etherscan_api_version = api_version;
self
}

Expand Down Expand Up @@ -425,7 +434,7 @@ impl ClientBuilder {
.clone()
.ok_or_else(|| EtherscanError::Builder("etherscan api url".to_string()))?,
// Set default API version to V1 if missing
etherscan_api_version: etherscan_api_version.unwrap_or(EtherscanApiVersion::V1),
etherscan_api_version: etherscan_api_version,
etherscan_url: etherscan_url
.ok_or_else(|| EtherscanError::Builder("etherscan url".to_string()))?,
cache,
Expand Down

0 comments on commit fae108b

Please sign in to comment.