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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use std::collections::HashMap;

use crate::errors::Error;
use crate::utils::{has_array_format, has_tuple_format};
use crate::TypeDeclaration;

pub struct FunctionSelector {
    pub signature: String,
    pub encoded_selector: String,
    pub final_selector: Vec<u8>,
}

/// Builds a string representation of a function selector,
/// i.e: <fn_name>(<type_1>, <type_2>, ..., <type_n>)
pub fn build_fn_selector(
    fn_name: &str,
    params: &[TypeDeclaration],
    types: &HashMap<usize, TypeDeclaration>,
) -> Result<String, Error> {
    let fn_selector = fn_name.to_owned();

    let mut result: String = format!("{}(", fn_selector);

    for (idx, param) in params.iter().enumerate() {
        result.push_str(&build_fn_selector_params(param, types));
        if idx + 1 < params.len() {
            result.push(',');
        }
    }

    result.push(')');

    Ok(result)
}

fn build_fn_selector_params(
    prop: &TypeDeclaration,
    types: &HashMap<usize, TypeDeclaration>,
) -> String {
    let mut result: String = String::new();

    let type_declaration = types.get(&prop.type_id).expect("couldn't find type");

    if type_declaration.is_custom_type(types) {
        // Custom type, need to break down inner fields.
        // Will return `"e(field_1,field_2,...,field_n)"` if the type is an `Enum`,
        // `"s(field_1,field_2,...,field_n)"` if the type is a `Struct`,
        // `"a[type;length]"` if the type is an `Array`,
        // `(type_1,type_2,...,type_n)` if the type is a `Tuple`.
        if type_declaration.is_struct_type() {
            result.push_str("s(");
        } else if type_declaration.is_enum_type() {
            result.push_str("e(");
        } else if type_declaration.has_custom_type_in_array(types) {
            result.push_str("a[");
        } else if type_declaration.has_custom_type_in_tuple(types) {
            result.push('(');
        } else {
            panic!("unexpected custom type");
        }

        for (idx, component) in type_declaration
            .components
            .as_ref()
            .expect("No components found")
            .iter()
            .enumerate()
        {
            let t = types
                .get(&component.type_field)
                .expect("couldn't find type");

            result.push_str(&build_fn_selector_params(t, types));

            if idx + 1 < type_declaration.components.as_ref().unwrap().len() {
                result.push(',');
            }
        }

        if result.starts_with("a[") {
            let array_type_field = type_declaration.type_field.clone();

            // Type field, in this case, looks like
            // "[struct Person; 2]" and we want to extract the
            // length, which in this example is 2.
            // First, get the last part after `;`: `"<length>]"`.
            let mut array_length = array_type_field.split(';').collect::<Vec<&str>>()[1]
                .trim()
                .to_string();

            array_length.pop(); // Remove the trailing "]"

            // Make sure the length is a valid number.
            let array_length = array_length.parse::<usize>().expect("Invalid array length");

            result.push(';');
            result.push_str(array_length.to_string().as_str());
            result.push(']');
            result
        } else {
            result.push(')');
            result
        }
    } else {
        // Not a custom type.

        // Tuple.
        if has_tuple_format(&type_declaration.type_field) {
            let components = type_declaration
                .components
                .as_ref()
                .expect("tuples should have components");

            // get types of components
            let component_types: Vec<String> = components
                .iter()
                .map(|component| {
                    let t = types
                        .get(&component.type_field)
                        .expect("couldn't find type");
                    t.type_field.clone()
                })
                .collect();

            // join component_types
            let joined = component_types.join(",");

            let tuple_primitive_types_only_signature = format!("{}{}{}", "(", joined, ")");
            return tuple_primitive_types_only_signature;
        }

        // Check if the parameter is an array.
        if has_array_format(&type_declaration.type_field) {
            // The representation of an array in a function selector should be `a[<type>;<length>]`.
            // Because this is coming in as `[<type>;<length>]` (not prefixed with an 'a'), here
            // we must prefix it with an 'a' so the function selector will be properly encoded.

            let array_component_type = types
                .get(&type_declaration.components.as_ref().unwrap()[0].type_field)
                .expect("couldn't find type");

            // Get array size from type field string
            let array_type_field = type_declaration.type_field.clone();
            // Get number in string
            let array_length = array_type_field.split(';').collect::<Vec<&str>>()[1]
                .trim()
                .to_string();

            let array = format!("a[{};{}", array_component_type.type_field, array_length);
            result.push_str(array.as_str());
            result
        } else {
            let param_str_no_whitespace: String = type_declaration
                .type_field
                .chars()
                .filter(|c| !c.is_whitespace())
                .collect();
            result.push_str(&param_str_no_whitespace);
            result
        }
    }
}

#[cfg(test)]
mod tests {

    use crate::TypeApplication;

    use super::*;

    #[test]
    fn test_fn_selector_builder() -> Result<(), Error> {
        let fn_name = "some_abi_fn";
        let fn_params = vec![
            TypeApplication {
                name: "s1".to_string(),
                type_field: 3,
                type_arguments: None,
            },
            TypeApplication {
                name: "s2".to_string(),
                type_field: 4,
                type_arguments: None,
            },
        ];

        let all_types = vec![
            TypeDeclaration {
                type_id: 0,
                type_field: "u64".to_string(),
                type_parameters: None,
                components: None,
            },
            TypeDeclaration {
                type_id: 1,
                type_field: "b256".to_string(),
                type_parameters: None,
                components: None,
            },
            TypeDeclaration {
                type_id: 2,
                type_field: "bool".to_string(),
                type_parameters: None,
                components: None,
            },
            TypeDeclaration {
                type_id: 3,
                type_field: "struct MyStruct1".to_string(),
                type_parameters: None,
                components: Some(vec![
                    TypeApplication {
                        name: "x".to_string(),
                        type_field: 0,
                        type_arguments: None,
                    },
                    TypeApplication {
                        name: "y".to_string(),
                        type_field: 1,
                        type_arguments: None,
                    },
                ]),
            },
            TypeDeclaration {
                type_id: 4,
                type_field: "struct MyStruct2".to_string(),
                type_parameters: None,
                components: Some(vec![
                    TypeApplication {
                        name: "x".to_string(),
                        type_field: 2,
                        type_arguments: None,
                    },
                    TypeApplication {
                        name: "y".to_string(),
                        type_field: 3,
                        type_arguments: None,
                    },
                ]),
            },
        ];

        let all_types = all_types
            .into_iter()
            .map(|t| (t.type_id, t))
            .collect::<HashMap<usize, TypeDeclaration>>();

        let fn_param_types = fn_params
            .iter()
            .map(|t| all_types.get(&t.type_field).unwrap().clone())
            .collect::<Vec<TypeDeclaration>>();

        let result = build_fn_selector(fn_name, &fn_param_types, &all_types).unwrap();

        assert_eq!("some_abi_fn(s(u64,b256),s(bool,s(u64,b256)))", result);

        Ok(())
    }
}