Skip to main content

df_hyperliquid_core/deploy/outcomes/actions/
bounded.rs

1//! The two decimals an outcome action carries, each in its range and sent in normal form: a
2//! deployer's [`FeeScale`] and a settlement's [`Fraction`].
3
4use std::{fmt, str::FromStr};
5
6use crate::deploy::outcomes::actions::error::DeployError;
7use crate::utils::Dec;
8
9/// `text` as a decimal from 0 to `most`, written with no sign and no
10/// exponent. It goes on the wire in normal form: `1.0` as `1`.
11fn bounded(text: &str, most: Dec) -> Option<Dec> {
12    let text = text.trim();
13    if text.starts_with(['+', '-']) {
14        return None;
15    }
16    let value = Dec::parse(text).ok()?;
17    (!value.is_negative() && value <= most).then_some(value)
18}
19
20/// What a deployer takes of the fees on its outcomes, from 0 to 10.
21/// `outcomeMeta` reads a scale back as `1.0`; it is sent as `1`. Only a
22/// standalone outcome and a question carry one: a named outcome added later
23/// takes its question's.
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub struct FeeScale(Dec);
26
27impl FeeScale {
28    /// A scale of 0: the deployer takes no fees.
29    pub const ZERO: FeeScale = FeeScale(Dec::ZERO);
30    /// A scale of 1.
31    pub const ONE: FeeScale = FeeScale(Dec::ONE);
32
33    /// The scale as a decimal.
34    #[must_use]
35    pub const fn value(self) -> Dec {
36        self.0
37    }
38
39    /// The string hashed: no trailing zeros.
40    #[must_use]
41    pub fn to_wire(self) -> String {
42        self.0.to_wire()
43    }
44}
45
46impl FromStr for FeeScale {
47    type Err = DeployError;
48
49    /// Takes `1.0`, `01` or `.50`, and keeps what the venue hashes: `1`, `1`,
50    /// `0.5`.
51    fn from_str(text: &str) -> Result<Self, Self::Err> {
52        bounded(text, Dec::from_int(10)).map(FeeScale).ok_or_else(|| DeployError::FeeScale(text.trim().to_owned()))
53    }
54}
55
56impl fmt::Display for FeeScale {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        write!(f, "{}", self.0)
59    }
60}
61
62/// The share of an outcome's pool its first side is paid when it settles,
63/// from 0 to 1: `1` pays the first side in full and `0` the second. A
64/// standalone outcome settles to any fraction; a question's only to 0 or 1.
65#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66pub struct Fraction(Dec);
67
68impl Fraction {
69    /// A fraction of 0: the second side is paid in full.
70    pub const ZERO: Fraction = Fraction(Dec::ZERO);
71    /// A fraction of 1: the first side is paid in full.
72    pub const ONE: Fraction = Fraction(Dec::ONE);
73
74    /// The fraction as a decimal.
75    #[must_use]
76    pub const fn value(self) -> Dec {
77        self.0
78    }
79
80    /// The string hashed: no trailing zeros, `1.0` as `1`.
81    #[must_use]
82    pub fn to_wire(self) -> String {
83        self.0.to_wire()
84    }
85
86    /// Exactly 0 or exactly 1, the only fractions a question's outcome takes.
87    #[must_use]
88    pub fn is_whole(self) -> bool {
89        self == Fraction::ZERO || self == Fraction::ONE
90    }
91}
92
93impl FromStr for Fraction {
94    type Err = DeployError;
95
96    /// As [`FeeScale`]'s: `1.0` is kept as `1` and `.50` as `0.5`.
97    fn from_str(text: &str) -> Result<Self, Self::Err> {
98        bounded(text, Dec::ONE).map(Fraction).ok_or_else(|| DeployError::Fraction(text.trim().to_owned()))
99    }
100}
101
102impl fmt::Display for Fraction {
103    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104        write!(f, "{}", self.0)
105    }
106}
107
108#[cfg(test)]
109#[path = "tests/bounded.rs"]
110mod tests;