ethcontract_common/
abiext.rs

1//! This module implements extensions to the `ethabi` API.
2
3use crate::abi::{Event, Function, ParamType};
4use crate::errors::ParseParamTypeError;
5use crate::hash::{self, H32};
6use serde_json::json;
7
8/// Extension trait for `ethabi::Function`.
9pub trait FunctionExt {
10    /// Computes the method signature in the standard ABI format. This does not
11    /// include the output types.
12    fn abi_signature(&self) -> String;
13
14    /// Computes the Keccak256 function selector used by contract ABIs.
15    fn selector(&self) -> H32;
16}
17
18impl FunctionExt for Function {
19    fn abi_signature(&self) -> String {
20        let mut full_signature = self.signature();
21        if let Some(colon) = full_signature.find(':') {
22            full_signature.truncate(colon);
23        }
24
25        full_signature
26    }
27
28    fn selector(&self) -> H32 {
29        hash::function_selector(self.abi_signature())
30    }
31}
32
33/// Extension trait for `ethabi::Event`.
34pub trait EventExt {
35    /// Computes the event signature in human-readable format. The `keccak256`
36    /// hash of this value is the actual event signature that is used as topic0
37    /// in the transaction logs.
38    fn abi_signature(&self) -> String;
39}
40
41impl EventExt for Event {
42    fn abi_signature(&self) -> String {
43        format!(
44            "{}({}){}",
45            self.name,
46            self.inputs
47                .iter()
48                .map(|input| input.kind.to_string())
49                .collect::<Vec<_>>()
50                .join(","),
51            if self.anonymous { " anonymous" } else { "" },
52        )
53    }
54}
55
56/// An extension trait for Solidity parameter types.
57pub trait ParamTypeExt {
58    /// Parses a parameter type from a string value.
59    fn from_str(s: &str) -> Result<ParamType, ParseParamTypeError> {
60        serde_json::from_value(json!(s)).map_err(|_| ParseParamTypeError(s.into()))
61    }
62}
63
64impl ParamTypeExt for ParamType {}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn format_function_signature() {
72        for (f, expected) in &[
73            (r#"{"name":"foo","inputs":[],"outputs":[]}"#, "foo()"),
74            (
75                r#"{"name":"bar","inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"bool"}],"outputs":[]}"#,
76                "bar(uint256,bool)",
77            ),
78            (
79                r#"{"name":"baz","inputs":[{"name":"a","type":"uint256"}],"outputs":[{"name":"b","type":"bool"}]}"#,
80                "baz(uint256)",
81            ),
82            (
83                r#"{"name":"bax","inputs":[],"outputs":[{"name":"a","type":"uint256"},{"name":"b","type":"bool"}]}"#,
84                "bax()",
85            ),
86        ] {
87            let function: Function = serde_json::from_str(f).expect("invalid function JSON");
88            let signature = function.abi_signature();
89            assert_eq!(signature, *expected);
90        }
91    }
92
93    #[test]
94    fn format_event_signature() {
95        for (e, expected) in &[
96            (r#"{"name":"foo","inputs":[],"anonymous":false}"#, "foo()"),
97            (
98                r#"{"name":"bar","inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"bool"}],"anonymous":false}"#,
99                "bar(uint256,bool)",
100            ),
101            (
102                r#"{"name":"baz","inputs":[{"name":"a","type":"uint256"}],"anonymous":true}"#,
103                "baz(uint256) anonymous",
104            ),
105            (
106                r#"{"name":"bax","inputs":[],"anonymous":true}"#,
107                "bax() anonymous",
108            ),
109        ] {
110            let event: Event = serde_json::from_str(e).expect("invalid event JSON");
111            let signature = event.abi_signature();
112            assert_eq!(signature, *expected);
113        }
114    }
115}