Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/exchange/exchange_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
prelude::*,
req::HttpClient,
signature::sign_l1_action,
ws::WsPostClient,
BaseUrl, BulkCancelCloid, Error, ExchangeResponseStatus,
};
use crate::{ClassTransfer, SpotSend, SpotUser, VaultTransfer, Withdraw3};
Expand All @@ -39,6 +40,7 @@ pub struct ExchangeClient {
pub meta: Meta,
pub vault_address: Option<H160>,
pub coin_to_asset: HashMap<String, u32>,
pub ws_post_client: Option<WsPostClient>,
}

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -122,6 +124,7 @@ impl ExchangeClient {
base_url: base_url.get_url(),
},
coin_to_asset,
ws_post_client: None,
})
}

Expand Down Expand Up @@ -759,6 +762,75 @@ impl ExchangeClient {
let signature = sign_l1_action(wallet, connection_id, is_mainnet)?;
self.post(action, signature, timestamp).await
}

/// Initialize WebSocket post client for faster order execution
pub async fn init_ws_post_client(&mut self) -> Result<()> {
let base_url = match self.http_client.base_url.as_str() {
"https://api.hyperliquid.xyz" => BaseUrl::Mainnet,
"https://api.hyperliquid-testnet.xyz" => BaseUrl::Testnet,
_ => return Err(Error::GenericRequest("Invalid base URL".to_string())),
};

self.ws_post_client = Some(WsPostClient::new(base_url).await?);
Ok(())
}

/// Execute bulk order via WebSocket for lower latency
pub async fn bulk_order_ws(
&self,
orders: Vec<ClientOrderRequest>,
wallet: Option<&LocalWallet>,
) -> Result<ExchangeResponseStatus> {
let ws_client = self.ws_post_client.as_ref()
.ok_or_else(|| Error::GenericRequest("WebSocket client not initialized. Call init_ws_post_client() first.".to_string()))?;

let wallet = wallet.unwrap_or(&self.wallet);
let mut transformed_orders = Vec::new();

for order in orders {
transformed_orders.push(order.convert(&self.coin_to_asset)?);
}

let action = BulkOrder {
orders: transformed_orders,
grouping: "na".to_string(),
builder: None,
};

let is_mainnet = self.http_client.is_mainnet();
ws_client.bulk_order(action, wallet, is_mainnet, self.vault_address).await
}

/// Execute bulk cancel by cloid via WebSocket for lower latency
pub async fn bulk_cancel_by_cloid_ws(
&self,
cancels: Vec<ClientCancelRequestCloid>,
wallet: Option<&LocalWallet>,
) -> Result<ExchangeResponseStatus> {
let ws_client = self.ws_post_client.as_ref()
.ok_or_else(|| Error::GenericRequest("WebSocket client not initialized. Call init_ws_post_client() first.".to_string()))?;

let wallet = wallet.unwrap_or(&self.wallet);
let mut transformed_cancels: Vec<CancelRequestCloid> = Vec::new();

for cancel in cancels.into_iter() {
let &asset = self
.coin_to_asset
.get(&cancel.asset)
.ok_or(Error::AssetNotFound)?;
transformed_cancels.push(CancelRequestCloid {
asset,
cloid: uuid_to_hex_string(cancel.cloid),
});
}

let action = BulkCancelCloid {
cancels: transformed_cancels,
};

let is_mainnet = self.http_client.is_mainnet();
ws_client.bulk_cancel_by_cloid(action, wallet, is_mainnet, self.vault_address).await
}
}

fn round_to_decimals(value: f64, decimals: u32) -> f64 {
Expand Down
2 changes: 2 additions & 0 deletions src/ws/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod message_types;
mod sub_structs;
mod ws_manager;
mod ws_post_client;
pub use message_types::*;
pub use sub_structs::*;
pub(crate) use ws_manager::WsManager;
pub use ws_manager::{Message, Subscription};
pub use ws_post_client::WsPostClient;
Loading