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
//! Decoder of *.contract
use ceres_std::{BTreeMap, String, Vec};
use derivative::Derivative;
use serde::{Deserialize, Serialize};

type Method = (String, Vec<(Option<String>, u32)>);
type MethodWithName = (String, String, Vec<(Option<String>, u32)>);

/// A struct for operating *.contract
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Metadata {
    pub source: Source,
    pub contract: Contract,
    pub spec: Spec,
}

impl Metadata {
    /// Get all messages
    pub fn messages(&self) -> BTreeMap<String, Method> {
        let methods: Vec<MethodWithName> = self
            .spec
            .messages
            .iter()
            .map(|c| {
                (
                    if c.name.is_empty() {
                        "".into()
                    } else {
                        c.name[0].clone()
                    },
                    c.selector.clone(),
                    c.args
                        .iter()
                        .map(|a| {
                            (
                                if !a.r#type.display_name.is_empty() {
                                    Some(a.r#type.display_name[0].clone())
                                } else {
                                    None
                                },
                                a.r#type.r#type,
                            )
                        })
                        .collect(),
                )
            })
            .collect();

        let mut map = BTreeMap::new();
        for v in methods {
            map.insert(v.0.clone(), (v.1.clone(), v.2));
        }
        map
    }

    /// Get all constructors
    pub fn constructors(&self) -> BTreeMap<String, Method> {
        let methods: Vec<MethodWithName> = self
            .spec
            .constructors
            .iter()
            .map(|c| {
                (
                    if !c.name.is_empty() {
                        c.name[0].clone()
                    } else {
                        "".into()
                    },
                    c.selector.clone(),
                    c.args
                        .iter()
                        .map(|a| {
                            (
                                if !a.r#type.display_name.is_empty() {
                                    Some(a.r#type.display_name[0].clone())
                                } else {
                                    None
                                },
                                a.r#type.r#type,
                            )
                        })
                        .collect(),
                )
            })
            .collect();

        let mut map = BTreeMap::new();
        for v in methods {
            map.insert(v.0.clone(), (v.1.clone(), v.2));
        }
        map
    }
}

/// Source section in *.contract

#[derive(Clone, Derivative, Deserialize, Serialize)]
#[derivative(Debug)]
pub struct Source {
    /// code hash
    pub hash: String,
    /// hex string in the output metadata, and
    /// this is required in our decoder
    #[derivative(Debug = "ignore")]
    pub wasm: String,
}

/// Contract section in *.contract
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Contract {
    /// Just for displaying this on UIs
    pub name: String,
}

/// ABI of methods
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Spec {
    pub constructors: Vec<Constructor>,
    pub messages: Vec<Message>,
}

/// Constructor
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Constructor {
    pub args: Vec<Arg>,
    pub docs: Vec<String>,
    pub name: Vec<String>,
    pub selector: String,
}

/// Message
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Message {
    pub args: Vec<Arg>,
    pub docs: Vec<String>,
    pub mutates: bool,
    pub name: Vec<String>,
    pub payable: bool,
    // # NOTE
    //
    // For deserializing this field, implement `Deserialize` trait for this.
    //
    // - - - - - - - - -
    //
    // pub return_type: Type,
    pub selector: String,
}

/// Custom arg interface
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Arg {
    pub name: String,
    pub r#type: Type,
}

/// Type defination
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Type {
    pub display_name: Vec<String>,
    pub r#type: u32,
}