use af_utilities::IFixed;
use fastcrypto::hash::{Blake2b256, HashFunction};
use serde::{Deserialize, Serialize};
use sui_framework_sdk::object::ID;
use crate::order_helpers::{OrderType, Side};
pub trait StopOrderTicketDetails {
fn encrypted_details(&self, salt: Vec<u8>) -> Result<Vec<u8>, sui_sdk_types::bcs::Error>
where
Self: serde::Serialize,
{
let mut bytes = sui_sdk_types::bcs::ToBcs::to_bcs(&self)?;
bytes.extend(salt);
Ok(Blake2b256::digest(bytes).to_vec())
}
}
#[derive(Debug, serde::Serialize)]
pub struct SLTPDetails {
pub clearing_house_id: ID,
pub expire_timestamp: Option<u64>,
pub is_limit_order: bool,
pub stop_loss_price: Option<IFixed>,
pub take_profit_price: Option<IFixed>,
pub position_is_ask: bool,
pub size: u64,
pub price: u64,
pub order_type: OrderType,
}
impl StopOrderTicketDetails for SLTPDetails {}
#[derive(Debug, serde::Serialize)]
pub struct StandaloneDetails {
pub clearing_house_id: ID,
pub expire_timestamp: Option<u64>,
pub is_limit_order: bool,
pub stop_index_price: IFixed,
pub ge_stop_index_price: bool,
pub side: Side,
pub size: u64,
pub price: u64,
pub order_type: OrderType,
pub reduce_only: bool,
}
impl StopOrderTicketDetails for StandaloneDetails {}
#[derive(Clone, Copy, Debug, clap::ValueEnum, Serialize, Deserialize)]
#[serde(into = "u64")]
pub enum StopOrderType {
SLTP,
Standalone,
}
impl From<StopOrderType> for u64 {
fn from(value: StopOrderType) -> Self {
match value {
StopOrderType::SLTP => 0,
StopOrderType::Standalone => 1,
}
}
}
#[derive(thiserror::Error, Debug)]
#[error("Invalid stop order type value")]
pub struct InvalidStopOrderTypeValue;
impl TryFrom<u64> for StopOrderType {
type Error = InvalidStopOrderTypeValue;
fn try_from(value: u64) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::SLTP),
1 => Ok(Self::Standalone),
_ => Err(InvalidStopOrderTypeValue),
}
}
}