Skip to content

Commit

Permalink
add: chain_id param in etherscan query req (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-atreya authored Mar 29, 2024
1 parent 9f01048 commit b669d44
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub struct Client {
etherscan_url: Url,
/// Path to where ABI files should be cached
cache: Option<Cache>,
/// Chain ID
chain_id: Option<u64>,
}

impl Client {
Expand Down Expand Up @@ -263,6 +265,7 @@ impl Client {
apikey: self.api_key.as_deref().map(Cow::Borrowed),
module: Cow::Borrowed(module),
action: Cow::Borrowed(action),
chain_id: self.chain_id,
other,
}
}
Expand All @@ -280,13 +283,17 @@ pub struct ClientBuilder {
etherscan_url: Option<Url>,
/// Path to where ABI files should be cached
cache: Option<Cache>,
/// Chain ID
chain_id: Option<u64>,
}

// === impl ClientBuilder ===

impl ClientBuilder {
/// Configures the etherscan url and api url for the given chain
///
/// Note: This method also sets the chain_id for etherscan multichain verification: <https://docs.etherscan.io/contract-verification/multichain-verification>
///
/// # Errors
///
/// Fails if the chain is not supported by etherscan
Expand All @@ -302,7 +309,8 @@ impl ClientBuilder {
.etherscan_urls()
.map(urls)
.ok_or_else(|| EtherscanError::ChainNotSupported(chain))?;
self.with_api_url(etherscan_api_url?)?.with_url(etherscan_url?)

self.with_chain_id(chain).with_api_url(etherscan_api_url?)?.with_url(etherscan_url?)
}

/// Configures the etherscan url
Expand Down Expand Up @@ -343,6 +351,17 @@ impl ClientBuilder {
self
}

/// Configures the chain id for etherscan verification: <https://docs.etherscan.io/contract-verification/multichain-verification>
pub fn with_chain_id(mut self, chain: Chain) -> Self {
self.chain_id = Some(chain.id());
self
}

/// Returns the chain the client is built on.
pub fn get_chain(&self) -> Option<Chain> {
self.chain_id.map(Chain::from_id)
}

/// Returns a Client that uses this ClientBuilder configuration.
///
/// # Errors
Expand All @@ -351,7 +370,8 @@ impl ClientBuilder {
/// - `etherscan_api_url`
/// - `etherscan_url`
pub fn build(self) -> Result<Client> {
let ClientBuilder { client, api_key, etherscan_api_url, etherscan_url, cache } = self;
let ClientBuilder { client, api_key, etherscan_api_url, etherscan_url, cache, chain_id } =
self;

let client = Client {
client: client.unwrap_or_default(),
Expand All @@ -361,6 +381,7 @@ impl ClientBuilder {
etherscan_url: etherscan_url
.ok_or_else(|| EtherscanError::Builder("etherscan url".to_string()))?,
cache,
chain_id,
};
Ok(client)
}
Expand Down Expand Up @@ -460,6 +481,8 @@ struct Query<'a, T: Serialize> {
apikey: Option<Cow<'a, str>>,
module: Cow<'a, str>,
action: Cow<'a, str>,
#[serde(rename = "chainId", skip_serializing_if = "Option::is_none")]
chain_id: Option<u64>,
#[serde(flatten)]
other: T,
}
Expand Down

0 comments on commit b669d44

Please sign in to comment.