Skip to content

Commit 81b166a

Browse files
authored
Websocket subscribe webData2 channel (#72)
1 parent cff25ea commit 81b166a

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

src/bin/ws_web_data2.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use log::info;
2+
3+
use std::str::FromStr;
4+
5+
use ethers::types::H160;
6+
use hyperliquid_rust_sdk::{BaseUrl, InfoClient, Message, Subscription};
7+
use tokio::{
8+
spawn,
9+
sync::mpsc::unbounded_channel,
10+
time::{sleep, Duration},
11+
};
12+
13+
#[tokio::main]
14+
async fn main() {
15+
env_logger::init();
16+
let mut info_client = InfoClient::new(None, Some(BaseUrl::Testnet)).await.unwrap();
17+
let user = H160::from_str("0xc64cc00b46101bd40aa1c3121195e85c0b0918d8").unwrap();
18+
19+
let (sender, mut receiver) = unbounded_channel();
20+
let subscription_id = info_client
21+
.subscribe(Subscription::WebData2 { user }, sender)
22+
.await
23+
.unwrap();
24+
25+
spawn(async move {
26+
sleep(Duration::from_secs(30)).await;
27+
info!("Unsubscribing from web data2");
28+
info_client.unsubscribe(subscription_id).await.unwrap()
29+
});
30+
31+
// this loop ends when we unsubscribe
32+
while let Some(Message::WebData2(web_data2)) = receiver.recv().await {
33+
info!("Received web data: {web_data2:?}");
34+
}
35+
}

src/ws/message_types.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,8 @@ pub struct UserNonFundingLedgerUpdates {
5050
pub struct Notification {
5151
pub data: NotificationData,
5252
}
53+
54+
#[derive(Deserialize, Clone, Debug)]
55+
pub struct WebData2 {
56+
pub data: WebData2Data,
57+
}

src/ws/sub_structs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,9 @@ pub struct SpotGenesis {
273273
pub struct NotificationData {
274274
pub notification: String,
275275
}
276+
277+
#[derive(Deserialize, Clone, Debug)]
278+
#[serde(rename_all = "camelCase")]
279+
pub struct WebData2Data {
280+
pub user: H160,
281+
}

src/ws/ws_manager.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
prelude::*,
33
ws::message_types::{AllMids, Candle, L2Book, OrderUpdates, Trades, User},
4-
Error, Notification, UserFills, UserFundings, UserNonFundingLedgerUpdates,
4+
Error, Notification, UserFills, UserFundings, UserNonFundingLedgerUpdates, WebData2,
55
};
66
use futures_util::{stream::SplitSink, SinkExt, StreamExt};
77
use log::{error, info, warn};
@@ -59,6 +59,7 @@ pub enum Subscription {
5959
UserFundings { user: H160 },
6060
UserNonFundingLedgerUpdates { user: H160 },
6161
Notification { user: H160 },
62+
WebData2 { user: H160 },
6263
}
6364

6465
#[derive(Deserialize, Clone, Debug)]
@@ -78,6 +79,7 @@ pub enum Message {
7879
UserFundings(UserFundings),
7980
UserNonFundingLedgerUpdates(UserNonFundingLedgerUpdates),
8081
Notification(Notification),
82+
WebData2(WebData2),
8183
Pong,
8284
}
8385

@@ -252,6 +254,10 @@ impl WsManager {
252254
.map_err(|e| Error::JsonParse(e.to_string()))
253255
}
254256
Message::Notification(_) => Ok("notification".to_string()),
257+
Message::WebData2(web_data2) => serde_json::to_string(&Subscription::WebData2 {
258+
user: web_data2.data.user,
259+
})
260+
.map_err(|e| Error::JsonParse(e.to_string())),
255261
Message::SubscriptionResponse | Message::Pong => Ok(String::default()),
256262
Message::NoData => Ok("".to_string()),
257263
Message::HyperliquidError(err) => Ok(format!("hyperliquid error: {err:?}")),

0 commit comments

Comments
 (0)