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
use std::result::Result as StdResult;

/// The Multicall contract bindings. Auto-generated with `abigen`.
pub mod contract;

pub mod constants;

if_providers! {
    mod middleware;
    pub use middleware::{Call, Multicall, MulticallContract};

    pub mod error;
}

/// The version of the [`Multicall`].
/// Used to determine which methods of the Multicall smart 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 {
    /// V1
    Multicall = 1,
    /// V2
    Multicall2 = 2,
    /// 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 {
    /// True if call is v1
    #[inline]
    pub fn is_v1(&self) -> bool {
        matches!(self, Self::Multicall)
    }

    /// True if call is v2
    #[inline]
    pub fn is_v2(&self) -> bool {
        matches!(self, Self::Multicall2)
    }

    /// True if call is v3
    #[inline]
    pub fn is_v3(&self) -> bool {
        matches!(self, Self::Multicall3)
    }
}