dip721_rs/
event.rs

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
use candid::{CandidType, Deserialize, Principal};
use serde::Serialize;

use crate::GenericValue;

/// Transaction event
#[derive(CandidType, Deserialize, Serialize, Clone, Debug, PartialEq)]
pub struct TxEvent {
    pub caller: Principal,
    pub details: Vec<(String, GenericValue)>,
    pub operation: String,
    pub time: u64,
}

#[cfg(feature = "ic-stable-structures")]
impl ic_stable_structures::Storable for TxEvent {
    const BOUND: ic_stable_structures::storable::Bound =
        ic_stable_structures::storable::Bound::Unbounded;

    fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
        use candid::Encode;
        Encode!(&self).unwrap().into()
    }

    fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
        use candid::Decode;
        Decode!(&bytes, Self).unwrap()
    }
}

#[cfg(test)]
mod test {

    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    #[cfg(feature = "ic-stable-structures")]
    fn test_should_encode_and_decode_tx_event() {
        use ic_stable_structures::Storable as _;
        let tx_event = TxEvent {
            caller: Principal::management_canister(),
            details: vec![(
                "key".to_string(),
                GenericValue::TextContent("value".to_string()),
            )],
            operation: "operation".to_string(),
            time: 1,
        };
        let bytes = tx_event.to_bytes();
        let decoded = TxEvent::from_bytes(bytes);
        assert_eq!(tx_event, decoded);
    }
}