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
#[cfg(not(feature = "std"))]
use crate::no_std_prelude::*;
use crate::ParamType;
pub struct Writer;
impl Writer {
pub fn write(param: &ParamType) -> String {
Writer::write_for_abi(param, true)
}
pub fn write_for_abi(param: &ParamType, serialize_tuple_contents: bool) -> String {
match *param {
ParamType::Address => "address".to_owned(),
ParamType::Bytes => "bytes".to_owned(),
ParamType::FixedBytes(len) => format!("bytes{len}"),
ParamType::Int(len) => format!("int{len}"),
ParamType::Uint(len) => format!("uint{len}"),
ParamType::Bool => "bool".to_owned(),
ParamType::String => "string".to_owned(),
ParamType::FixedArray(ref param, len) => {
format!("{}[{len}]", Writer::write_for_abi(param, serialize_tuple_contents))
}
ParamType::Array(ref param) => {
format!("{}[]", Writer::write_for_abi(param, serialize_tuple_contents))
}
ParamType::Tuple(ref params) => {
if serialize_tuple_contents {
let formatted = params
.iter()
.map(|t| Writer::write_for_abi(t, serialize_tuple_contents))
.collect::<Vec<String>>()
.join(",");
format!("({formatted})")
} else {
"tuple".to_owned()
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::Writer;
#[cfg(not(feature = "std"))]
use crate::no_std_prelude::*;
use crate::ParamType;
#[test]
fn test_write_param() {
assert_eq!(Writer::write(&ParamType::Address), "address".to_owned());
assert_eq!(Writer::write(&ParamType::Bytes), "bytes".to_owned());
assert_eq!(Writer::write(&ParamType::FixedBytes(32)), "bytes32".to_owned());
assert_eq!(Writer::write(&ParamType::Uint(256)), "uint256".to_owned());
assert_eq!(Writer::write(&ParamType::Int(64)), "int64".to_owned());
assert_eq!(Writer::write(&ParamType::Bool), "bool".to_owned());
assert_eq!(Writer::write(&ParamType::String), "string".to_owned());
assert_eq!(Writer::write(&ParamType::Array(Box::new(ParamType::Bool))), "bool[]".to_owned());
assert_eq!(Writer::write(&ParamType::FixedArray(Box::new(ParamType::String), 2)), "string[2]".to_owned());
assert_eq!(
Writer::write(&ParamType::FixedArray(Box::new(ParamType::Array(Box::new(ParamType::Bool))), 2)),
"bool[][2]".to_owned()
);
assert_eq!(
Writer::write(&ParamType::Array(Box::new(ParamType::Tuple(vec![
ParamType::Array(Box::new(ParamType::Tuple(vec![ParamType::Int(256), ParamType::Uint(256)]))),
ParamType::FixedBytes(32),
])))),
"((int256,uint256)[],bytes32)[]".to_owned()
);
assert_eq!(
Writer::write_for_abi(
&ParamType::Array(Box::new(ParamType::Tuple(vec![
ParamType::Array(Box::new(ParamType::Int(256))),
ParamType::FixedBytes(32),
]))),
false
),
"tuple[]".to_owned()
);
}
}