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
use super::{
    cosmos_modules::{
        abci::{AbciMessageLog, Attribute, StringEvent, TxResponse},
        tendermint_abci::Event,
    },
    error::DaemonError,
};
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};

use cosmwasm_std::{to_binary, Binary, StdError, StdResult};
use cw_orch_core::environment::IndexResponse;
use serde::{Deserialize, Serialize};

const FORMAT: &str = "%Y-%m-%dT%H:%M:%S%.f";
const FORMAT_TZ_SUPPLIED: &str = "%Y-%m-%dT%H:%M:%S.%f%:z";
const FORMAT_SHORT_Z: &str = "%Y-%m-%dT%H:%M:%SZ";
const FORMAT_SHORT_Z2: &str = "%Y-%m-%dT%H:%M:%S.%fZ";

/// The response from a transaction performed on a blockchain.
#[derive(Debug, Default, Clone)]
pub struct CosmTxResponse {
    /// Height of the block in which the transaction was included.
    pub height: u64,
    /// Transaction hash.
    pub txhash: String,
    /// Transaction index within the block.
    pub codespace: String,
    /// Transaction result code
    pub code: usize,
    /// Arbitrary data that can be included in a transaction.
    pub data: String,
    /// Raw log message.
    pub raw_log: String,
    /// Logs of the transaction.
    pub logs: Vec<TxResultBlockMsg>,
    /// Transaction info.
    pub info: String,
    /// Gas limit.
    pub gas_wanted: u64,
    /// Gas used.
    pub gas_used: u64,
    /// Timestamp of the block in which the transaction was included.
    pub timestamp: DateTime<Utc>,
    /// Transaction events.
    pub events: Vec<Event>,
}

impl CosmTxResponse {
    /// find a attribute's value from TX logs.
    /// returns: msg_index and value
    pub fn get_attribute_from_logs(
        &self,
        event_type: &str,
        attribute_key: &str,
    ) -> Vec<(usize, String)> {
        let mut response: Vec<(usize, String)> = Default::default();
        let logs = &self.logs;

        for log_part in logs {
            let msg_index = log_part.msg_index.unwrap_or_default();
            let events = &log_part.events;

            let events_filtered = events
                .iter()
                .filter(|event| event.s_type == event_type)
                .collect::<Vec<_>>();

            if let Some(event) = events_filtered.first() {
                let attributes_filtered = event
                    .attributes
                    .iter()
                    .filter(|attr| attr.key == attribute_key)
                    .map(|f| f.value.clone())
                    .collect::<Vec<_>>();

                if let Some(attr_key) = attributes_filtered.first() {
                    response.push((msg_index, attr_key.clone()));
                }
            }
        }

        response
    }

    /// get the list of event types from a TX record
    pub fn get_events(&self, event_type: &str) -> Vec<TxResultBlockEvent> {
        let mut response: Vec<TxResultBlockEvent> = Default::default();

        for log_part in &self.logs {
            let events = &log_part.events;

            let events_filtered = events
                .iter()
                .filter(|event| event.s_type == event_type)
                .collect::<Vec<_>>();

            for event in events_filtered {
                response.push(event.clone());
            }
        }

        response
    }
}

// NOTE: Should we keep this here or only for tests?
impl From<&serde_json::Value> for TxResultBlockMsg {
    fn from(value: &serde_json::Value) -> Self {
        serde_json::from_value(value.clone()).unwrap()
    }
}

impl From<TxResponse> for CosmTxResponse {
    fn from(tx: TxResponse) -> Self {
        Self {
            height: tx.height as u64,
            txhash: tx.txhash,
            codespace: tx.codespace,
            code: tx.code as usize,
            data: tx.data,
            raw_log: tx.raw_log,
            logs: tx.logs.into_iter().map(TxResultBlockMsg::from).collect(),
            info: tx.info,
            gas_wanted: tx.gas_wanted as u64,
            gas_used: tx.gas_used as u64,
            timestamp: parse_timestamp(tx.timestamp).unwrap(),
            events: tx.events,
        }
    }
}

