Skip to content

Commit

Permalink
Blockscout API key (#2788)
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz authored Jun 24, 2024
1 parent 27d3f96 commit c11ee2a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions crates/shared/src/bad_token/token_owner_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ pub struct Arguments {
#[clap(long, env)]
pub blockscout_api_url: Option<Url>,

/// The blockscout API key.
#[clap(long, env)]
pub blockscout_api_key: Option<String>,

/// Override the default ethplorer API url
#[clap(long, env)]
pub ethplorer_api_url: Option<Url>,
Expand Down Expand Up @@ -169,6 +173,7 @@ impl Display for Arguments {
token_owner_finders,
token_owner_finder_uniswap_v3_fee_values,
blockscout_api_url,
blockscout_api_key,
ethplorer_api_url,
ethplorer_api_key,
token_owner_finder_rate_limiter,
Expand All @@ -184,6 +189,7 @@ impl Display for Arguments {
token_owner_finder_uniswap_v3_fee_values
)?;
display_option(f, "blockscout_api_url", blockscout_api_url)?;
display_secret_option(f, "blockscout_api_key", blockscout_api_key)?;
display_option(f, "ethplorer_api_url", ethplorer_api_url)?;
display_secret_option(f, "ethplorer_api_key", ethplorer_api_key)?;
display_option(
Expand Down Expand Up @@ -256,6 +262,9 @@ pub async fn init(
if let Some(base_url) = args.blockscout_api_url.clone() {
blockscout.with_base_url(base_url);
}
if let Some(api_key) = args.blockscout_api_key.clone() {
blockscout.with_api_key(api_key);
}
if let Some(strategy) = args.token_owner_finder_rate_limiter.clone() {
blockscout.with_rate_limiter(strategy);
}
Expand Down
12 changes: 12 additions & 0 deletions crates/shared/src/bad_token/token_owner_finder/blockscout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use {
pub struct BlockscoutTokenOwnerFinder {
client: Client,
base: Url,
api_key: Option<String>,
rate_limiter: Option<RateLimiter>,
}

Expand All @@ -29,6 +30,7 @@ impl BlockscoutTokenOwnerFinder {
Ok(Self {
client,
base: Url::parse(base_url)?,
api_key: None,
rate_limiter: None,
})
}
Expand All @@ -38,6 +40,11 @@ impl BlockscoutTokenOwnerFinder {
self
}

pub fn with_api_key(&mut self, api_key: String) -> &mut Self {
self.api_key = Some(api_key);
self
}

pub fn with_rate_limiter(&mut self, strategy: Strategy) -> &mut Self {
self.rate_limiter = Some(RateLimiter::from_strategy(
strategy,
Expand All @@ -53,8 +60,13 @@ impl BlockscoutTokenOwnerFinder {
.append_pair("action", "getTokenHolders")
.append_pair("contractaddress", &format!("{token:#x}"));

// Don't log the API key!
tracing::debug!(%url, "Querying Blockscout API");

if let Some(api_key) = &self.api_key {
url.query_pairs_mut().append_pair("apikey", api_key);
}

let request = self.client.get(url).send();
let response = match &self.rate_limiter {
Some(limiter) => limiter.execute(request, back_off::on_http_429).await??,
Expand Down

0 comments on commit c11ee2a

Please sign in to comment.