Skip to main content

df_hyperliquid_core/trade/outcomes/
split.rs

1//! `userOutcome`: an account turning quote tokens into one of each side of
2//! an outcome, and back; merging a Yes of every outcome of a question back into quote tokens; and
3//! turning an outcome's No into a Yes of every other outcome of its question. Each may act for a
4//! subaccount through the body's `vaultAddress`, which the hash covers.
5
6use crate::markets::outcomes::error::OutcomeError;
7use crate::utils::Dec;
8use crate::utils::sign::{L1Action, Map, Value};
9
10/// The most places a quote amount may have.
11pub const MAX_AMOUNT_PLACES: u32 = 8;
12
13fn amount(value: Dec) -> Result<Dec, OutcomeError> {
14    match value.is_positive() && value.places() <= MAX_AMOUNT_PLACES {
15        true => Ok(value),
16        false => Err(OutcomeError::Amount(value)),
17    }
18}
19
20/// `{"type":"userOutcome","splitOutcome":{"outcome","amount"}}`: `amount` of
21/// the quote token into that many of each side. The amount goes as a decimal
22/// with no trailing zeros, so `12.50` and `12.5` are one action.
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub struct SplitOutcome {
25    outcome: u32,
26    amount: Dec,
27}
28
29impl SplitOutcome {
30    /// `amount_of_quote` of the quote token on `outcome`; refused when not positive or finer than
31    /// [`MAX_AMOUNT_PLACES`] places.
32    pub fn new(outcome: u32, amount_of_quote: Dec) -> Result<SplitOutcome, OutcomeError> {
33        Ok(SplitOutcome { outcome, amount: amount(amount_of_quote)? })
34    }
35}
36
37impl L1Action for SplitOutcome {
38    fn action(&self) -> Map {
39        Map::new()
40            .put("type", "userOutcome")
41            .put("splitOutcome", Map::new().put("outcome", self.outcome).put("amount", self.amount.to_wire()))
42    }
43}
44
45/// [`SplitOutcome`] undone: as many of each side back into the quote token.
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub struct MergeOutcome {
48    outcome: u32,
49    amount: Option<Dec>,
50}
51
52impl MergeOutcome {
53    /// `amount_of_quote` of each side of `outcome`; refused when not positive or finer than
54    /// [`MAX_AMOUNT_PLACES`] places.
55    pub fn new(outcome: u32, amount_of_quote: Dec) -> Result<MergeOutcome, OutcomeError> {
56        Ok(MergeOutcome { outcome, amount: Some(amount(amount_of_quote)?) })
57    }
58
59    /// Every pair the account holds: the amount goes as nil, not a figure.
60    #[must_use]
61    pub const fn all(outcome: u32) -> MergeOutcome {
62        MergeOutcome { outcome, amount: None }
63    }
64}
65
66impl L1Action for MergeOutcome {
67    fn action(&self) -> Map {
68        let amount = match self.amount {
69            Some(amount) => Value::from(amount.to_wire()),
70            None => Value::Nil,
71        };
72        Map::new()
73            .put("type", "userOutcome")
74            .put("mergeOutcome", Map::new().put("outcome", self.outcome).put("amount", amount))
75    }
76}
77
78/// `{"type":"userOutcome","mergeQuestion":{"question","amount"}}`: `amount` Yes shares of each
79/// outcome of the question back into that many quote tokens.
80#[derive(Debug, Clone, Copy, PartialEq, Eq)]
81pub struct MergeQuestion {
82    question: u32,
83    amount: Option<Dec>,
84}
85
86impl MergeQuestion {
87    /// `amount` of each outcome's Yes, at most eight places.
88    pub fn new(question: u32, amount: Dec) -> Result<MergeQuestion, OutcomeError> {
89        Ok(MergeQuestion { question, amount: Some(self::amount(amount)?) })
90    }
91
92    /// Every full set the account holds: the amount goes as nil, not a figure.
93    #[must_use]
94    pub const fn all(question: u32) -> MergeQuestion {
95        MergeQuestion { question, amount: None }
96    }
97}
98
99impl L1Action for MergeQuestion {
100    fn action(&self) -> Map {
101        let amount = match self.amount {
102            Some(amount) => Value::from(amount.to_wire()),
103            None => Value::Nil,
104        };
105        Map::new()
106            .put("type", "userOutcome")
107            .put("mergeQuestion", Map::new().put("question", self.question).put("amount", amount))
108    }
109}
110
111/// `{"type":"userOutcome","negateOutcome":{"question","outcome","amount"}}`: `amount` No shares of
112/// `outcome` into as many Yes shares of every other outcome of `question`.
113#[derive(Debug, Clone, Copy, PartialEq, Eq)]
114pub struct NegateOutcome {
115    question: u32,
116    outcome: u32,
117    amount: Dec,
118}
119
120impl NegateOutcome {
121    /// `amount` of the outcome's No, at most eight places.
122    pub fn new(question: u32, outcome: u32, amount: Dec) -> Result<NegateOutcome, OutcomeError> {
123        Ok(NegateOutcome { question, outcome, amount: self::amount(amount)? })
124    }
125}
126
127impl L1Action for NegateOutcome {
128    fn action(&self) -> Map {
129        Map::new().put("type", "userOutcome").put(
130            "negateOutcome",
131            Map::new().put("question", self.question).put("outcome", self.outcome).put("amount", self.amount.to_wire()),
132        )
133    }
134}
135
136#[cfg(test)]
137#[path = "tests/split.rs"]
138mod tests;