Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket subscribe webData2 channel #72

Merged
merged 1 commit into from
Dec 21, 2024
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
35 changes: 35 additions & 0 deletions src/bin/ws_web_data2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use log::info;

use std::str::FromStr;

use ethers::types::H160;
use hyperliquid_rust_sdk::{BaseUrl, InfoClient, Message, Subscription};
use tokio::{
spawn,
sync::mpsc::unbounded_channel,
time::{sleep, Duration},
};

#[tokio::main]
async fn main() {
env_logger::init();
let mut info_client = InfoClient::new(None, Some(BaseUrl::Testnet)).await.unwrap();
let user = H160::from_str("0xc64cc00b46101bd40aa1c3121195e85c0b0918d8").unwrap();

let (sender, mut receiver) = unbounded_channel();
let subscription_id = info_client
.subscribe(Subscription::WebData2 { user }, sender)
.await
.unwrap();

spawn(async move {
sleep(Duration::from_secs(30)).await;
info!("Unsubscribing from web data2");
info_client.unsubscribe(subscription_id).await.unwrap()
});

// this loop ends when we unsubscribe
while let Some(Message::WebData2(web_data2)) = receiver.recv().await {
info!("Received web data: {web_data2:?}");
}
}
5 changes: 5 additions & 0 deletions src/ws/message_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ pub struct UserNonFundingLedgerUpdates {
pub struct Notification {
pub data: NotificationData,
}

#[derive(Deserialize, Clone, Debug)]
pub struct WebData2 {
pub data: WebData2Data,
}
6 changes: 6 additions & 0 deletions src/ws/sub_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,9 @@ pub struct SpotGenesis {
pub struct NotificationData {
pub notification: String,
}

#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct WebData2Data {
pub user: H160,
}
8 changes: 7 additions & 1 deletion src/ws/ws_manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
prelude::*,
ws::message_types::{AllMids, Candle, L2Book, OrderUpdates, Trades, User},
Error, Notification, UserFills, UserFundings, UserNonFundingLedgerUpdates,
Error, Notification, UserFills, UserFundings, UserNonFundingLedgerUpdates, WebData2,
};
use futures_util::{stream::SplitSink, SinkExt, StreamExt};
use log::{error, info, warn};
Expand Down Expand Up @@ -59,6 +59,7 @@ pub enum Subscription {
UserFundings { user: H160 },
UserNonFundingLedgerUpdates { user: H160 },
Notification { user: H160 },
WebData2 { user: H160 },
}

#[derive(Deserialize, Clone, Debug)]
Expand All @@ -78,6 +79,7 @@ pub enum Message {
UserFundings(UserFundings),
UserNonFundingLedgerUpdates(UserNonFundingLedgerUpdates),
Notification(Notification),
WebData2(WebData2),
Pong,
}

Expand Down Expand Up @@ -252,6 +254,10 @@ impl WsManager {
.map_err(|e| Error::JsonParse(e.to_string()))
}
Message::Notification(_) => Ok("notification".to_string()),
Message::WebData2(web_data2) => serde_json::to_string(&Subscription::WebData2 {
user: web_data2.data.user,
})
.map_err(|e| Error::JsonParse(e.to_string())),
Message::SubscriptionResponse | Message::Pong => Ok(String::default()),
Message::NoData => Ok("".to_string()),
Message::HyperliquidError(err) => Ok(format!("hyperliquid error: {err:?}")),
Expand Down
Loading