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

use crate::tx_type_dto::{
    ClaimSpotMarketRoyalty, ClaimSpotMarketTradingFee, SpotMarketRegistration,
    UpdateSpotMarketFeeTo, UpdateSpotMarketInitialFeeTo, UpdateSpotMarketInitialTradingFee,
    UpdateSpotMarketRoyalty, UpdateSpotMarketTradingFee, UpdateSpotMarketTradingSettings,
};
use crate::PayloadDecoder;
use crate::Result;

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

    /// Decode the payload to update spot market settings
    pub fn decode_to_update_spot_market_trading_settings(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::UpdateSpotMarketTradingSettings> {
        let update_market_settings = cbor::deserialize::<UpdateSpotMarketTradingSettings>(payload)
            .map_err(|e| {
                <OtherError as Into<Error>>::into(OtherError::new(format!(
                    "Failed to deserialize payload to UpdateSpotMarketTradingSettings: {}",
                    e
                )))
            })?;
        Ok(update_market_settings.into())
    }

    /// Decode the payload to update spot market initial fee to
    pub fn decode_to_update_spot_market_initial_fee_to(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::UpdateSpotMarketInitialFeeTo> {
        let update_market_initial_fee_to =
            cbor::deserialize::<UpdateSpotMarketInitialFeeTo>(payload).map_err(|e| {
                <OtherError as Into<Error>>::into(OtherError::new(format!(
                    "Failed to deserialize payload to UpdateMarketInitialFeeTo: {}",
                    e
                )))
            })?;
        Ok(update_market_initial_fee_to.into())
    }

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

    /// Decode the payload to update spot market initial trading fee
    pub fn decode_to_update_spot_market_initial_trading_fee(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::UpdateSpotMarketInitialTradingFee> {
        let update_market_initial_fee =
            cbor::deserialize::<UpdateSpotMarketInitialTradingFee>(payload).map_err(|e| {
                <OtherError as Into<Error>>::into(OtherError::new(format!(
                    "Failed to deserialize payload to UpdateSpotMarketInitialFee: {}",
                    e
                )))
            })?;
        Ok(update_market_initial_fee.into())
    }

    /// Decode the payload to update spot market trading fee
    pub fn decode_to_update_spot_market_trading_fee(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::UpdateSpotMarketTradingFee> {
        let update_market_fee =
            cbor::deserialize::<UpdateSpotMarketTradingFee>(payload).map_err(|e| {
                <OtherError as Into<Error>>::into(OtherError::new(format!(
                    "Failed to deserialize payload to UpdateSpotMarketFee: {}",
                    e
                )))
            })?;
        Ok(update_market_fee.into())
    }

    /// Decode the payload to update spot market royalty
    pub fn decode_to_update_spot_market_royalty(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::UpdateSpotMarketRoyalty> {
        let update_market_royalty =
            cbor::deserialize::<UpdateSpotMarketRoyalty>(payload).map_err(|e| {
                <OtherError as Into<Error>>::into(OtherError::new(format!(
                    "Failed to deserialize payload to UpdateSpotMarketRoyalty: {}",
                    e
                )))
            })?;
        Ok(update_market_royalty.into())
    }

    /// Decode the payload to claim spot market royalty
    pub fn decode_to_claim_spot_market_royalty(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::ClaimSpotMarketRoyalty> {
        let claim_market_royalty =
            cbor::deserialize::<ClaimSpotMarketRoyalty>(payload).map_err(|e| {
                <OtherError as Into<Error>>::into(OtherError::new(format!(
                    "Failed to deserialize payload to ClaimSpotMarketRoyalty: {}",
                    e
                )))
            })?;
        Ok(claim_market_royalty.into())
    }

    /// Decode the payload to claim spot market trading fee
    pub fn decode_to_claim_spot_market_trading_fee(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::ClaimSpotMarketTradingFee> {
        let claim_market_trading_fee = cbor::deserialize::<ClaimSpotMarketTradingFee>(payload)
            .map_err(|e| {
                <OtherError as Into<Error>>::into(OtherError::new(format!(
                    "Failed to deserialize payload to ClaimSpotMarketTradingFee: {}",
                    e
                )))
            })?;
        Ok(claim_market_trading_fee.into())
    }
}