|
| 1 | +use hyperliquid_api_examples::Client; |
| 2 | +use serde_json::json; |
| 3 | + |
| 4 | +const COIN: &str = "BTC"; |
| 5 | + |
| 6 | +#[tokio::main] |
| 7 | +async fn main() { |
| 8 | + let client = Client::from_env(); |
| 9 | + |
| 10 | + let mid = client.get_mid(COIN).await; |
| 11 | + if mid == 0.0 { |
| 12 | + eprintln!("Could not fetch {COIN} mid price"); |
| 13 | + std::process::exit(1); |
| 14 | + } |
| 15 | + |
| 16 | + let sz = format!("{:.5}", 11.0 / mid); |
| 17 | + let rest_px = format!("{}", (mid * 0.97) as u64); |
| 18 | + |
| 19 | + println!("{COIN} mid: ${mid:.2}"); |
| 20 | + println!("Placing resting BUY {sz} @ {rest_px} (GTC, 3% below mid)\n"); |
| 21 | + |
| 22 | + // Place resting order |
| 23 | + let res = client |
| 24 | + .exchange(&json!({ |
| 25 | + "action": { |
| 26 | + "type": "order", |
| 27 | + "orders": [{"asset": COIN, "side": "buy", "price": rest_px, "size": sz, "tif": "gtc"}], |
| 28 | + }, |
| 29 | + })) |
| 30 | + .await; |
| 31 | + |
| 32 | + let hash = res["hash"].as_str().unwrap(); |
| 33 | + let sig = client.sign_hash(hash).await; |
| 34 | + |
| 35 | + let result = client |
| 36 | + .exchange(&json!({ |
| 37 | + "action": res["action"], |
| 38 | + "nonce": res["nonce"], |
| 39 | + "signature": sig, |
| 40 | + })) |
| 41 | + .await; |
| 42 | + |
| 43 | + let exchange_resp = &result["exchangeResponse"]; |
| 44 | + let statuses = exchange_resp["response"]["data"]["statuses"] |
| 45 | + .as_array() |
| 46 | + .expect("No statuses in response"); |
| 47 | + |
| 48 | + let oid = statuses |
| 49 | + .iter() |
| 50 | + .find_map(|s| s["resting"]["oid"].as_u64()) |
| 51 | + .expect("Could not extract OID from resting order"); |
| 52 | + |
| 53 | + let new_px = format!("{}", (mid * 0.96) as u64); |
| 54 | + println!("Order resting (OID: {oid})"); |
| 55 | + println!("Modifying price: {rest_px} -> {new_px}\n"); |
| 56 | + |
| 57 | + // Modify order |
| 58 | + let modify_action = json!({ |
| 59 | + "type": "batchModify", |
| 60 | + "modifies": [{ |
| 61 | + "oid": oid, |
| 62 | + "order": {"asset": COIN, "side": "buy", "price": new_px, "size": sz, "tif": "gtc"}, |
| 63 | + }], |
| 64 | + }); |
| 65 | + |
| 66 | + let res = client.exchange(&json!({"action": modify_action})).await; |
| 67 | + |
| 68 | + let hash = res["hash"].as_str().unwrap(); |
| 69 | + let sig = client.sign_hash(hash).await; |
| 70 | + |
| 71 | + let modify_result = client |
| 72 | + .exchange(&json!({ |
| 73 | + "action": modify_action, |
| 74 | + "nonce": res["nonce"], |
| 75 | + "signature": sig, |
| 76 | + })) |
| 77 | + .await; |
| 78 | + |
| 79 | + println!( |
| 80 | + "{}", |
| 81 | + serde_json::to_string_pretty(&modify_result["exchangeResponse"]).unwrap() |
| 82 | + ); |
| 83 | + println!("\nOrder modified."); |
| 84 | +} |
0 commit comments