1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
use crate::{
error::MulticallError,
constants,
contract::IMulticall3::{self, IMulticall3Instance},
};
use alloy_contract::{
private::{Transport, Network},
CallBuilder, CallDecoder,
};
use alloy_dyn_abi::{DynSolValue, JsonAbiExt};
use alloy_json_abi::Function;
use alloy_primitives::{Address, Bytes, U256};
use alloy_provider::Provider;
use std::{marker::PhantomData, result::Result as StdResult};
use IMulticall3::Result as MulticallResult;
/// Alias for [std::result::Result]<T, [MulticallError]>
pub type Result<T> = std::result::Result<T, MulticallError>;
/// An individual call within a multicall
#[derive(Debug, Clone)]
pub struct Call {
/// The target
target: Address,
/// The calldata
calldata: Bytes,
/// Whether the call is allowed to fail
allow_failure: bool,
/// The decoder
decoder: Function,
}
impl Call {
pub fn build_call(target: Address, func: &Function, args: &[DynSolValue], allow_failure: bool) -> Self {
Self {
target,
calldata: func.abi_encode_input(args).unwrap().into(),
allow_failure,
decoder: func.clone(),
}
}
}
/// The [Multicall] version - used to determine which methods of the Multicall contract to use:
/// - [`Multicall`] : `aggregate((address,bytes)[])`
/// - [`Multicall2`] : `try_aggregate(bool, (address,bytes)[])`
/// - [`Multicall3`] : `aggregate3((address,bool,bytes)[])` or
/// `aggregate3Value((address,bool,uint256,bytes)[])`
///
/// [`Multicall`]: #variant.Multicall
/// [`Multicall2`]: #variant.Multicall2
/// [`Multicall3`]: #variant.Multicall3
#[repr(u8)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub enum MulticallVersion {
/// Multicall V1
Multicall = 1,
/// Multicall V2
Multicall2 = 2,
/// Multicall V3
#[default]
Multicall3 = 3,
}
impl From<MulticallVersion> for u8 {
fn from(v: MulticallVersion) -> Self {
v as u8
}
}
impl TryFrom<u8> for MulticallVersion {
type Error = String;
fn try_from(v: u8) -> StdResult<Self, Self::Error> {
match v {
1 => Ok(MulticallVersion::Multicall),
2 => Ok(MulticallVersion::Multicall2),
3 => Ok(MulticallVersion::Multicall3),
_ => Err(format!("Invalid Multicall version: {v}. Accepted values: 1, 2, 3.")),
}
}
}
impl MulticallVersion {
/// Returns true if the version is v1
pub const fn is_v1(&self) -> bool {
matches!(self, Self::Multicall)
}
/// Returns true if the version is v2
pub const fn is_v2(&self) -> bool {
matches!(self, Self::Multicall2)
}
/// Returns true if the version is v2
pub const fn is_v3(&self) -> bool {
matches!(self, Self::Multicall3)
}
}
#[derive(Debug, Clone)]
#[must_use = "Multicall does nothing unless you use `call`"]
pub struct Multicall<T, P, N>
where
N: Network,
T: Transport + Clone,
P: Provider<T, N> + Clone,
{
/// The internal calls vector
calls: Vec<Call>,
/// The Multicall3 contract
contract: IMulticall3Instance<T, P, N>,
/// The Multicall version to use. The default is 3.
version: MulticallVersion,
}
impl<T, P, N> Multicall<T, P, N>
where
N: Network,
T: Transport + Clone,
P: Provider<T, N> + Clone,
{
/// Asynchronously creates a new [Multicall] instance from the given provider.
///
/// If provided with an `address`, it instantiates the Multicall contract with that address,
/// otherwise it defaults to
/// [0xcA11bde05977b3631167028862bE2a173976CA11](`constants::MULTICALL_ADDRESS`).
///
/// # Errors
///
/// Returns a [`MulticallError`] if:
/// - The provider returns an error whilst calling `eth_chainId`.
/// - A `None` address is provided, and the provider's chain ID is [not
/// supported](constants::MULTICALL_SUPPORTED_CHAINS).
pub async fn new(provider: P, address: Option<Address>) -> Result<Self> {
// If an address is provided by the user, we'll use this.
// Otherwise fetch the chain ID to confirm it's supported.
let address = match address {
Some(address) => address,
None => {
let chain_id = provider
.get_chain_id()
.await
.map_err(MulticallError::TransportError)?
.into();
if !constants::MULTICALL_SUPPORTED_CHAINS.contains(&chain_id) {
return Err(MulticallError::InvalidChainId(chain_id));
}
constants::MULTICALL_ADDRESS
}
};
// Create the multicall contract
let contract = IMulticall3::new(address, provider);
Ok(Self { calls: vec![], contract, version: MulticallVersion::Multicall3 })
}
/// Synchronously creates a new [Multicall] instance from the given provider.
///
/// If provided with an `address`, it instantiates the Multicall contract with that address.
/// Otherwise if a supported chain_id is provided, it defaults to
/// [0xcA11bde05977b3631167028862bE2a173976CA11](`constants::MULTICALL_ADDRESS`).
///
/// # Errors
///
/// Returns a [`MulticallError`] if:
/// - The provided `chain_id` is [not supported](constants::MULTICALL_SUPPORTED_CHAINS).
/// - Neither an `address` or `chain_id` is provided. This method requires at least one of these
/// to be provided.
pub async fn new_with_chain_id(
provider: P,
address: Option<Address>,
chain_id: Option<impl Into<u64>>,
) -> Result<Self> {
let address = match (address, chain_id) {
(Some(address), _) => address,
(_, Some(chain_id)) => {
let chain_id = chain_id.into();
if !constants::MULTICALL_SUPPORTED_CHAINS.contains(&chain_id) {
return Err(MulticallError::InvalidChainId(chain_id));
}
constants::MULTICALL_ADDRESS
}
// If neither an address or chain_id is provided then return an error.
_ => return Err(MulticallError::InvalidInitializationParams),
};
// Create the multicall contract
let contract = IMulticall3::new(address, provider);
Ok(Self { calls: vec![], contract, version: MulticallVersion::Multicall3 })
}
/// Sets the [MulticallVersion] which is used to determine which functions to use when making
/// the contract call. The default is 3, and the default will also be used if an invalid version
/// is provided.
///
/// Version differences (adapted from [here](https://github.com/mds1/multicall#multicall---)):
///
/// - Multicall (v1): This is the version of the original [MakerDAO Multicall](https://github.com/makerdao/multicall).
/// It provides an `aggregate` method to allow batching calls, and none of the calls are
/// allowed to fail.
///
/// - Multicall2 (v2): The same as Multicall, but provides additional methods that allow either
/// all or no calls within the batch to fail. Included for backward compatibility. Use v3 to
/// allow failure on a per-call basis.
///
/// - Multicall3 (v3): This is the recommended version, which is backwards compatible with both
/// Multicall & Multicall2. It provides additional methods for specifying whether calls are
/// allowed to fail on a per-call basis, and is also cheaper to use (so you can fit more calls
/// into a single request).
///
/// Note: all these versions are available in the same contract address
/// ([`constants::MULTICALL_ADDRESS`]) so changing version just changes the methods used,
/// not the contract address.
pub fn set_version(&mut self, version: impl TryInto<MulticallVersion>) {
match version.try_into() {
Ok(v) => self.version = v,
Err(_) => self.version = MulticallVersion::Multicall3,
}
}
/// Same functionality as [set_version], but uses a builder pattern to return the updated
/// [Multicall] instance.
///
/// [set_version]: #method.set_version
pub fn with_version(&mut self, version: impl TryInto<MulticallVersion>) -> &mut Self {
match version.try_into() {
Ok(v) => self.version = v,
Err(_) => self.version = MulticallVersion::Multicall3,
};
self
}
/// Appends a [`Call`] to the internal calls vector.
///
/// Version specific details:
/// - `1`: `allow_failure` is ignored.
/// - `2`: `allow_failure` specifies whether or not this call is allowed to revert in the
/// multicall. If this is false for any of the calls, then the entire multicall will revert if
/// the individual call reverts.
/// - `3`: `allow_failure` specifies whether or not this call is allowed to revert in the
/// multicall. This is on a per-call basis, however if this is `false` for an individual call
/// and the call reverts, then this will cause the entire multicall to revert.
pub fn add_call(&mut self, target: Address, func: &Function, args: &[DynSolValue], allow_failure: bool) {
let call = Call::build_call(target, func, args, allow_failure);
self.calls.push(call)
}
/// Builder pattern to add a [Call] to the internal calls vector and return the [Multicall]. See
/// [`add_call`] for more details.
///
/// [`add_call`]: #method.add_call
pub fn with_call(&mut self, target: Address, func: &Function, args: &[DynSolValue], allow_failure: bool) -> &mut Self {
let call = Call::build_call(target, func, args, allow_failure);
self.calls.push(call);
self
}
/// Returns the current instantiated [Multicall] instance with an empty `calls` vector.
/// This allows the user to reuse the instance to perform another aggregate query.
pub fn clear_calls(&mut self) -> &mut Self {
self.calls.clear();
self
}
/// Queries the multicall contract via `eth_call` and returns the decoded result.
///
/// Returns a vector of [StdResult]<[DynSolValue], [Bytes]> for each internal call:
/// - Ok([DynSolValue]) if the call was successful.
/// - Err([Bytes]) if the individual call failed whilst `allowFailure` was true, or if the
/// return data was empty.
///
/// # Errors
///
/// Returns a [MulticallError] if the Multicall call failed. This can occur due to RPC errors,
/// or if an individual call failed whilst `allowFailure` was false.
pub async fn call(&self) -> Result<Vec<StdResult<DynSolValue, Bytes>>> {
match self.version {
MulticallVersion::Multicall => {
let call = self.as_aggregate();
let multicall_result = call.call().await?;
self.parse_multicall_result(
multicall_result.returnData.into_iter().map(|return_data| MulticallResult {
success: true,
returnData: return_data,
}),
)
}
MulticallVersion::Multicall2 => {
let call = self.as_try_aggregate();
let multicall_result = call.call().await?;
self.parse_multicall_result(multicall_result.returnData)
}
MulticallVersion::Multicall3 => {
let call = self.as_aggregate_3();
let multicall_result = call.call().await?;
self.parse_multicall_result(multicall_result.returnData)
}
}
}
/// Appends a `call` to the list of calls of the Multicall instance for querying the block hash
/// of a given block number.
///
/// Note: this call will return 0 if `block_number` is not one of the most recent 256 blocks.
/// ([Reference](https://docs.soliditylang.org/en/latest/units-and-global-variables.html?highlight=blockhash#block-and-transaction-properties))
pub fn add_get_block_hash(&mut self, block_number: impl Into<U256>) -> &mut Self {
let functions = IMulticall3::abi::functions();
let get_block_hash_function = functions.get("getBlockHash").unwrap().first().unwrap();
self.with_call(*self.contract.address(), get_block_hash_function, &[DynSolValue::from(block_number.into())], false)
}
/// Appends a `call` to the list of calls of the Multicall instance for querying the current
/// block number.
pub fn add_get_block_number(&mut self) -> &mut Self {
let functions = IMulticall3::abi::functions();
let get_block_hash_function = functions.get("getBlockNumber").unwrap().first().unwrap();
self.with_call(*self.contract.address(), get_block_hash_function, &[], false)
}
/// Appends a `call` to the list of calls of the Multicall instance for querying the current
/// block coinbase address.
pub fn add_get_current_block_coinbase(&mut self) -> &mut Self {
let functions = IMulticall3::abi::functions();
let get_block_hash_function =
functions.get("getCurrentBlockCoinbase").unwrap().first().unwrap();
self.with_call(*self.contract.address(), get_block_hash_function, &[], false)
}
/// Appends a `call` to the list of calls of the Multicall instance for querying the current
/// block difficulty.
///
/// Note: in a post-merge environment, the return value of this call will be the output of the
/// randomness beacon provided by the beacon chain.
/// ([Reference](https://eips.ethereum.org/EIPS/eip-4399#abstract))
pub fn add_get_current_block_difficulty(&mut self) -> &mut Self {
let functions = IMulticall3::abi::functions();
let get_block_hash_function =
functions.get("getCurrentBlockDifficulty").unwrap().first().unwrap();
self.with_call(*self.contract.address(), get_block_hash_function, &[], false)
}
/// Appends a `call` to the list of calls of the Multicall instance for querying the current
/// block gas limit.
pub fn add_get_current_block_gas_limit(&mut self) -> &mut Self {
let functions = IMulticall3::abi::functions();
let get_block_hash_function =
functions.get("getCurrentBlockGasLimit").unwrap().first().unwrap();
self.with_call(*self.contract.address(), get_block_hash_function, &[], false)
}
/// Appends a `call` to the list of calls of the Multicall instance for querying the current
/// block timestamp.
pub fn add_get_current_block_timestamp(&mut self) -> &mut Self {
let functions = IMulticall3::abi::functions();
let get_block_hash_function =
functions.get("getCurrentBlockTimestamp").unwrap().first().unwrap();
self.with_call(*self.contract.address(), get_block_hash_function, &[], false)
}
/// Appends a `call` to the list of calls of the Multicall instance for querying the ETH
/// balance of an address.
pub fn add_get_eth_balance(&mut self, address: impl Into<Address>) -> &mut Self {
let functions = IMulticall3::abi::functions();
let get_block_hash_function = functions.get("getEthBalance").unwrap().first().unwrap();
self.with_call(*self.contract.address(), get_block_hash_function, &[DynSolValue::from(address.into())], false)
}
/// Appends a `call` to the list of calls of the Multicall instance for querying the last
/// block hash.
pub fn add_get_last_block_hash(&mut self) -> &mut Self {
let functions = IMulticall3::abi::functions();
let get_block_hash_function = functions.get("getLastBlockHash").unwrap().first().unwrap();
self.with_call(*self.contract.address(), get_block_hash_function, &[], false)
}
/// Appends a `call` to the list of calls of the Multicall instance for querying the current
/// block base fee.
///
/// Note: this call will fail if the chain that it is called on does not implement the
/// [BASEFEE opcode](https://eips.ethereum.org/EIPS/eip-3198).
pub fn add_get_basefee(&mut self, allow_failure: bool) -> &mut Self {
let functions = IMulticall3::abi::functions();
let get_block_hash_function = functions.get("getBasefee").unwrap().first().unwrap();
self.with_call(*self.contract.address(), get_block_hash_function, &[], allow_failure)
}
/// Appends a `call` to the list of calls of the Multicall instance for querying the last
/// block hash.
pub fn add_get_chain_id(&mut self) -> &mut Self {
let functions = IMulticall3::abi::functions();
let get_block_hash_function = functions.get("getChainId").unwrap().first().unwrap();
self.with_call(*self.contract.address(), get_block_hash_function, &[], false)
}
/// Uses the Multicall `aggregate(Call[] calldata calls)` method which returns a tuple of
/// (uint256 blockNumber, bytes[] returnData) when called. The EVM call reverts if any
/// individual call fails. This is used when using [MulticallVersion] V1.
///
/// # Returns
/// Returns a [CallBuilder], which uses [IMulticall3::aggregateCall] for decoding.
pub fn as_aggregate(&self) -> CallBuilder<T, &P, PhantomData<IMulticall3::aggregateCall>, N> {
let calls = self
.calls
.clone()
.into_iter()
.map(|call| IMulticall3::Call { target: call.target, callData: call.calldata })
.collect::<Vec<IMulticall3::Call>>();
self.contract.aggregate(calls)
}
/// Uses the Multicall `tryAggregate(bool requireSuccess, Call[] calldata calls)` method which
/// returns a tuple of (bool success, bytes[] returnData)[] when called. The EVM call reverts if
/// any individual call fails. This is used when using [MulticallVersion] V2.
///
/// # Returns
/// Returns a [CallBuilder], which uses [IMulticall3::tryAggregateCall] for decoding.
pub fn as_try_aggregate(
&self,
) -> CallBuilder<T, &P, PhantomData<IMulticall3::tryAggregateCall>, N> {
let mut allow_failure = true;
let calls = self
.calls
.clone()
.into_iter()
.map(|call| {
// If any call has `allow_failure = false`, then set allow_failure to false. The
// `tryAggregate` contract call reverts if any of the individual calls revert.
allow_failure &= call.allow_failure;
IMulticall3::Call { target: call.target, callData: call.calldata }
})
.collect::<Vec<IMulticall3::Call>>();
self.contract.tryAggregate(!allow_failure, calls)
}
/// Uses the Multicall `aggregate3(Call3[] calldata calls)` method which returns `Results[]
/// returnData` when called.
///
/// If any of the individual calls has `allow_failure = false` then the entire multicall will
/// fail.
///
/// This is used when using [MulticallVersion] V3.
///
/// # Returns
/// Returns a [CallBuilder], which uses [IMulticall3::aggregate3Call] for decoding.
pub fn as_aggregate_3(
&self,
) -> CallBuilder<T, &P, PhantomData<IMulticall3::aggregate3Call>, N> {
let calls = self
.calls
.clone()
.into_iter()
.map(|call| IMulticall3::Call3 {
target: call.target,
callData: call.calldata,
allowFailure: call.allow_failure,
})
.collect::<Vec<IMulticall3::Call3>>();
self.contract.aggregate3(calls)
}
/// Decodes the return data for each individual call result within a multicall.
///
/// # Returns
/// - Err([MulticallError]) if an individual call failed and `allow_failure` was false.
/// - Err([MulticallError]) if there was an error decoding the return data of any individual
/// call.
/// - Ok([Vec]<[StdResult]<[DynSolValue], [Bytes]>>) (see below).
///
/// For each individual call it will return a [StdResult] based on the call's `success` value:
/// - true: returns Ok([`DynSolValue`]). If there is more than 1 return value, then it will
/// return a [`DynSolValue`] tuple of the return values. Otherwise returns the corresponding
/// [`DynSolValue`] variant.
/// - false: returns Err([Bytes]) containing the raw bytes of the error returned by the
/// contract.
pub fn parse_multicall_result(
&self,
return_data: impl IntoIterator<Item = MulticallResult>,
) -> Result<Vec<StdResult<DynSolValue, Bytes>>> {
let iter = return_data.into_iter();
let mut results = Vec::with_capacity(self.calls.len());
for (call, MulticallResult { success, returnData }) in self.calls.iter().zip(iter) {
// TODO - should empty return data also be considered a call failure, and returns an
// error when allow_failure = false?
let result = if !success {
if !call.allow_failure {
return Err(MulticallError::FailedCall);
}
Err(returnData)
} else {
let decoded = call
.decoder
.abi_decode_output(returnData, false)
.map_err(MulticallError::ContractError);
if let Err(err) = decoded {
// TODO should we return out of the function here, or assign empty bytes to
// result? Linked to above TODO
return Err(err);
} else {
let mut decoded = decoded.unwrap();
// Return the single `DynSolValue` if there's only 1 return value, otherwise
// return a tuple of `DynSolValue` elements
Ok(if decoded.len() == 1 {
decoded.pop().unwrap()
} else {
DynSolValue::Tuple(decoded)
})
}
};
results.push(result);
}
Ok(results)
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::{address, utils::format_ether};
use alloy_sol_types::sol;
sol! {
#[derive(Debug, PartialEq)]
#[sol(rpc, abi, extra_methods)]
interface ERC20 {
function totalSupply() external view returns (uint256 totalSupply);
function balanceOf(address owner) external view returns (uint256 balance);
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
}
}
#[tokio::test]
async fn test_create_multicall() {
let rpc_url = "https://rpc.ankr.com/eth".parse().unwrap();
let provider = alloy_provider::ProviderBuilder::new().on_http(rpc_url);
// New Multicall with default address 0xcA11bde05977b3631167028862bE2a173976CA11
let multicall = Multicall::new(&provider, None).await.unwrap();
assert_eq!(multicall.contract.address(), &constants::MULTICALL_ADDRESS);
// New Multicall with user provided address
let multicall_address = Address::ZERO;
let multicall = Multicall::new(&provider, Some(multicall_address)).await.unwrap();
assert_eq!(multicall.contract.address(), &multicall_address);
}
#[tokio::test]
async fn test_multicall_weth() {
let rpc_url = "https://rpc.ankr.com/eth".parse().unwrap();
let provider = alloy_provider::ProviderBuilder::new().on_http(rpc_url);
let weth_address = address!("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
// Create the multicall instance
let mut multicall = Multicall::new(provider.clone(), None).await.unwrap();
// Generate the WETH ERC20 instance we'll be using to create the individual calls
let functions = ERC20::abi::functions();
// Create the individual calls
let name_call = functions.get("name").unwrap().first().unwrap();
let total_supply_call = functions.get("totalSupply").unwrap().first().unwrap();
let decimals_call = functions.get("decimals").unwrap().first().unwrap();
let symbol_call = functions.get("symbol").unwrap().first().unwrap();
// Add the calls
multicall.add_call(weth_address, total_supply_call, &[], true);
multicall.add_call(weth_address, name_call, &[], true);
multicall.add_call(weth_address, decimals_call, &[], true);
multicall.add_call(weth_address, symbol_call, &[], true);
// Add the same calls via the builder pattern
multicall
.with_call(weth_address, total_supply_call, &[], true)
.with_call(weth_address, name_call, &[], true)
.with_call(weth_address, decimals_call, &[], true)
.with_call(weth_address, symbol_call, &[], true)
.add_get_chain_id();
// Send and await the multicall results
// MulticallV1
multicall.set_version(1);
let results = multicall.call().await.unwrap();
assert_results(results);
// MulticallV2
multicall.set_version(2);
let results = multicall.call().await.unwrap();
assert_results(results);
// MulticallV3
multicall.set_version(3);
let results = multicall.call().await.unwrap();
assert_results(results);
}
#[tokio::test]
async fn test_multicall_specific_methods() {
let rpc_url = "https://rpc.ankr.com/eth".parse().unwrap();
let provider = alloy_provider::ProviderBuilder::new().on_http(rpc_url);
let mut multicall = Multicall::new(provider, None).await.unwrap();
multicall
.add_get_basefee(false)
.add_get_block_hash(U256::from(20669406))
.add_get_block_number()
.add_get_chain_id()
.add_get_current_block_coinbase()
.add_get_current_block_difficulty()
.add_get_current_block_gas_limit()
.add_get_current_block_timestamp()
.add_get_last_block_hash()
.add_get_eth_balance(address!("3bfc20f0b9afcace800d73d2191166ff16540258"));
let results = multicall.call().await.unwrap();
let chain_id = results.get(3).unwrap().as_ref().unwrap().as_uint().unwrap().0.to::<u64>();
let gas_limit = results.get(6).unwrap().as_ref().unwrap().as_uint().unwrap().0.to::<u64>();
let eth_balance =
format_ether(results.get(9).unwrap().as_ref().unwrap().as_uint().unwrap().0)
.split('.')
.collect::<Vec<&str>>()
.first()
.unwrap()
.parse::<u64>()
.unwrap();
assert_eq!(chain_id, 1); // Provider forked from Mainnet should always have chain ID 1
assert_eq!(gas_limit, 30_000_000); // Mainnet gas limit is 30m
assert!((306_276..=306_277).contains(ð_balance)); // Parity multisig bug affected wallet
// - balance isn't expected to change
// significantly
}
fn assert_results(results: Vec<StdResult<DynSolValue, Bytes>>) {
// Get the expected individual results.
let name = results.get(1).unwrap().as_ref().unwrap().as_str().unwrap();
let decimals = results.get(2).unwrap().as_ref().unwrap().as_uint().unwrap().0.to::<u8>();
let symbol = results.get(3).unwrap().as_ref().unwrap().as_str().unwrap();
// Assert the returned results are as expected
assert_eq!(name, "Wrapped Ether");
assert_eq!(symbol, "WETH");
assert_eq!(decimals, 18);
// Also check the calls that were added via the builder pattern
let name = results.get(5).unwrap().as_ref().unwrap().as_str().unwrap();
let decimals = results.get(6).unwrap().as_ref().unwrap().as_uint().unwrap().0.to::<u8>();
let symbol = results.get(7).unwrap().as_ref().unwrap().as_str().unwrap();
let chain_id = results.get(8).unwrap().as_ref().unwrap().as_uint().unwrap().0.to::<u64>();
assert_eq!(name, "Wrapped Ether");
assert_eq!(symbol, "WETH");
assert_eq!(decimals, 18);
assert_eq!(chain_id, 1);
}
}