Skip to main content

df_hyperliquid_core/earn/vaults/
liquidator.rs

1//! `hip3LiquidatorTransfer`: `{type, dex, ntl, isDeposit}`, keys in that order, as
2//! `@nktkas/hyperliquid` 0.33.3 posts them. It moves a HIP-3 dex's quote token into or out of the
3//! dex's backstop liquidator. `ntl` is in millionths of the quote token, whatever its own decimals,
4//! and is a whole multiple of a thousand tokens. Only the principal comes back out, not the profit.
5
6use crate::account::{ActionKind, Signers};
7use crate::utils::sign::{L1Action, Map};
8use crate::utils::{BadUnits, Dec, Round, exact_units};
9
10/// What a deposit or withdrawal is a whole multiple of, in the quote token.
11pub const LIQUIDATOR_STEP: Dec = Dec::from_int(1_000);
12
13/// The places `ntl` counts the quote token in.
14pub const LIQUIDATOR_DECIMALS: u32 = 6;
15
16/// Why an amount is not one the backstop liquidator takes.
17#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
18#[non_exhaustive]
19pub enum BadLiquidatorTransfer {
20    /// An empty dex name, which would mean the main dex.
21    #[error("the main dex has no backstop liquidator; name a HIP-3 dex")]
22    NoDex,
23    /// An amount of zero or less, or not a whole multiple of [`LIQUIDATOR_STEP`]; holds the amount.
24    #[error("{0} is not a whole multiple of {LIQUIDATOR_STEP} of the quote token")]
25    NotAStep(Dec),
26    /// The amount has more than [`LIQUIDATOR_DECIMALS`] decimal places.
27    #[error(transparent)]
28    Units(#[from] BadUnits),
29}
30
31/// Quote token into or out of a HIP-3 dex's backstop liquidator.
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub struct Hip3LiquidatorTransfer {
34    dex: String,
35    ntl: u64,
36    is_deposit: bool,
37}
38
39impl Hip3LiquidatorTransfer {
40    /// L1: an agent may sign it for its master.
41    pub const KEY: Signers = ActionKind::Hip3LiquidatorTransfer.signers();
42
43    /// `amount` of the quote token into the liquidator of the HIP-3 dex `dex` (`xyz`).
44    pub fn deposit(dex: &str, amount: Dec) -> Result<Hip3LiquidatorTransfer, BadLiquidatorTransfer> {
45        Hip3LiquidatorTransfer::new(dex, amount, true)
46    }
47
48    /// `amount` of the quote token back out of the liquidator of the HIP-3 dex `dex`, up to the
49    /// principal put in.
50    pub fn withdraw(dex: &str, amount: Dec) -> Result<Hip3LiquidatorTransfer, BadLiquidatorTransfer> {
51        Hip3LiquidatorTransfer::new(dex, amount, false)
52    }
53
54    fn new(dex: &str, amount: Dec, is_deposit: bool) -> Result<Hip3LiquidatorTransfer, BadLiquidatorTransfer> {
55        if dex.is_empty() {
56            return Err(BadLiquidatorTransfer::NoDex);
57        }
58        let whole_steps = amount.checked_div(LIQUIDATOR_STEP, 0, Round::Down);
59        if !amount.is_positive() || whole_steps.and_then(|steps| steps.checked_mul(LIQUIDATOR_STEP)) != Some(amount) {
60            return Err(BadLiquidatorTransfer::NotAStep(amount));
61        }
62        Ok(Hip3LiquidatorTransfer { dex: dex.to_owned(), ntl: exact_units(amount, LIQUIDATOR_DECIMALS)?, is_deposit })
63    }
64
65    /// The dex whose liquidator it moves money for.
66    #[must_use]
67    pub fn dex(&self) -> &str {
68        &self.dex
69    }
70
71    /// The amount as the action carries it, in millionths of the quote token.
72    #[must_use]
73    pub const fn ntl(&self) -> u64 {
74        self.ntl
75    }
76
77    /// Whether the money goes in rather than out.
78    #[must_use]
79    pub const fn is_deposit(&self) -> bool {
80        self.is_deposit
81    }
82}
83
84impl L1Action for Hip3LiquidatorTransfer {
85    fn action(&self) -> Map {
86        Map::new()
87            .put("type", "hip3LiquidatorTransfer")
88            .put("dex", self.dex.clone())
89            .put("ntl", self.ntl)
90            .put("isDeposit", self.is_deposit)
91    }
92}
93
94#[cfg(test)]
95#[path = "tests/liquidator.rs"]
96mod tests;