df_hyperliquid_core/deploy/spot/
mod.rs1pub mod evm;
6pub mod share;
7
8pub use evm::{EvmFinalizer, FinalizeEvmContract};
9pub use share::FeeShare;
10
11use crate::deploy::annotation::Annotation;
12use crate::deploy::error::BadDeploy;
13use crate::deploy::name::DexName;
14use crate::deploy::tokens::TokenSpec;
15use crate::utils::sign::{L1Action, Map, Value};
16use crate::utils::{Address, Dec};
17
18#[derive(Debug, Clone, PartialEq, Eq)]
25pub enum SpotDeploy {
26 RegisterToken2 {
29 spec: TokenSpec,
31 max_gas: u64,
33 full_name: Option<String>,
35 },
36 UserGenesis {
40 token: u32,
42 user_and_wei: Vec<(Address, u128)>,
44 existing_token_and_wei: Vec<(u32, u128)>,
46 blacklist_users: Vec<(Address, bool)>,
48 },
49 Genesis {
53 token: u32,
55 max_supply: u128,
57 no_hyperliquidity: bool,
59 },
60 RegisterSpot {
63 base: u32,
65 quote: u32,
67 },
68 RegisterHyperliquidity {
73 spot: u32,
75 start_px: Dec,
77 order_sz: Dec,
79 n_orders: u32,
81 n_seeded_levels: Option<u32>,
83 },
84 SetDeployerTradingFeeShare {
87 token: u32,
89 share: FeeShare,
91 },
92 EnableQuoteToken(u32),
94 EnableAlignedQuoteToken(u32),
96 DisableQuoteToken(u32),
98 DisableAlignedQuoteToken(u32),
100 SetTokenAnnotation {
103 token: u32,
105 annotation: Annotation,
107 },
108 SetDeployerLabel(DexName),
111 EnableFreezePrivilege(u32),
114 FreezeUser {
117 token: u32,
119 user: Address,
121 freeze: bool,
123 },
124 RevokeFreezePrivilege(u32),
127 RequestEvmContract {
133 token: u32,
135 address: Address,
137 evm_extra_wei_decimals: i8,
139 },
140}
141
142fn pair(key: impl Into<Value>, value: impl Into<Value>) -> Value {
144 Value::Array(vec![key.into(), value.into()])
145}
146
147impl SpotDeploy {
148 pub fn register_hyperliquidity(
150 spot: u32,
151 start_px: Dec,
152 order_sz: Dec,
153 n_orders: u32,
154 n_seeded_levels: Option<u32>,
155 ) -> Result<Self, BadDeploy> {
156 for (what, value) in [("startPx", start_px), ("orderSz", order_sz)] {
157 if value.is_negative() {
158 return Err(BadDeploy::OutOfRange { what, least: "0", most: "any", value: value.to_wire() });
159 }
160 }
161 Ok(SpotDeploy::RegisterHyperliquidity { spot, start_px, order_sz, n_orders, n_seeded_levels })
162 }
163
164 pub fn request_evm_contract(token: u32, address: Address, evm_extra_wei_decimals: i8) -> Result<Self, BadDeploy> {
166 if !(-2..=18).contains(&evm_extra_wei_decimals) {
167 return Err(BadDeploy::EvmExtraWeiDecimals(evm_extra_wei_decimals));
168 }
169 Ok(SpotDeploy::RequestEvmContract { token, address, evm_extra_wei_decimals })
170 }
171
172 #[must_use]
174 pub const fn wire_name(&self) -> &'static str {
175 match self {
176 SpotDeploy::RegisterToken2 { .. } => "registerToken2",
177 SpotDeploy::UserGenesis { .. } => "userGenesis",
178 SpotDeploy::Genesis { .. } => "genesis",
179 SpotDeploy::RegisterSpot { .. } => "registerSpot",
180 SpotDeploy::RegisterHyperliquidity { .. } => "registerHyperliquidity",
181 SpotDeploy::SetDeployerTradingFeeShare { .. } => "setDeployerTradingFeeShare",
182 SpotDeploy::EnableQuoteToken(_) => "enableQuoteToken",
183 SpotDeploy::EnableAlignedQuoteToken(_) => "enableAlignedQuoteToken",
184 SpotDeploy::DisableQuoteToken(_) => "disableQuoteToken",
185 SpotDeploy::DisableAlignedQuoteToken(_) => "disableAlignedQuoteToken",
186 SpotDeploy::SetTokenAnnotation { .. } => "setTokenAnnotation",
187 SpotDeploy::SetDeployerLabel(_) => "setDeployerLabel",
188 SpotDeploy::EnableFreezePrivilege(_) => "enableFreezePrivilege",
189 SpotDeploy::FreezeUser { .. } => "freezeUser",
190 SpotDeploy::RevokeFreezePrivilege(_) => "revokeFreezePrivilege",
191 SpotDeploy::RequestEvmContract { .. } => "requestEvmContract",
192 }
193 }
194
195 #[must_use]
198 pub fn body(&self) -> Map {
199 let token = |token: u32| Map::new().put("token", token);
200 match self {
201 SpotDeploy::RegisterToken2 { spec, max_gas, full_name } => {
202 let spec = Map::new()
203 .put("name", spec.name.clone())
204 .put("szDecimals", spec.sz_decimals)
205 .put("weiDecimals", spec.wei_decimals);
206 Map::new().put("spec", spec).put("maxGas", *max_gas).put_some("fullName", full_name.clone())
207 }
208 SpotDeploy::UserGenesis { token, user_and_wei, existing_token_and_wei, blacklist_users } => {
209 let users = user_and_wei.iter().map(|(user, wei)| pair(user.to_hex(), wei.to_string()));
210 let existing = existing_token_and_wei.iter().map(|(token, wei)| pair(*token, wei.to_string()));
211 let blacklist = (!blacklist_users.is_empty())
212 .then(|| Value::array(blacklist_users.iter().map(|(user, listed)| pair(user.to_hex(), *listed))));
213 Map::new()
214 .put("token", *token)
215 .put("userAndWei", Value::array(users))
216 .put("existingTokenAndWei", Value::array(existing))
217 .put_some("blacklistUsers", blacklist)
218 }
219 SpotDeploy::Genesis { token, max_supply, no_hyperliquidity } => Map::new()
220 .put("token", *token)
221 .put("maxSupply", max_supply.to_string())
222 .put_some("noHyperliquidity", no_hyperliquidity.then_some(true)),
223 SpotDeploy::RegisterSpot { base, quote } => Map::new().put("tokens", Value::array([*base, *quote])),
224 SpotDeploy::RegisterHyperliquidity { spot, start_px, order_sz, n_orders, n_seeded_levels } => Map::new()
225 .put("spot", *spot)
226 .put("startPx", start_px.to_wire())
227 .put("orderSz", order_sz.to_wire())
228 .put("nOrders", *n_orders)
229 .put_some("nSeededLevels", *n_seeded_levels),
230 SpotDeploy::SetDeployerTradingFeeShare { token, share } => {
231 Map::new().put("token", *token).put("share", share.to_wire())
232 }
233 SpotDeploy::EnableQuoteToken(id)
234 | SpotDeploy::EnableAlignedQuoteToken(id)
235 | SpotDeploy::DisableQuoteToken(id)
236 | SpotDeploy::DisableAlignedQuoteToken(id)
237 | SpotDeploy::EnableFreezePrivilege(id)
238 | SpotDeploy::RevokeFreezePrivilege(id) => token(*id),
239 SpotDeploy::SetTokenAnnotation { token, annotation } => {
240 Map::new().put("token", *token).put("annotation", annotation.wire_after(Map::new()))
241 }
242 SpotDeploy::SetDeployerLabel(label) => Map::new().put("label", label.as_str().to_owned()),
243 SpotDeploy::FreezeUser { token, user, freeze } => {
244 Map::new().put("token", *token).put("user", user.to_hex()).put("freeze", *freeze)
245 }
246 SpotDeploy::RequestEvmContract { token, address, evm_extra_wei_decimals } => Map::new()
247 .put("token", *token)
248 .put("address", address.to_hex())
249 .put("evmExtraWeiDecimals", i64::from(*evm_extra_wei_decimals)),
250 }
251 }
252}
253
254impl L1Action for SpotDeploy {
255 fn action(&self) -> Map {
256 Map::new().put("type", "spotDeploy").put(self.wire_name(), self.body())
257 }
258}
259
260#[cfg(test)]
261#[path = "tests/spot.rs"]
262mod tests;