pub mod evm;
pub mod share;
pub use evm::{EvmFinalizer, FinalizeEvmContract};
pub use share::FeeShare;
use crate::deploy::annotation::Annotation;
use crate::deploy::error::BadDeploy;
use crate::deploy::name::DexName;
use crate::deploy::tokens::TokenSpec;
use crate::utils::sign::{L1Action, Map, Value};
use crate::utils::{Address, Dec};
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum SpotDeploy {
RegisterToken2 {
spec: TokenSpec,
max_gas: u64,
full_name: Option<String>,
},
UserGenesis {
token: u32,
user_and_wei: Vec<(Address, u128)>,
existing_token_and_wei: Vec<(u32, u128)>,
blacklist_users: Vec<(Address, bool)>,
},
Genesis {
token: u32,
max_supply: u128,
no_hyperliquidity: bool,
},
RegisterSpot {
base: u32,
quote: u32,
},
RegisterHyperliquidity {
spot: u32,
start_px: Dec,
order_sz: Dec,
n_orders: u32,
n_seeded_levels: Option<u32>,
},
SetDeployerTradingFeeShare {
token: u32,
share: FeeShare,
},
EnableQuoteToken(u32),
#[deprecated(since = "0.1.1", note = "Hyperliquid's nodes refuse this action; see the variant's docs")]
EnableAlignedQuoteToken(u32),
DisableQuoteToken(u32),
#[deprecated(since = "0.1.1", note = "Hyperliquid's nodes refuse this action; see the variant's docs")]
DisableAlignedQuoteToken(u32),
SetTokenAnnotation {
token: u32,
annotation: Annotation,
},
SetDeployerLabel(DexName),
EnableFreezePrivilege(u32),
FreezeUser {
token: u32,
user: Address,
freeze: bool,
},
RevokeFreezePrivilege(u32),
RequestEvmContract {
token: u32,
address: Address,
evm_extra_wei_decimals: i8,
},
}
fn pair(key: impl Into<Value>, value: impl Into<Value>) -> Value {
Value::Array(vec![key.into(), value.into()])
}
#[allow(deprecated)]
impl SpotDeploy {
pub fn register_hyperliquidity(
spot: u32,
start_px: Dec,
order_sz: Dec,
n_orders: u32,
n_seeded_levels: Option<u32>,
) -> Result<Self, BadDeploy> {
for (what, value) in [("startPx", start_px), ("orderSz", order_sz)] {
if value.is_negative() {
return Err(BadDeploy::OutOfRange { what, least: "0", most: "any", value: value.to_wire() });
}
}
Ok(SpotDeploy::RegisterHyperliquidity { spot, start_px, order_sz, n_orders, n_seeded_levels })
}
pub fn request_evm_contract(token: u32, address: Address, evm_extra_wei_decimals: i8) -> Result<Self, BadDeploy> {
if !(-2..=18).contains(&evm_extra_wei_decimals) {
return Err(BadDeploy::EvmExtraWeiDecimals(evm_extra_wei_decimals));
}
Ok(SpotDeploy::RequestEvmContract { token, address, evm_extra_wei_decimals })
}
#[must_use]
pub const fn wire_name(&self) -> &'static str {
match self {
SpotDeploy::RegisterToken2 { .. } => "registerToken2",
SpotDeploy::UserGenesis { .. } => "userGenesis",
SpotDeploy::Genesis { .. } => "genesis",
SpotDeploy::RegisterSpot { .. } => "registerSpot",
SpotDeploy::RegisterHyperliquidity { .. } => "registerHyperliquidity",
SpotDeploy::SetDeployerTradingFeeShare { .. } => "setDeployerTradingFeeShare",
SpotDeploy::EnableQuoteToken(_) => "enableQuoteToken",
SpotDeploy::EnableAlignedQuoteToken(_) => "enableAlignedQuoteToken",
SpotDeploy::DisableQuoteToken(_) => "disableQuoteToken",
SpotDeploy::DisableAlignedQuoteToken(_) => "disableAlignedQuoteToken",
SpotDeploy::SetTokenAnnotation { .. } => "setTokenAnnotation",
SpotDeploy::SetDeployerLabel(_) => "setDeployerLabel",
SpotDeploy::EnableFreezePrivilege(_) => "enableFreezePrivilege",
SpotDeploy::FreezeUser { .. } => "freezeUser",
SpotDeploy::RevokeFreezePrivilege(_) => "revokeFreezePrivilege",
SpotDeploy::RequestEvmContract { .. } => "requestEvmContract",
}
}
#[must_use]
pub fn body(&self) -> Map {
let token = |token: u32| Map::new().put("token", token);
match self {
SpotDeploy::RegisterToken2 { spec, max_gas, full_name } => {
let spec = Map::new()
.put("name", spec.name.clone())
.put("szDecimals", spec.sz_decimals)
.put("weiDecimals", spec.wei_decimals);
Map::new().put("spec", spec).put("maxGas", *max_gas).put_some("fullName", full_name.clone())
}
SpotDeploy::UserGenesis { token, user_and_wei, existing_token_and_wei, blacklist_users } => {
let users = user_and_wei.iter().map(|(user, wei)| pair(user.to_hex(), wei.to_string()));
let existing = existing_token_and_wei.iter().map(|(token, wei)| pair(*token, wei.to_string()));
let blacklist = (!blacklist_users.is_empty())
.then(|| Value::array(blacklist_users.iter().map(|(user, listed)| pair(user.to_hex(), *listed))));
Map::new()
.put("token", *token)
.put("userAndWei", Value::array(users))
.put("existingTokenAndWei", Value::array(existing))
.put_some("blacklistUsers", blacklist)
}
SpotDeploy::Genesis { token, max_supply, no_hyperliquidity } => Map::new()
.put("token", *token)
.put("maxSupply", max_supply.to_string())
.put_some("noHyperliquidity", no_hyperliquidity.then_some(true)),
SpotDeploy::RegisterSpot { base, quote } => Map::new().put("tokens", Value::array([*base, *quote])),
SpotDeploy::RegisterHyperliquidity { spot, start_px, order_sz, n_orders, n_seeded_levels } => Map::new()
.put("spot", *spot)
.put("startPx", start_px.to_wire())
.put("orderSz", order_sz.to_wire())
.put("nOrders", *n_orders)
.put_some("nSeededLevels", *n_seeded_levels),
SpotDeploy::SetDeployerTradingFeeShare { token, share } => {
Map::new().put("token", *token).put("share", share.to_wire())
}
SpotDeploy::EnableQuoteToken(id)
| SpotDeploy::EnableAlignedQuoteToken(id)
| SpotDeploy::DisableQuoteToken(id)
| SpotDeploy::DisableAlignedQuoteToken(id)
| SpotDeploy::EnableFreezePrivilege(id)
| SpotDeploy::RevokeFreezePrivilege(id) => token(*id),
SpotDeploy::SetTokenAnnotation { token, annotation } => {
Map::new().put("token", *token).put("annotation", annotation.wire_after(Map::new()))
}
SpotDeploy::SetDeployerLabel(label) => Map::new().put("label", label.as_str().to_owned()),
SpotDeploy::FreezeUser { token, user, freeze } => {
Map::new().put("token", *token).put("user", user.to_hex()).put("freeze", *freeze)
}
SpotDeploy::RequestEvmContract { token, address, evm_extra_wei_decimals } => Map::new()
.put("token", *token)
.put("address", address.to_hex())
.put("evmExtraWeiDecimals", i64::from(*evm_extra_wei_decimals)),
}
}
}
impl L1Action for SpotDeploy {
fn action(&self) -> Map {
Map::new().put("type", "spotDeploy").put(self.wire_name(), self.body())
}
}
#[cfg(test)]
#[path = "tests/spot.rs"]
mod tests;