ethcontract_common/
abiext.rs1use crate::abi::{Event, Function, ParamType};
4use crate::errors::ParseParamTypeError;
5use crate::hash::{self, H32};
6use serde_json::json;
7
8pub trait FunctionExt {
10 fn abi_signature(&self) -> String;
13
14 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
33pub trait EventExt {
35 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
56pub trait ParamTypeExt {
58 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}