Skip to main content

acta_maker_sdk/types/
ids.rs

1use std::fmt::Display;
2
3use derive_more::{Display as DeriveDisplay, From, Into};
4use serde::{Deserialize, Serialize};
5
6define_bytes32_newtype!(OrderId);
7
8define_numeric_newtype!(Strike, u64);
9define_numeric_newtype!(Price, u64);
10define_numeric_newtype!(Quantity, u64);
11
12impl Price {
13    /// Apply a protocol fee in basis points using the contract's rounding rule.
14    #[must_use]
15    pub fn after_fee_bps(self, bps: u16) -> Self {
16        let fee = u128::from(self.0) * u128::from(bps) / 10_000;
17        Self::new(
18            self.0
19                .saturating_sub(u64::try_from(fee).unwrap_or(u64::MAX)),
20        )
21    }
22}
23
24define_numeric_newtype!(Nonce, u64);
25define_numeric_newtype!(RfqVersion, u64);
26// Reserved wire field: producers emit 0, no domain source yet, no consumer gates on it.
27define_numeric_newtype!(OrderVersion, u64);
28define_numeric_newtype!(Slot, u64);
29define_numeric_newtype!(ChainId, u64);
30define_numeric_newtype!(DurationSeconds, u64);
31define_numeric_newtype!(Volume, u64);
32define_numeric_newtype!(Balance, u64);
33
34define_numeric_newtype!(QuoteCount, u32);
35define_numeric_newtype!(TradeCount, u32);
36define_numeric_newtype!(TimeoutSeconds, u32);
37
38#[derive(
39    Debug,
40    Default,
41    Clone,
42    Copy,
43    PartialEq,
44    Eq,
45    PartialOrd,
46    Ord,
47    Hash,
48    Serialize,
49    Deserialize,
50    From,
51    Into,
52)]
53pub struct Decimals(pub u8);
54
55impl Decimals {
56    #[must_use]
57    pub const fn new(value: u8) -> Self {
58        Self(value)
59    }
60
61    #[must_use]
62    pub const fn value(self) -> u8 {
63        self.0
64    }
65}
66
67impl Display for Decimals {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        write!(f, "{}", self.0)
70    }
71}
72
73define_string_newtype!(MarketId);
74define_string_newtype!(UserId);
75
76#[derive(
77    Debug,
78    Clone,
79    Copy,
80    PartialEq,
81    Eq,
82    Hash,
83    Serialize,
84    Deserialize,
85    strum::IntoStaticStr,
86    strum::EnumString,
87)]
88#[serde(rename_all = "snake_case")]
89#[strum(serialize_all = "snake_case")]
90#[repr(u8)]
91pub enum PositionType {
92    CoveredCall = 0,
93    CashSecuredPut = 1,
94}
95
96impl From<PositionType> for u8 {
97    fn from(position_type: PositionType) -> Self {
98        position_type as Self
99    }
100}
101
102#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
103#[error("invalid position type value: {0}")]
104pub struct PositionTypeParseError(pub u8);
105
106impl TryFrom<u8> for PositionType {
107    type Error = PositionTypeParseError;
108
109    fn try_from(value: u8) -> Result<Self, Self::Error> {
110        match value {
111            0 => Ok(Self::CoveredCall),
112            1 => Ok(Self::CashSecuredPut),
113            _ => Err(PositionTypeParseError(value)),
114        }
115    }
116}
117
118impl Display for PositionType {
119    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120        f.write_str(self.into())
121    }
122}