use crate::account::{ActionKind, Signers};
use crate::client::{ApiError, Transport, info, user_request};
use crate::utils::sign::{L1Action, Map};
use crate::utils::{Address, Dec, Round};
use serde::Deserialize;
pub const INITIAL_REQUESTS: u64 = 10_000;
pub const THROTTLED_EVERY_MS: u64 = 10_000;
pub const OPEN_ORDERS: u64 = 1_000;
pub const MOST_OPEN_ORDERS: u64 = 5_000;
pub const VOLUME_PER_OPEN_ORDER: i64 = 5_000_000;
pub const RESERVED_REQUEST_COST: Dec = Dec::lit::<4>(5);
pub const MOST_RESERVED_WEIGHT: u64 = 1_844_674_407_370_955;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("a reservation is 1 to {MOST_RESERVED_WEIGHT} requests, not {0}")]
pub struct BadWeight(pub u64);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ReserveRequestWeight {
weight: u64,
destination: Option<Address>,
}
impl ReserveRequestWeight {
pub const KEY: Signers = ActionKind::ReserveRequestWeight.signers();
pub fn new(weight: u64) -> Result<ReserveRequestWeight, BadWeight> {
match (1..=MOST_RESERVED_WEIGHT).contains(&weight) {
true => Ok(ReserveRequestWeight { weight, destination: None }),
false => Err(BadWeight(weight)),
}
}
#[must_use]
pub const fn for_user(mut self, destination: Address) -> ReserveRequestWeight {
self.destination = Some(destination);
self
}
#[must_use]
pub const fn weight(&self) -> u64 {
self.weight
}
#[must_use]
pub const fn destination(&self) -> Option<Address> {
self.destination
}
#[must_use]
pub fn cost(&self) -> Option<Dec> {
Dec::new(i128::from(self.weight), 0)?.checked_mul(RESERVED_REQUEST_COST)
}
}
impl L1Action for ReserveRequestWeight {
fn action(&self) -> Map {
Map::new()
.put("type", "reserveRequestWeight")
.put("weight", self.weight)
.put_some("destination", self.destination.map(|destination| destination.to_hex()))
}
}
#[must_use]
pub fn rate_limit_request(user: Address) -> String {
user_request("userRateLimit", user)
}
pub async fn rate_limit(transport: &impl Transport, user: Address) -> Result<RateLimit, ApiError> {
info(transport, rate_limit_request(user)).await
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
#[non_exhaustive]
pub struct RateLimit {
#[serde(rename = "cumVlm")]
pub volume: Dec,
#[serde(rename = "nRequestsUsed")]
pub used: u64,
#[serde(rename = "nRequestsCap")]
pub cap: u64,
#[serde(rename = "nRequestsSurplus", default)]
pub surplus: u64,
}
impl RateLimit {
#[must_use]
pub const fn remaining(&self) -> u64 {
self.cap.saturating_sub(self.used)
}
#[must_use]
pub const fn is_throttled(&self) -> bool {
self.remaining() == 0
}
#[must_use]
pub const fn cancel_cap(&self) -> u64 {
let wider = self.cap.saturating_add(100_000);
let doubled = self.cap.saturating_mul(2);
if wider < doubled { wider } else { doubled }
}
#[must_use]
pub fn earned_cap(volume: Dec) -> Option<u64> {
let whole = u64::try_from(volume.units_at(0, Round::Down)?).ok()?;
INITIAL_REQUESTS.checked_add(whole)
}
}
#[must_use]
pub fn volume_for(requests: u64) -> Dec {
Dec::lit::<0>(i128::from(requests))
}
#[must_use]
pub fn surplus_cost(requests: u64) -> Option<Dec> {
Dec::lit::<0>(i128::from(requests)).checked_mul(RESERVED_REQUEST_COST)
}
#[must_use]
pub fn address_requests(batched: usize) -> u64 {
u64::try_from(batched.max(1)).unwrap_or(u64::MAX)
}
#[must_use]
pub fn open_order_cap(volume: Dec) -> u64 {
let earned = volume
.checked_div(Dec::from_int(VOLUME_PER_OPEN_ORDER), 0, Round::Down)
.and_then(|earned| earned.units_at(0, Round::Down))
.and_then(|earned| u64::try_from(earned).ok())
.unwrap_or(0);
OPEN_ORDERS.saturating_add(earned).min(MOST_OPEN_ORDERS)
}
#[cfg(test)]
#[path = "tests/limits.rs"]
mod tests;