alloy_provider/layers/
batch.rs

1use crate::{
2    bindings::{ArbSys, IMulticall3},
3    Caller, Provider, ProviderCall, ProviderLayer, RootProvider, ARB_SYS_ADDRESS,
4    MULTICALL3_ADDRESS,
5};
6use alloy_eips::BlockId;
7use alloy_network::{Ethereum, Network, TransactionBuilder};
8use alloy_primitives::{Address, Bytes, U256};
9use alloy_rpc_client::WeakClient;
10use alloy_sol_types::{SolCall, SolType, SolValue};
11use alloy_transport::{utils::Spawnable, TransportErrorKind, TransportResult};
12use std::{fmt, future::IntoFuture, marker::PhantomData, sync::Arc, time::Duration};
13use tokio::sync::{mpsc, oneshot};
14
15#[cfg(target_family = "wasm")]
16use wasmtimer::tokio::sleep;
17
18#[cfg(not(target_family = "wasm"))]
19use tokio::time::sleep;
20
21/// This is chosen somewhat arbitrarily. It should be short enough to not cause a noticeable
22/// delay on individual requests, but long enough to allow for batching requests issued together in
23/// a short period of time, such as when using `join!` macro or similar future combinators.
24const DEFAULT_WAIT: Duration = Duration::from_millis(1);
25
26/// Provider layer that aggregates contract calls (`eth_call`) over a time period into a single
27/// [Multicall3] contract call.
28///
29/// Some methods, such as `eth_getBlockNumber`, are first converted into contract calls to the
30/// [Multicall3] contract itself and then aggregated with other `eth_call`s.
31///
32/// Only calls that:
33/// - target the latest block ID,
34/// - have no state overrides,
35/// - have a target address and calldata,
36/// - have no other properties (nonce, gas, etc.)
37///
38/// can be sent with a multicall. This of course requires that the [Multicall3] contract is
39/// deployed on the network, by default at [`MULTICALL3_ADDRESS`].
40///
41/// This layer is useful for reducing the number of network requests made.
42/// However, this only works when requests are made in parallel, for example when using the
43/// [`tokio::join!`] macro or in multiple threads/tasks, as otherwise the requests will be sent one
44/// by one as normal, but with an added delay.
45///
46/// # Examples
47///
48/// ```no_run
49/// use alloy_provider::{layers::CallBatchLayer, Provider, ProviderBuilder};
50/// use std::time::Duration;
51///
52/// # async fn f(url: &str) -> Result<(), Box<dyn std::error::Error>> {
53/// // Build a provider with the default call batching configuration.
54/// let provider = ProviderBuilder::new().with_call_batching().connect(url).await?;
55///
56/// // Build a provider with a custom call batching configuration.
57/// let provider = ProviderBuilder::new()
58///     .layer(CallBatchLayer::new().wait(Duration::from_millis(10)))
59///     .connect(url)
60///     .await?;
61///
62/// // Both of these requests will be batched together and only 1 network request will be made.
63/// let (block_number_result, chain_id_result) =
64///     tokio::join!(provider.get_block_number(), provider.get_chain_id());
65/// let block_number = block_number_result?;
66/// let chain_id = chain_id_result?;
67/// println!("block number: {block_number}, chain id: {chain_id}");
68/// # Ok(())
69/// # }
70/// ```
71///
72/// [Multicall3]: https://github.com/mds1/multicall3
73#[derive(Debug)]
74pub struct CallBatchLayer {
75    m3a: Address,
76    wait: Duration,
77    arbsys: bool,
78}
79
80impl Default for CallBatchLayer {
81    fn default() -> Self {
82        Self::new()
83    }
84}
85
86impl CallBatchLayer {
87    /// Create a new `CallBatchLayer` with a default wait of 1ms.
88    pub const fn new() -> Self {
89        Self { m3a: MULTICALL3_ADDRESS, wait: DEFAULT_WAIT, arbsys: false }
90    }
91
92    /// Set the amount of time to wait before sending the batch.
93    ///
94    /// This is the amount of time to wait after the first request is received before sending all
95    /// the requests received in that time period.
96    ///
97    /// This means that every request has a maximum delay of `wait` before being sent.
98    ///
99    /// The default is 1ms.
100    pub const fn wait(mut self, wait: Duration) -> Self {
101        self.wait = wait;
102        self
103    }
104
105    /// Set the multicall3 address.
106    ///
107    /// The default is [`MULTICALL3_ADDRESS`].
108    pub const fn multicall3_address(mut self, m3a: Address) -> Self {
109        self.m3a = m3a;
110        self
111    }
112
113    /// Use the Arbitrum `ArbSys` precompile for block number queries.
114    ///
115    /// On Arbitrum, `block.number` returns the parent chain’s block number (L1).
116    /// Without this setting, batched `eth_blockNumber` calls through Multicall3
117    /// will therefore return the wrong value. Enabling this queries the L2 block
118    /// number via `ArbSys` instead.
119    ///
120    /// The default is `false`.
121    /// This should only be enabled when interacting with Arbitrum rollups.
122    pub const fn arbitrum_compat(mut self) -> Self {
123        self.arbsys = true;
124        self
125    }
126}
127
128impl<P, N> ProviderLayer<P, N> for CallBatchLayer
129where
130    P: Provider<N> + 'static,
131    N: Network,
132{
133    type Provider = CallBatchProvider<P, N>;
134
135    fn layer(&self, inner: P) -> Self::Provider {
136        CallBatchProvider::new(inner, self)
137    }
138}
139
140type CallBatchMsgTx = TransportResult<IMulticall3::Result>;
141
142struct CallBatchMsg<N: Network> {
143    kind: CallBatchMsgKind<N>,
144    tx: oneshot::Sender<CallBatchMsgTx>,
145}
146
147impl<N: Network> Clone for CallBatchMsgKind<N>
148where
149    N::TransactionRequest: Clone,
150{
151    fn clone(&self) -> Self {
152        match self {
153            Self::Call(tx) => Self::Call(tx.clone()),
154            Self::BlockNumber => Self::BlockNumber,
155            Self::ChainId => Self::ChainId,
156            Self::Balance(addr) => Self::Balance(*addr),
157        }
158    }
159}
160
161impl<N: Network> fmt::Debug for CallBatchMsg<N> {
162    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163        f.write_str("BatchProviderMessage(")?;
164        self.kind.fmt(f)?;
165        f.write_str(")")
166    }
167}
168
169#[derive(Debug)]
170enum CallBatchMsgKind<N: Network = Ethereum> {
171    Call(N::TransactionRequest),
172    BlockNumber,
173    ChainId,
174    Balance(Address),
175}
176
177impl<N: Network> CallBatchMsg<N> {
178    fn new(kind: CallBatchMsgKind<N>) -> (Self, oneshot::Receiver<CallBatchMsgTx>) {
179        let (tx, rx) = oneshot::channel();
180        (Self { kind, tx }, rx)
181    }
182}
183
184impl<N: Network> CallBatchMsgKind<N> {
185    fn to_call3(&self, m3a: Address, arbsys: bool) -> IMulticall3::Call3 {
186        let m3a_call = |data: Vec<u8>| IMulticall3::Call3 {
187            target: m3a,
188            allowFailure: true,
189            callData: data.into(),
190        };
191        match self {
192            Self::Call(tx) => IMulticall3::Call3 {
193                target: tx.to().unwrap_or_default(),
194                allowFailure: true,
195                callData: tx.input().cloned().unwrap_or_default(),
196            },
197            Self::BlockNumber => {
198                if arbsys {
199                    return IMulticall3::Call3 {
200                        target: ARB_SYS_ADDRESS,
201                        allowFailure: false,
202                        callData: ArbSys::arbBlockNumberCall {}.abi_encode().into(),
203                    };
204                }
205                m3a_call(IMulticall3::getBlockNumberCall {}.abi_encode())
206            }
207            Self::ChainId => m3a_call(IMulticall3::getChainIdCall {}.abi_encode()),
208            &Self::Balance(addr) => m3a_call(IMulticall3::getEthBalanceCall { addr }.abi_encode()),
209        }
210    }
211}
212
213/// A provider that batches multiple requests into a single request.
214///
215/// See [`CallBatchLayer`] for more information.
216pub struct CallBatchProvider<P, N: Network = Ethereum> {
217    provider: Arc<P>,
218    inner: CallBatchProviderInner<N>,
219    _pd: PhantomData<N>,
220}
221
222impl<P, N: Network> Clone for CallBatchProvider<P, N> {
223    fn clone(&self) -> Self {
224        Self { provider: self.provider.clone(), inner: self.inner.clone(), _pd: PhantomData }
225    }
226}
227
228impl<P: fmt::Debug, N: Network> fmt::Debug for CallBatchProvider<P, N> {
229    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
230        f.write_str("BatchProvider(")?;
231        self.provider.fmt(f)?;
232        f.write_str(")")
233    }
234}
235
236impl<P: Provider<N> + 'static, N: Network> CallBatchProvider<P, N> {
237    fn new(inner: P, layer: &CallBatchLayer) -> Self {
238        let inner = Arc::new(inner);
239        let tx = CallBatchBackend::spawn(inner.clone(), layer);
240        Self { provider: inner, inner: CallBatchProviderInner { tx }, _pd: PhantomData }
241    }
242}
243
244#[derive(Clone)]
245struct CallBatchProviderInner<N: Network> {
246    tx: mpsc::UnboundedSender<CallBatchMsg<N>>,
247}
248
249impl<N: Network> CallBatchProviderInner<N> {
250    /// We only want to perform a scheduled multicall if:
251    /// - The request has no block ID or state overrides,
252    /// - The request has a target address,
253    /// - The request has no other properties (`nonce`, `gas`, etc cannot be sent with a multicall).
254    ///
255    /// Ref: <https://github.com/wevm/viem/blob/ba8319f71503af8033fd3c77cfb64c7eb235c6a9/src/actions/public/call.ts#L295>
256    fn should_batch_call(&self, params: &crate::EthCallParams<N>) -> bool {
257        // TODO: block ID is not yet implemented
258        if params.block().is_some_and(|block| block != BlockId::latest()) {
259            return false;
260        }
261        if params.overrides.as_ref().is_some_and(|overrides| !overrides.is_empty()) {
262            return false;
263        }
264        let tx = params.data();
265        if tx.to().is_none() {
266            return false;
267        }
268        if let Ok(serde_json::Value::Object(obj)) = serde_json::to_value(tx) {
269            if obj.keys().any(|k| !matches!(k.as_str(), "to" | "data" | "input")) {
270                return false;
271            }
272        }
273        true
274    }
275
276    async fn schedule(self, msg: CallBatchMsgKind<N>) -> TransportResult<Bytes> {
277        let (msg, rx) = CallBatchMsg::new(msg);
278        self.tx.send(msg).map_err(|_| TransportErrorKind::backend_gone())?;
279
280        let IMulticall3::Result { success, returnData } =
281            rx.await.map_err(|_| TransportErrorKind::backend_gone())??;
282
283        if !success {
284            let revert_data = if returnData.is_empty() {
285                "".to_string()
286            } else {
287                format!(" with data: {returnData}")
288            };
289            Err(TransportErrorKind::custom_str(&format!(
290                "multicall batched call reverted{revert_data}"
291            )))
292        } else {
293            Ok(returnData)
294        }
295    }
296
297    async fn schedule_and_decode<T>(self, msg: CallBatchMsgKind<N>) -> TransportResult<T>
298    where
299        T: SolValue + From<<T::SolType as SolType>::RustType>,
300    {
301        let data = self.schedule(msg).await?;
302        T::abi_decode(&data).map_err(TransportErrorKind::custom)
303    }
304}
305
306struct CallBatchBackend<P, N: Network = Ethereum> {
307    inner: Arc<P>,
308    m3a: Address,
309    wait: Duration,
310    arbsys: bool,
311    rx: mpsc::UnboundedReceiver<CallBatchMsg<N>>,
312    pending: Vec<CallBatchMsg<N>>,
313    _pd: PhantomData<N>,
314}
315
316impl<P: Provider<N> + 'static, N: Network> CallBatchBackend<P, N> {
317    fn spawn(inner: Arc<P>, layer: &CallBatchLayer) -> mpsc::UnboundedSender<CallBatchMsg<N>> {
318        let CallBatchLayer { m3a, wait, arbsys } = *layer;
319        let (tx, rx) = mpsc::unbounded_channel();
320        let this = Self { inner, m3a, wait, arbsys, rx, pending: Vec::new(), _pd: PhantomData };
321        this.run().spawn_task();
322        tx
323    }
324
325    async fn run(mut self) {
326        'outer: loop {
327            // Wait for the first message.
328            debug_assert!(self.pending.is_empty());
329            match self.rx.recv().await {
330                Some(msg) => self.process_msg(msg),
331                None => break,
332            }
333
334            // Handle all remaining messages after waiting the duration.
335            debug_assert!(!self.pending.is_empty());
336            sleep(self.wait).await;
337            'inner: loop {
338                match self.rx.try_recv() {
339                    Ok(msg) => self.process_msg(msg),
340                    Err(mpsc::error::TryRecvError::Empty) => break 'inner,
341                    Err(mpsc::error::TryRecvError::Disconnected) => break 'outer,
342                }
343            }
344            // No more messages, send the batch.
345            self.send_batch().await;
346        }
347    }
348
349    fn process_msg(&mut self, msg: CallBatchMsg<N>) {
350        self.pending.push(msg);
351    }
352
353    async fn send_batch(&mut self) {
354        let pending = std::mem::take(&mut self.pending);
355
356        // If there's only a single call, avoid batching and perform the request directly.
357        if pending.len() == 1 {
358            let msg = pending.into_iter().next().unwrap();
359            let result = self.call_one(msg.kind).await;
360            let _ = msg.tx.send(result);
361            return;
362        }
363
364        let result = self.send_batch_inner(&pending).await;
365        match result {
366            Ok(results) => {
367                debug_assert_eq!(results.len(), pending.len());
368                for (result, msg) in results.into_iter().zip(pending) {
369                    let _ = msg.tx.send(Ok(result));
370                }
371            }
372            Err(e) => {
373                for msg in pending {
374                    let _ = msg.tx.send(Err(TransportErrorKind::custom_str(&e.to_string())));
375                }
376            }
377        }
378    }
379
380    async fn call_one(&mut self, msg: CallBatchMsgKind<N>) -> TransportResult<IMulticall3::Result> {
381        let m3_res =
382            |success, return_data| IMulticall3::Result { success, returnData: return_data };
383        match msg {
384            CallBatchMsgKind::Call(tx) => self.inner.call(tx).await.map(|res| m3_res(true, res)),
385            CallBatchMsgKind::BlockNumber => {
386                self.inner.get_block_number().await.map(|res| m3_res(true, res.abi_encode().into()))
387            }
388            CallBatchMsgKind::ChainId => {
389                self.inner.get_chain_id().await.map(|res| m3_res(true, res.abi_encode().into()))
390            }
391            CallBatchMsgKind::Balance(addr) => {
392                self.inner.get_balance(addr).await.map(|res| m3_res(true, res.abi_encode().into()))
393            }
394        }
395    }
396
397    async fn send_batch_inner(
398        &self,
399        pending: &[CallBatchMsg<N>],
400    ) -> TransportResult<Vec<IMulticall3::Result>> {
401        let calls: Vec<_> =
402            pending.iter().map(|msg| msg.kind.to_call3(self.m3a, self.arbsys)).collect();
403
404        let tx = N::TransactionRequest::default()
405            .with_to(self.m3a)
406            .with_input(IMulticall3::aggregate3Call { calls }.abi_encode());
407
408        let bytes = self.inner.call(tx).await?;
409        if bytes.is_empty() {
410            return Err(TransportErrorKind::custom_str(&format!(
411                "Multicall3 not deployed at {}",
412                self.m3a
413            )));
414        }
415
416        let ret = IMulticall3::aggregate3Call::abi_decode_returns(&bytes)
417            .map_err(TransportErrorKind::custom)?;
418        Ok(ret)
419    }
420}
421
422impl<P: Provider<N> + 'static, N: Network> Provider<N> for CallBatchProvider<P, N> {
423    fn root(&self) -> &RootProvider<N> {
424        self.provider.root()
425    }
426
427    fn call(&self, tx: <N as Network>::TransactionRequest) -> crate::EthCall<N, Bytes> {
428        crate::EthCall::call(CallBatchCaller::new(self), tx)
429    }
430
431    fn get_block_number(
432        &self,
433    ) -> crate::ProviderCall<
434        alloy_rpc_client::NoParams,
435        alloy_primitives::U64,
436        alloy_primitives::BlockNumber,
437    > {
438        crate::ProviderCall::BoxedFuture(Box::pin(
439            self.inner.clone().schedule_and_decode::<u64>(CallBatchMsgKind::BlockNumber),
440        ))
441    }
442
443    fn get_chain_id(
444        &self,
445    ) -> crate::ProviderCall<
446        alloy_rpc_client::NoParams,
447        alloy_primitives::U64,
448        alloy_primitives::ChainId,
449    > {
450        crate::ProviderCall::BoxedFuture(Box::pin(
451            self.inner.clone().schedule_and_decode::<u64>(CallBatchMsgKind::ChainId),
452        ))
453    }
454
455    fn get_balance(&self, address: Address) -> crate::RpcWithBlock<Address, U256, U256> {
456        let this = self.clone();
457        crate::RpcWithBlock::new_provider(move |block| {
458            if block != BlockId::latest() {
459                this.provider.get_balance(address).block_id(block).into_future()
460            } else {
461                ProviderCall::BoxedFuture(Box::pin(
462                    this.inner
463                        .clone()
464                        .schedule_and_decode::<U256>(CallBatchMsgKind::Balance(address)),
465                ))
466            }
467        })
468    }
469}
470
471struct CallBatchCaller<N: Network> {
472    inner: CallBatchProviderInner<N>,
473    weak: WeakClient,
474}
475
476impl<N: Network> CallBatchCaller<N> {
477    fn new<P: Provider<N>>(provider: &CallBatchProvider<P, N>) -> Self {
478        Self { inner: provider.inner.clone(), weak: provider.provider.weak_client() }
479    }
480}
481
482impl<N: Network> Caller<N, Bytes> for CallBatchCaller<N> {
483    fn call(
484        &self,
485        params: crate::EthCallParams<N>,
486    ) -> TransportResult<crate::ProviderCall<crate::EthCallParams<N>, Bytes>> {
487        if !self.inner.should_batch_call(&params) {
488            return Caller::<N, Bytes>::call(&self.weak, params);
489        }
490
491        Ok(crate::ProviderCall::BoxedFuture(Box::pin(
492            self.inner.clone().schedule(CallBatchMsgKind::Call(params.into_data())),
493        )))
494    }
495
496    fn estimate_gas(
497        &self,
498        params: crate::EthCallParams<N>,
499    ) -> TransportResult<crate::ProviderCall<crate::EthCallParams<N>, Bytes>> {
500        Caller::<N, Bytes>::estimate_gas(&self.weak, params)
501    }
502
503    fn call_many(
504        &self,
505        params: crate::EthCallManyParams<'_>,
506    ) -> TransportResult<crate::ProviderCall<crate::EthCallManyParams<'static>, Bytes>> {
507        Caller::<N, Bytes>::call_many(&self.weak, params)
508    }
509}
510
511#[cfg(test)]
512mod tests {
513    use super::*;
514    use crate::ProviderBuilder;
515    use alloy_primitives::{address, hex};
516    use alloy_rpc_types_eth::TransactionRequest;
517    use alloy_transport::mock::Asserter;
518
519    // https://etherscan.io/address/0xcA11bde05977b3631167028862bE2a173976CA11#code
520    const MULTICALL3_DEPLOYED_CODE: &[u8] = &hex!("0x6080604052600436106100f35760003560e01c80634d2301cc1161008a578063a8b0574e11610059578063a8b0574e1461025a578063bce38bd714610275578063c3077fa914610288578063ee82ac5e1461029b57600080fd5b80634d2301cc146101ec57806372425d9d1461022157806382ad56cb1461023457806386d516e81461024757600080fd5b80633408e470116100c65780633408e47014610191578063399542e9146101a45780633e64a696146101c657806342cbb15c146101d957600080fd5b80630f28c97d146100f8578063174dea711461011a578063252dba421461013a57806327e86d6e1461015b575b600080fd5b34801561010457600080fd5b50425b6040519081526020015b60405180910390f35b61012d610128366004610a85565b6102ba565b6040516101119190610bbe565b61014d610148366004610a85565b6104ef565b604051610111929190610bd8565b34801561016757600080fd5b50437fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0140610107565b34801561019d57600080fd5b5046610107565b6101b76101b2366004610c60565b610690565b60405161011193929190610cba565b3480156101d257600080fd5b5048610107565b3480156101e557600080fd5b5043610107565b3480156101f857600080fd5b50610107610207366004610ce2565b73ffffffffffffffffffffffffffffffffffffffff163190565b34801561022d57600080fd5b5044610107565b61012d610242366004610a85565b6106ab565b34801561025357600080fd5b5045610107565b34801561026657600080fd5b50604051418152602001610111565b61012d610283366004610c60565b61085a565b6101b7610296366004610a85565b610a1a565b3480156102a757600080fd5b506101076102b6366004610d18565b4090565b60606000828067ffffffffffffffff8111156102d8576102d8610d31565b60405190808252806020026020018201604052801561031e57816020015b6040805180820190915260008152606060208201528152602001906001900390816102f65790505b5092503660005b8281101561047757600085828151811061034157610341610d60565b6020026020010151905087878381811061035d5761035d610d60565b905060200281019061036f9190610d8f565b6040810135958601959093506103886020850185610ce2565b73ffffffffffffffffffffffffffffffffffffffff16816103ac6060870187610dcd565b6040516103ba929190610e32565b60006040518083038185875af1925050503d80600081146103f7576040519150601f19603f3d011682016040523d82523d6000602084013e6103fc565b606091505b50602080850191909152901515808452908501351761046d577f08c379a000000000000000000000000000000000000000000000000000000000600052602060045260176024527f4d756c746963616c6c333a2063616c6c206661696c656400000000000000000060445260846000fd5b5050600101610325565b508234146104e6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601a60248201527f4d756c746963616c6c333a2076616c7565206d69736d6174636800000000000060448201526064015b60405180910390fd5b50505092915050565b436060828067ffffffffffffffff81111561050c5761050c610d31565b60405190808252806020026020018201604052801561053f57816020015b606081526020019060019003908161052a5790505b5091503660005b8281101561068657600087878381811061056257610562610d60565b90506020028101906105749190610e42565b92506105836020840184610ce2565b73ffffffffffffffffffffffffffffffffffffffff166105a66020850185610dcd565b6040516105b4929190610e32565b6000604051808303816000865af19150503d80600081146105f1576040519150601f19603f3d011682016040523d82523d6000602084013e6105f6565b606091505b5086848151811061060957610609610d60565b602090810291909101015290508061067d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601760248201527f4d756c746963616c6c333a2063616c6c206661696c656400000000000000000060448201526064016104dd565b50600101610546565b5050509250929050565b43804060606106a086868661085a565b905093509350939050565b6060818067ffffffffffffffff8111156106c7576106c7610d31565b60405190808252806020026020018201604052801561070d57816020015b6040805180820190915260008152606060208201528152602001906001900390816106e55790505b5091503660005b828110156104e657600084828151811061073057610730610d60565b6020026020010151905086868381811061074c5761074c610d60565b905060200281019061075e9190610e76565b925061076d6020840184610ce2565b73ffffffffffffffffffffffffffffffffffffffff166107906040850185610dcd565b60405161079e929190610e32565b6000604051808303816000865af19150503d80600081146107db576040519150601f19603f3d011682016040523d82523d6000602084013e6107e0565b606091505b506020808401919091529015158083529084013517610851577f08c379a000000000000000000000000000000000000000000000000000000000600052602060045260176024527f4d756c746963616c6c333a2063616c6c206661696c656400000000000000000060445260646000fd5b50600101610714565b6060818067ffffffffffffffff81111561087657610876610d31565b6040519080825280602002602001820160405280156108bc57816020015b6040805180820190915260008152606060208201528152602001906001900390816108945790505b5091503660005b82811015610a105760008482815181106108df576108df610d60565b602002602001015190508686838181106108fb576108fb610d60565b905060200281019061090d9190610e42565b925061091c6020840184610ce2565b73ffffffffffffffffffffffffffffffffffffffff1661093f6020850185610dcd565b60405161094d929190610e32565b6000604051808303816000865af19150503d806000811461098a576040519150601f19603f3d011682016040523d82523d6000602084013e61098f565b606091505b506020830152151581528715610a07578051610a07576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601760248201527f4d756c746963616c6c333a2063616c6c206661696c656400000000000000000060448201526064016104dd565b506001016108c3565b5050509392505050565b6000806060610a2b60018686610690565b919790965090945092505050565b60008083601f840112610a4b57600080fd5b50813567ffffffffffffffff811115610a6357600080fd5b6020830191508360208260051b8501011115610a7e57600080fd5b9250929050565b60008060208385031215610a9857600080fd5b823567ffffffffffffffff811115610aaf57600080fd5b610abb85828601610a39565b90969095509350505050565b6000815180845260005b81811015610aed57602081850181015186830182015201610ad1565b81811115610aff576000602083870101525b50601f017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0169290920160200192915050565b600082825180855260208086019550808260051b84010181860160005b84811015610bb1578583037fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe001895281518051151584528401516040858501819052610b9d81860183610ac7565b9a86019a9450505090830190600101610b4f565b5090979650505050505050565b602081526000610bd16020830184610b32565b9392505050565b600060408201848352602060408185015281855180845260608601915060608160051b870101935082870160005b82811015610c52577fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa0888703018452610c40868351610ac7565b95509284019290840190600101610c06565b509398975050505050505050565b600080600060408486031215610c7557600080fd5b83358015158114610c8557600080fd5b9250602084013567ffffffffffffffff811115610ca157600080fd5b610cad86828701610a39565b9497909650939450505050565b838152826020820152606060408201526000610cd96060830184610b32565b95945050505050565b600060208284031215610cf457600080fd5b813573ffffffffffffffffffffffffffffffffffffffff81168114610bd157600080fd5b600060208284031215610d2a57600080fd5b5035919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b600082357fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff81833603018112610dc357600080fd5b9190910192915050565b60008083357fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe1843603018112610e0257600080fd5b83018035915067ffffffffffffffff821115610e1d57600080fd5b602001915036819003821315610a7e57600080fd5b8183823760009101908152919050565b600082357fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc1833603018112610dc357600080fd5b600082357fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa1833603018112610dc357600080fdfea2646970667358221220bb2b5c71a328032f97c676ae39a1ec2148d3e5d6f73d95e9b17910152d61f16264736f6c634300080c0033");
521    const COUNTER_ADDRESS: Address = address!("0x1234123412341234123412341234123412341234");
522    const COUNTER_DEPLOYED_CODE: &[u8] = &hex!("0x6080604052348015600e575f5ffd5b5060043610603a575f3560e01c80633fb5c1cb14603e5780638381f58a14604f578063d09de08a146068575b5f5ffd5b604d6049366004607d565b5f55565b005b60565f5481565b60405190815260200160405180910390f35b604d5f805490806076836093565b9190505550565b5f60208284031215608c575f5ffd5b5035919050565b5f6001820160af57634e487b7160e01b5f52601160045260245ffd5b506001019056fea2646970667358221220f423ff7a9a85bf49c3769164d3bd24403940510478df27a6b1deac980db69e5664736f6c634300081b0033");
523
524    fn push_m3_success(asserter: &Asserter, returns: &[(bool, Vec<u8>)]) {
525        asserter.push_success(
526            &returns
527                .iter()
528                .map(|&(success, ref data)| IMulticall3::Result {
529                    success,
530                    returnData: Bytes::copy_from_slice(data),
531                })
532                .collect::<Vec<_>>()
533                .abi_encode(),
534        )
535    }
536
537    #[tokio::test]
538    async fn basic_mocked() {
539        let asserter = Asserter::new();
540        let provider =
541            ProviderBuilder::new().with_call_batching().connect_mocked_client(asserter.clone());
542        push_m3_success(
543            &asserter,
544            &[
545                (true, 1.abi_encode()),  // IMulticall3::getBlockNumberCall
546                (true, 2.abi_encode()),  // IMulticall3::getChainIdCall
547                (false, 3.abi_encode()), // IMulticall3::getBlockNumberCall
548                (false, 4.abi_encode()), // IMulticall3::getChainIdCall
549            ],
550        );
551        let (block_number_ok, chain_id_ok, block_number_err, chain_id_err) = tokio::join!(
552            provider.get_block_number(),
553            provider.get_chain_id(),
554            provider.get_block_number(),
555            provider.get_chain_id(),
556        );
557        assert_eq!(block_number_ok.unwrap(), 1);
558        assert_eq!(chain_id_ok.unwrap(), 2);
559        assert!(block_number_err.unwrap_err().to_string().contains("reverted"));
560        assert!(chain_id_err.unwrap_err().to_string().contains("reverted"));
561        assert!(asserter.read_q().is_empty(), "only 1 request should've been made");
562    }
563
564    #[tokio::test]
565    #[cfg(feature = "anvil-api")]
566    async fn basic() {
567        use crate::ext::AnvilApi;
568        let provider = ProviderBuilder::new().with_call_batching().connect_anvil();
569        provider.anvil_set_code(COUNTER_ADDRESS, COUNTER_DEPLOYED_CODE.into()).await.unwrap();
570        provider.anvil_set_balance(COUNTER_ADDRESS, U256::from(123)).await.unwrap();
571
572        let do_calls = || async {
573            tokio::join!(
574                provider.call(
575                    TransactionRequest::default()
576                        .with_to(COUNTER_ADDRESS)
577                        .with_input(hex!("0x8381f58a")) // number()
578                ),
579                provider.call(
580                    TransactionRequest::default()
581                        .with_to(MULTICALL3_ADDRESS)
582                        .with_input(IMulticall3::getBlockNumberCall {}.abi_encode())
583                ),
584                provider.get_block_number(),
585                provider.get_chain_id(),
586                provider.get_balance(COUNTER_ADDRESS),
587            )
588        };
589
590        // Multicall3 has not yet been deployed.
591        let (a, b, c, d, e) = do_calls().await;
592        assert!(a.unwrap_err().to_string().contains("Multicall3 not deployed"));
593        assert!(b.unwrap_err().to_string().contains("Multicall3 not deployed"));
594        assert!(c.unwrap_err().to_string().contains("Multicall3 not deployed"));
595        assert!(d.unwrap_err().to_string().contains("Multicall3 not deployed"));
596        assert!(e.unwrap_err().to_string().contains("Multicall3 not deployed"));
597
598        provider.anvil_set_code(MULTICALL3_ADDRESS, MULTICALL3_DEPLOYED_CODE.into()).await.unwrap();
599
600        let (counter, block_number_raw, block_number, chain_id, balance) = do_calls().await;
601        assert_eq!(counter.unwrap(), 0u64.abi_encode());
602        assert_eq!(block_number_raw.unwrap(), 1u64.abi_encode());
603        assert_eq!(block_number.unwrap(), 1);
604        assert_eq!(chain_id.unwrap(), alloy_chains::NamedChain::AnvilHardhat as u64);
605        assert_eq!(balance.unwrap(), U256::from(123));
606    }
607
608    #[tokio::test]
609    #[ignore]
610    async fn arbitrum() {
611        let url = "https://arbitrum.rpc.subquery.network/public";
612
613        let batched = ProviderBuilder::new().with_call_batching().connect(url).await.unwrap();
614
615        let batch_layer = CallBatchLayer::new().arbitrum_compat();
616        let batched_compat = ProviderBuilder::new().layer(batch_layer).connect(url).await.unwrap();
617
618        // single call so won't go through multicall3
619        let block = batched.get_block_number().await.unwrap();
620
621        // force batching
622        let (b, _) = tokio::join!(batched.get_block_number(), batched.get_chain_id());
623        // we expect this to be the L1 block number
624        let block_wrong = b.unwrap();
625
626        // force batch transaction
627        let (b, _) = tokio::join!(batched_compat.get_block_number(), batched.get_chain_id());
628        // compat mode returns correct block
629        let block_compat = b.unwrap();
630
631        dbg!(block, block_wrong, block_compat);
632
633        // arbitrum blocks move fast so we assert with some error margin
634        assert!(block.abs_diff(block_compat) < 10);
635        assert!(block.abs_diff(block_wrong) > 100_000);
636    }
637}