Struct CallBatchLayer

Source
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_calls.

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

Source

pub const fn new() -> Self

Create a new CallBatchLayer with a default wait of 1ms.

Source

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.

Source

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CallBatchLayer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<P, N> ProviderLayer<P, N> for CallBatchLayer
where P: Provider<N> + 'static, N: Network,

Source§

type Provider = CallBatchProvider<P, N>

The provider constructed by this layer.
Source§

fn layer(&self, inner: P) -> Self::Provider

Wrap the given provider in the layer’s provider.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,