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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use crate::{types::Selector, utils::id};
pub use ethabi::Contract as Abi;
pub use ethabi::*;
mod tokens;
pub use tokens::{Detokenize, InvalidOutputType, Tokenizable, TokenizableItem, Tokenize};
pub mod struct_def;
pub use struct_def::SolStruct;
mod error;
pub use error::ParseError;
mod human_readable;
pub use human_readable::{parse as parse_abi, parse_str as parse_abi_str, AbiParser};
pub trait FunctionExt {
fn abi_signature(&self) -> String;
fn selector(&self) -> Selector;
}
impl FunctionExt for Function {
fn abi_signature(&self) -> String {
let mut full_signature = self.signature();
if let Some(colon) = full_signature.find(':') {
full_signature.truncate(colon);
}
full_signature
}
fn selector(&self) -> Selector {
id(self.abi_signature())
}
}
pub trait EventExt {
fn abi_signature(&self) -> String;
}
impl EventExt for Event {
fn abi_signature(&self) -> String {
format!(
"{}({}){}",
self.name,
self.inputs
.iter()
.map(|input| input.kind.to_string())
.collect::<Vec<_>>()
.join(","),
if self.anonymous { " anonymous" } else { "" },
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn format_function_signature() {
for (f, expected) in &[
(
r#"{"name":"foo","inputs":[],"outputs":[], "stateMutability": "nonpayable"}"#,
"foo()",
),
(
r#"{"name":"bar","inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"bool"}],"outputs":[], "stateMutability": "nonpayable"}"#,
"bar(uint256,bool)",
),
(
r#"{"name":"baz","inputs":[{"name":"a","type":"uint256"}],"outputs":[{"name":"b","type":"bool"}], "stateMutability": "nonpayable"}"#,
"baz(uint256)",
),
(
r#"{"name":"bax","inputs":[],"outputs":[{"name":"a","type":"uint256"},{"name":"b","type":"bool"}], "stateMutability": "nonpayable"}"#,
"bax()",
),
] {
let function: Function = serde_json::from_str(f).expect("invalid function JSON");
let signature = function.abi_signature();
assert_eq!(signature, *expected);
}
}
#[test]
fn format_event_signature() {
for (e, expected) in &[
(r#"{"name":"foo","inputs":[],"anonymous":false}"#, "foo()"),
(
r#"{"name":"bar","inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"bool"}],"anonymous":false}"#,
"bar(uint256,bool)",
),
(
r#"{"name":"baz","inputs":[{"name":"a","type":"uint256"}],"anonymous":true}"#,
"baz(uint256) anonymous",
),
(
r#"{"name":"bax","inputs":[],"anonymous":true}"#,
"bax() anonymous",
),
] {
let event: Event = serde_json::from_str(e).expect("invalid event JSON");
let signature = event.abi_signature();
assert_eq!(signature, *expected);
}
}
}