From 50ef8a78f96217546f98a878b7c30e90c3baf65c Mon Sep 17 00:00:00 2001 From: Yiannis M Date: Sun, 22 Jan 2023 23:08:33 +0200 Subject: [PATCH] feat(etherscan): Allow `ClientBuilder` to create `Client` without API key (#2067) Etherscan allows 1 request per 5 seconds if the API key is not defined --- ethers-etherscan/src/lib.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/ethers-etherscan/src/lib.rs b/ethers-etherscan/src/lib.rs index 399d7f80..a77128f3 100644 --- a/ethers-etherscan/src/lib.rs +++ b/ethers-etherscan/src/lib.rs @@ -34,7 +34,7 @@ pub struct Client { /// Client that executes HTTP requests client: reqwest::Client, /// Etherscan API key - api_key: String, + api_key: Option, /// Etherscan API endpoint like etherscan_api_url: Url, /// Etherscan base endpoint like @@ -235,7 +235,7 @@ impl Client { other: T, ) -> Query { Query { - apikey: Cow::Borrowed(&self.api_key), + apikey: self.api_key.as_deref().map(Cow::Borrowed), module: Cow::Borrowed(module), action: Cow::Borrowed(action), other, @@ -307,7 +307,7 @@ impl ClientBuilder { /// Configures the etherscan api key pub fn with_api_key(mut self, api_key: impl Into) -> Self { - self.api_key = Some(api_key.into()); + self.api_key = Some(api_key.into()).filter(|s| !s.is_empty()); self } @@ -321,7 +321,6 @@ impl ClientBuilder { /// /// # Errors /// if required fields are missing: - /// - `api_key` /// - `etherscan_api_url` /// - `etherscan_url` pub fn build(self) -> Result { @@ -329,8 +328,7 @@ impl ClientBuilder { let client = Client { client: client.unwrap_or_default(), - api_key: api_key - .ok_or_else(|| EtherscanError::Builder("etherscan api key".to_string()))?, + api_key, etherscan_api_url: etherscan_api_url .ok_or_else(|| EtherscanError::Builder("etherscan api url".to_string()))?, etherscan_url: etherscan_url @@ -434,7 +432,8 @@ pub enum ResponseData { /// The type that gets serialized as query #[derive(Clone, Debug, Serialize)] struct Query<'a, T: Serialize> { - apikey: Cow<'a, str>, + #[serde(skip_serializing_if = "Option::is_none")] + apikey: Option>, module: Cow<'a, str>, action: Cow<'a, str>, #[serde(flatten)]