|
| 1 | +//! Demonstrate the Multicall Batch Layer. |
| 2 | +//! Provider layer that aggregates contract calls (`eth_call`) over a time period into a single |
| 3 | +//! Multicall3 contract call. This is useful for reducing the number of requests made to the RPC. |
| 4 | +
|
| 5 | +use std::time::Duration; |
| 6 | + |
| 7 | +use alloy::{ |
| 8 | + primitives::address, |
| 9 | + providers::{layers::CallBatchLayer, Provider, ProviderBuilder}, |
| 10 | + sol, |
| 11 | +}; |
| 12 | +use eyre::Result; |
| 13 | +use IWETH9::{balanceOfCall, totalSupplyCall, IWETH9Instance}; |
| 14 | + |
| 15 | +sol!( |
| 16 | + #[allow(missing_docs)] |
| 17 | + #[sol(rpc)] |
| 18 | + #[derive(Debug)] |
| 19 | + IWETH9, |
| 20 | + "examples/abi/IWETH9.json" |
| 21 | +); |
| 22 | + |
| 23 | +#[tokio::main] |
| 24 | +async fn main() -> Result<()> { |
| 25 | + // Instantiate a provider with the `CallBatchLayer` enabled. |
| 26 | + let provider = ProviderBuilder::new() |
| 27 | + // Enables `eth_call` batching by leveraging the Multicall3 contract. |
| 28 | + // The `CallBatchLayer` will wait for a certain amount of time before sending a request. See: <https://docs.rs/alloy-provider/latest/alloy_provider/layers/struct.CallBatchLayer.html#method.wait>. |
| 29 | + // This delay is added to aggregate any incoming `eth_calls` that can be together. |
| 30 | + // In this case, we set the delay to 10ms. |
| 31 | + .layer(CallBatchLayer::new().wait(Duration::from_secs(10))) |
| 32 | + // Can also use the shorthand `with_call_batching` on the build which set the delay to 1ms. |
| 33 | + // .with_call_batching() |
| 34 | + .on_anvil_with_wallet_and_config(|a| a.fork("https://eth.merkle.io"))?; |
| 35 | + |
| 36 | + // Create a new instance of the IWETH9 contract. |
| 37 | + let weth = |
| 38 | + IWETH9Instance::new(address!("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"), &provider); |
| 39 | + |
| 40 | + let alice = address!("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"); |
| 41 | + let bob = address!("0xc7bBeC68d12a0d1830360F8Ec58fA599bA1b0e9b"); |
| 42 | + |
| 43 | + // Calls that will be batched. |
| 44 | + let alice_weth = weth.balanceOf(alice).into_transaction_request(); |
| 45 | + let bob_weth = weth.balanceOf(bob).into_transaction_request(); |
| 46 | + let total_supply = weth.totalSupply().into_transaction_request(); |
| 47 | + |
| 48 | + // Requests need to be parallelized to be batched. |
| 49 | + let (alice_weth, bob_weth, total_supply, block_number, alice_eth) = tokio::try_join!( |
| 50 | + // Batch `eth_call` requests. |
| 51 | + provider.call(alice_weth).decode_resp::<balanceOfCall>(), |
| 52 | + provider.call(bob_weth).decode_resp::<balanceOfCall>(), |
| 53 | + provider.call(total_supply).decode_resp::<totalSupplyCall>(), |
| 54 | + // Get block number and get balance calls can also be batched. |
| 55 | + provider.get_block_number(), |
| 56 | + provider.get_balance(alice) |
| 57 | + )?; |
| 58 | + |
| 59 | + // Resolve `Ok`. |
| 60 | + let alice_weth = alice_weth?; |
| 61 | + let bob_weth = bob_weth?; |
| 62 | + let total_supply = total_supply?; |
| 63 | + |
| 64 | + println!("Block Number: {block_number}"); |
| 65 | + println!( |
| 66 | + "Alice's WETH balance: {}\nBob's WETH balance: {}\nTotal WETH supply: {}\nAlice's ETH |
| 67 | + balance: {}", |
| 68 | + alice_weth._0, bob_weth._0, total_supply._0, alice_eth |
| 69 | + ); |
| 70 | + |
| 71 | + Ok(()) |
| 72 | +} |
0 commit comments