df_hyperliquid_core/earn/vaults/
liquidator.rs1use crate::account::{ActionKind, Signers};
7use crate::utils::sign::{L1Action, Map};
8use crate::utils::{BadUnits, Dec, Round, exact_units};
9
10pub const LIQUIDATOR_STEP: Dec = Dec::from_int(1_000);
12
13pub const LIQUIDATOR_DECIMALS: u32 = 6;
15
16#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
18#[non_exhaustive]
19pub enum BadLiquidatorTransfer {
20 #[error("the main dex has no backstop liquidator; name a HIP-3 dex")]
22 NoDex,
23 #[error("{0} is not a whole multiple of {LIQUIDATOR_STEP} of the quote token")]
25 NotAStep(Dec),
26 #[error(transparent)]
28 Units(#[from] BadUnits),
29}
30
31#[derive(Debug, Clone, PartialEq, Eq)]
33pub struct Hip3LiquidatorTransfer {
34 dex: String,
35 ntl: u64,
36 is_deposit: bool,
37}
38
39impl Hip3LiquidatorTransfer {
40 pub const KEY: Signers = ActionKind::Hip3LiquidatorTransfer.signers();
42
43 pub fn deposit(dex: &str, amount: Dec) -> Result<Hip3LiquidatorTransfer, BadLiquidatorTransfer> {
45 Hip3LiquidatorTransfer::new(dex, amount, true)
46 }
47
48 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 #[must_use]
67 pub fn dex(&self) -> &str {
68 &self.dex
69 }
70
71 #[must_use]
73 pub const fn ntl(&self) -> u64 {
74 self.ntl
75 }
76
77 #[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;