Skip to content

Commit be348b7

Browse files
feat(cast): support websockets
1 parent 06a17bf commit be348b7

File tree

8 files changed

+257
-40
lines changed

8 files changed

+257
-40
lines changed

Cargo.lock

Lines changed: 13 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cast/bin/cmd/call.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ use foundry_cli::{
99
opts::{EthereumOpts, TransactionOpts},
1010
utils::{self, handle_traces, parse_ether_value, TraceResult},
1111
};
12+
use foundry_common::runtime_client::RuntimeClient;
1213
use foundry_config::{find_project_root_path, Config};
1314
use foundry_evm::{executor::opts::EvmOpts, trace::TracingExecutor};
1415
use std::str::FromStr;
1516

16-
type Provider =
17-
ethers::providers::Provider<ethers::providers::RetryClient<ethers::providers::Http>>;
17+
type Provider = ethers::providers::Provider<RuntimeClient>;
1818

1919
/// CLI arguments for `cast call`.
2020
#[derive(Debug, Parser)]

crates/common/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ tempfile = "3"
3333

3434
# misc
3535
auto_impl = "1.1.0"
36+
async-trait = "0.1"
3637
serde = "1"
3738
serde_json = "1"
3839
thiserror = "1"
@@ -43,6 +44,8 @@ once_cell = "1"
4344
dunce = "1"
4445
regex = "1"
4546
globset = "0.4"
47+
tokio = "1"
48+
url = "2"
4649

4750
[dev-dependencies]
4851
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }

crates/common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub mod fmt;
1414
pub mod fs;
1515
pub mod glob;
1616
pub mod provider;
17+
pub mod runtime_client;
1718
pub mod selectors;
1819
pub mod shell;
1920
pub mod term;

crates/common/src/provider.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
//! Commonly used helpers to construct `Provider`s
22
3-
use crate::{ALCHEMY_FREE_TIER_CUPS, REQUEST_TIMEOUT};
3+
use crate::{runtime_client::RuntimeClient, ALCHEMY_FREE_TIER_CUPS, REQUEST_TIMEOUT};
44
use ethers_core::types::{Chain, U256};
55
use ethers_middleware::gas_oracle::{GasCategory, GasOracle, Polygon};
6-
use ethers_providers::{
7-
is_local_endpoint, Http, HttpRateLimitRetryPolicy, Middleware, Provider, RetryClient,
8-
RetryClientBuilder, DEFAULT_LOCAL_POLL_INTERVAL,
9-
};
6+
use ethers_providers::{is_local_endpoint, Middleware, Provider, DEFAULT_LOCAL_POLL_INTERVAL};
107
use eyre::WrapErr;
118
use reqwest::{IntoUrl, Url};
129
use std::{borrow::Cow, time::Duration};
10+
use url::ParseError;
1311

1412
/// Helper type alias for a retry provider
15-
pub type RetryProvider = Provider<RetryClient<Http>>;
13+
pub type RetryProvider = Provider<RuntimeClient>;
1614

1715
/// Helper type alias for a rpc url
1816
pub type RpcUrl = String;
@@ -67,8 +65,15 @@ impl ProviderBuilder {
6765
return Self::new(format!("http://{url_str}"))
6866
}
6967
let err = format!("Invalid provider url: {url_str}");
68+
let url = Url::parse(url_str)
69+
.and_then(|url| match url.scheme() {
70+
"http" | "https" | "wss" | "ws" | "file" => Ok(url),
71+
_ => Err(ParseError::EmptyHost),
72+
})
73+
.wrap_err(err);
74+
7075
Self {
71-
url: url.into_url().wrap_err(err),
76+
url,
7277
chain: Chain::Mainnet,
7378
max_retry: 100,
7479
timeout_retry: 5,
@@ -166,20 +171,16 @@ impl ProviderBuilder {
166171
} = self;
167172
let url = url?;
168173

169-
let client = reqwest::Client::builder().timeout(timeout).build()?;
170-
let is_local = is_local_endpoint(url.as_str());
171-
172-
let provider = Http::new_with_client(url, client);
174+
let mut provider = Provider::new(RuntimeClient::new(
175+
url.clone(),
176+
max_retry,
177+
timeout_retry,
178+
initial_backoff,
179+
timeout,
180+
compute_units_per_second,
181+
));
173182

174-
#[allow(clippy::box_default)]
175-
let mut provider = Provider::new(
176-
RetryClientBuilder::default()
177-
.initial_backoff(Duration::from_millis(initial_backoff))
178-
.rate_limit_retries(max_retry)
179-
.timeout_retries(timeout_retry)
180-
.compute_units_per_second(compute_units_per_second)
181-
.build(provider, Box::new(HttpRateLimitRetryPolicy)),
182-
);
183+
let is_local = is_local_endpoint(url.as_str());
183184

184185
if is_local {
185186
provider = provider.interval(DEFAULT_LOCAL_POLL_INTERVAL);

0 commit comments

Comments
 (0)