Skip to main content

df_hyperliquid_core/deploy/spot/
share.rs

1//! The deployer trading fee share: the part of a token's trading fees its deployer keeps, sent as a
2//! percentage string.
3
4use std::{fmt, str::FromStr};
5
6use crate::deploy::error::BadDeploy;
7use crate::utils::Dec;
8
9/// What a token's deployer keeps of its trading fees, from `0%` to `100%`.
10/// It starts at 100% and can be sent again only to lower it. Sent as the
11/// percentage with no trailing zeros: `99.40%` as `99.4%`.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
13pub struct FeeShare(Dec);
14
15impl FeeShare {
16    /// 100%, the share a token starts with.
17    pub const ALL: FeeShare = FeeShare(Dec::lit::<0>(100));
18
19    /// `percent` per cent, from 0 to 100.
20    pub fn percent(percent: Dec) -> Result<Self, BadDeploy> {
21        if percent.is_negative() || percent > Dec::from_int(100) {
22            return Err(BadDeploy::FeeShare(format!("{percent}%")));
23        }
24        Ok(FeeShare(percent))
25    }
26
27    /// The share in per cent.
28    #[must_use]
29    pub const fn value(self) -> Dec {
30        self.0
31    }
32
33    /// The string hashed, such as `0.012%`.
34    #[must_use]
35    pub fn to_wire(self) -> String {
36        format!("{}%", self.0.to_wire())
37    }
38}
39
40impl FromStr for FeeShare {
41    type Err = BadDeploy;
42
43    /// Takes `99.4%` or `100%`; the sign is required, as the venue writes it.
44    fn from_str(text: &str) -> Result<Self, Self::Err> {
45        let bad = || BadDeploy::FeeShare(text.trim().to_owned());
46        let number = text.trim().strip_suffix('%').ok_or_else(bad)?;
47        if number.starts_with(['+', '-']) || number.trim() != number {
48            return Err(bad());
49        }
50        let percent = Dec::parse(number).map_err(|_| bad())?;
51        FeeShare::percent(percent).map_err(|_| bad())
52    }
53}
54
55impl fmt::Display for FeeShare {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        write!(f, "{}%", self.0)
58    }
59}