forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.rs
More file actions
225 lines (200 loc) · 6.15 KB
/
Copy pathfunction.rs
File metadata and controls
225 lines (200 loc) · 6.15 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use fe_common::utils::keccak;
use serde::Serialize;
use super::types::AbiType;
/// The mutability of a public function.
#[derive(Serialize, Debug, PartialEq, Eq, Clone)]
#[serde(rename_all = "lowercase")]
pub enum StateMutability {
Pure,
View,
Nonpayable,
Payable,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct AbiFunction {
#[serde(rename = "type")]
func_type: AbiFunctionType,
name: String,
inputs: Vec<AbiFunctionParamInner>,
outputs: Vec<AbiFunctionParamInner>,
#[serde(rename = "stateMutability")]
state_mutability: StateMutability,
}
impl AbiFunction {
pub fn new(
func_type: AbiFunctionType,
name: String,
args: Vec<(String, AbiType)>,
ret_ty: Option<AbiType>,
) -> Self {
let inputs = args
.into_iter()
.map(|(arg_name, arg_ty)| AbiFunctionParamInner::new(arg_name, arg_ty))
.collect();
let outputs = ret_ty.map_or_else(Vec::new, |ret_ty| {
vec![AbiFunctionParamInner::new("".into(), ret_ty)]
});
Self {
func_type,
name,
inputs,
outputs,
// In the future we will derive that based on the fact whether `self` or `ctx` are taken as `mut` or not.
// For now, we default to payable so that tooling such as hardhat simply assumes all functions need to be
// called with a transaction.
state_mutability: StateMutability::Payable,
}
}
pub fn selector(&self) -> AbiFunctionSelector {
AbiFunctionSelector::new(self)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum AbiFunctionType {
Function,
Constructor,
Receive,
Payable,
Fallback,
}
pub struct AbiFunctionSelector {
selector_sig: String,
}
impl AbiFunctionSelector {
fn new(func_sig: &AbiFunction) -> Self {
let selector_sig = format!(
"{}({})",
func_sig.name,
func_sig
.inputs
.iter()
.map(|param| param.ty.selector_type_name())
.collect::<Vec<_>>()
.join(",")
);
Self { selector_sig }
}
pub fn selector_signature(&self) -> &str {
&self.selector_sig
}
pub fn selector_raw(&self) -> [u8; 4] {
keccak::full_as_bytes(self.selector_sig.as_bytes())[..4]
.try_into()
.unwrap()
}
/// Returns first 4 bytes of signature hash in hex.
pub fn hex(&self) -> String {
keccak::partial(self.selector_sig.as_bytes(), 4)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
struct AbiFunctionParamInner {
name: String,
#[serde(flatten)]
ty: AbiType,
}
impl AbiFunctionParamInner {
fn new(name: String, ty: AbiType) -> Self {
Self { name, ty }
}
}
#[cfg(test)]
mod tests {
use crate::types::AbiTupleField;
use super::*;
use serde_test::{assert_ser_tokens, Token};
fn simple_tuple() -> AbiType {
let u16_ty = AbiType::UInt(16);
let bool_ty = AbiType::Bool;
let field1 = AbiTupleField::new("field1".into(), u16_ty);
let field2 = AbiTupleField::new("field2".into(), bool_ty);
AbiType::Tuple(vec![field1, field2])
}
fn test_func() -> AbiFunction {
let i32_ty = AbiType::Int(32);
let tuple_ty = simple_tuple();
let u64_ty = AbiType::UInt(64);
AbiFunction::new(
AbiFunctionType::Function,
"test_func".into(),
vec![("arg1".into(), i32_ty), ("arg2".into(), tuple_ty)],
Some(u64_ty),
)
}
#[test]
fn serialize_func() {
let func = test_func();
assert_ser_tokens(
&func,
&[
Token::Struct {
name: "AbiFunction",
len: 5,
},
Token::Str("type"),
Token::UnitVariant {
name: "AbiFunctionType",
variant: "function",
},
Token::String("name"),
Token::String("test_func"),
Token::Str("inputs"),
Token::Seq { len: Some(2) },
Token::Map { len: None },
Token::String("name"),
Token::String("arg1"),
Token::String("type"),
Token::String("int32"),
Token::MapEnd,
Token::Map { len: None },
Token::String("name"),
Token::String("arg2"),
Token::String("type"),
Token::String("tuple"),
Token::String("components"),
Token::Seq { len: Some(2) },
Token::Map { len: None },
Token::String("name"),
Token::String("field1"),
Token::String("type"),
Token::String("uint16"),
Token::MapEnd,
Token::Map { len: None },
Token::String("name"),
Token::String("field2"),
Token::String("type"),
Token::String("bool"),
Token::MapEnd,
Token::SeqEnd,
Token::MapEnd,
Token::SeqEnd,
Token::Str("outputs"),
Token::Seq { len: Some(1) },
Token::Map { len: None },
Token::String("name"),
Token::String(""),
Token::String("type"),
Token::String("uint64"),
Token::MapEnd,
Token::SeqEnd,
Token::Str("stateMutability"),
Token::UnitVariant {
name: "StateMutability",
variant: "payable",
},
Token::StructEnd,
],
)
}
#[test]
fn func_selector() {
let func = test_func();
let selector = func.selector();
debug_assert_eq!(
selector.selector_signature(),
"test_func(int32,(uint16,bool))"
);
debug_assert_eq!(selector.hex(), "79c3c8b2");
}
}