2022-09-14 05:11:48 +03:00
|
|
|
use anyhow::Context;
|
2022-08-24 02:56:47 +03:00
|
|
|
use derive_more::From;
|
2022-09-14 05:11:48 +03:00
|
|
|
use std::time::Duration;
|
2022-08-24 02:56:47 +03:00
|
|
|
|
|
|
|
/// Use HTTP and WS providers.
|
|
|
|
// TODO: instead of an enum, I tried to use Box<dyn Provider>, but hit <https://github.com/gakonst/ethers-rs/issues/592>
|
|
|
|
#[derive(From)]
|
|
|
|
pub enum Web3Provider {
|
|
|
|
Http(ethers::providers::Provider<ethers::providers::Http>),
|
|
|
|
Ws(ethers::providers::Provider<ethers::providers::Ws>),
|
2022-11-23 01:45:22 +03:00
|
|
|
// TODO: only include this for tests.
|
|
|
|
Mock,
|
2022-08-24 02:56:47 +03:00
|
|
|
}
|
2022-08-24 03:11:49 +03:00
|
|
|
|
|
|
|
impl Web3Provider {
|
2022-09-06 19:49:07 +03:00
|
|
|
pub fn ready(&self) -> bool {
|
|
|
|
match self {
|
2022-11-23 01:45:22 +03:00
|
|
|
Self::Mock => true,
|
2022-09-06 19:49:07 +03:00
|
|
|
Self::Http(_) => true,
|
|
|
|
Self::Ws(provider) => provider.as_ref().ready(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 03:11:49 +03:00
|
|
|
pub async fn from_str(
|
|
|
|
url_str: &str,
|
|
|
|
http_client: Option<reqwest::Client>,
|
|
|
|
) -> anyhow::Result<Self> {
|
|
|
|
let provider = if url_str.starts_with("http") {
|
|
|
|
let url: url::Url = url_str.parse()?;
|
|
|
|
|
2022-09-14 05:11:48 +03:00
|
|
|
let http_client = http_client.context("no http_client")?;
|
2022-08-24 03:11:49 +03:00
|
|
|
|
|
|
|
let provider = ethers::providers::Http::new_with_client(url, http_client);
|
|
|
|
|
|
|
|
// TODO: dry this up (needs https://github.com/gakonst/ethers-rs/issues/592)
|
|
|
|
// TODO: i don't think this interval matters for our uses, but we should probably set it to like `block time / 2`
|
|
|
|
ethers::providers::Provider::new(provider)
|
2023-01-03 22:37:42 +03:00
|
|
|
.interval(Duration::from_secs(12))
|
2022-08-24 03:11:49 +03:00
|
|
|
.into()
|
|
|
|
} else if url_str.starts_with("ws") {
|
2022-11-12 11:24:32 +03:00
|
|
|
let provider = ethers::providers::Ws::connect(url_str).await?;
|
2022-08-24 03:11:49 +03:00
|
|
|
|
|
|
|
// TODO: dry this up (needs https://github.com/gakonst/ethers-rs/issues/592)
|
|
|
|
// TODO: i don't think this interval matters
|
|
|
|
ethers::providers::Provider::new(provider).into()
|
|
|
|
} else {
|
|
|
|
return Err(anyhow::anyhow!("only http and ws servers are supported"));
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(provider)
|
|
|
|
}
|
|
|
|
}
|