pub struct CallBatchLayer { /* private fields */ }
Expand description
Provider layer that aggregates contract calls (eth_call
) over a time period into a single
Multicall3 contract call.
Some methods, such as eth_getBlockNumber
, are first converted into contract calls to the
Multicall3 contract itself and then aggregated with other eth_call
s.
Only calls that:
- target the latest block ID,
- have no state overrides,
- have a target address and calldata,
- have no other properties (nonce, gas, etc.)
can be sent with a multicall. This of course requires that the Multicall3 contract is
deployed on the network, by default at MULTICALL3_ADDRESS
.
This layer is useful for reducing the number of network requests made.
However, this only works when requests are made in parallel, for example when using the
tokio::join!
macro or in multiple threads/tasks, as otherwise the requests will be sent one
by one as normal, but with an added delay.
§Examples
use alloy_provider::{layers::CallBatchLayer, Provider, ProviderBuilder};
use std::time::Duration;
// Build a provider with the default call batching configuration.
let provider = ProviderBuilder::new().with_call_batching().connect(url).await?;
// Build a provider with a custom call batching configuration.
let provider = ProviderBuilder::new()
.layer(CallBatchLayer::new().wait(Duration::from_millis(10)))
.connect(url)
.await?;
// Both of these requests will be batched together and only 1 network request will be made.
let (block_number_result, chain_id_result) =
tokio::join!(provider.get_block_number(), provider.get_chain_id());
let block_number = block_number_result?;
let chain_id = chain_id_result?;
println!("block number: {block_number}, chain id: {chain_id}");
Implementations§
Source§impl CallBatchLayer
impl CallBatchLayer
Sourcepub const fn wait(self, wait: Duration) -> Self
pub const fn wait(self, wait: Duration) -> Self
Set the amount of time to wait before sending the batch.
This is the amount of time to wait after the first request is received before sending all the requests received in that time period.
This means that every request has a maximum delay of wait
before being sent.
The default is 1ms.
Sourcepub const fn multicall3_address(self, m3a: Address) -> Self
pub const fn multicall3_address(self, m3a: Address) -> Self
Set the multicall3 address.
The default is MULTICALL3_ADDRESS
.
Trait Implementations§
Source§impl Debug for CallBatchLayer
impl Debug for CallBatchLayer
Source§impl Default for CallBatchLayer
impl Default for CallBatchLayer
Source§impl<P, N> ProviderLayer<P, N> for CallBatchLayer
impl<P, N> ProviderLayer<P, N> for CallBatchLayer
Auto Trait Implementations§
impl Freeze for CallBatchLayer
impl RefUnwindSafe for CallBatchLayer
impl Send for CallBatchLayer
impl Sync for CallBatchLayer
impl Unpin for CallBatchLayer
impl UnwindSafe for CallBatchLayer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more