Skip to main content

df_hyperliquid_core/deploy/
annotation.rs

1//! What frontends show for a spot token or a HIP-3 perp: the text `setTokenAnnotation` and
2//! `setPerpAnnotation` carry, checked against the lengths the docs give. The venue counts those
3//! lengths in UTF-8 bytes, not characters: testnet refused a Chinese display name of 6
4//! characters and 18 bytes, and took one of 2 characters and 6 bytes.
5
6use crate::deploy::error::BadDeploy;
7use crate::utils::sign::{Map, Value};
8
9/// The longest category, in bytes.
10pub const MAX_CATEGORY_LEN: usize = 15;
11/// The longest description, in bytes.
12pub const MAX_DESCRIPTION_LEN: usize = 400;
13/// The longest display name, in bytes.
14pub const MAX_DISPLAY_NAME_LEN: usize = 9;
15/// The most keywords an annotation takes.
16pub const MAX_KEYWORDS: usize = 2;
17/// The longest keyword, in bytes.
18pub const MAX_KEYWORD_LEN: usize = 10;
19
20/// A token's or perp's annotation: a category, a description, a display name
21/// frontends show instead of the listed name (`None` to keep the listed
22/// name) and up to two keywords searches match against.
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct Annotation {
25    category: String,
26    description: String,
27    display_name: Option<String>,
28    keywords: Vec<String>,
29}
30
31/// `text` if it is at most `most` bytes.
32fn short(what: &'static str, text: String, most: usize) -> Result<String, BadDeploy> {
33    if text.len() > most {
34        return Err(BadDeploy::TooLong { what, most, value: text });
35    }
36    Ok(text)
37}
38
39impl Annotation {
40    /// Refuses a category over 15 bytes, a description over 400, a display
41    /// name over 9, more than 2 keywords or a keyword over 10.
42    pub fn new(
43        category: impl Into<String>,
44        description: impl Into<String>,
45        display_name: Option<String>,
46        keywords: Vec<String>,
47    ) -> Result<Self, BadDeploy> {
48        if keywords.len() > MAX_KEYWORDS {
49            return Err(BadDeploy::Keywords(keywords.len()));
50        }
51        Ok(Annotation {
52            category: short("a category", category.into(), MAX_CATEGORY_LEN)?,
53            description: short("a description", description.into(), MAX_DESCRIPTION_LEN)?,
54            display_name: display_name.map(|name| short("a display name", name, MAX_DISPLAY_NAME_LEN)).transpose()?,
55            keywords: keywords
56                .into_iter()
57                .map(|keyword| short("a keyword", keyword, MAX_KEYWORD_LEN))
58                .collect::<Result<_, _>>()?,
59        })
60    }
61
62    /// The category, at most 15 bytes.
63    #[must_use]
64    pub fn category(&self) -> &str {
65        &self.category
66    }
67
68    /// The description, at most 400 bytes.
69    #[must_use]
70    pub fn description(&self) -> &str {
71        &self.description
72    }
73
74    /// The name frontends show instead of the listed name; `None` keeps the listed name.
75    #[must_use]
76    pub fn display_name(&self) -> Option<&str> {
77        self.display_name.as_deref()
78    }
79
80    /// Up to two words searches match against, each at most 10 bytes.
81    #[must_use]
82    pub fn keywords(&self) -> &[String] {
83        &self.keywords
84    }
85
86    /// `{"category","description","displayName","keywords"}` after the keys
87    /// already in `map`; a missing display name is sent as null.
88    #[must_use]
89    pub fn wire_after(&self, map: Map) -> Map {
90        let display_name = self.display_name.clone().map_or(Value::Nil, Value::from);
91        map.put("category", self.category.clone())
92            .put("description", self.description.clone())
93            .put("displayName", display_name)
94            .put("keywords", Value::array(self.keywords.iter().cloned()))
95    }
96}
97
98#[cfg(test)]
99#[path = "tests/annotation.rs"]
100mod tests;