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
use crate::generator::graphql::MAPPING_RUST_TYPES_TO_DB;
use crate::generator::Generator;
use crate::schema::{Schema, Variant, VariantArray};
use inflector::Inflector;
use std::fmt::Write;

const MODULES: &str = r#"
use crate::generated::instruction::*;
use crate::STORE;
use crate::{Attribute, Entity, EntityFilter, EntityOrder, EntityRange, Value};
//use crate::models::*;
use solana_program::pubkey::Pubkey;
use solana_sdk::account::Account;
use uuid::Uuid;
use serde_json;
use massbit_chain_solana::data_type::{SolanaBlock, SolanaLogMessages, SolanaTransaction};
use solana_transaction_status::{parse_instruction, ConfirmedBlock, TransactionWithStatusMeta};
use std::collections::HashMap;
"#;
const ENTITY_SAVE: &str = r#"
pub trait EntityExt {
    fn save(&self, entity_name: &str);
}
impl EntityExt for Entity {
    fn save(&self, entity_name: &str) {
        unsafe {
            STORE
                .as_mut()
                .unwrap()
                .save(String::from(entity_name), self.clone());
        }
    }
}
pub trait ValueExt<T>: Sized {
    fn try_from(_: T) -> Self;
}    
impl ValueExt<String> for Value {
    fn try_from(value: String) -> Value {
        Value::String(value)
    }
}
impl ValueExt<u8> for Value {
    fn try_from(value: u8) -> Value {
        Value::Int(value as i32)
    }
}
impl ValueExt<i8> for Value {
    fn try_from(value: i8) -> Value {
        Value::Int(value as i32)
    }
}
impl ValueExt<u16> for Value {
    fn try_from(value: u16) -> Value {
        Value::Int(value as i32)
    }
}
impl ValueExt<i16> for Value {
    fn try_from(value: i16) -> Value {
        Value::Int(value as i32)
    }
}
impl ValueExt<u32> for Value {
    fn try_from(value: u32) -> Value {
        Value::Int(value as i32)
    }
}
impl ValueExt<i32> for Value {
    fn try_from(value: i32) -> Value {
        Value::Int(value)
    }
}
impl ValueExt<u64> for Value {
    fn try_from(value: u64) -> Value {
        Value::BigInt(value.into())
    }
}
impl ValueExt<i64> for Value {
    fn try_from(value: i64) -> Value {
        Value::BigInt(value.into())
    }
}
impl ValueExt<Vec<Value>> for Value {
    fn try_from(value: Vec<Value>) -> Value {
        Value::List(value)
    }
}
"#;

