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
use ::serde::{ser, Deserialize, Deserializer, Serialize, Serializer};
use chrono::{DateTime, Utc};
use cosmwasm_std::StdResult;
use serde::de;
use serde::de::Visitor;

use std::fmt;
use std::str::FromStr;

use prost::Message;
#[derive(Clone, PartialEq, Eq, ::prost::Message, schemars::JsonSchema)]
pub struct Timestamp {
    /// Represents seconds of UTC time since Unix epoch
    /// 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
    /// 9999-12-31T23:59:59Z inclusive.
    #[prost(int64, tag = "1")]
    pub seconds: i64,
    /// Non-negative fractions of a second at nanosecond resolution. Negative
    /// second values with fractions must still have non-negative nanos values
    /// that count forward in time. Must be from 0 to 999,999,999
    /// inclusive.
    #[prost(int32, tag = "2")]
    pub nanos: i32,
}

impl Serialize for Timestamp {
    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
    where
        S: Serializer,
    {
        let mut ts = prost_types::Timestamp {
            seconds: self.seconds,
            nanos: self.nanos,
        };
        ts.normalize();
        let dt = DateTime::from_timestamp(ts.seconds, ts.nanos as u32).ok_or_else(|| {
            ser::Error::custom(format!(
                "failed to parse as NativeDateTime: seconds: {}, nanos: {}",
                self.seconds, self.nanos
            ))
        })?;
        serializer.serialize_str(format!("{:?}", dt).as_str())
    }
}

impl<'de> Deserialize<'de> for Timestamp {
    fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
    where
        D: Deserializer<'de>,
    {
        struct TimestampVisitor;

        impl<'de> Visitor<'de> for TimestampVisitor {
            type Value = Timestamp;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("Timestamp in RFC3339 format")
            }

            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                let utc: DateTime<Utc> = chrono::DateTime::from_str(value).map_err(|err| {
                    de::Error::custom(format!("Failed to parse {} as datetime: {:?}", value, err))
                })?;
                let ts = Timestamp::from(utc);
                Ok(ts)
            }
        }
        deserializer.deserialize_str(TimestampVisitor)
    }
}

impl From<DateTime<Utc>> for Timestamp {
    fn from(dt: DateTime<Utc>) -> Self {
        Timestamp {
            seconds: dt.timestamp(),
            nanos: dt.timestamp_subsec_nanos() as i32,
        }
    }
}
#[derive(Clone, PartialEq, Eq, ::prost::Message, schemars::JsonSchema)]
pub struct Duration {
    /// Signed seconds of the span of time. Must be from -315,576,000,000
    /// to +315,576,000,000 inclusive. Note: these bounds are computed from:
    /// 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years
    #[prost(int64, tag = "1")]
    pub seconds: i64,
    /// Signed fractions of a second at nanosecond resolution of the span
    /// of time. Durations less than one second are represented with a 0
    /// `seconds` field and a positive or negative `nanos` field. For durations
    /// of one second or more, a non-zero value for the `nanos` field must be
    /// of the same sign as the `seconds` field. Must be from -999,999,999
    /// to +999,999,999 inclusive.
    #[prost(int32, tag = "2")]
    pub nanos: i32,
}

impl Serialize for Duration {
    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
    where
        S: Serializer,
    {
        let mut d = prost_types::Duration::from(self.to_owned());
        d.normalize();

        serializer.serialize_str(d.to_string().as_str())
    }
}

impl<'de> Deserialize<'de> for Duration {
    fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
    where
        D: Deserializer<'de>,
    {
        struct DurationVisitor;

        impl<'de> Visitor<'de> for DurationVisitor {
            type Value = Duration;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("Timestamp in RFC3339 format")
            }

            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                value
                    .parse::<prost_types::Duration>()
                    .map(Into::into)
                    .map_err(de::Error::custom)
            }
        }
        deserializer.deserialize_str(DurationVisitor)
    }
}

#[derive(Clone, PartialEq, Eq, ::prost::Message, schemars::JsonSchema)]
pub struct Any {
    /// A URL/resource name that uniquely identifies the type of the serialized
    /// protocol buffer message. This string must contain at least
    /// one "/" character. The last segment of the URL's path must represent
    /// the fully qualified name of the type (as in
    /// `path/google.protobuf.Duration`). The name should be in a canonical form
    /// (e.g., leading "." is not accepted).
    ///
    /// In practice, teams usually precompile into the binary all types that they
    /// expect it to use in the context of Any. However, for URLs which use the
    /// scheme `http`, `https`, or no scheme, one can optionally set up a type
    /// server that maps type URLs to message definitions as follows:
    ///
    /// * If no scheme is provided, `https` is assumed.
    /// * An HTTP GET on the URL must yield a \[google.protobuf.Type][\]
    ///   value in binary format, or produce an error.
    /// * Applications are allowed to cache lookup results based on the
    ///   URL, or have them precompiled into a binary to avoid any
    ///   lookup. Therefore, binary compatibility needs to be preserved
    ///   on changes to types. (Use versioned type names to manage
    ///   breaking changes.)
    ///
    /// Note: this functionality is not currently available in the official
    /// protobuf release, and it is not used for type URLs beginning with
    /// type.googleapis.com.
    ///
    /// Schemes other than `http`, `https` (or the empty scheme) might be
    /// used with implementation specific semantics.
    ///
    #[prost(string, tag = "1")]
    pub type_url: String,
    /// Must be a valid serialized protocol buffer of the above specified type.
    #[prost(bytes = "vec", tag = "2")]
    pub value: Vec<u8>,
}

