af_iperps/
slo.rs

1//! Helpers for stop-loss orders.
2
3use af_utilities::types::Fixed;
4use fastcrypto::hash::{Blake2b256, HashFunction};
5use sui_framework_sdk::object::ID;
6
7use crate::order_helpers::{OrderType, Side};
8
9/// The details to be hashed for the `encrypted_details` argument of `create_stop_order_ticket`.
10#[derive(Debug, serde::Serialize)]
11pub struct StopOrderTicketDetails {
12    pub clearing_house_id: ID,
13    /// The `Clock` value after (>=) which the order isn't valid anymore
14    pub expire_timestamp: u64,
15    /// `true` if limit order, `false` if market order
16    pub is_limit_order: bool,
17    pub stop_index_price: Fixed,
18    /// `true` means the order can be placed when oracle index price is >= than chosen
19    /// `stop_index_price`
20    pub ge_stop_index_price: bool,
21    pub side: Side,
22    pub size: u64,
23    /// Can be set at random value if `is_limit_order` is false
24    pub price: u64,
25    /// Can be set at random value if `is_limit_order` is false
26    pub order_type: OrderType,
27    pub reduce_only: bool,
28    pub margin_ratio: Option<Fixed>,
29}
30
31impl StopOrderTicketDetails {
32    /// Pure transaction input to use when calling `create_stop_order_ticket`.
33    pub fn encrypted_details(&self, salt: Vec<u8>) -> bcs::Result<Vec<u8>> {
34        let mut bytes = bcs::to_bytes(self)?;
35        bytes.extend(salt);
36        Ok(Blake2b256::digest(bytes).to_vec())
37    }
38}