This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 795
/
lib.rs
521 lines (462 loc) · 17.2 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#![doc = include_str!("../README.md")]
#![deny(unsafe_code, rustdoc::broken_intra_doc_links)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
use crate::errors::{
is_blocked_by_cloudflare_response, is_cloudflare_security_challenge,
is_security_challenge_prompt,
};
use contract::ContractMetadata;
use errors::EtherscanError;
use ethers_core::{
abi::{Abi, Address},
types::{Chain, H256},
};
use reqwest::{header, IntoUrl, Url};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{
borrow::Cow,
io::Write,
path::PathBuf,
time::{Duration, SystemTime, UNIX_EPOCH},
};
use tracing::{error, trace};
pub mod account;
pub mod blocks;
pub mod contract;
pub mod errors;
pub mod gas;
pub mod source_tree;
pub mod stats;
mod transaction;
pub mod utils;
pub mod verify;
pub(crate) type Result<T, E = EtherscanError> = std::result::Result<T, E>;
/// The Etherscan.io API client.
#[derive(Clone, Debug)]
pub struct Client {
/// Client that executes HTTP requests
client: reqwest::Client,
/// Etherscan API key
api_key: Option<String>,
/// Etherscan API endpoint like <https://api(-chain).etherscan.io/api>
etherscan_api_url: Url,
/// Etherscan base endpoint like <https://etherscan.io>
etherscan_url: Url,
/// Path to where ABI files should be cached
cache: Option<Cache>,
}
impl Client {
/// Creates a `ClientBuilder` to configure a `Client`.
///
/// This is the same as `ClientBuilder::default()`.
///
/// # Example
///
/// ```rust
/// use ethers_core::types::Chain;
/// use ethers_etherscan::Client;
/// let client = Client::builder().with_api_key("<API KEY>").chain(Chain::Mainnet).unwrap().build().unwrap();
/// ```
pub fn builder() -> ClientBuilder {
ClientBuilder::default()
}
/// Creates a new instance that caches etherscan requests
pub fn new_cached(
chain: Chain,
api_key: impl Into<String>,
cache_root: Option<PathBuf>,
cache_ttl: Duration,
) -> Result<Self> {
let mut this = Self::new(chain, api_key)?;
this.cache = cache_root.map(|root| Cache::new(root, cache_ttl));
Ok(this)
}
/// Create a new client with the correct endpoints based on the chain and provided API key
pub fn new(chain: Chain, api_key: impl Into<String>) -> Result<Self> {
Client::builder().with_api_key(api_key).chain(chain)?.build()
}
/// Create a new client with the correct endpoints based on the chain and API key
/// from the default environment variable defined in [`Chain`].
pub fn new_from_env(chain: Chain) -> Result<Self> {
let api_key = match chain {
// Extra aliases
Chain::Fantom | Chain::FantomTestnet => std::env::var("FMTSCAN_API_KEY")
.or_else(|_| std::env::var("FANTOMSCAN_API_KEY"))
.map_err(Into::into),
// Backwards compatibility, ideally these should return an error.
Chain::Chiado |
Chain::Sepolia |
Chain::Rsk |
Chain::Sokol |
Chain::Poa |
Chain::Oasis |
Chain::Emerald |
Chain::EmeraldTestnet |
Chain::Evmos |
Chain::EvmosTestnet => Ok(String::new()),
Chain::AnvilHardhat | Chain::Dev => Err(EtherscanError::LocalNetworksNotSupported),
_ => chain
.etherscan_api_key_name()
.ok_or_else(|| EtherscanError::ChainNotSupported(chain))
.and_then(|key_name| std::env::var(key_name).map_err(Into::into)),
}?;
Self::new(chain, api_key)
}
/// Create a new client with the correct endpoints based on the chain and API key
/// from the default environment variable defined in [`Chain`].
///
/// If the environment variable is not set, create a new client without it.
pub fn new_from_opt_env(chain: Chain) -> Result<Self> {
match Self::new_from_env(chain) {
Ok(client) => Ok(client),
Err(EtherscanError::EnvVarNotFound(_)) => {
Self::builder().chain(chain).and_then(|c| c.build())
}
Err(e) => Err(e),
}
}
/// Sets the root to the cache dir and the ttl to use
pub fn set_cache(&mut self, root: impl Into<PathBuf>, ttl: Duration) -> &mut Self {
self.cache = Some(Cache { root: root.into(), ttl });
self
}
pub fn etherscan_api_url(&self) -> &Url {
&self.etherscan_api_url
}
pub fn etherscan_url(&self) -> &Url {
&self.etherscan_url
}
/// Return the URL for the given block number
pub fn block_url(&self, block: u64) -> String {
format!("{}block/{block}", self.etherscan_url)
}
/// Return the URL for the given address
pub fn address_url(&self, address: Address) -> String {
format!("{}address/{address:?}", self.etherscan_url)
}
/// Return the URL for the given transaction hash
pub fn transaction_url(&self, tx_hash: H256) -> String {
format!("{}tx/{tx_hash:?}", self.etherscan_url)
}
/// Return the URL for the given token hash
pub fn token_url(&self, token_hash: Address) -> String {
format!("{}token/{token_hash:?}", self.etherscan_url)
}
/// Execute an GET request with parameters.
async fn get_json<T: DeserializeOwned, Q: Serialize>(&self, query: &Q) -> Result<Response<T>> {
let res = self.get(query).await?;
self.sanitize_response(res)
}
/// Execute a GET request with parameters, without sanity checking the response.
async fn get<Q: Serialize>(&self, query: &Q) -> Result<String> {
trace!(target: "etherscan", "GET {}", self.etherscan_api_url);
let response = self
.client
.get(self.etherscan_api_url.clone())
.header(header::ACCEPT, "application/json")
.query(query)
.send()
.await?
.text()
.await?;
Ok(response)
}
/// Execute a POST request with a form.
async fn post_form<T: DeserializeOwned, F: Serialize>(&self, form: &F) -> Result<Response<T>> {
let res = self.post(form).await?;
self.sanitize_response(res)
}
/// Execute a POST request with a form, without sanity checking the response.
async fn post<F: Serialize>(&self, form: &F) -> Result<String> {
trace!(target: "etherscan", "POST {}", self.etherscan_api_url);
let response = self
.client
.post(self.etherscan_api_url.clone())
.form(form)
.send()
.await?
.text()
.await?;
Ok(response)
}
/// Perform sanity checks on a response and deserialize it into a [Response].
fn sanitize_response<T: DeserializeOwned>(&self, res: impl AsRef<str>) -> Result<Response<T>> {
let res = res.as_ref();
let res: ResponseData<T> = serde_json::from_str(res).map_err(|err| {
error!(target: "etherscan", ?res, "Failed to deserialize response: {}", err);
if res == "Page not found" {
EtherscanError::PageNotFound
} else if is_blocked_by_cloudflare_response(res) {
EtherscanError::BlockedByCloudflare
} else if is_cloudflare_security_challenge(res) {
EtherscanError::CloudFlareSecurityChallenge
} else if is_security_challenge_prompt(res) {
EtherscanError::SecurityChallenge(self.etherscan_api_url.clone())
} else {
EtherscanError::Serde(err)
}
})?;
match res {
ResponseData::Error { result, message, status } => {
if let Some(ref result) = result {
if result.starts_with("Max rate limit reached") {
return Err(EtherscanError::RateLimitExceeded)
} else if result.to_lowercase() == "invalid api key" {
return Err(EtherscanError::InvalidApiKey)
}
}
Err(EtherscanError::ErrorResponse { status, message, result })
}
ResponseData::Success(res) => Ok(res),
}
}
fn create_query<T: Serialize>(
&self,
module: &'static str,
action: &'static str,
other: T,
) -> Query<T> {
Query {
apikey: self.api_key.as_deref().map(Cow::Borrowed),
module: Cow::Borrowed(module),
action: Cow::Borrowed(action),
other,
}
}
}
#[derive(Clone, Debug, Default)]
pub struct ClientBuilder {
/// Client that executes HTTP requests
client: Option<reqwest::Client>,
/// Etherscan API key
api_key: Option<String>,
/// Etherscan API endpoint like <https://api(-chain).etherscan.io/api>
etherscan_api_url: Option<Url>,
/// Etherscan base endpoint like <https://etherscan.io>
etherscan_url: Option<Url>,
/// Path to where ABI files should be cached
cache: Option<Cache>,
}
// === impl ClientBuilder ===
impl ClientBuilder {
/// Configures the etherscan url and api url for the given chain
///
/// # Errors
///
/// Fails if the chain is not supported by etherscan
pub fn chain(self, chain: Chain) -> Result<Self> {
fn urls(
api: impl IntoUrl,
url: impl IntoUrl,
) -> (reqwest::Result<Url>, reqwest::Result<Url>) {
(api.into_url(), url.into_url())
}
let (etherscan_api_url, etherscan_url) = chain
.etherscan_urls()
.map(|(api, base)| urls(api, base))
.ok_or_else(|| EtherscanError::ChainNotSupported(chain))?;
self.with_api_url(etherscan_api_url?)?.with_url(etherscan_url?)
}
/// Configures the etherscan url
///
/// # Errors
///
/// Fails if the `etherscan_url` is not a valid `Url`
pub fn with_url(mut self, etherscan_url: impl IntoUrl) -> Result<Self> {
self.etherscan_url = Some(ensure_url(etherscan_url)?);
Ok(self)
}
/// Configures the `reqwest::Client`
pub fn with_client(mut self, client: reqwest::Client) -> Self {
self.client = Some(client);
self
}
/// Configures the etherscan api url
///
/// # Errors
///
/// Fails if the `etherscan_api_url` is not a valid `Url`
pub fn with_api_url(mut self, etherscan_api_url: impl IntoUrl) -> Result<Self> {
self.etherscan_api_url = Some(ensure_url(etherscan_api_url)?);
Ok(self)
}
/// Configures the etherscan api key
pub fn with_api_key(mut self, api_key: impl Into<String>) -> Self {
self.api_key = Some(api_key.into()).filter(|s| !s.is_empty());
self
}
/// Configures cache for etherscan request
pub fn with_cache(mut self, cache_root: Option<PathBuf>, cache_ttl: Duration) -> Self {
self.cache = cache_root.map(|root| Cache::new(root, cache_ttl));
self
}
/// Returns a Client that uses this ClientBuilder configuration.
///
/// # Errors
///
/// If the following required fields are missing:
/// - `etherscan_api_url`
/// - `etherscan_url`
pub fn build(self) -> Result<Client> {
let ClientBuilder { client, api_key, etherscan_api_url, etherscan_url, cache } = self;
let client = Client {
client: client.unwrap_or_default(),
api_key,
etherscan_api_url: etherscan_api_url
.ok_or_else(|| EtherscanError::Builder("etherscan api url".to_string()))?,
etherscan_url: etherscan_url
.ok_or_else(|| EtherscanError::Builder("etherscan url".to_string()))?,
cache,
};
Ok(client)
}
}
/// A wrapper around an Etherscan cache object with an expiry
#[derive(Clone, Debug, Deserialize, Serialize)]
struct CacheEnvelope<T> {
expiry: u64,
data: T,
}
/// Simple cache for etherscan requests
#[derive(Clone, Debug)]
struct Cache {
root: PathBuf,
ttl: Duration,
}
impl Cache {
fn new(root: PathBuf, ttl: Duration) -> Self {
Self { root, ttl }
}
fn get_abi(&self, address: Address) -> Option<Option<ethers_core::abi::Abi>> {
self.get("abi", address)
}
fn set_abi(&self, address: Address, abi: Option<&Abi>) {
self.set("abi", address, abi)
}
fn get_source(&self, address: Address) -> Option<Option<ContractMetadata>> {
self.get("sources", address)
}
fn set_source(&self, address: Address, source: Option<&ContractMetadata>) {
self.set("sources", address, source)
}
fn set<T: Serialize>(&self, prefix: &str, address: Address, item: T) {
let path = self.root.join(prefix).join(format!("{address:?}.json"));
let writer = std::fs::File::create(path).ok().map(std::io::BufWriter::new);
if let Some(mut writer) = writer {
let _ = serde_json::to_writer(
&mut writer,
&CacheEnvelope {
expiry: SystemTime::now()
.checked_add(self.ttl)
.expect("cache ttl overflowed")
.duration_since(UNIX_EPOCH)
.expect("system time is before unix epoch")
.as_secs(),
data: item,
},
);
let _ = writer.flush();
}
}
fn get<T: DeserializeOwned>(&self, prefix: &str, address: Address) -> Option<T> {
let path = self.root.join(prefix).join(format!("{address:?}.json"));
let Ok(contents) = std::fs::read_to_string(path) else { return None };
let Ok(inner) = serde_json::from_str::<CacheEnvelope<T>>(&contents) else { return None };
// If this does not return None then we have passed the expiry
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("system time is before unix epoch")
.checked_sub(Duration::from_secs(inner.expiry))
.map(|_| inner.data)
}
}
/// The API response type
#[derive(Debug, Clone, Deserialize)]
pub struct Response<T> {
pub status: String,
pub message: String,
pub result: T,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum ResponseData<T> {
Success(Response<T>),
Error { status: String, message: String, result: Option<String> },
}
/// The type that gets serialized as query
#[derive(Clone, Debug, Serialize)]
struct Query<'a, T: Serialize> {
#[serde(skip_serializing_if = "Option::is_none")]
apikey: Option<Cow<'a, str>>,
module: Cow<'a, str>,
action: Cow<'a, str>,
#[serde(flatten)]
other: T,
}
/// Ensures that the url is well formatted to be used by the Client's functions that join paths.
fn ensure_url(url: impl IntoUrl) -> std::result::Result<Url, reqwest::Error> {
let url_str = url.as_str();
// ensure URL ends with `/`
if url_str.ends_with('/') {
url.into_url()
} else {
into_url(format!("{url_str}/"))
}
}
/// This is a hack to work around `IntoUrl`'s sealed private functions, which can't be called
/// normally.
#[inline]
fn into_url(url: impl IntoUrl) -> std::result::Result<Url, reqwest::Error> {
url.into_url()
}
#[cfg(test)]
mod tests {
use crate::{Client, EtherscanError, ResponseData};
use ethers_core::types::{Address, Chain, H256};
// <https://github.com/foundry-rs/foundry/issues/4406>
#[test]
fn can_parse_block_scout_err() {
let err = "{\"message\":\"Something went wrong.\",\"result\":null,\"status\":\"0\"}";
let resp: ResponseData<Address> = serde_json::from_str(err).unwrap();
assert!(matches!(resp, ResponseData::Error { .. }));
}
#[test]
fn test_api_paths() {
let client = Client::new(Chain::Goerli, "").unwrap();
assert_eq!(client.etherscan_api_url.as_str(), "https://api-goerli.etherscan.io/api/");
assert_eq!(client.block_url(100), "https://goerli.etherscan.io/block/100");
}
#[test]
fn stringifies_block_url() {
let etherscan = Client::new(Chain::Mainnet, "").unwrap();
let block: u64 = 1;
let block_url: String = etherscan.block_url(block);
assert_eq!(block_url, format!("https://etherscan.io/block/{block}"));
}
#[test]
fn stringifies_address_url() {
let etherscan = Client::new(Chain::Mainnet, "").unwrap();
let addr: Address = Address::zero();
let address_url: String = etherscan.address_url(addr);
assert_eq!(address_url, format!("https://etherscan.io/address/{addr:?}"));
}
#[test]
fn stringifies_transaction_url() {
let etherscan = Client::new(Chain::Mainnet, "").unwrap();
let tx_hash = H256::zero();
let tx_url: String = etherscan.transaction_url(tx_hash);
assert_eq!(tx_url, format!("https://etherscan.io/tx/{tx_hash:?}"));
}
#[test]
fn stringifies_token_url() {
let etherscan = Client::new(Chain::Mainnet, "").unwrap();
let token_hash = Address::zero();
let token_url: String = etherscan.token_url(token_hash);
assert_eq!(token_url, format!("https://etherscan.io/token/{token_hash:?}"));
}
#[test]
fn local_networks_not_supported() {
let err = Client::new_from_env(Chain::Dev).unwrap_err();
assert!(matches!(err, EtherscanError::LocalNetworksNotSupported));
}
}