Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cast): add --rpc-timeout option #9044

Merged
merged 14 commits into from
Oct 24, 2024
Merged
20 changes: 20 additions & 0 deletions crates/cli/src/opts/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ pub struct RpcOpts {
/// "0x6bb38c26db65749ab6e472080a3d20a2f35776494e72016d1e339593f21c59bc"]'
#[arg(long, env = "ETH_RPC_JWT_SECRET")]
pub jwt_secret: Option<String>,

/// Timeout for the rpc request
///
/// The rpc-timeout will be used to override the default timeout for RPC.
/// Default value is 45s
zerosnacks marked this conversation as resolved.
Show resolved Hide resolved
#[arg(long, env = "ETH_RPC_TIMEOUT")]
pub rpc_timeout: Option<u64>,
}

impl_figment_convert_cast!(RpcOpts);
Expand Down Expand Up @@ -76,6 +83,16 @@ impl RpcOpts {
Ok(jwt)
}

/// Returns the rpc timeout.
zerosnacks marked this conversation as resolved.
Show resolved Hide resolved
pub fn rpc_timeout<'a>(&'a self, config: Option<&'a Config>) -> Result<Option<Cow<'a, u64>>> {
let rpc_timeout = match (self.rpc_timeout.as_ref(), config) {
(Some(rpc_timeout), _) => Some(Cow::Borrowed(rpc_timeout)),
(None, Some(config)) => config.get_rpc_timeout()?,
(None, None) => None,
};
Ok(rpc_timeout)
}

pub fn dict(&self) -> Dict {
let mut dict = Dict::new();
if let Ok(Some(url)) = self.url(None) {
Expand All @@ -84,6 +101,9 @@ impl RpcOpts {
if let Ok(Some(jwt)) = self.jwt(None) {
dict.insert("eth_rpc_jwt".into(), jwt.into_owned().into());
}
if let Ok(Some(rpc_timeout)) = self.rpc_timeout(None) {
dict.insert("eth_rpc_timeout".into(), rpc_timeout.into_owned().into());
}
dict
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/cli/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ pub fn get_provider_builder(config: &Config) -> Result<ProviderBuilder> {
builder = builder.jwt(jwt.as_ref());
}

let rpc_timeout = config.get_rpc_timeout()?;
zerosnacks marked this conversation as resolved.
Show resolved Hide resolved
if let Some(rpc_timeout) = rpc_timeout {
builder = builder.timeout(Duration::from_secs(*rpc_timeout.as_ref()));
}

Ok(builder)
}

Expand Down
21 changes: 21 additions & 0 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ pub struct Config {
pub eth_rpc_url: Option<String>,
/// JWT secret that should be used for any rpc calls
pub eth_rpc_jwt: Option<String>,
/// timeout that should be used for any rpc calls
pub eth_rpc_timeout: Option<u64>,
zerosnacks marked this conversation as resolved.
Show resolved Hide resolved
/// etherscan API key, or alias for an `EtherscanConfig` in `etherscan` table
pub etherscan_api_key: Option<String>,
/// Multiple etherscan api configs and their aliases
Expand Down Expand Up @@ -1126,6 +1128,24 @@ impl Config {
Ok(self.eth_rpc_jwt.as_ref().map(|jwt| Cow::Borrowed(jwt.as_str())))
}

/// Returns the configured RPC timeout value.
///
/// Returns:
/// - The RPC timeout value, if configured.
///
/// # Example
///
/// ```
/// use foundry_config::Config;
/// # fn t() {
/// let config = Config::with_root("./");
/// let rpc_timeout = config.get_rpc_timeout().unwrap().unwrap();
/// # }
/// ```
pub fn get_rpc_timeout(&self) -> Result<Option<Cow<'_, u64>>, UnresolvedEnvVarError> {
Ok(self.eth_rpc_timeout.as_ref().map(Cow::Borrowed))
}

/// Returns the configured rpc url
///
/// Returns:
Expand Down Expand Up @@ -2208,6 +2228,7 @@ impl Default for Config {
memory_limit: 1 << 27, // 2**27 = 128MiB = 134_217_728 bytes
eth_rpc_url: None,
eth_rpc_jwt: None,
eth_rpc_timeout: None,
etherscan_api_key: None,
verbosity: 0,
remappings: vec![],
Expand Down
1 change: 1 addition & 0 deletions crates/forge/tests/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ forgetest!(can_extract_config_values, |prj, cmd| {
memory_limit: 1 << 27,
eth_rpc_url: Some("localhost".to_string()),
eth_rpc_jwt: None,
eth_rpc_timeout: None,
etherscan_api_key: None,
etherscan: Default::default(),
verbosity: 4,
Expand Down
Loading