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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use crate::AbiParserError;
use crate::ParameterType;
pub struct Function {
pub inputs: Vec<FunctionParameter>,
pub outputs: Vec<FunctionParameter>,
pub state_mutability: Option<StateMutability>,
pub payable: Option<bool>,
pub constant: Option<bool>,
}
impl Function {
pub fn parse(raw_func: &serde_json::Value) -> Result<Self, AbiParserError> {
let inputs = Self::parse_parameters(&raw_func["inputs"])?;
let outputs = Self::parse_parameters(&raw_func["outputs"])?;
Ok(Self {
inputs,
outputs,
state_mutability: StateMutability::parse(raw_func),
payable: raw_func["payable"].as_bool(),
constant: raw_func["constant"].as_bool(),
})
}
fn parse_parameters(
raw_func: &serde_json::Value,
) -> Result<Vec<FunctionParameter>, AbiParserError> {
match raw_func {
serde_json::Value::Array(parameters) => {
let mut result = Vec::new();
for parameter in parameters {
let p_type = parameter["type"].as_str().ok_or_else(|| {
AbiParserError::MissingData("Missing parameter type".to_owned())
})?;
let p_name = parameter["name"].as_str().ok_or_else(|| {
AbiParserError::MissingData("Missing parameter name".to_owned())
})?;
let parameter_type = ParameterType::parse(p_type)?;
result.push(FunctionParameter {
name: p_name.to_owned(),
parameter_type,
});
}
Ok(result)
}
_ => Err(AbiParserError::InvalidAbiEncoding(
"Function parameters are not given as an array".to_owned(),
)),
}
}
}
pub struct FunctionParameter {
pub name: String,
pub parameter_type: ParameterType,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum StateMutability {
Pure,
View,
NonPayable,
Payable,
}
impl StateMutability {
pub fn parse(raw_func: &serde_json::Value) -> Option<Self> {
match raw_func["stateMutability"].as_str() {
Some("pure") => Some(StateMutability::Pure),
Some("view") => Some(StateMutability::View),
Some("nonpayable") => Some(StateMutability::NonPayable),
Some("payable") => Some(StateMutability::Payable),
_ => None,
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::ParameterType;
#[test]
fn parse_function() {
let json: serde_json::Value = serde_json::from_str(
r#"{
"constant": true,
"name": "stuff",
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "",
"type": "bytes64"
}
],
"outputs": [],
"payable": false,
"type": "function",
"stateMutability": "view"
}"#,
)
.unwrap();
let function = Function::parse(&json).unwrap();
assert_eq!(function.inputs.len(), 2);
assert_eq!(function.inputs[0].parameter_type, ParameterType::Address);
assert_eq!(function.inputs[0].name, "_spender");
assert_eq!(
function.inputs[1].parameter_type,
ParameterType::FixedBytes(64)
);
assert!(function.inputs[1].name.is_empty());
assert!(function.outputs.is_empty());
assert_eq!(function.constant, Some(true));
assert_eq!(function.payable, Some(false));
assert_eq!(function.state_mutability, Some(StateMutability::View));
}
}