-
Notifications
You must be signed in to change notification settings - Fork 246
Expand file tree
/
Copy pathorder_and_cancel_cloid.rs
More file actions
52 lines (44 loc) · 1.61 KB
/
order_and_cancel_cloid.rs
File metadata and controls
52 lines (44 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use ethers::signers::LocalWallet;
use log::info;
use std::sync::Arc;
use hyperliquid_rust_sdk::{
BaseUrl, ClientCancelRequestCloid, ClientLimit, 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, Arc::new(wallet), Some(BaseUrl::Testnet), None, None)
.await
.unwrap();
// Order 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:?}");
// 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:?}");
}