impl IndexResponse for CosmTxResponse {
    fn events(&self) -> Vec<cosmwasm_std::Event> {
        let mut parsed_events = vec![];

        for event in &self.events {
            let mut pattr = vec![];

            for attr in &event.attributes {
                pattr.push(cosmwasm_std::Attribute {
                    key: attr.key.clone(),
                    value: attr.value.clone(),
                })
            }

            let pevent = cosmwasm_std::Event::new(event.r#type.clone()).add_attributes(pattr);

            parsed_events.push(pevent);
        }

        parsed_events
    }

    fn data(&self) -> Option<Binary> {
        if self.data.is_empty() {
            None
        } else {
            Some(to_binary(self.data.as_bytes()).unwrap())
        }
    }

    fn event_attr_value(&self, event_type: &str, attr_key: &str) -> StdResult<String> {
        for event in &self.events {
            if event.r#type == event_type {
                for attr in &event.attributes {
                    if attr.key == attr_key {
                        return Ok(attr.value.clone());
                    }
                }
            }
        }

        Err(StdError::generic_err(format!(
            "event of type {event_type} does not have a value at key {attr_key}"
        )))
    }
}

/// The events from a single message in a transaction.
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct TxResultBlockMsg {
    /// index of the message in the transaction
    pub msg_index: Option<usize>,
    /// Events from this message
    pub events: Vec<TxResultBlockEvent>,
}

impl From<AbciMessageLog> for TxResultBlockMsg {
    fn from(msg: AbciMessageLog) -> Self {
        Self {
            msg_index: Some(msg.msg_index as usize),
            events: msg
                .events
                .into_iter()
                .map(TxResultBlockEvent::from)
                .collect(),
        }
    }
}

/// A single event from a transaction and its attributes.
#[derive(Deserialize, Clone, Serialize, Debug)]
pub struct TxResultBlockEvent {
    #[serde(rename = "type")]
    /// Type of the event
    pub s_type: String,
    /// Attributes of the event
    pub attributes: Vec<TxResultBlockAttribute>,
}

impl From<StringEvent> for TxResultBlockEvent {
    fn from(event: StringEvent) -> Self {
        Self {
            s_type: event.r#type,
            attributes: event
                .attributes
                .into_iter()
                .map(TxResultBlockAttribute::from)
                .collect(),
        }
    }
}

impl TxResultBlockEvent {
    /// get all key/values from the event that have the key 'key'
    pub fn get_attributes(&self, key: &str) -> Vec<TxResultBlockAttribute> {
        self.attributes
            .iter()
            .filter(|attr| attr.key == key)
            .cloned()
            .collect()
    }

    /// return the first value of the first attribute that has the key 'key'
    pub fn get_first_attribute_value(&self, key: &str) -> Option<String> {
        self.get_attributes(key)
            .first()
            .map(|attr| attr.value.clone())
    }
}

/// A single attribute of an event.
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct TxResultBlockAttribute {
    /// Key of the attribute
    pub key: String,
    /// Value of the attribute
    pub value: String,
}

impl From<Attribute> for TxResultBlockAttribute {
    fn from(a: Attribute) -> Self {
        Self {
            key: a.key,
            value: a.value,
        }
    }
}

/// Parse a string timestamp into a `DateTime<Utc>`
pub fn parse_timestamp(s: String) -> Result<DateTime<Utc>, DaemonError> {
    let len = s.len();

    let slice_len = if s.contains('.') {
        len.saturating_sub(4)
    } else {
        len
    };

    let sliced = &s[0..slice_len];

    match NaiveDateTime::parse_from_str(sliced, FORMAT) {
        Err(_e) => match NaiveDateTime::parse_from_str(&s, FORMAT_TZ_SUPPLIED) {
            Err(_e2) => match NaiveDateTime::parse_from_str(sliced, FORMAT_SHORT_Z) {
                // block 6877827 has this
                Err(_e3) => match NaiveDateTime::parse_from_str(&s, FORMAT_SHORT_Z2) {
                    Err(_e4) => {
                        eprintln!("DateTime Fail {s} {_e4:#?}");
                        Err(DaemonError::StdErr(_e4.to_string()))
                    }
                    Ok(dt) => Ok(Utc.from_utc_datetime(&dt)),
                },
                Ok(dt) => Ok(Utc.from_utc_datetime(&dt)),
            },
            Ok(dt) => Ok(Utc.from_utc_datetime(&dt)),
        },
        Ok(dt) => Ok(Utc.from_utc_datetime(&dt)),
    }
}