Skip to main content

df_hyperliquid_core/trade/builder/
fee.rs

1//! A builder's fee in tenths of a basis point, as an order's `f` and `maxBuilderFee` count it (10
2//! is 1 basis point). `approveBuilderFee` names the same figure as a percent.
3
4use crate::utils::dec::{Dec, Round};
5use crate::utils::sign::Map;
6use crate::utils::{Address, AssetKind};
7use serde::Deserialize;
8use std::fmt;
9
10/// Tenths of a basis point: 10 is 0.01%, 1000 is 1%.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Deserialize)]
12#[serde(transparent)]
13pub struct BuilderFee(u64);
14
15/// The most a builder may take on a perp order, main dex or HIP-3: 0.1%.
16pub const PERP_BUILDER_CAP: BuilderFee = BuilderFee(100);
17
18/// The most a builder may take on a spot order: 1%.
19pub const SPOT_BUILDER_CAP: BuilderFee = BuilderFee(1_000);
20
21/// HIP-4 takes builder codes as spot does.
22pub const OUTCOME_BUILDER_CAP: BuilderFee = SPOT_BUILDER_CAP;
23
24impl BuilderFee {
25    /// No fee.
26    pub const ZERO: BuilderFee = BuilderFee(0);
27
28    /// A fee of `tenths` tenths of a basis point.
29    #[must_use]
30    pub const fn from_tenths(tenths: u64) -> BuilderFee {
31        BuilderFee(tenths)
32    }
33
34    /// The fee in tenths of a basis point, as an order's `f` writes it.
35    #[must_use]
36    pub const fn tenths(self) -> u64 {
37        self.0
38    }
39
40    /// The percent string `approveBuilderFee` takes as `maxFeeRate`: 880 is `0.88%`, 100 is `0.1%`,
41    /// 1000 is `1%`. It always matches `^[0-9]+(\.[0-9]+)?%$`.
42    #[must_use]
43    pub fn percent(self) -> String {
44        let figure = Dec::lit::<3>(i128::from(self.0)).to_wire();
45        format!("{figure}%")
46    }
47
48    /// A percent string back in tenths. `None` for anything that is not a
49    /// whole number of tenths of a basis point.
50    #[must_use]
51    pub fn from_percent(text: &str) -> Option<BuilderFee> {
52        let digits = text.trim().strip_suffix('%')?;
53        if !digits.bytes().all(|byte| byte.is_ascii_digit() || byte == b'.') {
54            return None;
55        }
56        let figure = Dec::parse(digits).ok()?;
57        if figure.places() > 3 {
58            return None;
59        }
60        u64::try_from(figure.units_at(3, Round::Down)?).ok().map(BuilderFee)
61    }
62}
63
64impl fmt::Display for BuilderFee {
65    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66        f.write_str(&self.percent())
67    }
68}
69
70/// Who routed the order and what it takes: `{b, f}`, the address lowercase,
71/// the fee in tenths of a basis point.
72#[derive(Debug, Clone, Copy, PartialEq, Eq)]
73pub struct Builder {
74    address: Address,
75    fee: BuilderFee,
76}
77
78impl Builder {
79    /// The account must have approved at least `fee` for this builder
80    /// (`approveBuilderFee`), or the venue refuses the order.
81    #[must_use]
82    pub const fn new(address: Address, fee: BuilderFee) -> Builder {
83        Builder { address, fee }
84    }
85
86    /// `b`: the builder's address.
87    #[must_use]
88    pub const fn address(&self) -> Address {
89        self.address
90    }
91
92    /// `f`: what the builder takes, in tenths of a basis point.
93    #[must_use]
94    pub const fn fee(&self) -> BuilderFee {
95        self.fee
96    }
97
98    /// `{b, f}`, as an `order` action carries it.
99    #[must_use]
100    pub fn wire(&self) -> Map {
101        Map::new().put("b", self.address.to_hex()).put("f", self.fee.tenths())
102    }
103}
104
105/// A builder fee over what the venue lets a builder take on these orders.
106#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
107#[error("the builder fee of {} is over the {} {} order allows ({})", .fee.tenths(), .cap.tenths(), .kind.one(), .cap)]
108#[non_exhaustive]
109pub struct OverCap {
110    /// The fee asked for.
111    pub fee: BuilderFee,
112    /// The most the market kind allows.
113    pub cap: BuilderFee,
114    /// The market kind whose cap applies: the strictest among the orders.
115    pub kind: AssetKind,
116}
117
118#[cfg(test)]
119#[path = "tests/fee.rs"]
120mod tests;