af_iperps/
stop_order_helpers.rs1use af_utilities::IFixed;
4use fastcrypto::hash::{Blake2b256, HashFunction};
5use serde::{Deserialize, Serialize};
6use sui_framework_sdk::object::ID;
7
8use crate::order_helpers::{OrderType, Side};
9
10pub trait StopOrderTicketDetails {
11 fn encrypted_details(&self, salt: Vec<u8>) -> Result<Vec<u8>, sui_sdk_types::bcs::Error>
13 where
14 Self: serde::Serialize,
15 {
16 let mut bytes = sui_sdk_types::bcs::ToBcs::to_bcs(&self)?;
17 bytes.extend(salt);
18 Ok(Blake2b256::digest(bytes).to_vec())
19 }
20}
21
22#[derive(Debug, serde::Serialize)]
24pub struct SLTPDetails {
25 pub clearing_house_id: ID,
26 pub expire_timestamp: Option<u64>,
28 pub is_limit_order: bool,
30 pub stop_loss_price: Option<IFixed>,
32 pub take_profit_price: Option<IFixed>,
34 pub position_is_ask: bool,
36 pub size: u64,
37 pub price: u64,
39 pub order_type: OrderType,
41}
42
43impl StopOrderTicketDetails for SLTPDetails {}
44
45#[derive(Debug, serde::Serialize)]
47pub struct StandaloneDetails {
48 pub clearing_house_id: ID,
49 pub expire_timestamp: Option<u64>,
51 pub is_limit_order: bool,
53 pub stop_index_price: IFixed,
54 pub ge_stop_index_price: bool,
57 pub side: Side,
58 pub size: u64,
59 pub price: u64,
61 pub order_type: OrderType,
63 pub reduce_only: bool,
64}
65
66impl StopOrderTicketDetails for StandaloneDetails {}
67
68#[derive(Clone, Copy, Debug, clap::ValueEnum, Serialize, Deserialize)]
69#[serde(into = "u64")]
70pub enum StopOrderType {
71 SLTP,
73 Standalone,
75}
76
77impl From<StopOrderType> for u64 {
78 fn from(value: StopOrderType) -> Self {
79 match value {
80 StopOrderType::SLTP => 0,
81 StopOrderType::Standalone => 1,
82 }
83 }
84}
85
86#[derive(thiserror::Error, Debug)]
87#[error("Invalid stop order type value")]
88pub struct InvalidStopOrderTypeValue;
89
90impl TryFrom<u64> for StopOrderType {
91 type Error = InvalidStopOrderTypeValue;
92
93 fn try_from(value: u64) -> Result<Self, Self::Error> {
94 match value {
95 0 => Ok(Self::SLTP),
96 1 => Ok(Self::Standalone),
97 _ => Err(InvalidStopOrderTypeValue),
98 }
99 }
100}