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>) -> bcs::Result<Vec<u8>>
13 where
14 Self: serde::Serialize,
15 {
16 let mut bytes = bcs::to_bytes(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_index_price: IFixed,
31 pub is_stop_loss: bool,
33 pub position_is_ask: bool,
35 pub size: u64,
36 pub price: u64,
38 pub order_type: OrderType,
40}
41
42impl StopOrderTicketDetails for SLTPDetails {}
43
44#[derive(Debug, serde::Serialize)]
46pub struct StandaloneDetails {
47 pub clearing_house_id: ID,
48 pub expire_timestamp: Option<u64>,
50 pub is_limit_order: bool,
52 pub stop_index_price: IFixed,
53 pub ge_stop_index_price: bool,
56 pub side: Side,
57 pub size: u64,
58 pub price: u64,
60 pub order_type: OrderType,
62 pub reduce_only: bool,
63}
64
65impl StopOrderTicketDetails for StandaloneDetails {}
66
67#[derive(Clone, Copy, Debug, clap::ValueEnum, Serialize, Deserialize)]
68#[serde(into = "u64")]
69pub enum StopOrderType {
70 SLTP,
72 Standalone,
74}
75
76impl From<StopOrderType> for u64 {
77 fn from(value: StopOrderType) -> Self {
78 match value {
79 StopOrderType::SLTP => 0,
80 StopOrderType::Standalone => 1,
81 }
82 }
83}
84
85#[derive(thiserror::Error, Debug)]
86#[error("Invalid stop order type value")]
87pub struct InvalidStopOrderTypeValue;
88
89impl TryFrom<u64> for StopOrderType {
90 type Error = InvalidStopOrderTypeValue;
91
92 fn try_from(value: u64) -> Result<Self, Self::Error> {
93 match value {
94 0 => Ok(Self::SLTP),
95 1 => Ok(Self::Standalone),
96 _ => Err(InvalidStopOrderTypeValue),
97 }
98 }
99}