2023-05-11 23:07:31 +03:00
|
|
|
use ethers::providers::{Authorization, ConnectionDetails};
|
2023-05-24 00:40:34 +03:00
|
|
|
use std::time::Duration;
|
2023-05-11 23:07:31 +03:00
|
|
|
use url::Url;
|
2022-08-24 02:56:47 +03:00
|
|
|
|
2023-02-12 12:22:53 +03:00
|
|
|
// TODO: our own structs for these that handle streaming large responses
|
2023-05-23 01:32:15 +03:00
|
|
|
pub type EthersHttpProvider = ethers::providers::Provider<ethers::providers::Http>;
|
|
|
|
pub type EthersWsProvider = ethers::providers::Provider<ethers::providers::Ws>;
|
2023-02-12 12:22:53 +03:00
|
|
|
|
2023-05-24 00:40:34 +03:00
|
|
|
pub fn extract_auth(url: &mut Url) -> Option<Authorization> {
|
2023-05-23 01:32:15 +03:00
|
|
|
if let Some(pass) = url.password().map(|x| x.to_string()) {
|
|
|
|
// to_string is needed because we are going to remove these items from the url
|
|
|
|
let user = url.username().to_string();
|
2022-08-24 03:11:49 +03:00
|
|
|
|
2023-05-23 01:32:15 +03:00
|
|
|
// clear username and password from the url
|
2023-05-24 00:40:34 +03:00
|
|
|
url.set_username("")
|
2023-05-23 01:32:15 +03:00
|
|
|
.expect("unable to clear username on websocket");
|
2023-05-24 00:40:34 +03:00
|
|
|
url.set_password(None)
|
2023-05-23 01:32:15 +03:00
|
|
|
.expect("unable to clear password on websocket");
|
2022-09-06 19:49:07 +03:00
|
|
|
|
2023-05-23 01:32:15 +03:00
|
|
|
// keep them
|
|
|
|
Some(Authorization::basic(user, pass))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2022-08-24 03:11:49 +03:00
|
|
|
|
2023-05-23 01:32:15 +03:00
|
|
|
/// Note, if the http url has an authority the http_client param is ignored and a dedicated http_client will be used
|
|
|
|
/// TODO: take a reqwest::Client or a reqwest::ClientBuilder. that way we can do things like set compression even when auth is set
|
|
|
|
pub fn connect_http(
|
2023-05-24 00:40:34 +03:00
|
|
|
mut url: Url,
|
2023-05-23 01:32:15 +03:00
|
|
|
http_client: Option<reqwest::Client>,
|
2023-05-24 00:40:34 +03:00
|
|
|
interval: Duration,
|
2023-05-23 01:32:15 +03:00
|
|
|
) -> anyhow::Result<EthersHttpProvider> {
|
|
|
|
let auth = extract_auth(&mut url);
|
2023-05-11 23:07:31 +03:00
|
|
|
|
2023-05-24 00:40:34 +03:00
|
|
|
let mut provider = if url.scheme().starts_with("http") {
|
2023-05-23 01:32:15 +03:00
|
|
|
let provider = if let Some(auth) = auth {
|
2023-05-24 00:40:34 +03:00
|
|
|
ethers::providers::Http::new_with_auth(url, auth)?
|
2023-05-23 01:32:15 +03:00
|
|
|
} else if let Some(http_client) = http_client {
|
2023-05-24 00:40:34 +03:00
|
|
|
ethers::providers::Http::new_with_client(url, http_client)
|
2023-05-11 23:07:31 +03:00
|
|
|
} else {
|
2023-05-24 00:40:34 +03:00
|
|
|
ethers::providers::Http::new(url)
|
2023-05-11 23:07:31 +03:00
|
|
|
};
|
|
|
|
|
2023-05-23 01:32:15 +03:00
|
|
|
// TODO: i don't think this interval matters for our uses, but we should probably set it to like `block time / 2`
|
2023-05-24 00:40:34 +03:00
|
|
|
ethers::providers::Provider::new(provider).interval(Duration::from_secs(2))
|
2023-05-23 01:32:15 +03:00
|
|
|
} else {
|
2023-05-24 00:40:34 +03:00
|
|
|
return Err(anyhow::anyhow!(
|
|
|
|
"only http servers are supported. cannot use {}",
|
|
|
|
url
|
|
|
|
));
|
2023-05-23 01:32:15 +03:00
|
|
|
};
|
|
|
|
|
2023-05-24 00:40:34 +03:00
|
|
|
provider.set_interval(interval);
|
|
|
|
|
2023-05-23 01:32:15 +03:00
|
|
|
Ok(provider)
|
|
|
|
}
|
2022-08-24 03:11:49 +03:00
|
|
|
|
2023-05-24 00:40:34 +03:00
|
|
|
pub async fn connect_ws(mut url: Url, reconnects: usize) -> anyhow::Result<EthersWsProvider> {
|
2023-05-23 01:32:15 +03:00
|
|
|
let auth = extract_auth(&mut url);
|
2023-05-11 23:07:31 +03:00
|
|
|
|
2023-05-23 01:32:15 +03:00
|
|
|
let provider = if url.scheme().starts_with("ws") {
|
|
|
|
let provider = if auth.is_some() {
|
|
|
|
let connection_details = ConnectionDetails::new(url.as_str(), auth);
|
2022-08-24 03:11:49 +03:00
|
|
|
|
2023-05-23 01:32:15 +03:00
|
|
|
// if they error, we do our own reconnection with backoff
|
|
|
|
ethers::providers::Ws::connect_with_reconnects(connection_details, reconnects).await?
|
2022-08-24 03:11:49 +03:00
|
|
|
} else {
|
2023-05-23 01:32:15 +03:00
|
|
|
ethers::providers::Ws::connect_with_reconnects(url.as_str(), reconnects).await?
|
2022-08-24 03:11:49 +03:00
|
|
|
};
|
|
|
|
|
2023-05-23 01:32:15 +03:00
|
|
|
// TODO: dry this up (needs https://github.com/gakonst/ethers-rs/issues/592)
|
|
|
|
// TODO: i don't think this interval matters
|
2023-05-24 00:40:34 +03:00
|
|
|
ethers::providers::Provider::new(provider)
|
2023-05-23 01:32:15 +03:00
|
|
|
} else {
|
|
|
|
return Err(anyhow::anyhow!("ws servers are supported"));
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(provider)
|
2022-08-24 03:11:49 +03:00
|
|
|
}
|