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)]
18pub enum BadLiquidatorTransfer {
19 #[error("the main dex has no backstop liquidator; name a HIP-3 dex")]
21 NoDex,
22 #[error("{0} is not a whole multiple of {LIQUIDATOR_STEP} of the quote token")]
24 NotAStep(Dec),
25 #[error(transparent)]
27 Units(#[from] BadUnits),
28}
29
30#[derive(Debug, Clone, PartialEq, Eq)]
32pub struct Hip3LiquidatorTransfer {
33 dex: String,
34 ntl: u64,
35 is_deposit: bool,
36}
37
38impl Hip3LiquidatorTransfer {
39 pub const KEY: Signers = ActionKind::Hip3LiquidatorTransfer.signers();
41
42 pub fn deposit(dex: &str, amount: Dec) -> Result<Hip3LiquidatorTransfer, BadLiquidatorTransfer> {
44 Hip3LiquidatorTransfer::new(dex, amount, true)
45 }
46
47 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 #[must_use]
66 pub fn dex(&self) -> &str {
67 &self.dex
68 }
69
70 #[must_use]
72 pub const fn ntl(&self) -> u64 {
73 self.ntl
74 }
75
76 #[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;