hyperliquid_rust_sdk_abrkn/exchange/
order.rs1use crate::{
2 errors::Error,
3 helpers::{float_to_string_for_hashing, uuid_to_hex_string},
4 prelude::*,
5};
6use ethers::signers::LocalWallet;
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9use uuid::Uuid;
10
11#[derive(Deserialize, Serialize, Clone, Debug)]
12pub struct Limit {
13 pub tif: String,
14}
15
16#[derive(Deserialize, Serialize, Debug, Clone)]
17#[serde(rename_all = "camelCase")]
18pub struct Trigger {
19 pub is_market: bool,
20 pub trigger_px: String,
21 pub tpsl: String,
22}
23
24#[derive(Deserialize, Serialize, Debug, Clone)]
25#[serde(rename_all = "camelCase")]
26pub enum Order {
27 Limit(Limit),
28 Trigger(Trigger),
29}
30
31#[derive(Deserialize, Serialize, Debug, Clone)]
32#[serde(rename_all = "camelCase")]
33pub struct OrderRequest {
34 #[serde(rename = "a", alias = "asset")]
35 pub asset: u32,
36 #[serde(rename = "b", alias = "isBuy")]
37 pub is_buy: bool,
38 #[serde(rename = "p", alias = "limitPx")]
39 pub limit_px: String,
40 #[serde(rename = "s", alias = "sz")]
41 pub sz: String,
42 #[serde(rename = "r", alias = "reduceOnly", default)]
43 pub reduce_only: bool,
44 #[serde(rename = "t", alias = "orderType")]
45 pub order_type: Order,
46 #[serde(rename = "c", alias = "cloid", skip_serializing_if = "Option::is_none")]
47 pub cloid: Option<String>,
48}
49
50pub struct ClientLimit {
51 pub tif: String,
52}
53
54pub struct ClientTrigger {
55 pub is_market: bool,
56 pub trigger_px: f64,
57 pub tpsl: String,
58}
59
60pub struct MarketOrderParams<'a> {
61 pub asset: &'a str,
62 pub is_buy: bool,
63 pub sz: f64,
64 pub px: Option<f64>,
65 pub slippage: Option<f64>,
66 pub cloid: Option<Uuid>,
67 pub wallet: Option<&'a LocalWallet>,
68}
69
70pub struct MarketCloseParams<'a> {
71 pub asset: &'a str,
72 pub sz: Option<f64>,
73 pub px: Option<f64>,
74 pub slippage: Option<f64>,
75 pub cloid: Option<Uuid>,
76 pub wallet: Option<&'a LocalWallet>,
77}
78
79pub enum ClientOrder {
80 Limit(ClientLimit),
81 Trigger(ClientTrigger),
82}
83pub struct ClientOrderRequest {
84 pub asset: String,
85 pub is_buy: bool,
86 pub reduce_only: bool,
87 pub limit_px: f64,
88 pub sz: f64,
89 pub cloid: Option<Uuid>,
90 pub order_type: ClientOrder,
91}
92
93impl ClientOrderRequest {
94 pub(crate) fn convert(self, coin_to_asset: &HashMap<String, u32>) -> Result<OrderRequest> {
95 let order_type = match self.order_type {
96 ClientOrder::Limit(limit) => Order::Limit(Limit { tif: limit.tif }),
97 ClientOrder::Trigger(trigger) => Order::Trigger(Trigger {
98 trigger_px: float_to_string_for_hashing(trigger.trigger_px),
99 is_market: trigger.is_market,
100 tpsl: trigger.tpsl,
101 }),
102 };
103 let &asset = coin_to_asset.get(&self.asset).ok_or(Error::AssetNotFound)?;
104
105 let cloid = self.cloid.map(uuid_to_hex_string);
106
107 Ok(OrderRequest {
108 asset,
109 is_buy: self.is_buy,
110 reduce_only: self.reduce_only,
111 limit_px: float_to_string_for_hashing(self.limit_px),
112 sz: float_to_string_for_hashing(self.sz),
113 order_type,
114 cloid,
115 })
116 }
117}