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

use crate::tx_type_dto::{
    RegisterSpotMarket, UpdateMarketFeeTo, UpdateMarketInitialFeeTo, UpdateSpotMarketFee,
    UpdateSpotMarketInitialFee, 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::RegisterSpotMarket> {
        let register_market = cbor::deserialize::<RegisterSpotMarket>(payload).map_err(|e| {
            <OtherError as Into<Error>>::into(OtherError::new(format!(
                "Failed to deserialize payload to RegisterSpotMarket: {}",
                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 market global fee to
    pub fn decode_to_update_market_global_fee_to(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::UpdateMarketInitialFeeTo> {
        let update_market_global_fee_to = cbor::deserialize::<UpdateMarketInitialFeeTo>(payload)
            .map_err(|e| {
                <OtherError as Into<Error>>::into(OtherError::new(format!(
                    "Failed to deserialize payload to UpdateMarketInitialFeeTo: {}",
                    e
                )))
            })?;
        Ok(update_market_global_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::UpdateMarketFeeTo> {
        let update_market_fee_to =
            cbor::deserialize::<UpdateMarketFeeTo>(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 market global fee
    pub fn decode_to_update_market_global_fee(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::UpdateSpotMarketInitialFee> {
        let update_market_global_fee = cbor::deserialize::<UpdateSpotMarketInitialFee>(payload)
            .map_err(|e| {
                <OtherError as Into<Error>>::into(OtherError::new(format!(
                    "Failed to deserialize payload to UpdateSpotMarketInitialFee: {}",
                    e
                )))
            })?;
        Ok(update_market_global_fee.into())
    }

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