df_hyperliquid_core/account/settings/
limits.rs1use crate::account::{ActionKind, Signers};
7use crate::client::{ApiError, Transport, info, user_request};
8use crate::utils::sign::{L1Action, Map};
9use crate::utils::{Address, Dec, Round};
10use serde::Deserialize;
11
12pub const INITIAL_REQUESTS: u64 = 10_000;
14
15pub const THROTTLED_EVERY_MS: u64 = 10_000;
17
18pub const OPEN_ORDERS: u64 = 1_000;
20pub const MOST_OPEN_ORDERS: u64 = 5_000;
22
23pub const VOLUME_PER_OPEN_ORDER: i64 = 5_000_000;
25
26pub const RESERVED_REQUEST_COST: Dec = Dec::lit::<4>(5);
28
29pub const MOST_RESERVED_WEIGHT: u64 = 1_844_674_407_370_955;
31
32#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
34#[error("a reservation is 1 to {MOST_RESERVED_WEIGHT} requests, not {0}")]
35pub struct BadWeight(pub u64);
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub struct ReserveRequestWeight {
42 weight: u64,
43 destination: Option<Address>,
44}
45
46impl ReserveRequestWeight {
47 pub const KEY: Signers = ActionKind::ReserveRequestWeight.signers();
49
50 pub fn new(weight: u64) -> Result<ReserveRequestWeight, BadWeight> {
52 match (1..=MOST_RESERVED_WEIGHT).contains(&weight) {
53 true => Ok(ReserveRequestWeight { weight, destination: None }),
54 false => Err(BadWeight(weight)),
55 }
56 }
57
58 #[must_use]
60 pub const fn for_user(mut self, destination: Address) -> ReserveRequestWeight {
61 self.destination = Some(destination);
62 self
63 }
64
65 #[must_use]
67 pub const fn weight(&self) -> u64 {
68 self.weight
69 }
70
71 #[must_use]
73 pub const fn destination(&self) -> Option<Address> {
74 self.destination
75 }
76
77 #[must_use]
79 pub fn cost(&self) -> Option<Dec> {
80 Dec::new(i128::from(self.weight), 0)?.checked_mul(RESERVED_REQUEST_COST)
81 }
82}
83
84impl L1Action for ReserveRequestWeight {
85 fn action(&self) -> Map {
86 Map::new()
87 .put("type", "reserveRequestWeight")
88 .put("weight", self.weight)
89 .put_some("destination", self.destination.map(|destination| destination.to_hex()))
90 }
91}
92
93#[must_use]
95pub fn rate_limit_request(user: Address) -> String {
96 user_request("userRateLimit", user)
97}
98
99pub async fn rate_limit(transport: &impl Transport, user: Address) -> Result<RateLimit, ApiError> {
101 info(transport, rate_limit_request(user)).await
102}
103
104#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
106pub struct RateLimit {
107 #[serde(rename = "cumVlm")]
109 pub volume: Dec,
110 #[serde(rename = "nRequestsUsed")]
112 pub used: u64,
113 #[serde(rename = "nRequestsCap")]
115 pub cap: u64,
116 #[serde(rename = "nRequestsSurplus", default)]
118 pub surplus: u64,
119}
120
121impl RateLimit {
122 #[must_use]
125 pub const fn remaining(&self) -> u64 {
126 self.cap.saturating_sub(self.used)
127 }
128
129 #[must_use]
132 pub const fn is_throttled(&self) -> bool {
133 self.remaining() == 0
134 }
135
136 #[must_use]
138 pub const fn cancel_cap(&self) -> u64 {
139 let wider = self.cap.saturating_add(100_000);
140 let doubled = self.cap.saturating_mul(2);
141 if wider < doubled { wider } else { doubled }
142 }
143
144 #[must_use]
147 pub fn earned_cap(volume: Dec) -> Option<u64> {
148 let whole = u64::try_from(volume.units_at(0, Round::Down)?).ok()?;
149 INITIAL_REQUESTS.checked_add(whole)
150 }
151}
152
153#[must_use]
155pub fn volume_for(requests: u64) -> Dec {
156 Dec::lit::<0>(i128::from(requests))
157}
158
159#[must_use]
162pub fn surplus_cost(requests: u64) -> Option<Dec> {
163 Dec::lit::<0>(i128::from(requests)).checked_mul(RESERVED_REQUEST_COST)
164}
165
166#[must_use]
169pub fn address_requests(batched: usize) -> u64 {
170 u64::try_from(batched.max(1)).unwrap_or(u64::MAX)
171}
172
173#[must_use]
175pub fn open_order_cap(volume: Dec) -> u64 {
176 let earned = volume
177 .checked_div(Dec::from_int(VOLUME_PER_OPEN_ORDER), 0, Round::Down)
178 .and_then(|earned| earned.units_at(0, Round::Down))
179 .and_then(|earned| u64::try_from(earned).ok())
180 .unwrap_or(0);
181 OPEN_ORDERS.saturating_add(earned).min(MOST_OPEN_ORDERS)
182}
183
184#[cfg(test)]
185#[path = "tests/limits.rs"]
186mod tests;