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

use crate::asset::CryptoAsset;
use crate::chain::Chain;
use crate::number::Ex3Uint;
use crate::{AssetId, WalletRegisterId};

/// Register an asset
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize, Hash)]
pub struct AssetRegistration {
    pub parent_id: Option<AssetId>,
    pub asset: CryptoAsset,
}

/// Update the confirmation times for a specific network and chain
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct UpdateChainConfirmationTimes {
    /// The chain of tokens belongs to
    pub chain: Chain,
    /// The confirmation time of the tx of the token can be confirmed
    pub confirmation_times: Ex3Uint,
}

/// Update global fee to
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct UpdateGlobalWithdrawalFeeTo {
    pub fee_to: WalletRegisterId,
}

/// Update asset fee to
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct UpdateAssetWithdrawalFeeTo {
    /// The asset id
    pub asset_id: AssetId,

    /// The fee to
    pub fee_to: Option<WalletRegisterId>,
}

#[cfg(test)]
mod tests {
    use ex3_serde::{bincode, cbor};

    use crate::asset::TokenType;
    use crate::chain::ChainType;

    use super::*;

    #[test]
    fn test_register_asset_bincode_serde() {
        let register_asset = AssetRegistration {
            parent_id: None,
            asset: CryptoAsset {
                chain: Chain {
                    r#type: ChainType::Ethereum,
                    network: 1u8.into(),
                },
                r#type: TokenType::ERC20,
                address: "0x01283012830123".to_string(),
            },
        };

        // bincode
        let encoded = bincode::serialize(&register_asset).unwrap();
        let decoded: AssetRegistration = bincode::deserialize(&encoded).unwrap();
        assert_eq!(register_asset, decoded);

        // cbor
        let encoded = cbor::serialize(&register_asset).unwrap();
        let decoded: AssetRegistration = cbor::deserialize(&encoded).unwrap();
        assert_eq!(register_asset, decoded);
    }

    #[test]
    fn test_update_token_type_confirmation_times_bincode_serde() {
        let update_token_type_confirmation_times = UpdateChainConfirmationTimes {
            chain: Chain {
                r#type: ChainType::Ethereum,
                network: 1u8.into(),
            },
            confirmation_times: Ex3Uint::from(100u32),
        };

        let encoded = bincode::serialize(&update_token_type_confirmation_times).unwrap();
        let decoded: UpdateChainConfirmationTimes = bincode::deserialize(&encoded).unwrap();
        assert_eq!(update_token_type_confirmation_times, decoded);
    }

    #[test]
    fn test_update_token_type_confirmation_times_serde() {
        let update_token_type_confirmation_times = UpdateChainConfirmationTimes {
            chain: Chain {
                r#type: ChainType::Ethereum,
                network: 1u8.into(),
            },
            confirmation_times: Ex3Uint::from(100u32),
        };

        // bincode
        let encoded = cbor::serialize(&update_token_type_confirmation_times).unwrap();
        let decoded: UpdateChainConfirmationTimes = cbor::deserialize(&encoded).unwrap();
        assert_eq!(update_token_type_confirmation_times, decoded);

        // cbor
        let encoded = cbor::serialize(&update_token_type_confirmation_times).unwrap();
        let decoded: UpdateChainConfirmationTimes = cbor::deserialize(&encoded).unwrap();
        assert_eq!(update_token_type_confirmation_times, decoded);
    }

    #[test]
    fn test_update_global_fee_to_serde() {
        let update_global_fee_to = UpdateGlobalWithdrawalFeeTo {
            fee_to: WalletRegisterId::from(100u32),
        };

        // bincode
        let encoded = bincode::serialize(&update_global_fee_to).unwrap();
        let decoded: UpdateGlobalWithdrawalFeeTo = bincode::deserialize(&encoded).unwrap();
        assert_eq!(update_global_fee_to, decoded);

        // cbor
        let encoded = cbor::serialize(&update_global_fee_to).unwrap();
        let decoded: UpdateGlobalWithdrawalFeeTo = cbor::deserialize(&encoded).unwrap();
        assert_eq!(update_global_fee_to, decoded);
    }

    #[test]
    fn test_update_asset_fee_to_serde() {
        let update_asset_fee_to = UpdateAssetWithdrawalFeeTo {
            asset_id: AssetId::from(100u32),
            fee_to: Some(WalletRegisterId::from(100u32)),
        };

        // bincode
        let encoded = bincode::serialize(&update_asset_fee_to).unwrap();
        let decoded: UpdateAssetWithdrawalFeeTo = bincode::deserialize(&encoded).unwrap();
        assert_eq!(update_asset_fee_to, decoded);

        // cbor
        let encoded = cbor::serialize(&update_asset_fee_to).unwrap();
        let decoded: UpdateAssetWithdrawalFeeTo = cbor::deserialize(&encoded).unwrap();
        assert_eq!(update_asset_fee_to, decoded);
    }
}