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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use crate::order::OrderInfo;
use injective_math::FPDecimal;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[allow(non_snake_case)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Position {
#[serde(default)]
pub isLong: bool,
pub quantity: FPDecimal,
pub entry_price: FPDecimal,
#[serde(default)]
pub margin: FPDecimal,
pub cumulative_funding_entry: FPDecimal,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct EffectivePosition {
#[serde(default)]
pub is_long: bool,
pub quantity: FPDecimal,
pub entry_price: FPDecimal,
#[serde(default)]
pub effective_margin: FPDecimal,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DerivativePosition {
pub subaccount_id: String,
pub market_id: String,
pub position: Position,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DerivativeOrder {
pub market_id: String,
pub order_info: OrderInfo,
pub order_type: i32,
pub margin: FPDecimal,
pub trigger_price: Option<String>,
}
impl DerivativeOrder {
pub fn new(
price: FPDecimal,
quantity: FPDecimal,
margin: FPDecimal,
is_buy: bool,
market_id: &str,
subaccount_id: &str,
fee_recipient: &str,
) -> Self {
DerivativeOrder {
market_id: market_id.to_string(),
order_info: OrderInfo {
subaccount_id: subaccount_id.to_string(),
fee_recipient: fee_recipient.to_string(),
price,
quantity,
},
order_type: if is_buy { 1 } else { 2 },
margin,
trigger_price: None,
}
}
pub fn is_reduce_only(&self) -> bool {
self.margin.is_zero()
}
pub fn get_price(&self) -> FPDecimal {
self.order_info.price
}
pub fn get_qty(&self) -> FPDecimal {
self.order_info.quantity
}
pub fn get_val(&self) -> FPDecimal {
self.get_price() * self.get_qty()
}
pub fn get_margin(&self) -> FPDecimal {
self.margin
}
pub fn non_reduce_only_is_invalid(&self) -> bool {
self.get_margin().is_zero() || self.get_price().is_zero() || self.get_qty().is_zero()
}
pub fn reduce_only_price_is_invalid(&self) -> bool {
self.get_price().is_zero()
}
pub fn reduce_only_qty_is_invalid(&self) -> bool {
self.get_qty().is_zero()
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DerivativeLimitOrder {
pub order_info: OrderInfo,
pub order_type: i32,
pub margin: FPDecimal,
pub fillable: FPDecimal,
pub trigger_price: Option<FPDecimal>,
pub order_hash: String,
}
impl DerivativeLimitOrder {
pub fn new(
margin: FPDecimal,
fillable: FPDecimal,
order_hash: String,
trigger_price: Option<FPDecimal>,
order_type: i32,
order_info: OrderInfo,
) -> Self {
DerivativeLimitOrder {
margin,
fillable,
order_hash,
trigger_price,
order_type,
order_info,
}
}
pub fn is_reduce_only(&self) -> bool {
self.margin.is_zero()
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DerivativeMarketOrder {
pub order_info: OrderInfo,
pub order_type: i32,
pub margin: FPDecimal,
pub fillable: FPDecimal,
pub trigger_price: Option<FPDecimal>,
pub order_hash: String,
}
impl DerivativeMarketOrder {
pub fn new(
order_info: OrderInfo,
order_type: i32,
margin: FPDecimal,
fillable: FPDecimal,
trigger_price: Option<FPDecimal>,
order_hash: String,
) -> Self {
DerivativeMarketOrder {
margin,
fillable,
order_hash,
trigger_price,
order_type,
order_info,
}
}
pub fn is_reduce_only(&self) -> bool {
self.margin.is_zero()
}
}
#[allow(non_snake_case)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct TrimmedDerivativeLimitOrder {
pub price: FPDecimal,
pub quantity: FPDecimal,
#[serde(default)]
pub margin: FPDecimal,
pub fillable: FPDecimal,
#[serde(default)]
pub isBuy: bool,
pub order_hash: String,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DerivativeMetadataStatistics {
pub group_count: u32,
pub records_sample_size: u32,
pub mean: FPDecimal,
pub twap: FPDecimal,
pub first_timestamp: i64,
pub last_timestamp: i64,
pub min_price: FPDecimal,
pub max_price: FPDecimal,
pub median_price: FPDecimal,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DerivativeTradeRecord {
timestamp: i64,
price: FPDecimal,
quantity: FPDecimal,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DerivativePriceRecord {
timestamp: i64,
price: FPDecimal,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DerivativeOracleInfo {
pub symbol: String,
pub oracle_type: i32,
pub scale_factor: u32,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DerivativeOracleHistoryOptions {
pub max_age: u64,
pub include_raw_history: bool,
pub include_metadata: bool,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DerivativeTradeHistoryOptions {
pub trade_grouping_sec: u64,
pub max_age: u64,
pub include_raw_history: bool,
pub include_metadata: bool,
}