From 6d457a507dffc773bc6af6401f8566def02a89d5 Mon Sep 17 00:00:00 2001 From: Enrique Date: Mon, 5 Feb 2024 10:44:51 -0400 Subject: [PATCH] feat(`alloy-providers`): additional missing methods (#184) * feat(providers): additional missing methods * comments --- crates/providers/src/provider.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/crates/providers/src/provider.rs b/crates/providers/src/provider.rs index ba96982989c..4192048a788 100644 --- a/crates/providers/src/provider.rs +++ b/crates/providers/src/provider.rs @@ -78,9 +78,15 @@ pub trait TempProvider: Send + Sync { full: bool, ) -> TransportResult>; + /// Gets the client version of the chain client. + async fn get_client_version(&self) -> TransportResult; + /// Gets the chain ID. async fn get_chain_id(&self) -> TransportResult; + /// Gets the network ID. Same as `eth_chainId`. + async fn get_net_version(&self) -> TransportResult; + /// Gets the specified storage value from [Address]. async fn get_storage_at( &self, @@ -276,11 +282,20 @@ impl TempProvider for Provider { self.inner.prepare("eth_getBlockByNumber", (number, full)).await } + /// Gets the client version of the chain client. + async fn get_client_version(&self) -> TransportResult { + self.inner.prepare("web3_clientVersion", ()).await + } + /// Gets the chain ID. async fn get_chain_id(&self) -> TransportResult { self.inner.prepare("eth_chainId", ()).await } + async fn get_net_version(&self) -> TransportResult { + self.inner.prepare("net_version", ()).await + } + /// Gets the specified storage value from [Address]. async fn get_storage_at( &self, @@ -613,6 +628,14 @@ mod tests { assert_eq!(block.header.number.unwrap(), U256::from(num)); } + #[tokio::test] + async fn gets_client_version() { + let anvil = Anvil::new().spawn(); + let provider = Provider::try_from(&anvil.endpoint()).unwrap(); + let version = provider.get_client_version().await.unwrap(); + assert!(version.contains("anvil")); + } + #[tokio::test] async fn gets_chain_id() { let chain_id: u64 = 13371337; @@ -622,6 +645,15 @@ mod tests { assert_eq!(chain_id, U64::from(chain_id)); } + #[tokio::test] + async fn gets_network_id() { + let chain_id: u64 = 13371337; + let anvil = Anvil::new().args(["--chain-id", chain_id.to_string().as_str()]).spawn(); + let provider = Provider::try_from(&anvil.endpoint()).unwrap(); + let chain_id = provider.get_net_version().await.unwrap(); + assert_eq!(chain_id, U64::from(chain_id)); + } + #[tokio::test] #[cfg(feature = "anvil")] async fn gets_code_at() {