impl<'a> Generator<'a> {
    pub fn generate_handler(&self, schema: &Schema) -> String {
        let mut out = String::new();
        let _ = writeln!(out, "{}", MODULES);
        let _ = writeln!(out, "{}", ENTITY_SAVE);
        if schema.name.is_some() && schema.variants.is_some() {
            let name = schema.get_pascal_name(schema.name.as_ref().unwrap());
            let patterns = self.expand_handler_patterns(&name, schema.variants.as_ref().unwrap());
            let handler_functions =
                self.expand_handler_functions(schema.variants.as_ref().unwrap());
            let _ = write!(
                &mut out,
                r#"pub struct Handler {{}}
                    impl Handler {{
                        pub fn process(
                            &self, 
                            block: &SolanaBlock, 
                            transaction: &TransactionWithStatusMeta, 
                            program_id: &Pubkey, 
                            accounts: &Vec<Pubkey>, 
                            input: &[u8],
                        ) {{
                            println!("Process block {{}} with input {{:?}}", block.block_number, input);
                            if let Some(instruction) = {name}::unpack(input) {{
                                match instruction {{
                                    {patterns}
                                }}
                            }}
                        }}
                        {handler_functions}
                    }}"#,
                name = name,
                patterns = patterns.join(",\n"),
                handler_functions = handler_functions.join("\n")
            );
        }
        out
    }
    pub fn expand_handler_patterns(
        &self,
        enum_name: &String,
        variants: &VariantArray,
    ) -> Vec<String> {
        variants
            .iter()
            .map(|variant| {
                let method_name = format!("process_{}", &variant.name.to_snake_case());
                let args = match &variant.inner_type {
                    None => (String::default(), String::default()),
                    Some(_) => (String::from("(arg)"), String::from(", arg")),
                };
                format!(
                    r#"{enum_name}::{var_name}{var_inner} => {{
                        self.{method_name}(block,transaction,program_id, accounts{arg});
                    }}"#,
                    enum_name = enum_name,
                    var_name = &variant.name,
                    method_name = method_name,
                    var_inner = &args.0,
                    arg = &args.1
                )
            })
            .collect::<Vec<String>>()
    }
    pub fn expand_handler_functions(&self, variants: &VariantArray) -> Vec<String> {
        variants
            .iter()
            .map(|variant| {
                let function_name = format!("process_{}", &variant.name.to_snake_case());
                let function_body = self.expand_function_body(variant);
                let mut inner_arg = String::default();
                if let Some(inner_type) = &variant.inner_type {
                    let _ =  write!(&mut inner_arg, "arg: {}", inner_type);
                };
                let log = if let Some(_inner_type) = &variant.inner_type {
                    format!(r#"println!("call function {} for handle incoming block {{}} with argument {{:?}}", block.block_number, &arg);"#, function_name)
                } else {
                    format!(r#"println!("call function {} for handle incoming block {{}}", block.block_number);"#, function_name)
                };
                format!(
                    r#"pub fn {function_name}(
                                &self,
                                block: &SolanaBlock,
                                transaction: &TransactionWithStatusMeta,
                                program_id: &Pubkey,
                                accounts: &Vec<Pubkey>,
                                {inner_arg}
                            ) -> Result<(), anyhow::Error> {{
                                {log}
                                {function_body}
                            }}"#,
                    function_name = function_name,
                    log = log,
                    function_body = function_body,
                    inner_arg = inner_arg
                )
            })
            .collect::<Vec<String>>()
    }
    pub fn expand_function_body(&self, variant: &Variant) -> String {
        let mut assignments: Vec<String> = Vec::default();
        //Account assigment
        if let Some(accounts) = &variant.accounts {
            for account in accounts {
                assignments.push(format!(
                    r#"map.insert("{}".to_string(), Value::try_from(
                        accounts.get({})
                            .and_then(|pubkey| Some(pubkey.to_string()))
                            .unwrap_or_default()));"#,
                    account.name, account.index
                ));
            }
        }
        // Write table if there is inner_type
        if let Some(inner_type) = &variant.inner_type {
            // Get definitions
            if let Some(inner_schema) = self.definitions.get(inner_type.as_str()) {
                // get a table corresponding to inner_schema
                self.expand_entity_assignment(&mut assignments, inner_schema);
            } else if MAPPING_RUST_TYPES_TO_DB.contains_key(inner_type.as_str()) {
                //Inner type is primitive
                self.expand_single_assignment(&mut assignments, "value", "arg");
            }
        }
        format!(
            r#"
                let mut map : HashMap<Attribute, Value> = HashMap::default();
                map.insert("id".to_string(), Value::try_from(Uuid::new_v4().to_simple().to_string()));
                {assignments}
                Entity::from(map).save("{entity_name}");
                Ok(())
            "#,
            assignments = assignments.join("\n"),
            entity_name = &variant.name
        )
    }
    fn expand_single_assignment(
        &self,
        assignments: &mut Vec<String>,
        field_name: &str,
        field_value: &str,
    ) {
        assignments.push(format!(
            r#"map.insert("{}".to_string(), Value::try_from({}));"#,
            field_name, field_value
        ));
    }
    pub fn expand_entity_assignment(&self, assignments: &mut Vec<String>, inner_schema: &Schema) {
        //If inner schema is a struct
        if let Some(properties) = &inner_schema.properties {
            for property in properties {
                let db_type = MAPPING_RUST_TYPES_TO_DB.get(property.data_type.as_str());
                //.unwrap_or(&*DEFAULT_TYPE_DB);
                // If data_type is not primitive (e.g. Enum, Struct)
                match db_type {
                    // If data_type is primitive (e.g. Enum, Struct)
                    Some(db_type) => {
                        let elm_value = if property.data_type.starts_with("NonZero") {
                            format!("{}.get()", property.name)
                        } else {
                            format!("{}", property.name)
                        };
                        let property_value = match property.array_length {
                            Some(_) => {
                                format!(
                                    r#"arg.{property_name}.iter().map(|&{property_name}| Value::try_from({elm_value})).collect::<Vec<Value>>()"#,
                                    property_name = &property.name,
                                    elm_value = elm_value
                                )
                            }
                            None => {
                                format!("arg.{}", elm_value)
                            }
                        };
                        assignments.push(format!(
                            r#"map.insert("{}".to_string(), Value::try_from({}));"#,
                            &property.name, &property_value
                        ));
                    }
                    None => {
                        assignments.push(format!(
                            r#"map.insert("{name}".to_string(), Value::try_from(serde_json::to_string(&arg.{name}).unwrap_or(Default::default())));"#,
                            name=&property.name
                        ));
                    }
                }
            }
        }
    }
}