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