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
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,
}