Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-atreya committed Jul 2, 2024
1 parent b16ab35 commit 095ed89
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
7 changes: 5 additions & 2 deletions crates/provider/src/provider/prov_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Conn, Params, Resp, Output, Map>> {
Expand Down Expand Up @@ -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
}
Expand Down
43 changes: 29 additions & 14 deletions crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:
///
/// 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
Expand All @@ -137,17 +137,9 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:
/// # 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>(
Expand All @@ -160,6 +152,30 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:

/// 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<P: Provider<BoxTransport>>(
/// # provider: P,
/// # my_overrides: StateOverride
/// # ) -> Result<(), Box<dyn std::error::Error>> {
/// # 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,
Expand Down Expand Up @@ -187,7 +203,7 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:

/// 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
Expand Down Expand Up @@ -1000,7 +1016,7 @@ impl<T: Transport + Clone, N: Network> Provider<T, N> for RootProvider<T, N> {
#[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};
Expand Down Expand Up @@ -1199,6 +1215,7 @@ mod tests {
assert_eq!(0, num.to::<u64>())
}

#[cfg(feature = "anvil-api")]
#[tokio::test]
async fn gets_transaction_count() {
init_tracing();
Expand All @@ -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)),
Expand All @@ -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);
}

Expand Down
6 changes: 2 additions & 4 deletions crates/provider/src/provider/with_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,15 @@ 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 } }
}
state => {
self.state = state;
self
}
};

call
}
}
}

Expand Down

0 comments on commit 095ed89

Please sign in to comment.