1use crate::types::NormalizedValue;
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct DecodedCall {
13 pub function_name: String,
15 pub selector: Option<[u8; 4]>,
17 pub inputs: Vec<(String, NormalizedValue)>,
19 pub raw_data: Vec<u8>,
21 pub decode_errors: HashMap<String, String>,
23}
24
25impl DecodedCall {
26 pub fn selector_hex(&self) -> Option<String> {
28 self.selector
29 .map(|s| format!("0x{}", hex::encode(s)))
30 }
31
32 pub fn input(&self, name: &str) -> Option<&NormalizedValue> {
34 self.inputs
35 .iter()
36 .find(|(n, _)| n == name)
37 .map(|(_, v)| v)
38 }
39
40 pub fn is_clean(&self) -> bool {
42 self.decode_errors.is_empty()
43 }
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct DecodedConstructor {
49 pub args: Vec<(String, NormalizedValue)>,
51 pub raw_data: Vec<u8>,
53 pub decode_errors: HashMap<String, String>,
55}
56
57impl DecodedConstructor {
58 pub fn arg(&self, name: &str) -> Option<&NormalizedValue> {
60 self.args
61 .iter()
62 .find(|(n, _)| n == name)
63 .map(|(_, v)| v)
64 }
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct HumanReadable {
70 pub summary: String,
72 pub description: Option<String>,
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79
80 #[test]
81 fn selector_hex_format() {
82 let call = DecodedCall {
83 function_name: "transfer".into(),
84 selector: Some([0xa9, 0x05, 0x9c, 0xbb]),
85 inputs: vec![],
86 raw_data: vec![],
87 decode_errors: HashMap::new(),
88 };
89 assert_eq!(call.selector_hex(), Some("0xa9059cbb".to_string()));
90 }
91
92 #[test]
93 fn input_lookup() {
94 let call = DecodedCall {
95 function_name: "transfer".into(),
96 selector: None,
97 inputs: vec![
98 ("to".into(), NormalizedValue::Address("0xabc".into())),
99 ("amount".into(), NormalizedValue::Uint(1000)),
100 ],
101 raw_data: vec![],
102 decode_errors: HashMap::new(),
103 };
104 assert!(call.input("to").is_some());
105 assert!(call.input("nonexistent").is_none());
106 }
107}