From 095ed893f73487ed5cba22b7e14b429c0873e102 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Tue, 2 Jul 2024 12:48:15 +0530 Subject: [PATCH] nits --- crates/provider/src/provider/prov_call.rs | 7 +++- crates/provider/src/provider/trait.rs | 43 +++++++++++++++------- crates/provider/src/provider/with_block.rs | 6 +-- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/crates/provider/src/provider/prov_call.rs b/crates/provider/src/provider/prov_call.rs index 338c0b16028..144575d96fe 100644 --- a/crates/provider/src/provider/prov_call.rs +++ b/crates/provider/src/provider/prov_call.rs @@ -134,10 +134,12 @@ where } } + /// True if this is a RPC call with block. pub const fn is_rpc_call_with_block(&self) -> bool { matches!(self, Self::RpcCallWithBlock(_)) } + /// Fallible cast to mutable RPC call with block. pub fn as_mut_rpc_call_with_block( &mut self, ) -> Option<&mut RpcWithBlockFut> { @@ -186,11 +188,12 @@ where Map: Fn(Resp) -> Output, Map: Clone, { - pub fn block_id(mut self, block_id: BlockId) -> Self { + /// Set the block id for a RPC call with block. + pub fn block(mut self, block_id: BlockId) -> Self { if let Some(call) = self.as_mut_rpc_call_with_block() { let call = call.clone(); - return ProviderCall::RpcCallWithBlock(call.block_id(block_id)); + return Self::RpcCallWithBlock(call.block_id(block_id)); } self } diff --git a/crates/provider/src/provider/trait.rs b/crates/provider/src/provider/trait.rs index c48b526a004..43c75655df7 100644 --- a/crates/provider/src/provider/trait.rs +++ b/crates/provider/src/provider/trait.rs @@ -118,7 +118,7 @@ pub trait Provider: /// /// This function returns [`ProviderCall`] which can be used to execute the /// call. This method executes the call on the latest block with the current state. - /// Use [`call_internal`] to set the block or [`StateOverride`] if you want to + /// Use `call_internal` to set the block or [`StateOverride`] if you want to /// execute the call on a different block or with overriden state. /// /// [`StateOverride`]: alloy_rpc_types_eth::state::StateOverride @@ -137,17 +137,9 @@ pub trait Provider: /// # let tx = alloy_rpc_types_eth::transaction::TransactionRequest::default(); /// // Execute a call on the latest block, with no state overrides /// let output = provider.call(&tx).await?; - /// // Execute a call with a block ID. - /// let output = provider.call(&tx).block(1.into()).await?; - /// // Execute a call with state overrides. - /// let output = provider.call(&tx).overrides(&my_overrides).await?; /// # Ok(()) /// # } /// ``` - /// - /// # Note - /// - /// Not all client implementations support state overrides. #[doc(alias = "eth_call")] #[doc(alias = "call_with_overrides")] fn call<'req>( @@ -160,6 +152,30 @@ pub trait Provider: /// This method returns `EthCall` struct. /// It is useful when we have to set the block or state overrides after generating the struct. + /// + /// /// ## Example + /// + /// ``` + /// # use alloy_provider::Provider; + /// # use alloy_eips::BlockId; + /// # use alloy_rpc_types_eth::state::StateOverride; + /// # use alloy_transport::BoxTransport; + /// # async fn example>( + /// # provider: P, + /// # my_overrides: StateOverride + /// # ) -> Result<(), Box> { + /// # let tx = alloy_rpc_types_eth::transaction::TransactionRequest::default(); + /// // Execute a call with a block ID. + /// let output = provider.call_internal(&tx).block(1.into()).await?; + /// // Execute a call with state overrides. + /// let output = provider.call_internal(&tx).overrides(&my_overrides).await?; + /// # Ok(()) + /// # } + /// ``` + /// + /// # Note + /// + /// Not all client implementations support state overrides. fn call_internal<'req>( &'req self, tx: &'req N::TransactionRequest, @@ -187,7 +203,7 @@ pub trait Provider: /// This function returns an [`ProviderCall`] which can be used to get a gas estimate, /// The gas estimate will be computed for the latest block with the current state. - /// Use [`estimate_gas_internal`] to set the block or [`StateOverride`] if you want to + /// Use `estimate_gas_internal` to set the block or [`StateOverride`] if you want to /// calculate the gas on a different block or with overriden state. /// /// [`StateOverride`]: alloy_rpc_types_eth::state::StateOverride @@ -1000,7 +1016,7 @@ impl Provider for RootProvider { #[cfg(test)] mod tests { use super::*; - use crate::{builder, ext::AnvilApi, ProviderBuilder, WalletProvider}; + use crate::{builder, ProviderBuilder, WalletProvider}; use alloy_network::AnyNetwork; use alloy_node_bindings::Anvil; use alloy_primitives::{address, b256, bytes}; @@ -1199,6 +1215,7 @@ mod tests { assert_eq!(0, num.to::()) } + #[cfg(feature = "anvil-api")] #[tokio::test] async fn gets_transaction_count() { init_tracing(); @@ -1210,8 +1227,6 @@ mod tests { let count = provider.get_transaction_count(sender).await.unwrap(); assert_eq!(count, 0); - let _ = provider.anvil_auto_impersonate_account(true); - // Send Tx let tx = TransactionRequest { value: Some(U256::from(100)), @@ -1228,7 +1243,7 @@ mod tests { assert_eq!(count, 1); // Tx count should be 0 at block 0 - let count = provider.get_transaction_count(sender).block_id(0.into()).await.unwrap(); + let count = provider.get_transaction_count(sender).block(0.into()).await.unwrap(); assert_eq!(count, 0); } diff --git a/crates/provider/src/provider/with_block.rs b/crates/provider/src/provider/with_block.rs index 5c11395896f..44a4c26d3b0 100644 --- a/crates/provider/src/provider/with_block.rs +++ b/crates/provider/src/provider/with_block.rs @@ -88,7 +88,7 @@ where Map: Fn(Resp) -> Output, { pub fn block_id(mut self, block_id: BlockId) -> Self { - let call = match std::mem::replace(&mut self.state, States::Invalid) { + match std::mem::replace(&mut self.state, States::Invalid) { States::Preparing { client, method, params, map, .. } => { Self { state: States::Preparing { client, method, params, block_id, map } } } @@ -96,9 +96,7 @@ where self.state = state; self } - }; - - call + } } }