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
use ethers_core::{
abi::{self, InvalidOutputType},
types::Bytes,
};
use ethers_providers::{Middleware, ProviderError};
use crate::{ContractError, EthError};
#[derive(Debug, thiserror::Error)]
pub enum MulticallError<M: Middleware> {
#[error(transparent)]
ContractError(#[from] ContractError<M>),
#[error("Chain ID {0} is currently not supported by Multicall. Provide an address instead.")]
InvalidChainId(u64),
#[error("Illegal revert: Multicall2 call reverted when it wasn't allowed to.")]
IllegalRevert,
}
impl<M: Middleware> From<abi::Error> for MulticallError<M> {
fn from(value: abi::Error) -> Self {
Self::ContractError(ContractError::DecodingError(value))
}
}
impl<M: Middleware> From<InvalidOutputType> for MulticallError<M> {
fn from(value: InvalidOutputType) -> Self {
Self::ContractError(ContractError::DetokenizationError(value))
}
}
impl<M: Middleware> MulticallError<M> {
pub fn as_contract_error(&self) -> Option<&ContractError<M>> {
match self {
MulticallError::ContractError(e) => Some(e),
_ => None,
}
}
pub fn is_contract_error(&self) -> bool {
matches!(self, MulticallError::ContractError(_))
}
pub fn as_middleware_error(&self) -> Option<&M::Error> {
self.as_contract_error().and_then(ContractError::as_middleware_error)
}
pub fn is_middleware_error(&self) -> bool {
self.as_contract_error().map(ContractError::is_middleware_error).unwrap_or_default()
}
pub fn as_provider_error(&self) -> Option<&ProviderError> {
self.as_contract_error().and_then(ContractError::as_provider_error)
}
pub fn is_provider_error(&self) -> bool {
self.as_contract_error().map(ContractError::is_provider_error).unwrap_or_default()
}
pub fn as_revert(&self) -> Option<&Bytes> {
self.as_contract_error().and_then(ContractError::as_revert)
}
pub fn is_revert(&self) -> bool {
self.as_contract_error().map(ContractError::is_revert).unwrap_or_default()
}
pub fn decode_revert<Err: EthError>(&self) -> Option<Err> {
self.as_revert().and_then(|data| Err::decode_with_selector(data))
}
}