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