df_hyperliquid_core/deploy/spot/mod.rs
1//! Deploying a HIP-1 spot token and its HIP-2 hyperliquidity: every `spotDeploy` variant, all L1
2//! actions, and `finalizeEvmContract`, which completes a token's link to a HyperEVM contract. The
3//! reads that go with them are in [`tokens`](crate::deploy::tokens).
4
5pub 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/// `{"type":"spotDeploy",<variant>:{..}}`. A token is deployed in five steps
19/// sent in this order: [`RegisterToken2`](SpotDeploy::RegisterToken2),
20/// [`UserGenesis`](SpotDeploy::UserGenesis) (as many times as needed),
21/// [`Genesis`](SpotDeploy::Genesis), [`RegisterSpot`](SpotDeploy::RegisterSpot)
22/// and [`RegisterHyperliquidity`](SpotDeploy::RegisterHyperliquidity). The
23/// rest are optional. Wei amounts are integers in the token's wei decimals.
24#[derive(Debug, Clone, PartialEq, Eq)]
25#[non_exhaustive]
26pub enum SpotDeploy {
27 /// `registerToken2`: claims a token index, paying the token auction up
28 /// to `max_gas`. `fullName` is left out when `None`.
29 ///
30 /// The gas paid is not refunded. `max_gas` is in HYPE wei, 8 decimals: 1 HYPE is
31 /// `100_000_000`, so `1_000_000_000_000` is 10,000 HYPE. The spec is fixed for good.
32 RegisterToken2 {
33 /// The name, size decimals and wei decimals to register.
34 spec: TokenSpec,
35 /// The most to pay for the token auction (`maxGas`), in HYPE wei: 8 decimals.
36 max_gas: u64,
37 /// The full name (`fullName`); `None` leaves it out.
38 full_name: Option<String>,
39 },
40 /// `userGenesis`: wei for addresses, wei shared among the holders of
41 /// existing tokens, and addresses to blacklist (`true`) or take off the
42 /// blacklist (`false`). `blacklistUsers` is left out when empty.
43 ///
44 /// Amounts are the token's wei, the integer of whole tokens times 10^`weiDecimals`: with
45 /// `weiDecimals` 8, `100_000_000` is one token. A later `userGenesis` for the same address
46 /// replaces its amount rather than adding to it. The address `0xff..ff` holds the
47 /// hyperliquidity allocation. Balances are fixed for good at [`Genesis`](SpotDeploy::Genesis).
48 /// Confirmed against mainnet deploys and `tokenDetails`.
49 UserGenesis {
50 /// The token index.
51 token: u32,
52 /// Wei given to each address (`userAndWei`).
53 user_and_wei: Vec<(Address, u128)>,
54 /// Wei shared among the holders of each existing token, by token index (`existingTokenAndWei`).
55 existing_token_and_wei: Vec<(u32, u128)>,
56 /// Addresses to blacklist (`true`) or take off the blacklist (`false`) (`blacklistUsers`).
57 blacklist_users: Vec<(Address, bool)>,
58 },
59 /// `genesis`: the maximum supply in wei, which must equal what the
60 /// user genesis steps gave out. `noHyperliquidity` is sent, as `true`,
61 /// only when set.
62 ///
63 /// Irreversible: the supply and every genesis balance are final. `maxSupply` is the token's
64 /// wei, whole tokens times 10^`weiDecimals`.
65 Genesis {
66 /// The token index.
67 token: u32,
68 /// The maximum supply in wei (`maxSupply`), sent as a string.
69 max_supply: u128,
70 /// `true` deploys without hyperliquidity (`noHyperliquidity`).
71 no_hyperliquidity: bool,
72 },
73 /// `registerSpot`: the pair of a base and a quote token. Between two
74 /// existing tokens it pays the spot pair auction.
75 RegisterSpot {
76 /// The base token index, sent first in `tokens`.
77 base: u32,
78 /// The quote token index, sent second in `tokens`.
79 quote: u32,
80 },
81 /// `registerHyperliquidity`: the pair index (not the token's), the
82 /// starting price, each order's size in tokens (not wei), the number of
83 /// orders (0 after a genesis with no hyperliquidity) and the number of
84 /// levels seeded with USDC. `nSeededLevels` is left out when `None`.
85 ///
86 /// Irreversible. `startPx` is the lowest level's price in the quote token per whole token;
87 /// each level above is 0.3% higher. `orderSz` is whole tokens per level, and `nOrders` times
88 /// `orderSz` must be the `0xff..ff` genesis balance. Confirmed against mainnet deploys and
89 /// their books.
90 RegisterHyperliquidity {
91 /// The spot pair index.
92 spot: u32,
93 /// The starting price (`startPx`).
94 start_px: Dec,
95 /// Each order's size in tokens, not wei (`orderSz`).
96 order_sz: Dec,
97 /// The number of orders (`nOrders`).
98 n_orders: u32,
99 /// The number of levels seeded with USDC (`nSeededLevels`); `None` leaves it out.
100 n_seeded_levels: Option<u32>,
101 },
102 /// `setDeployerTradingFeeShare`, at any time after `registerToken2`, and
103 /// again only to a strictly lower share: the venue refuses a raise and the same value.
104 /// Sent as a percentage string, `"5%"`; `spotMeta` shows it as a fraction, 0.05.
105 SetDeployerTradingFeeShare {
106 /// The token index.
107 token: u32,
108 /// The share the deployer keeps.
109 share: FeeShare,
110 },
111 /// `enableQuoteToken`: lets the token quote spot pairs.
112 EnableQuoteToken(u32),
113 /// `enableAlignedQuoteToken`: makes the token an aligned quote asset, as the docs write it.
114 ///
115 /// Hyperliquid's nodes do not take this action yet: mainnet and testnet refuse it with
116 /// `422 Failed to deserialize` for every shape, the answer for an action that does not
117 /// exist (checked 2026-11-24 by `just testnet`).
118 #[deprecated(since = "0.1.1", note = "Hyperliquid's nodes refuse this action; see the variant's docs")]
119 EnableAlignedQuoteToken(u32),
120 /// `disableQuoteToken`.
121 DisableQuoteToken(u32),
122 /// `disableAlignedQuoteToken`, sent after `disableQuoteToken`, as the docs write it.
123 ///
124 /// Hyperliquid's nodes do not take this action yet: mainnet and testnet refuse it with
125 /// `422 Failed to deserialize` for every shape (checked 2026-11-24 by `just testnet`).
126 #[deprecated(since = "0.1.1", note = "Hyperliquid's nodes refuse this action; see the variant's docs")]
127 DisableAlignedQuoteToken(u32),
128 /// `setTokenAnnotation`, at any time after `registerToken2`, at most
129 /// once a day.
130 SetTokenAnnotation {
131 /// The token index.
132 token: u32,
133 /// The annotation.
134 annotation: Annotation,
135 },
136 /// `setDeployerLabel`: the label shown for the deployer's tokens, set
137 /// once per deployer.
138 SetDeployerLabel(DexName),
139 /// `enableFreezePrivilege`: lets the deployer freeze holders of the
140 /// token. In Hyperliquid's Python SDK; not in the API docs.
141 EnableFreezePrivilege(u32),
142 /// `freezeUser`: freezes (`true`) or unfreezes (`false`) one holder. In
143 /// Hyperliquid's Python SDK; not in the API docs.
144 FreezeUser {
145 /// The token index.
146 token: u32,
147 /// The holder to freeze or unfreeze.
148 user: Address,
149 /// `true` freezes, `false` unfreezes.
150 freeze: bool,
151 },
152 /// `revokeFreezePrivilege`: gives the freeze privilege up. In
153 /// Hyperliquid's Python SDK; not in the API docs.
154 RevokeFreezePrivilege(u32),
155 /// `requestEvmContract`: asks to link the token to an ERC-20 on the
156 /// HyperEVM; the contract's deployer then sends
157 /// [`FinalizeEvmContract`]. `evm_extra_wei_decimals` is the EVM
158 /// contract's decimals less the token's wei decimals, from -2 to 18.
159 /// Linking cannot be undone.
160 RequestEvmContract {
161 /// The token index.
162 token: u32,
163 /// The ERC-20 contract's address on the HyperEVM.
164 address: Address,
165 /// The EVM contract's decimals less the token's wei decimals (`evmExtraWeiDecimals`), from -2 to 18.
166 evm_extra_wei_decimals: i8,
167 },
168}
169
170/// `[key, value]`, the tuple form the venue's lists take.
171fn pair(key: impl Into<Value>, value: impl Into<Value>) -> Value {
172 Value::Array(vec![key.into(), value.into()])
173}
174
175// The aligned quote token variants are deprecated, not gone: they still build and hash.
176#[allow(deprecated)]
177impl SpotDeploy {
178 /// `registerHyperliquidity`, refusing a negative price or size.
179 pub fn register_hyperliquidity(
180 spot: u32,
181 start_px: Dec,
182 order_sz: Dec,
183 n_orders: u32,
184 n_seeded_levels: Option<u32>,
185 ) -> Result<Self, BadDeploy> {
186 for (what, value) in [("startPx", start_px), ("orderSz", order_sz)] {
187 if value.is_negative() {
188 return Err(BadDeploy::OutOfRange { what, least: "0", most: "any", value: value.to_wire() });
189 }
190 }
191 Ok(SpotDeploy::RegisterHyperliquidity { spot, start_px, order_sz, n_orders, n_seeded_levels })
192 }
193
194 /// `requestEvmContract`, refusing extra wei decimals outside -2 to 18.
195 pub fn request_evm_contract(token: u32, address: Address, evm_extra_wei_decimals: i8) -> Result<Self, BadDeploy> {
196 if !(-2..=18).contains(&evm_extra_wei_decimals) {
197 return Err(BadDeploy::EvmExtraWeiDecimals(evm_extra_wei_decimals));
198 }
199 Ok(SpotDeploy::RequestEvmContract { token, address, evm_extra_wei_decimals })
200 }
201
202 /// The key the variant travels under.
203 #[must_use]
204 pub const fn wire_name(&self) -> &'static str {
205 match self {
206 SpotDeploy::RegisterToken2 { .. } => "registerToken2",
207 SpotDeploy::UserGenesis { .. } => "userGenesis",
208 SpotDeploy::Genesis { .. } => "genesis",
209 SpotDeploy::RegisterSpot { .. } => "registerSpot",
210 SpotDeploy::RegisterHyperliquidity { .. } => "registerHyperliquidity",
211 SpotDeploy::SetDeployerTradingFeeShare { .. } => "setDeployerTradingFeeShare",
212 SpotDeploy::EnableQuoteToken(_) => "enableQuoteToken",
213 SpotDeploy::EnableAlignedQuoteToken(_) => "enableAlignedQuoteToken",
214 SpotDeploy::DisableQuoteToken(_) => "disableQuoteToken",
215 SpotDeploy::DisableAlignedQuoteToken(_) => "disableAlignedQuoteToken",
216 SpotDeploy::SetTokenAnnotation { .. } => "setTokenAnnotation",
217 SpotDeploy::SetDeployerLabel(_) => "setDeployerLabel",
218 SpotDeploy::EnableFreezePrivilege(_) => "enableFreezePrivilege",
219 SpotDeploy::FreezeUser { .. } => "freezeUser",
220 SpotDeploy::RevokeFreezePrivilege(_) => "revokeFreezePrivilege",
221 SpotDeploy::RequestEvmContract { .. } => "requestEvmContract",
222 }
223 }
224
225 /// The body under [`wire_name`](Self::wire_name), keys in the order the
226 /// docs and the venue's clients write them.
227 #[must_use]
228 pub fn body(&self) -> Map {
229 let token = |token: u32| Map::new().put("token", token);
230 match self {
231 SpotDeploy::RegisterToken2 { spec, max_gas, full_name } => {
232 let spec = Map::new()
233 .put("name", spec.name.clone())
234 .put("szDecimals", spec.sz_decimals)
235 .put("weiDecimals", spec.wei_decimals);
236 Map::new().put("spec", spec).put("maxGas", *max_gas).put_some("fullName", full_name.clone())
237 }
238 SpotDeploy::UserGenesis { token, user_and_wei, existing_token_and_wei, blacklist_users } => {
239 let users = user_and_wei.iter().map(|(user, wei)| pair(user.to_hex(), wei.to_string()));
240 let existing = existing_token_and_wei.iter().map(|(token, wei)| pair(*token, wei.to_string()));
241 let blacklist = (!blacklist_users.is_empty())
242 .then(|| Value::array(blacklist_users.iter().map(|(user, listed)| pair(user.to_hex(), *listed))));
243 Map::new()
244 .put("token", *token)
245 .put("userAndWei", Value::array(users))
246 .put("existingTokenAndWei", Value::array(existing))
247 .put_some("blacklistUsers", blacklist)
248 }
249 SpotDeploy::Genesis { token, max_supply, no_hyperliquidity } => Map::new()
250 .put("token", *token)
251 .put("maxSupply", max_supply.to_string())
252 .put_some("noHyperliquidity", no_hyperliquidity.then_some(true)),
253 SpotDeploy::RegisterSpot { base, quote } => Map::new().put("tokens", Value::array([*base, *quote])),
254 SpotDeploy::RegisterHyperliquidity { spot, start_px, order_sz, n_orders, n_seeded_levels } => Map::new()
255 .put("spot", *spot)
256 .put("startPx", start_px.to_wire())
257 .put("orderSz", order_sz.to_wire())
258 .put("nOrders", *n_orders)
259 .put_some("nSeededLevels", *n_seeded_levels),
260 SpotDeploy::SetDeployerTradingFeeShare { token, share } => {
261 Map::new().put("token", *token).put("share", share.to_wire())
262 }
263 SpotDeploy::EnableQuoteToken(id)
264 | SpotDeploy::EnableAlignedQuoteToken(id)
265 | SpotDeploy::DisableQuoteToken(id)
266 | SpotDeploy::DisableAlignedQuoteToken(id)
267 | SpotDeploy::EnableFreezePrivilege(id)
268 | SpotDeploy::RevokeFreezePrivilege(id) => token(*id),
269 SpotDeploy::SetTokenAnnotation { token, annotation } => {
270 Map::new().put("token", *token).put("annotation", annotation.wire_after(Map::new()))
271 }
272 SpotDeploy::SetDeployerLabel(label) => Map::new().put("label", label.as_str().to_owned()),
273 SpotDeploy::FreezeUser { token, user, freeze } => {
274 Map::new().put("token", *token).put("user", user.to_hex()).put("freeze", *freeze)
275 }
276 SpotDeploy::RequestEvmContract { token, address, evm_extra_wei_decimals } => Map::new()
277 .put("token", *token)
278 .put("address", address.to_hex())
279 .put("evmExtraWeiDecimals", i64::from(*evm_extra_wei_decimals)),
280 }
281 }
282}
283
284impl L1Action for SpotDeploy {
285 fn action(&self) -> Map {
286 Map::new().put("type", "spotDeploy").put(self.wire_name(), self.body())
287 }
288}
289
290#[cfg(test)]
291#[path = "tests/spot.rs"]
292mod tests;