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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
use serde::{
    // de::DeserializeOwned,
    // de::Deserializer,
    Deserialize,
    Serialize,
};

use std::collections::HashMap;

use eosio_scale_info::{
    Type,
    Path
};

pub struct ActionInfo {
    pub name: String,
    pub info: Type,
}

pub struct TableInfo {
    pub name: String,
    pub info: Type,
}

pub struct ABIInfo {
    pub actions: Vec<ActionInfo>,
    pub tables: Vec<TableInfo>,
    pub structs: Vec<Type>,
    pub variants: Vec<Type>,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ABIType {
    name: String,
    #[serde(rename = "type")]
    ty: String,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ABIStruct {
    name: String,
    base: String,
    fields: Vec<ABIType>,
}

///
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ABIAction {
    name: String,
    #[serde(rename = "type")]
    ty: String,
    ricardian_contract: String,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ABITable {
    name: String,
    #[serde(rename = "type")]
    ty: String,
    index_type: String,
    key_names: Vec<String>,
    key_types: Vec<String>,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ABIVariant {
	name: String,
    // #[serde(deserialize_with = "string_or_seq_string")]
    types: Vec<String>,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ABI {
	version: String,
	types: Vec<String>,
    structs: Vec<ABIStruct>,
    actions: Vec<ABIAction>,
    tables: Vec<ABITable>,
    variants: Vec<ABIVariant>,
    abi_extensions: Vec<String>,
    error_messages: Vec<String>,
    ricardian_clauses: Vec<String>,
}

fn native_type_to_abi_type(tp: &str) -> &str {
    match tp {
        "bool" => "bool",
        "i8" => "int8",
        "u8" => "uint8",
        "i16" => "int16",
        "u16" => "uint16",
        "i32" => "int32",
        "u32" => "uint32",
        "i64" => "int64",
        "u64" => "uint64",
        "i128" => "int128",
        "u128" => "uint128",
        "Varint32" => "varint32",
        "VarUint32" => "varuint32",
        "f32" => "float32",
        "f64" => "float64",
        "Float128" => "float128",
        "TimePoint" => "time_point",
        "TimePointSec" => "time_point_sec",
        "BlockTimeStampType" => "block_timestamp_type",
        "Name" => "name",
        "&[u8]" => "bytes",
        "String" => "string",
        "Checksum160" => "checksum160",
        "Checksum256" => "checksum256",
        "Uint256" => "checksum256",
        "Checksum512" => "checksum512",
        "PublicKey" => "public_key",
        "Signature" => "signature",
        "Symbol" => "symbol",
        "SymbolCode" => "symbol_code",
        "Asset" => "asset",
        "ExtendedAsset" => "extended_asset",
        _ => tp,
    }
}

fn is_intrinsic_abi_type(name: &str) -> bool {
    match name {
        "bool" | "i8" | "u8" | "i16" | "u16" | "i32" | "u32" | "i64" | "u64" | "f32" | "f64" | "i128" | "u128" |
        "String" |
        "Varint32" | "VarUint32" | "Float128" | "TimePoint" | "TimePointSec" |
        "BlockTimeStampType" | "Name" | "Checksum160" | "Checksum256" | "Uint256" |
        "Checksum512" | "PublicKey" | "Signature" | "Symbol" | "SymbolCode" | "Asset" |
        "ExtendedAsset"  => {
            return true;
        }
        _=> {
            return false;
        }
    }
}

fn get_full_path_name(path: &Path) -> String {
    return path.segments().to_vec().join("::");
}

fn get_last_path_name(path: &Path) -> String {
    let len = path.segments().len();
    if len == 0 {
        return String::from("");
    }
    return String::from(path.segments()[len-1]);
}

pub fn verify_abi_structs(main_contract_structs: &Vec<Type>) -> Vec<Type> {
    //
    let mut main_contract_structs_map: HashMap<String, &Type> = HashMap::new();
    //<name, full_name>
    let mut all_structs_map: HashMap<String, &Type> = HashMap::new();

    for ty in main_contract_structs {
        let last_name = get_last_path_name(ty.path());
        let full_name = get_full_path_name(ty.path());
        if let Some(ty) = main_contract_structs_map.get(&last_name) {
            let full_name2 = get_full_path_name(ty.path());
            if full_name != full_name2 {
                panic!("Same struct name live in different modules is not supported by ABI\n{}\n<==>\n{}\n", full_name, full_name2);
            }
        } else {
            main_contract_structs_map.insert(last_name.clone(), ty);
        }

        if let Some(ty2) = all_structs_map.get(&last_name) {
            let full_name2 = get_full_path_name(ty2.path());
            if full_name2 != full_name {
                panic!("Same struct name live in different modules is not supported by ABI\n{}\n<==>\n{}\n", full_name, full_name2);
            }
        } else {
            all_structs_map.insert(last_name, ty);
        }
    }

    let hashmap_mutex = eosio_scale_info::get_scale_type_map();
    let global_hash_map = &*hashmap_mutex.lock().unwrap();
    for (full_name, ty) in  global_hash_map {
        let last_name = get_last_path_name(ty.path());
        if let Some(ty) = all_structs_map.get(&last_name) {
            let full_name2 = get_full_path_name(ty.path());
            if full_name2 != *full_name {
                panic!("Same struct name live in different modules is not supported by ABI\n{}\n<==>\n{}\n", *full_name, full_name2);
            }
        } else {
            all_structs_map.insert(last_name, ty);
        }
    }

    let mut other_structs_map: HashMap<String, &Type> = HashMap::new();

    let mut check_rust_type = |struct_name: &str, field_name: &str, rust_type: &str| {
        if is_intrinsic_abi_type(rust_type) {
            return;
        }

        if let Some(_) = main_contract_structs_map.get(rust_type) {
            return;
        }

        if let Some(ty) = all_structs_map.get(rust_type) {
            let name = String::from(rust_type);
            if let Some(_) = other_structs_map.get(&name) {
                //
            } else {
                other_structs_map.insert(name, *ty);
            }
            return;
        }
        panic!("abi struct not found: {}.{}: {}", struct_name, field_name, rust_type);
    };

    main_contract_structs.iter().for_each(|item|{
        let struct_name = &get_last_path_name(item.path());
        match item.type_def() {
            ::eosio_scale_info::TypeDef::Composite(x) => {
                x.fields().iter().for_each(|field|{
                    let field_name = *field.name().unwrap();
                    let rust_type = *field.type_name().unwrap();
                    if let Some(_) = rust_type.find("Option<") {
                        let inner_rust_type = &rust_type["Option<".len()..rust_type.len() -1];
                        check_rust_type(struct_name, field_name, inner_rust_type);
                    } else if let Some(_) = rust_type.find("Vec<") {
                        let inner_rust_type = &rust_type["Vec<".len()..rust_type.len() -1];
                        check_rust_type(struct_name, field_name, inner_rust_type);
                    } else if let Some(_) = rust_type.find("BinaryExtension<") {
                        let inner_rust_type = &rust_type["BinaryExtension<".len()..rust_type.len() -1];
                        check_rust_type(struct_name, field_name, inner_rust_type);
                    } else {
                        check_rust_type(struct_name, field_name, rust_type);
                    }
                });
            }
            ::eosio_scale_info::TypeDef::Variant(x) => {
                x.variants().iter().for_each(|v|{
                    let name = *v.name();
                    let rust_type = v.fields()[0].type_name().unwrap();
                    check_rust_type(struct_name, name, *rust_type);
                });
            }
            _ => {
                println!("+++unknown abi type: {:?}", item);
            }
        }
    });

    let mut other_structs: Vec<Type> = Vec::new();
    for (_, ty) in other_structs_map {
        other_structs.push(ty.clone());
    }
    return other_structs;
}

pub fn parse_abi_info(info: &mut ABIInfo) -> String {
    let mut abi = ABI {
        version: String::from("eosio::abi/1.1"),
        types: Vec::new(),
        structs: Vec::new(),
        actions: Vec::new(),
        tables: Vec::new(),
        variants: Vec::new(),
        abi_extensions: Vec::new(),
        error_messages: Vec::new(),
        ricardian_clauses: Vec::new(),    
    };


    let other_structs = verify_abi_structs(&info.structs);
    info.structs.extend(other_structs);

    info.structs.iter().for_each(|item|{
        match item.type_def() {
            ::eosio_scale_info::TypeDef::Composite(x) => {
                if is_intrinsic_abi_type(&get_last_path_name(item.path())) {
                    return;
                }

                let name = item.path().segments().last().unwrap();
                let mut s = ABIStruct{
                    name: String::from(*name),
                    base: String::from(""),
                    fields: Vec::new(),
                };
                x.fields().iter().for_each(|field|{
                    let ty: String;
                    let rust_type = *field.type_name().unwrap();
                    if let Some(_) = rust_type.find("Option<") {
                        ty = String::from(native_type_to_abi_type(&rust_type["Option<".len()..rust_type.len() -1])) + "?";
                    } else if let Some(_) = rust_type.find("BinaryExtension<") {
                        ty = String::from(native_type_to_abi_type(&rust_type["BinaryExtension<".len()..rust_type.len() -1])) + "$";
                    } else if let Some(_) = rust_type.find("Vec<") {
                        let inner_rust_type = &rust_type["Vec<".len()..rust_type.len() -1];
                        if inner_rust_type == "u8" {
                            ty = String::from("bytes");
                        } else {
                            ty = String::from(native_type_to_abi_type(inner_rust_type)) + "[]";
                        }
                    } else {
                        ty = String::from(native_type_to_abi_type(rust_type));
                    }
                    s.fields.push(
                        ABIType{
                            name: String::from(*field.name().unwrap()),
                            ty,
                        }
                    )
                });
                abi.structs.push(s);
            }
            ::eosio_scale_info::TypeDef::Variant(x) => {
                let name = item.path().segments().last().unwrap();
                let mut abi_variant = ABIVariant{
                    name: String::from(*name),
                    types: Vec::new(),
                };
                x.variants().iter().for_each(|v|{
                    let rust_type = v.fields()[0].type_name().unwrap();
                    abi_variant.types.push(native_type_to_abi_type(rust_type).into());
                });
                abi.variants.push(abi_variant);    
            }
            _ => {
                println!("+++unknown abi type: {:?}", item);
                // panic!("unknown abi type {:?}", item);
            }
        }
    });

    info.tables.iter().for_each(|table|{
		if let ::eosio_scale_info::TypeDef::Composite(_) = table.info.type_def() {
			let name = table.info.path().segments().last().unwrap();
            abi.tables.push(ABITable {
                name: table.name.clone(),
                ty: String::from(*name),
                index_type: String::from("i64"),
                key_names: Vec::new(),
                key_types: Vec::new(),
            });
        }
    });

    info.actions.iter().for_each(|action|{
		if let ::eosio_scale_info::TypeDef::Composite(_) = action.info.type_def() {
			let name = action.info.path().segments().last().unwrap();
            abi.actions.push(ABIAction {
                name: String::from(*name),
                ty: String::from(*name),
                ricardian_contract: String::from(""),
            });
        }
    });

    let cmp = |x: &str, y: &str| -> std::cmp::Ordering {
        if x == y {
            return std::cmp::Ordering::Equal;
        }
        if x < y {
            return std::cmp::Ordering::Less;
        }
        return std::cmp::Ordering::Greater;
    };

    abi.structs.sort_by(|x, y| -> std::cmp::Ordering {
        cmp(&x.name, &y.name)
    });

    abi.actions.sort_by(|x, y| -> std::cmp::Ordering {
        cmp(&x.name, &y.name)
    });

    abi.tables.sort_by(|x, y| -> std::cmp::Ordering {
        cmp(&x.name, &y.name)
    });

    abi.variants.sort_by(|x, y| -> std::cmp::Ordering {
        cmp(&x.name, &y.name)
    });

    if let Ok(contents) = serde_json::to_string_pretty(&abi) {
        return contents;
    }
    return String::from("");
}