|
| 1 | +use crate::order::OrderInfo; |
| 2 | +use injective_math::FPDecimal; |
| 3 | +use schemars::JsonSchema; |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | + |
| 6 | +#[allow(non_snake_case)] |
| 7 | +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] |
| 8 | +pub struct Position { |
| 9 | + #[serde(default)] |
| 10 | + pub isLong: bool, |
| 11 | + pub quantity: FPDecimal, |
| 12 | + pub entry_price: FPDecimal, |
| 13 | + #[serde(default)] |
| 14 | + pub margin: FPDecimal, |
| 15 | + pub cumulative_funding_entry: FPDecimal, |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] |
| 19 | +pub struct EffectivePosition { |
| 20 | + #[serde(default)] |
| 21 | + pub is_long: bool, |
| 22 | + pub quantity: FPDecimal, |
| 23 | + pub entry_price: FPDecimal, |
| 24 | + #[serde(default)] |
| 25 | + pub effective_margin: FPDecimal, |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] |
| 29 | +pub struct DerivativePosition { |
| 30 | + pub subaccount_id: String, |
| 31 | + pub market_id: String, |
| 32 | + pub position: Position, |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] |
| 36 | +pub struct DerivativeOrder { |
| 37 | + pub market_id: String, |
| 38 | + pub order_info: OrderInfo, |
| 39 | + pub order_type: i32, |
| 40 | + pub margin: FPDecimal, |
| 41 | + pub trigger_price: Option<String>, |
| 42 | +} |
| 43 | + |
| 44 | +impl DerivativeOrder { |
| 45 | + pub fn new( |
| 46 | + price: FPDecimal, |
| 47 | + quantity: FPDecimal, |
| 48 | + margin: FPDecimal, |
| 49 | + is_buy: bool, |
| 50 | + market_id: &str, |
| 51 | + subaccount_id: &str, |
| 52 | + fee_recipient: &str, |
| 53 | + ) -> Self { |
| 54 | + DerivativeOrder { |
| 55 | + market_id: market_id.to_string(), |
| 56 | + order_info: OrderInfo { |
| 57 | + subaccount_id: subaccount_id.to_string(), |
| 58 | + fee_recipient: fee_recipient.to_string(), |
| 59 | + price, |
| 60 | + quantity, |
| 61 | + }, |
| 62 | + order_type: if is_buy { 1 } else { 2 }, // TODO PO-orders |
| 63 | + margin, |
| 64 | + trigger_price: None, |
| 65 | + } |
| 66 | + } |
| 67 | + pub fn is_reduce_only(&self) -> bool { |
| 68 | + self.margin.is_zero() |
| 69 | + } |
| 70 | + pub fn get_price(&self) -> FPDecimal { |
| 71 | + self.order_info.price |
| 72 | + } |
| 73 | + pub fn get_qty(&self) -> FPDecimal { |
| 74 | + self.order_info.quantity |
| 75 | + } |
| 76 | + pub fn get_val(&self) -> FPDecimal { |
| 77 | + self.get_price() * self.get_qty() |
| 78 | + } |
| 79 | + pub fn get_margin(&self) -> FPDecimal { |
| 80 | + self.margin |
| 81 | + } |
| 82 | + pub fn non_reduce_only_is_invalid(&self) -> bool { |
| 83 | + self.get_margin().is_zero() || self.get_price().is_zero() || self.get_qty().is_zero() |
| 84 | + } |
| 85 | + pub fn reduce_only_price_is_invalid(&self) -> bool { |
| 86 | + self.get_price().is_zero() |
| 87 | + } |
| 88 | + pub fn reduce_only_qty_is_invalid(&self) -> bool { |
| 89 | + self.get_qty().is_zero() |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] |
| 94 | +pub struct DerivativeLimitOrder { |
| 95 | + pub order_info: OrderInfo, |
| 96 | + pub order_type: i32, |
| 97 | + pub margin: FPDecimal, |
| 98 | + pub fillable: FPDecimal, |
| 99 | + pub trigger_price: Option<FPDecimal>, |
| 100 | + pub order_hash: String, |
| 101 | +} |
| 102 | + |
| 103 | +impl DerivativeLimitOrder { |
| 104 | + pub fn new( |
| 105 | + margin: FPDecimal, |
| 106 | + fillable: FPDecimal, |
| 107 | + order_hash: String, |
| 108 | + trigger_price: Option<FPDecimal>, |
| 109 | + order_type: i32, |
| 110 | + order_info: OrderInfo, |
| 111 | + ) -> Self { |
| 112 | + DerivativeLimitOrder { |
| 113 | + margin, |
| 114 | + fillable, |
| 115 | + order_hash, |
| 116 | + trigger_price, |
| 117 | + order_type, |
| 118 | + order_info, |
| 119 | + } |
| 120 | + } |
| 121 | + pub fn is_reduce_only(&self) -> bool { |
| 122 | + self.margin.is_zero() |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +#[allow(non_snake_case)] |
| 127 | +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] |
| 128 | +pub struct TrimmedDerivativeLimitOrder { |
| 129 | + pub price: FPDecimal, |
| 130 | + pub quantity: FPDecimal, |
| 131 | + pub margin: FPDecimal, |
| 132 | + pub fillable: FPDecimal, |
| 133 | + pub isBuy: bool, |
| 134 | + pub order_hash: String, |
| 135 | +} |
| 136 | + |
| 137 | +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] |
| 138 | +pub struct DerivativeMarketOrder { |
| 139 | + pub order_info: OrderInfo, |
| 140 | + pub order_type: i32, |
| 141 | + pub margin: FPDecimal, |
| 142 | + pub fillable: FPDecimal, |
| 143 | + pub trigger_price: Option<FPDecimal>, |
| 144 | + pub order_hash: String, |
| 145 | +} |
| 146 | + |
| 147 | +impl DerivativeMarketOrder { |
| 148 | + pub fn new( |
| 149 | + order_info: OrderInfo, |
| 150 | + order_type: i32, |
| 151 | + margin: FPDecimal, |
| 152 | + fillable: FPDecimal, |
| 153 | + trigger_price: Option<FPDecimal>, |
| 154 | + order_hash: String, |
| 155 | + ) -> Self { |
| 156 | + DerivativeMarketOrder { |
| 157 | + margin, |
| 158 | + fillable, |
| 159 | + order_hash, |
| 160 | + trigger_price, |
| 161 | + order_type, |
| 162 | + order_info, |
| 163 | + } |
| 164 | + } |
| 165 | + pub fn is_reduce_only(&self) -> bool { |
| 166 | + self.margin.is_zero() |
| 167 | + } |
| 168 | +} |
0 commit comments