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
use ex3_node_error::{Error, OtherError};
use ex3_serde::cbor;

use crate::tx_type_dto::{
    RegisterAsset, UpdateAssetWithdrawalFeeTo, UpdateChainConfirmationTimes,
    UpdateGlobalWithdrawalFeeTo,
};
use crate::PayloadDecoder;
use crate::Result;

impl PayloadDecoder {
    /// Decode the payload to register asset
    pub fn decode_to_register_asset(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::RegisterAsset> {
        let register_asset = cbor::deserialize::<RegisterAsset>(payload).map_err(|e| {
            <OtherError as Into<Error>>::into(OtherError::new(format!(
                "Failed to deserialize payload to RegisterAsset: {}",
                e
            )))
        })?;
        Ok(register_asset.into())
    }

    /// Decode the payload to update global withdrawal fee to
    pub fn decode_to_update_global_withdrawal_fee_to(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::UpdateGlobalWithdrawalFeeTo> {
        let update_global_withdrawal_fee_to =
            cbor::deserialize::<UpdateGlobalWithdrawalFeeTo>(payload).map_err(|e| {
                <OtherError as Into<Error>>::into(OtherError::new(format!(
                    "Failed to deserialize payload to update_global_withdrawal_fee_to: {}",
                    e
                )))
            })?;
        Ok(update_global_withdrawal_fee_to.into())
    }

    /// Decode the payload to update asset withdrawal fee to
    pub fn decode_to_update_asset_withdrawal_fee_to(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::UpdateAssetWithdrawalFeeTo> {
        let update_asset_withdrawal_fee_to =
            cbor::deserialize::<UpdateAssetWithdrawalFeeTo>(payload).map_err(|e| {
                <OtherError as Into<Error>>::into(OtherError::new(format!(
                    "Failed to deserialize payload to UpdateAssetWithdrawalFee: {}",
                    e
                )))
            })?;
        Ok(update_asset_withdrawal_fee_to.into())
    }

    /// Decode the payload to update chain confirmation times
    pub fn decode_to_update_chain_confirmation_times(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::UpdateChainConfirmationTimes> {
        let update_chain_confirmation_times =
            cbor::deserialize::<UpdateChainConfirmationTimes>(payload).map_err(|e| {
                <OtherError as Into<Error>>::into(OtherError::new(format!(
                    "Failed to deserialize payload to UpdateChainConfirmationTimes: {}",
                    e
                )))
            })?;
        Ok(update_chain_confirmation_times.into())
    }
}