Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ccb9ea8
Added ws bbo
austinzhang1018 May 2, 2025
d171dd1
cleaned up import
austinzhang1018 May 2, 2025
6da990b
added cloid support to modify requests
austinzhang1018 May 19, 2025
d32db23
Merge pull request #2 from austinzhang1018/modify-request-cloid
austinzhang1018 May 19, 2025
ebc269a
added users to trade field
austinzhang1018 Jun 14, 2025
f28d955
updated tokio tungstenite and updated websocket params for faster web…
austinzhang1018 Jul 5, 2025
7527b5a
Merge pull request #3 from austinzhang1018/fast-websockets
austinzhang1018 Jul 7, 2025
4abb2cb
updated reqwest version
austinzhang1018 Jul 15, 2025
b4ab3f5
added ws place and cancel methods
austinzhang1018 Jul 19, 2025
4cc2e7f
added post client
austinzhang1018 Jul 19, 2025
98c0410
Merge pull request #4 from austinzhang1018/ws-place-cancel
austinzhang1018 Jul 20, 2025
ecb7f82
added ping
austinzhang1018 Jul 20, 2025
2358d44
Merge pull request #5 from austinzhang1018/add-post-ws-ping
austinzhang1018 Jul 20, 2025
3f4df43
added ping
austinzhang1018 Jul 20, 2025
7357a00
Merge pull request #6 from austinzhang1018/add-post-ws-ping
austinzhang1018 Jul 20, 2025
ffcf4c9
added ws post performance metrics
austinzhang1018 Oct 11, 2025
ac5871d
Merge pull request #7 from austinzhang1018/austin/added-ws-post-perfo…
austinzhang1018 Oct 11, 2025
38399e2
add nonce as a return type to place orders
spiderman279 Oct 13, 2025
73eb7e8
add noop as an action
spiderman279 Oct 13, 2025
63b95d2
switch to a prepare the send model
spiderman279 Oct 13, 2025
c6ef834
Merge branch 'add-nonce' of github.com:austinzhang1018/hyperliquid-ru…
spiderman279 Oct 16, 2025
0958f38
add test case
spiderman279 Oct 16, 2025
6af4d09
Merge pull request #9 from austinzhang1018/add-noop
spiderman279 Oct 16, 2025
98e0180
Merge pull request #8 from austinzhang1018/add-nonce
spiderman279 Oct 16, 2025
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
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ http = "0.2.9"
lazy_static = "1.3"
log = "0.4.19"
rand = "0.8.5"
reqwest = "0.11.18"
reqwest = "0.12"
serde = {version = "1.0.175", features = ["derive"]}
serde_json = "1.0.103"
rmp-serde = "1.0.0"
thiserror = "1.0.44"
tokio = {version = "1.29.1", features = ["full"]}
tokio-tungstenite = {version = "0.20.0", features = ["native-tls"]}
tokio-tungstenite = {version = "0.27.0", features = ["native-tls"]}
uuid = {version = "1.6.1", features = ["v4"]}

openssl-sys = { version = "0.9", features = ["vendored"] }
90 changes: 90 additions & 0 deletions src/bin/order_and_modify_and_cancel.rs
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:?}");
}
71 changes: 71 additions & 0 deletions src/bin/order_and_modify_and_cancel_cloid.rs
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:?}");
}
73 changes: 73 additions & 0 deletions src/bin/order_and_noop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use ethers::signers::LocalWallet;
use log::info;
use std::sync::Arc;
use tokio;

use hyperliquid_rust_sdk::{BaseUrl, ClientLimit, ClientOrder, ClientOrderRequest, ExchangeClient};
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 = "fake".parse().unwrap();

let mut exchange_client = ExchangeClient::new(None, wallet, Some(BaseUrl::Testnet), None, None)
.await
.unwrap();

// Initialize the WebSocket client to send low-latency requests
exchange_client.init_ws_post_client().await.unwrap();

// Wrap the client in an Arc to allow safe, shared access across multiple tasks
let exchange_client = Arc::new(exchange_client);

// Define the order we intend to place
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(),
}),
};

// 1. Prepare the order request without sending it. This calculates all signatures
// and crucially, assigns a nonce to the transaction.
let prepared_order = exchange_client
.prepare_bulk_order_ws(vec![order], None)
.unwrap();

// 2. Extract the nonce that was used to prepare the order.
let nonce = prepared_order.nonce;
info!("Using nonce: {nonce} to race an order and a noop transaction.");

// 3. Concurrently send both the prepared order and a new noop transaction
// using the SAME nonce. The server will only accept the first one it sees.

// Clone the Arc for the first task
let client_for_order = Arc::clone(&exchange_client);
let order_task = tokio::spawn(async move {
let result = client_for_order
.send_prepared_bulk_order_ws(prepared_order)
.await;
info!("Order send result: {:?}", result);
});

// Clone the Arc for the second task
let client_for_noop = Arc::clone(&exchange_client);
let noop_task = tokio::spawn(async move {
// Use the WebSocket noop for the lowest latency race
let result = client_for_noop.noop_ws(nonce, None).await;
info!("No-op send result: {:?}", result);
});

// Wait for both tasks to complete
let _ = tokio::join!(order_task, noop_task);

info!("Both tasks completed. Check logs to see which one succeeded and which one failed due to the nonce.");
}
30 changes: 30 additions & 0 deletions src/bin/ws_bbo.rs
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:?}");
}
}
Loading