-
Notifications
You must be signed in to change notification settings - Fork 242
Add Bbo subscription type to ws_client #100
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
ccb9ea8
Added ws bbo
austinzhang1018 d171dd1
cleaned up import
austinzhang1018 6da990b
added cloid support to modify requests
austinzhang1018 d32db23
Merge pull request #2 from austinzhang1018/modify-request-cloid
austinzhang1018 ebc269a
added users to trade field
austinzhang1018 f28d955
updated tokio tungstenite and updated websocket params for faster web…
austinzhang1018 7527b5a
Merge pull request #3 from austinzhang1018/fast-websockets
austinzhang1018 4abb2cb
updated reqwest version
austinzhang1018 b4ab3f5
added ws place and cancel methods
austinzhang1018 4cc2e7f
added post client
austinzhang1018 98c0410
Merge pull request #4 from austinzhang1018/ws-place-cancel
austinzhang1018 ecb7f82
added ping
austinzhang1018 2358d44
Merge pull request #5 from austinzhang1018/add-post-ws-ping
austinzhang1018 3f4df43
added ping
austinzhang1018 7357a00
Merge pull request #6 from austinzhang1018/add-post-ws-ping
austinzhang1018 ffcf4c9
added ws post performance metrics
austinzhang1018 ac5871d
Merge pull request #7 from austinzhang1018/austin/added-ws-post-perfo…
austinzhang1018 38399e2
add nonce as a return type to place orders
spiderman279 73eb7e8
add noop as an action
spiderman279 63b95d2
switch to a prepare the send model
spiderman279 c6ef834
Merge branch 'add-nonce' of github.com:austinzhang1018/hyperliquid-ru…
spiderman279 0958f38
add test case
spiderman279 6af4d09
Merge pull request #9 from austinzhang1018/add-noop
spiderman279 98e0180
Merge pull request #8 from austinzhang1018/add-nonce
spiderman279 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| use ethers::signers::LocalWallet; | ||
| use log::info; | ||
|
|
||
| use hyperliquid_rust_sdk::{ | ||
| BaseUrl, ClientCancelRequest, ClientLimit, ClientModifyRequest, ClientOrder, ClientOrderRequest, ExchangeClient, ExchangeDataStatus, ExchangeResponseStatus | ||
| }; | ||
| use std::{thread::sleep, time::Duration}; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| env_logger::init(); | ||
| // Key was randomly generated for testing and shouldn't be used with any real funds | ||
| let wallet: LocalWallet = "e908f86dbb4d55ac876378565aafeabc187f6690f046459397b17d9b9a19688e" | ||
| .parse() | ||
| .unwrap(); | ||
|
|
||
| let exchange_client = ExchangeClient::new(None, wallet, Some(BaseUrl::Testnet), None, None) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| let order = ClientOrderRequest { | ||
| asset: "ETH".to_string(), | ||
| is_buy: true, | ||
| reduce_only: false, | ||
| limit_px: 1800.0, | ||
| sz: 0.01, | ||
| cloid: None, | ||
| order_type: ClientOrder::Limit(ClientLimit { | ||
| tif: "Gtc".to_string(), | ||
| }), | ||
| }; | ||
|
|
||
| let response = exchange_client.order(order, None).await.unwrap(); | ||
| info!("Order placed: {response:?}"); | ||
|
|
||
| let response = match response { | ||
| ExchangeResponseStatus::Ok(exchange_response) => exchange_response, | ||
| ExchangeResponseStatus::Err(e) => panic!("error with exchange response: {e}"), | ||
| }; | ||
| let status = response.data.unwrap().statuses[0].clone(); | ||
| let oid = match status { | ||
| ExchangeDataStatus::Filled(order) => order.oid, | ||
| ExchangeDataStatus::Resting(order) => order.oid, | ||
| _ => panic!("Error: {status:?}"), | ||
| }; | ||
|
|
||
| sleep(Duration::from_secs(5)); | ||
| // Shift the order by 1 dollar | ||
| let modify = ClientModifyRequest { | ||
| oid: oid.into(), | ||
| order: | ||
| ClientOrderRequest { | ||
| asset: "ETH".to_string(), | ||
| is_buy: true, | ||
| reduce_only: false, | ||
| limit_px: 1801.0, | ||
| sz: 0.01, | ||
| cloid: None, | ||
| order_type: ClientOrder::Limit(ClientLimit { | ||
| tif: "Gtc".to_string(), | ||
| }), | ||
| }, | ||
| }; | ||
|
|
||
| let modify_response = exchange_client.modify(modify, None).await.unwrap(); | ||
| info!("Order potentially modified: {modify_response:?}"); | ||
| // This response will return an error if order was filled (since you can't modify a filled order), otherwise it will modify the order | ||
| let modify_response = match modify_response { | ||
| ExchangeResponseStatus::Ok(exchange_response) => exchange_response, | ||
| ExchangeResponseStatus::Err(e) => panic!("error with exchange response: {e}"), | ||
| }; | ||
| let status = modify_response.data.unwrap().statuses[0].clone(); | ||
| let oid = match status { | ||
| ExchangeDataStatus::Filled(order) => order.oid, | ||
| ExchangeDataStatus::Resting(order) => order.oid, | ||
| _ => panic!("Error: {status:?}"), | ||
| }; | ||
|
|
||
| // So you can see the order before it's cancelled | ||
| sleep(Duration::from_secs(10)); | ||
|
|
||
| let cancel = ClientCancelRequest { | ||
| asset: "ETH".to_string(), | ||
| oid, | ||
| }; | ||
|
|
||
| // This response will return an error if order was filled (since you can't cancel a filled order), otherwise it will cancel the order | ||
| let response = exchange_client.cancel(cancel, None).await.unwrap(); | ||
| info!("Order potentially cancelled: {response:?}"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| use ethers::signers::LocalWallet; | ||
| use log::info; | ||
|
|
||
| use hyperliquid_rust_sdk::{ | ||
| BaseUrl, ClientCancelRequestCloid, ClientLimit, ClientModifyId, ClientModifyRequest, ClientOrder, ClientOrderRequest, ExchangeClient | ||
| }; | ||
| use std::{thread::sleep, time::Duration}; | ||
| use uuid::Uuid; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| env_logger::init(); | ||
| // Key was randomly generated for testing and shouldn't be used with any real funds | ||
| let wallet: LocalWallet = "e908f86dbb4d55ac876378565aafeabc187f6690f046459397b17d9b9a19688e" | ||
| .parse() | ||
| .unwrap(); | ||
|
|
||
| let exchange_client = ExchangeClient::new(None, wallet, Some(BaseUrl::Testnet), None, None) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| // Order and Modify and Cancel with cloid | ||
| let cloid = Uuid::new_v4(); | ||
| let order = ClientOrderRequest { | ||
| asset: "ETH".to_string(), | ||
| is_buy: true, | ||
| reduce_only: false, | ||
| limit_px: 1800.0, | ||
| sz: 0.01, | ||
| cloid: Some(cloid), | ||
| order_type: ClientOrder::Limit(ClientLimit { | ||
| tif: "Gtc".to_string(), | ||
| }), | ||
| }; | ||
|
|
||
| let response = exchange_client.order(order, None).await.unwrap(); | ||
| info!("Order placed: {response:?}"); | ||
|
|
||
| sleep(Duration::from_secs(5)); | ||
|
|
||
| // Shift the order by 1 dollar | ||
| let modify = ClientModifyRequest { | ||
| oid: ClientModifyId::Cloid(cloid), | ||
| order: ClientOrderRequest { | ||
| asset: "ETH".to_string(), | ||
| is_buy: true, | ||
| reduce_only: false, | ||
| limit_px: 1801.0, | ||
| sz: 0.01, | ||
| cloid: Some(cloid), | ||
| order_type: ClientOrder::Limit(ClientLimit { | ||
| tif: "Gtc".to_string(), | ||
| }), | ||
| }, | ||
| }; | ||
|
|
||
| let response = exchange_client.modify(modify, None).await.unwrap(); | ||
| info!("Order potentially modified: {response:?}"); | ||
|
|
||
| // So you can see the order before it's cancelled | ||
| sleep(Duration::from_secs(10)); | ||
|
|
||
| let cancel = ClientCancelRequestCloid { | ||
| asset: "ETH".to_string(), | ||
| cloid, | ||
| }; | ||
|
|
||
| // This response will return an error if order was filled (since you can't cancel a filled order), otherwise it will cancel the order | ||
| let response = exchange_client.cancel_by_cloid(cancel, None).await.unwrap(); | ||
| info!("Order potentially cancelled: {response:?}"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| use hyperliquid_rust_sdk::{BaseUrl, InfoClient, Message, Subscription}; | ||
| use log::info; | ||
| 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 coin = "BTC".to_string(); | ||
|
|
||
| let (sender, mut receiver) = unbounded_channel(); | ||
| let subscription_id = info_client | ||
| .subscribe(Subscription::Bbo { coin }, sender) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| spawn(async move { | ||
| sleep(Duration::from_secs(30)).await; | ||
| info!("Unsubscribing from bbo"); | ||
| info_client.unsubscribe(subscription_id).await.unwrap() | ||
| }); | ||
|
|
||
| while let Some(Message::Bbo(bbo)) = receiver.recv().await { | ||
| info!("Received bbo: {bbo:?}"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,51 @@ | ||
| use crate::helpers::uuid_to_hex_string; | ||
|
|
||
| use super::{order::OrderRequest, ClientOrderRequest}; | ||
| use serde::{Deserialize, Serialize}; | ||
| use uuid::Uuid; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub enum ClientModifyId { | ||
| Oid(u64), | ||
| Cloid(Uuid), | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize, Debug, Clone)] | ||
| #[serde(untagged)] | ||
| pub enum SerializedClientId { | ||
| U64(u64), | ||
| Str(String), | ||
| } | ||
|
|
||
| impl From<u64> for ClientModifyId { | ||
| fn from(oid: u64) -> Self { | ||
| ClientModifyId::Oid(oid) | ||
| } | ||
| } | ||
|
|
||
| impl From<Uuid> for ClientModifyId { | ||
| fn from(cloid: Uuid) -> Self { | ||
| ClientModifyId::Cloid(cloid) | ||
| } | ||
| } | ||
|
|
||
| impl From<ClientModifyId> for SerializedClientId { | ||
| fn from(client_id: ClientModifyId) -> Self { | ||
| match client_id { | ||
| ClientModifyId::Oid(oid) => SerializedClientId::U64(oid), | ||
| ClientModifyId::Cloid(cloid) => SerializedClientId::Str(uuid_to_hex_string(cloid)), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct ClientModifyRequest { | ||
| pub oid: u64, | ||
| pub oid: ClientModifyId, | ||
| pub order: ClientOrderRequest, | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize, Debug, Clone)] | ||
| pub struct ModifyRequest { | ||
| pub oid: u64, | ||
| pub oid: SerializedClientId, | ||
| pub order: OrderRequest, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
OidOrCloid