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 {
    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())
    }
    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())
    }
    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())
    }
    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())
    }
}