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

use crate::tx_type_dto::{
    AddAmmV2Liquidity, CancelSpotOrder, RemoveAmmV2Liquidity, SubmitSpotOrder,
};
use crate::PayloadDecoder;
use crate::Result;

impl PayloadDecoder {
    /// Decode the payload to submit spot order
    pub fn decode_to_submit_spot_order(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::SubmitSpotOrder> {
        let submit_order = cbor::deserialize::<SubmitSpotOrder>(payload).map_err(|e| {
            <OtherError as Into<Error>>::into(OtherError::new(format!(
                "Failed to deserialize payload to SubmitOrder: {}",
                e
            )))
        })?;
        Ok(submit_order.into())
    }

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

    /// Decode the payload to add liquidity
    pub fn decode_to_add_amm_v2_liquidity(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::AddAmmV2Liquidity> {
        let add_liquidity = cbor::deserialize::<AddAmmV2Liquidity>(payload).map_err(|e| {
            <OtherError as Into<Error>>::into(OtherError::new(format!(
                "Failed to deserialize payload to AddLiquidity: {}",
                e
            )))
        })?;
        Ok(add_liquidity.into())
    }

    /// Decode the payload to remove liquidity
    pub fn decode_to_remove_amm_v2_liquidity(
        payload: &[u8],
    ) -> Result<ex3_node_types::transaction::RemoveAmmV2Liquidity> {
        let remove_liquidity = cbor::deserialize::<RemoveAmmV2Liquidity>(payload).map_err(|e| {
            <OtherError as Into<Error>>::into(OtherError::new(format!(
                "Failed to deserialize payload to RemoveLiquidity: {}",
                e
            )))
        })?;
        Ok(remove_liquidity.into())
    }
}