macro_rules! expand_as_any {
    ($($ty:path,)*) => {

        impl Serialize for Any {
            fn serialize<S>(
                &self,
                serializer: S,
            ) -> Result<<S as ::serde::Serializer>::Ok, <S as ::serde::Serializer>::Error>
            where
                S: ::serde::Serializer,
            {
                $(
                    if self.type_url == <$ty>::TYPE_URL {
                        let value: Result<$ty, <S as ::serde::Serializer>::Error> =
                            prost::Message::decode(self.value.as_slice()).map_err(ser::Error::custom);

                        if let Ok(value) = value {
                            return value.serialize(serializer);
                        }
                    }
                )*

                Err(serde::ser::Error::custom(
                    "data did not match any type that supports serialization as `Any`",
                ))
            }
        }

        impl<'de> Deserialize<'de> for Any {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: serde::Deserializer<'de>,
            {
                let value = match serde_cw_value::Value::deserialize(deserializer) {
                    Ok(value) => value,
                    Err(err) => {
                        return Err(err);
                    }
                };

                // must be map er else error
                let type_url = if let serde_cw_value::Value::Map(m) = value.clone() {
                    m.get(&serde_cw_value::Value::String("@type".to_string()))
                        .map(|t| match t.to_owned() {
                            serde_cw_value::Value::String(s) => Ok(s),
                            _ => Err(serde::de::Error::custom("type_url must be String")),
                        })
                        .transpose()
                } else {
                    Err(serde::de::Error::custom("data must have map structure"))
                }?;

                match type_url {
                    // @type found
                    Some(t) => {
                        $(
                            if t == <$ty>::TYPE_URL {
                                return <$ty>::deserialize(
                                    serde_cw_value::ValueDeserializer::<serde_cw_value::DeserializerError>::new(
                                        value.clone(),
                                    ),
                                )
                                .map(|v| Any {
                                    type_url: <$ty>::TYPE_URL.to_string(),
                                    value: v.encode_to_vec(),
                                })
                                .map_err(serde::de::Error::custom);
                            }
                        )*
                    }
                    // @type not found, try match the type structure
                    None => {
                        $(
                            if let Ok(v) = <$ty>::deserialize(
                                serde_cw_value::ValueDeserializer::<serde_cw_value::DeserializerError>::new(
                                    value.clone(),
                                ),
                            ) {
                                return Ok(Any {
                                    type_url: <$ty>::TYPE_URL.to_string(),
                                    value: v.encode_to_vec(),
                                });
                            }
                        )*
                    }
                };

                Err(serde::de::Error::custom(
                    "data did not match any type that supports deserialization as `Any`",
                ))
            }
        }
    };
}

// [HACK] Register all types that can serde as Any manually for now.
// must order by type that has more information for Any deserialization to
// work correctly. Since after serialization, it currently loses @type tag.
// And deserialization works by trying to iteratively match the structure.
expand_as_any!(crate::types::provenance::marker::v1::MarkerAccount,);

macro_rules! impl_prost_types_exact_conversion {
    ($t:ident | $($arg:ident),*) => {
        impl From<$t> for prost_types::$t {
            fn from(src: $t) -> Self {
                prost_types::$t {
                    $(
                        $arg: src.$arg,
                    )*
                }
            }
        }

        impl From<prost_types::$t> for $t {
            fn from(src: prost_types::$t) -> Self {
                $t {
                    $(
                        $arg: src.$arg,
                    )*
                }
            }
        }
    };
}

impl_prost_types_exact_conversion! { Timestamp | seconds, nanos }
impl_prost_types_exact_conversion! { Duration | seconds, nanos }
impl_prost_types_exact_conversion! { Any | type_url, value }

impl From<cosmwasm_std::Coin> for crate::types::cosmos::base::v1beta1::Coin {
    fn from(cosmwasm_std::Coin { denom, amount }: cosmwasm_std::Coin) -> Self {
        crate::types::cosmos::base::v1beta1::Coin {
            denom,
            amount: amount.into(),
        }
    }
}

impl TryFrom<crate::types::cosmos::base::v1beta1::Coin> for cosmwasm_std::Coin {
    type Error = cosmwasm_std::StdError;

    fn try_from(
        crate::types::cosmos::base::v1beta1::Coin { denom, amount }: crate::types::cosmos::base::v1beta1::Coin,
    ) -> StdResult<Self> {
        Ok(cosmwasm_std::Coin {
            denom,
            amount: amount.parse()?,
        })
    }
}

/// Convert a list of `Coin` from provenance proto generated `Coin` type to cosmwasm `Coin` type
pub fn try_proto_to_cosmwasm_coins(
    coins: impl IntoIterator<Item = crate::types::cosmos::base::v1beta1::Coin>,
) -> StdResult<Vec<cosmwasm_std::Coin>> {
    coins.into_iter().map(|c| c.try_into()).collect()
}

/// Convert a list of `Coin` from cosmwasm `Coin` type to proto generated `Coin` type
pub fn cosmwasm_to_proto_coins(
    coins: impl IntoIterator<Item = cosmwasm_std::Coin>,
) -> Vec<crate::types::cosmos::base::v1beta1::Coin> {
    coins.into_iter().map(|c| c.into()).collect()
}

#[cfg(test)]
mod tests {
    use cosmwasm_std::Uint128;

    use super::*;

    #[test]
    fn test_coins_conversion() {
        let coins = vec![
            cosmwasm_std::Coin {
                denom: "uatom".to_string(),
                amount: Uint128::new(100),
            },
            cosmwasm_std::Coin {
                denom: "nhash".to_string(),
                amount: Uint128::new(200),
            },
        ];

        let proto_coins = cosmwasm_to_proto_coins(coins.clone());
        let cosmwasm_coins = try_proto_to_cosmwasm_coins(proto_coins).unwrap();

        assert_eq!(coins, cosmwasm_coins);
    }
}