Skip to main content

cbrzn_ethers_core/abi/
mod.rs

1//! This module implements extensions to the [`ethabi`](https://docs.rs/ethabi) API.
2// Adapted from [Gnosis' ethcontract](https://github.com/gnosis/ethcontract-rs/blob/master/common/src/abiext.rs)
3use crate::{
4    types::{Bytes, Selector, Uint8, H256, H512, I256, U128, U256, U64},
5    utils::id,
6};
7pub use ethabi::{self, Contract as Abi, *};
8
9mod tokens;
10pub use tokens::{Detokenize, InvalidOutputType, Tokenizable, TokenizableItem, Tokenize};
11
12pub mod struct_def;
13pub use struct_def::SolStruct;
14
15mod codec;
16pub use codec::{AbiDecode, AbiEncode};
17
18mod error;
19pub use error::{AbiError, ParseError};
20
21mod human_readable;
22pub use human_readable::{
23    lexer::HumanReadableParser, parse as parse_abi, parse_str as parse_abi_str, AbiParser,
24};
25
26mod raw;
27pub use raw::{AbiObject, Component, Item, JsonAbi, RawAbi};
28
29mod packed;
30pub use packed::{encode_packed, EncodePackedError};
31
32mod sealed {
33    use ethabi::{Event, Function};
34
35    /// private trait to ensure extension traits are used as intended
36    pub trait Sealed {}
37    impl Sealed for Function {}
38    impl Sealed for Event {}
39    impl Sealed for ethabi::AbiError {}
40}
41
42/// Extension trait for `ethabi::Function`.
43pub trait FunctionExt: sealed::Sealed {
44    /// Compute the method signature in the standard ABI format. This does not
45    /// include the output types.
46    fn abi_signature(&self) -> String;
47
48    /// Compute the Keccak256 function selector used by contract ABIs.
49    fn selector(&self) -> Selector;
50}
51
52impl FunctionExt for Function {
53    fn abi_signature(&self) -> String {
54        let mut full_signature = self.signature();
55        if let Some(colon) = full_signature.find(':') {
56            full_signature.truncate(colon);
57        }
58
59        full_signature
60    }
61
62    fn selector(&self) -> Selector {
63        id(self.abi_signature())
64    }
65}
66
67/// Extension trait for `ethabi::Event`.
68pub trait EventExt: sealed::Sealed {
69    /// Compute the event signature in human-readable format. The `keccak256`
70    /// hash of this value is the actual event signature that is used as topic0
71    /// in the transaction logs.
72    fn abi_signature(&self) -> String;
73}
74
75impl EventExt for Event {
76    fn abi_signature(&self) -> String {
77        format!(
78            "{}({}){}",
79            self.name,
80            self.inputs.iter().map(|input| input.kind.to_string()).collect::<Vec<_>>().join(","),
81            if self.anonymous { " anonymous" } else { "" },
82        )
83    }
84}
85
86/// Extension trait for `ethabi::AbiError`.
87pub trait ErrorExt: sealed::Sealed {
88    /// Compute the method signature in the standard ABI format.
89    fn abi_signature(&self) -> String;
90
91    /// Compute the Keccak256 error selector used by contract ABIs.
92    fn selector(&self) -> Selector;
93}
94
95impl ErrorExt for ethabi::AbiError {
96    fn abi_signature(&self) -> String {
97        if self.inputs.is_empty() {
98            return format!("{}()", self.name)
99        }
100        let inputs = self.inputs.iter().map(|p| p.kind.to_string()).collect::<Vec<_>>().join(",");
101        format!("{}({inputs})", self.name)
102    }
103
104    fn selector(&self) -> Selector {
105        id(self.abi_signature())
106    }
107}
108
109/// A trait for types that can be represented in the ethereum ABI.
110pub trait AbiType {
111    /// The native ABI type this type represents.
112    fn param_type() -> ParamType;
113}
114
115impl AbiType for u8 {
116    fn param_type() -> ParamType {
117        ParamType::Uint(8)
118    }
119}
120
121/// Additional trait for types that can appear in arrays
122///
123/// NOTE: this is necessary to handle the special case of `Vec<u8> => Bytes`
124pub trait AbiArrayType: AbiType {}
125
126impl<T: AbiArrayType> AbiType for Vec<T> {
127    fn param_type() -> ParamType {
128        ParamType::Array(Box::new(T::param_type()))
129    }
130}
131impl<T: AbiArrayType> AbiArrayType for Vec<T> {}
132
133impl<T: AbiArrayType, const N: usize> AbiType for [T; N] {
134    fn param_type() -> ParamType {
135        ParamType::FixedArray(Box::new(T::param_type()), N)
136    }
137}
138
139impl<T: AbiArrayType, const N: usize> AbiArrayType for [T; N] {}
140
141impl<const N: usize> AbiType for [u8; N] {
142    fn param_type() -> ParamType {
143        ParamType::FixedBytes(N)
144    }
145}
146impl<const N: usize> AbiArrayType for [u8; N] {}
147
148macro_rules! impl_abi_type {
149    ($($name:ty => $var:ident $(($value:expr))? ),*) => {
150        $(
151            impl AbiType for $name {
152                fn param_type() -> ParamType {
153                    ParamType::$var $( ($value) )?
154                }
155            }
156
157            impl AbiArrayType for $name {}
158        )*
159    };
160}
161
162impl_abi_type!(
163    Bytes => Bytes,
164    bytes::Bytes => Bytes,
165    Vec<u8> =>  Array(Box::new(ParamType::Uint(8))),
166    Address => Address,
167    bool => Bool,
168    String => String,
169    str => String,
170    H256 => FixedBytes(32),
171    H512 => FixedBytes(64),
172    Uint8 => Uint(8),
173    U64 => Uint(64),
174    U128 => Uint(128),
175    U256 => Uint(256),
176    u16 => Uint(16),
177    u32 => Uint(32),
178    u64 => Uint(64),
179    u128 => Uint(128),
180    i8 => Int(8),
181    i16 => Int(16),
182    i32 => Int(32),
183    i64 => Int(64),
184    i128 => Int(128),
185    I256 => Int(256)
186);
187
188impl<'a> AbiType for &'a str {
189    fn param_type() -> ParamType {
190        ParamType::String
191    }
192}
193
194impl<'a> AbiArrayType for &'a str {}
195
196macro_rules! impl_abi_type_tuple {
197    ($num: expr, $( $ty: ident),+) => {
198        impl<$($ty, )+> AbiType for ($($ty,)+) where
199            $(
200                $ty: AbiType,
201            )+
202        {
203            fn param_type() -> ParamType {
204                ParamType::Tuple(
205                    ::std::vec![
206                         $(
207                           $ty::param_type(),
208                        )+
209                    ]
210                )
211            }
212        }
213
214        impl<$($ty, )+> AbiArrayType for ($($ty,)+) where
215            $(
216                $ty: AbiType,
217            )+ {}
218    }
219}
220
221impl_abi_type_tuple!(1, A);
222impl_abi_type_tuple!(2, A, B);
223impl_abi_type_tuple!(3, A, B, C);
224impl_abi_type_tuple!(4, A, B, C, D);
225impl_abi_type_tuple!(5, A, B, C, D, E);
226impl_abi_type_tuple!(6, A, B, C, D, E, F);
227impl_abi_type_tuple!(7, A, B, C, D, E, F, G);
228impl_abi_type_tuple!(8, A, B, C, D, E, F, G, H);
229impl_abi_type_tuple!(9, A, B, C, D, E, F, G, H, I);
230impl_abi_type_tuple!(10, A, B, C, D, E, F, G, H, I, J);
231impl_abi_type_tuple!(11, A, B, C, D, E, F, G, H, I, J, K);
232impl_abi_type_tuple!(12, A, B, C, D, E, F, G, H, I, J, K, L);
233impl_abi_type_tuple!(13, A, B, C, D, E, F, G, H, I, J, K, L, M);
234impl_abi_type_tuple!(14, A, B, C, D, E, F, G, H, I, J, K, L, M, N);
235impl_abi_type_tuple!(15, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
236impl_abi_type_tuple!(16, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);
237impl_abi_type_tuple!(17, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q);
238impl_abi_type_tuple!(18, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R);
239impl_abi_type_tuple!(19, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S);
240impl_abi_type_tuple!(20, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T);
241impl_abi_type_tuple!(21, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U);
242
243#[allow(clippy::extra_unused_type_parameters)]
244#[cfg(test)]
245mod tests {
246    use super::*;
247
248    #[test]
249    fn format_function_signature() {
250        for (f, expected) in &[
251            (
252                r#"{"name":"foo","inputs":[],"outputs":[], "stateMutability": "nonpayable"}"#,
253                "foo()",
254            ),
255            (
256                r#"{"name":"bar","inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"bool"}],"outputs":[], "stateMutability": "nonpayable"}"#,
257                "bar(uint256,bool)",
258            ),
259            (
260                r#"{"name":"baz","inputs":[{"name":"a","type":"uint256"}],"outputs":[{"name":"b","type":"bool"}], "stateMutability": "nonpayable"}"#,
261                "baz(uint256)",
262            ),
263            (
264                r#"{"name":"bax","inputs":[],"outputs":[{"name":"a","type":"uint256"},{"name":"b","type":"bool"}], "stateMutability": "nonpayable"}"#,
265                "bax()",
266            ),
267        ] {
268            let function: Function = serde_json::from_str(f).expect("invalid function JSON");
269            let signature = function.abi_signature();
270            assert_eq!(signature, *expected);
271        }
272    }
273
274    #[test]
275    fn format_event_signature() {
276        for (e, expected) in &[
277            (r#"{"name":"foo","inputs":[],"anonymous":false}"#, "foo()"),
278            (
279                r#"{"name":"bar","inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"bool"}],"anonymous":false}"#,
280                "bar(uint256,bool)",
281            ),
282            (
283                r#"{"name":"baz","inputs":[{"name":"a","type":"uint256"}],"anonymous":true}"#,
284                "baz(uint256) anonymous",
285            ),
286            (r#"{"name":"bax","inputs":[],"anonymous":true}"#, "bax() anonymous"),
287        ] {
288            let event: Event = serde_json::from_str(e).expect("invalid event JSON");
289            let signature = event.abi_signature();
290            assert_eq!(signature, *expected);
291        }
292    }
293
294    #[test]
295    fn abi_type_works() {
296        assert_eq!(ParamType::Bytes, Bytes::param_type());
297        assert_eq!(ParamType::Array(Box::new(ParamType::Uint(8))), Vec::<u8>::param_type());
298        assert_eq!(ParamType::Array(Box::new(ParamType::Bytes)), Vec::<Bytes>::param_type());
299        assert_eq!(
300            ParamType::Array(Box::new(ParamType::Array(Box::new(ParamType::Uint(8))))),
301            Vec::<Vec<u8>>::param_type()
302        );
303        assert_eq!(
304            ParamType::Array(Box::new(ParamType::Array(Box::new(ParamType::Array(Box::new(
305                ParamType::Uint(8)
306            )))))),
307            Vec::<Vec<Vec<u8>>>::param_type()
308        );
309
310        assert_eq!(ParamType::Array(Box::new(ParamType::Uint(16))), Vec::<u16>::param_type());
311
312        assert_eq!(
313            ParamType::Tuple(vec![ParamType::Bytes, ParamType::Address]),
314            <(Bytes, Address)>::param_type()
315        );
316
317        assert_eq!(ParamType::FixedBytes(32), <[u8; 32]>::param_type());
318        assert_eq!(
319            ParamType::Array(Box::new(ParamType::FixedBytes(32))),
320            Vec::<[u8; 32]>::param_type()
321        );
322
323        assert_eq!(
324            ParamType::FixedArray(Box::new(ParamType::Uint(16)), 32),
325            <[u16; 32]>::param_type()
326        );
327
328        assert_eq!(ParamType::String, str::param_type());
329        assert_eq!(ParamType::String, <&str>::param_type());
330    }
331
332    #[test]
333    fn abi_type_tuples_work() {
334        fn assert_abitype<T: AbiType>() {}
335        fn assert_abiarraytype<T: AbiArrayType>() {}
336
337        assert_abitype::<(u64, u64)>();
338        assert_abiarraytype::<(u64, u64)>();
339
340        assert_abitype::<(u8, u8)>();
341        assert_abiarraytype::<(u8, u8)>();
342
343        assert_abitype::<Vec<(u64, u64)>>();
344        assert_abiarraytype::<Vec<(u64, u64)>>();
345
346        assert_abitype::<Vec<(u8, u8)>>();
347        assert_abiarraytype::<Vec<(u8, u8)>>();
348    }
349}