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
// Copyright (c) 2023 MASSA LABS <info@massa.net>

use crate::{AsyncMessage, AsyncMessageTrigger, AsyncMessageUpdate};
use massa_models::types::SetOrKeep;
use massa_proto_rs::massa::model::v1 as grpc_model;

impl From<AsyncMessage> for grpc_model::AsyncMessage {
    fn from(value: AsyncMessage) -> Self {
        grpc_model::AsyncMessage {
            emission_slot: Some(value.emission_slot.into()),
            emission_index: value.emission_index,
            sender: value.sender.to_string(),
            destination: value.destination.to_string(),
            handler: value.function.to_string(),
            max_gas: value.max_gas,
            fee: Some(value.fee.into()),
            coins: Some(value.coins.into()),
            validity_start: Some(value.validity_start.into()),
            validity_end: Some(value.validity_start.into()),
            data: value.function_params,
            trigger: value.trigger.map(|trigger| trigger.into()),
            can_be_executed: value.can_be_executed,
        }
    }
}

//TODO to be checked, use functions
impl From<AsyncMessageUpdate> for grpc_model::AsyncMessageUpdate {
    fn from(value: AsyncMessageUpdate) -> Self {
        grpc_model::AsyncMessageUpdate {
            emission_slot: match value.emission_slot {
                SetOrKeep::Set(value) => Some(grpc_model::SetOrKeepSlot {
                    change: Some(grpc_model::set_or_keep_slot::Change::Set(value.into())),
                }),
                SetOrKeep::Keep => Some(grpc_model::SetOrKeepSlot {
                    change: Some(grpc_model::set_or_keep_slot::Change::Keep(
                        grpc_model::Empty {},
                    )),
                }),
            },
            emission_index: match value.emission_index {
                SetOrKeep::Set(value) => Some(grpc_model::SetOrKeepUint64 {
                    change: Some(grpc_model::set_or_keep_uint64::Change::Set(value)),
                }),
                SetOrKeep::Keep => Some(grpc_model::SetOrKeepUint64 {
                    change: Some(grpc_model::set_or_keep_uint64::Change::Keep(
                        grpc_model::Empty {},
                    )),
                }),
            },
            sender: match value.sender {
                SetOrKeep::Set(value) => Some(grpc_model::SetOrKeepString {
                    change: Some(grpc_model::set_or_keep_string::Change::Set(
                        value.to_string(),
                    )),
                }),
                SetOrKeep::Keep => Some(grpc_model::SetOrKeepString {
                    change: Some(grpc_model::set_or_keep_string::Change::Keep(
                        grpc_model::Empty {},
                    )),
                }),
            },
            destination: match value.destination {
                SetOrKeep::Set(value) => Some(grpc_model::SetOrKeepString {
                    change: Some(grpc_model::set_or_keep_string::Change::Set(
                        value.to_string(),
                    )),
                }),
                SetOrKeep::Keep => Some(grpc_model::SetOrKeepString {
                    change: Some(grpc_model::set_or_keep_string::Change::Keep(
                        grpc_model::Empty {},
                    )),
                }),
            },
            handler: match value.function {
                SetOrKeep::Set(value) => Some(grpc_model::SetOrKeepString {
                    change: Some(grpc_model::set_or_keep_string::Change::Set(value)),
                }),
                SetOrKeep::Keep => Some(grpc_model::SetOrKeepString {
                    change: Some(grpc_model::set_or_keep_string::Change::Keep(
                        grpc_model::Empty {},
                    )),
                }),
            },
            max_gas: match value.max_gas {
                SetOrKeep::Set(value) => Some(grpc_model::SetOrKeepUint64 {
                    change: Some(grpc_model::set_or_keep_uint64::Change::Set(value)),
                }),
                SetOrKeep::Keep => Some(grpc_model::SetOrKeepUint64 {
                    change: Some(grpc_model::set_or_keep_uint64::Change::Keep(
                        grpc_model::Empty {},
                    )),
                }),
            },
            //TODO check Amount usage
            fee: match value.fee {
                SetOrKeep::Set(value) => Some(grpc_model::SetOrKeepUint64 {
                    change: Some(grpc_model::set_or_keep_uint64::Change::Set(value.to_raw())),
                }),
                SetOrKeep::Keep => Some(grpc_model::SetOrKeepUint64 {
                    change: Some(grpc_model::set_or_keep_uint64::Change::Keep(
                        grpc_model::Empty {},
                    )),
                }),
            },
            //TODO check Amount usage
            coins: match value.coins {
                SetOrKeep::Set(value) => Some(grpc_model::SetOrKeepUint64 {
                    change: Some(grpc_model::set_or_keep_uint64::Change::Set(value.to_raw())),
                }),
                SetOrKeep::Keep => Some(grpc_model::SetOrKeepUint64 {
                    change: Some(grpc_model::set_or_keep_uint64::Change::Keep(
                        grpc_model::Empty {},
                    )),
                }),
            },
            validity_start: match value.validity_start {
                SetOrKeep::Set(value) => Some(grpc_model::SetOrKeepSlot {
                    change: Some(grpc_model::set_or_keep_slot::Change::Set(value.into())),
                }),
                SetOrKeep::Keep => Some(grpc_model::SetOrKeepSlot {
                    change: Some(grpc_model::set_or_keep_slot::Change::Keep(
                        grpc_model::Empty {},
                    )),
                }),
            },
            validity_end: match value.validity_end {
                SetOrKeep::Set(value) => Some(grpc_model::SetOrKeepSlot {
                    change: Some(grpc_model::set_or_keep_slot::Change::Set(value.into())),
                }),
                SetOrKeep::Keep => Some(grpc_model::SetOrKeepSlot {
                    change: Some(grpc_model::set_or_keep_slot::Change::Keep(
                        grpc_model::Empty {},
                    )),
                }),
            },
            data: match value.function_params {
                SetOrKeep::Set(value) => Some(grpc_model::SetOrKeepBytes {
                    change: Some(grpc_model::set_or_keep_bytes::Change::Set(value)),
                }),
                SetOrKeep::Keep => Some(grpc_model::SetOrKeepBytes {
                    change: Some(grpc_model::set_or_keep_bytes::Change::Keep(
                        grpc_model::Empty {},
                    )),
                }),
            },
            trigger: match value.trigger {
                SetOrKeep::Set(value) => match value {
                    None => Some(grpc_model::SetOrKeepAsyncMessageTrigger { change: None }),
                    Some(trigger) => Some(grpc_model::SetOrKeepAsyncMessageTrigger {
                        change: Some(grpc_model::set_or_keep_async_message_trigger::Change::Set(
                            trigger.into(),
                        )),
                    }),
                },
                SetOrKeep::Keep => Some(grpc_model::SetOrKeepAsyncMessageTrigger {
                    change: Some(grpc_model::set_or_keep_async_message_trigger::Change::Keep(
                        grpc_model::Empty {},
                    )),
                }),
            },
            can_be_executed: match value.can_be_executed {
                SetOrKeep::Set(value) => Some(grpc_model::SetOrKeepBool {
                    change: Some(grpc_model::set_or_keep_bool::Change::Set(value)),
                }),
                SetOrKeep::Keep => Some(grpc_model::SetOrKeepBool {
                    change: Some(grpc_model::set_or_keep_bool::Change::Keep(
                        grpc_model::Empty {},
                    )),
                }),
            },
        }
    }
}

impl From<AsyncMessageTrigger> for grpc_model::AsyncMessageTrigger {
    fn from(value: AsyncMessageTrigger) -> Self {
        grpc_model::AsyncMessageTrigger {
            address: value.address.to_string(),
            datastore_key: value.datastore_key,
        }
    }
}