-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ExchangeClient): Add market open and close methods (#55)
## Description This PR introduces `market_open` and `market_close` methods to the `ExchangeClient`, aligning the Rust SDK's functionality with the existing Hyperliquid Python SDK. ## Key Changes - Added `market_open` method for placing market buy/sell orders - Added `market_close` method for closing positions with market orders - Implemented price slippage calculation with respect to asset-specific decimal requirements - Added helper functions for rounding to significant figures and decimal places ## Implementation Details - Mirrors the functionality of the Python SDK's `market_open` and `market_close` methods - Uses the existing `InfoClient` to fetch asset metadata and current mid-prices - Simulates market orders using IOC (Immediate-or-Cancel) limit orders - Respects Hyperliquid's tick size and lot size rules for each asset ## Usage Example ```rust let market_open_params = MarketOrderParams { asset: "ETH", is_buy: true, sz: 0.01, px: None, slippage: Some(0.01), cloid: None, wallet: None, }; let response = exchange_client.market_open(market_open_params).await?; ``` ## Testing - Manually tested against Hyperliquid testnet - Verified behavior matches Python SDK output for equivalent inputs <img width="1332" alt="Screenshot 2024-09-04 at 02 19 37" src="https://github.com/user-attachments/assets/10629c0c-973b-43a5-86c1-bc553a1cf397"> ## Related Issues Closes #44
- Loading branch information
Showing
4 changed files
with
245 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,79 @@ | ||
use ethers::signers::LocalWallet; | ||
use log::info; | ||
|
||
use hyperliquid_rust_sdk::{ | ||
BaseUrl, ExchangeClient, ExchangeDataStatus, ExchangeResponseStatus, MarketCloseParams, | ||
MarketOrderParams, | ||
}; | ||
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(); | ||
|
||
// Market open order | ||
let market_open_params = MarketOrderParams { | ||
asset: "ETH", | ||
is_buy: true, | ||
sz: 0.01, | ||
px: None, | ||
slippage: Some(0.01), // 1% slippage | ||
cloid: None, | ||
wallet: None, | ||
}; | ||
|
||
let response = exchange_client | ||
.market_open(market_open_params) | ||
.await | ||
.unwrap(); | ||
info!("Market open 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(); | ||
match status { | ||
ExchangeDataStatus::Filled(order) => info!("Order filled: {order:?}"), | ||
ExchangeDataStatus::Resting(order) => info!("Order resting: {order:?}"), | ||
_ => panic!("Unexpected status: {status:?}"), | ||
}; | ||
|
||
// Wait for a while before closing the position | ||
sleep(Duration::from_secs(10)); | ||
|
||
// Market close order | ||
let market_close_params = MarketCloseParams { | ||
asset: "ETH", | ||
sz: None, // Close entire position | ||
px: None, | ||
slippage: Some(0.01), // 1% slippage | ||
cloid: None, | ||
wallet: None, | ||
}; | ||
|
||
let response = exchange_client | ||
.market_close(market_close_params) | ||
.await | ||
.unwrap(); | ||
info!("Market close 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(); | ||
match status { | ||
ExchangeDataStatus::Filled(order) => info!("Close order filled: {order:?}"), | ||
ExchangeDataStatus::Resting(order) => info!("Close order resting: {order:?}"), | ||
_ => panic!("Unexpected status: {status:?}"), | ||
}; | ||
} |
This file contains 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 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 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