diff --git a/crates/trade/src/lib.rs b/crates/trade/src/lib.rs index 93e87f7e7..ba9fee82c 100644 --- a/crates/trade/src/lib.rs +++ b/crates/trade/src/lib.rs @@ -77,3 +77,9 @@ impl ContractSymbol { } } } + +#[derive(Debug, Clone, Copy, Serialize, Deserialize)] +pub enum Direction { + Long, + Short, +} diff --git a/mobile/native/src/api.rs b/mobile/native/src/api.rs index 73a7cc351..f5a59384a 100644 --- a/mobile/native/src/api.rs +++ b/mobile/native/src/api.rs @@ -1,5 +1,4 @@ use crate::calculations; -use crate::common::api::Direction; use crate::config; use crate::config::api::Config; use crate::db; @@ -18,6 +17,7 @@ use flutter_rust_bridge::frb; use flutter_rust_bridge::StreamSink; use flutter_rust_bridge::SyncReturn; pub use trade::ContractSymbol; +pub use trade::Direction; /// Initialise logging infrastructure for Rust pub fn init_logging(sink: StreamSink) { @@ -79,6 +79,14 @@ pub enum _ContractSymbol { BtcUsd, } +#[allow(dead_code)] +#[frb(mirror(Direction))] +#[derive(Debug, Clone, Copy)] +pub enum _Direction { + Long, + Short, +} + pub fn calculate_liquidation_price( price: f64, leverage: f64, diff --git a/mobile/native/src/calculations/mod.rs b/mobile/native/src/calculations/mod.rs index ee6c158d3..57097de26 100644 --- a/mobile/native/src/calculations/mod.rs +++ b/mobile/native/src/calculations/mod.rs @@ -1,7 +1,7 @@ -use crate::common::api::Direction; use rust_decimal::prelude::ToPrimitive; use rust_decimal::Decimal; use trade::cfd; +use trade::Direction; /// Calculate the collateral in BTC. pub fn calculate_margin(opening_price: f64, quantity: f64, leverage: f64) -> u64 { diff --git a/mobile/native/src/common/api.rs b/mobile/native/src/common/api.rs deleted file mode 100644 index 86d65acc6..000000000 --- a/mobile/native/src/common/api.rs +++ /dev/null @@ -1,5 +0,0 @@ -#[derive(Debug, Clone, Copy)] -pub enum Direction { - Long, - Short, -} diff --git a/mobile/native/src/common/mod.rs b/mobile/native/src/common/mod.rs deleted file mode 100644 index e5fdf85ee..000000000 --- a/mobile/native/src/common/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod api; diff --git a/mobile/native/src/db/models.rs b/mobile/native/src/db/models.rs index 76a3991a5..00acbbf26 100644 --- a/mobile/native/src/db/models.rs +++ b/mobile/native/src/db/models.rs @@ -1,4 +1,3 @@ -use crate::common; use crate::schema; use crate::schema::last_login; use crate::schema::orders; @@ -113,8 +112,8 @@ impl Order { } } -impl From for Order { - fn from(value: crate::trade::order::OrderTrade) -> Self { +impl From for Order { + fn from(value: crate::trade::order::Order) -> Self { let (order_type, limit_price) = value.order_type.into(); let (status, execution_price) = value.status.into(); @@ -153,11 +152,11 @@ pub enum Direction { Short, } -impl From for Direction { - fn from(value: common::api::Direction) -> Self { +impl From for Direction { + fn from(value: trade::Direction) -> Self { match value { - common::api::Direction::Long => Direction::Long, - common::api::Direction::Short => Direction::Short, + trade::Direction::Long => Direction::Long, + trade::Direction::Short => Direction::Short, } } } @@ -169,11 +168,11 @@ pub enum OrderType { Limit, } -impl From for (OrderType, Option) { - fn from(value: crate::trade::order::OrderTypeTrade) -> Self { +impl From for (OrderType, Option) { + fn from(value: crate::trade::order::OrderType) -> Self { match value { - crate::trade::order::OrderTypeTrade::Market => (OrderType::Market, None), - crate::trade::order::OrderTypeTrade::Limit { price } => (OrderType::Limit, Some(price)), + crate::trade::order::OrderType::Market => (OrderType::Market, None), + crate::trade::order::OrderType::Limit { price } => (OrderType::Limit, Some(price)), } } } @@ -188,14 +187,14 @@ pub enum OrderState { Filled, } -impl From for (OrderState, Option) { - fn from(value: crate::trade::order::OrderStateTrade) -> Self { +impl From for (OrderState, Option) { + fn from(value: crate::trade::order::OrderState) -> Self { match value { - crate::trade::order::OrderStateTrade::Initial => (OrderState::Initial, None), - crate::trade::order::OrderStateTrade::Rejected => (OrderState::Rejected, None), - crate::trade::order::OrderStateTrade::Open => (OrderState::Open, None), - crate::trade::order::OrderStateTrade::Failed => (OrderState::Failed, None), - crate::trade::order::OrderStateTrade::Filled { execution_price } => { + crate::trade::order::OrderState::Initial => (OrderState::Initial, None), + crate::trade::order::OrderState::Rejected => (OrderState::Rejected, None), + crate::trade::order::OrderState::Open => (OrderState::Open, None), + crate::trade::order::OrderState::Failed => (OrderState::Failed, None), + crate::trade::order::OrderState::Filled { execution_price } => { (OrderState::Filled, Some(execution_price)) } } @@ -204,7 +203,6 @@ impl From for (OrderState, Option) { #[cfg(test)] pub mod test { - use crate::common; use crate::db::models::LastLogin; use crate::db::models::Order; use crate::db::MIGRATIONS; @@ -256,9 +254,9 @@ pub mod test { let leverage = 2.0; let quantity = 100.0; let contract_symbol = trade::ContractSymbol::BtcUsd; - let direction = common::api::Direction::Long; - let (order_type, limit_price) = crate::trade::order::OrderTypeTrade::Market.into(); - let (status, execution_price) = crate::trade::order::OrderStateTrade::Initial.into(); + let direction = trade::Direction::Long; + let (order_type, limit_price) = crate::trade::order::OrderType::Market.into(); + let (status, execution_price) = crate::trade::order::OrderState::Initial.into(); let order = Order { id: uuid.to_string(), leverage, @@ -272,14 +270,14 @@ pub mod test { }; Order::insert( - crate::trade::order::OrderTrade { + crate::trade::order::Order { id: uuid, leverage, quantity, contract_symbol, direction, - order_type: crate::trade::order::OrderTypeTrade::Market, - status: crate::trade::order::OrderStateTrade::Initial, + order_type: crate::trade::order::OrderType::Market, + status: crate::trade::order::OrderState::Initial, } .into(), &mut connection, @@ -288,14 +286,14 @@ pub mod test { // Insert another one, just so that there is not just one order in the db Order::insert( - crate::trade::order::OrderTrade { + crate::trade::order::Order { id: uuid::Uuid::new_v4(), leverage, quantity, contract_symbol, - direction: common::api::Direction::Long, - order_type: crate::trade::order::OrderTypeTrade::Market, - status: crate::trade::order::OrderStateTrade::Initial, + direction: trade::Direction::Long, + order_type: crate::trade::order::OrderType::Market, + status: crate::trade::order::OrderState::Initial, } .into(), &mut connection, diff --git a/mobile/native/src/event/mod.rs b/mobile/native/src/event/mod.rs index 625159051..323cf5601 100644 --- a/mobile/native/src/event/mod.rs +++ b/mobile/native/src/event/mod.rs @@ -8,8 +8,8 @@ use trade::TradeParams; use crate::event::event_hub::get; use crate::event::subscriber::Subscriber; -use crate::trade::order::OrderTrade; -use crate::trade::position::PositionTrade; +use crate::trade::order::Order; +use crate::trade::position::Position; pub fn subscribe(subscriber: impl Subscriber + 'static + Send + Sync + Clone) { get().subscribe(subscriber); @@ -23,10 +23,10 @@ pub fn publish(event: &EventInternal) { pub enum EventInternal { Init(String), Log(String), - OrderUpdateNotification(OrderTrade), + OrderUpdateNotification(Order), WalletInfoUpdateNotification(WalletInfo), OrderFilledWith(Box), - PositionUpdateNotification(PositionTrade), + PositionUpdateNotification(Position), } impl From for EventType { diff --git a/mobile/native/src/lib.rs b/mobile/native/src/lib.rs index d98c9bfcd..f82ff5cc2 100644 --- a/mobile/native/src/lib.rs +++ b/mobile/native/src/lib.rs @@ -6,7 +6,6 @@ pub mod schema; mod api; mod bridge_generated; pub mod calculations; -pub mod common; pub(crate) mod config; pub mod event; pub mod ln_dlc; diff --git a/mobile/native/src/ln_dlc/mod.rs b/mobile/native/src/ln_dlc/mod.rs index fd4fe6dfe..c64ea0892 100644 --- a/mobile/native/src/ln_dlc/mod.rs +++ b/mobile/native/src/ln_dlc/mod.rs @@ -1,12 +1,11 @@ use crate::api::Balances; use crate::api::WalletInfo; -use crate::common::api::Direction; use crate::config; use crate::event; use crate::event::EventInternal; use crate::trade::position; -use crate::trade::position::PositionStateTrade; -use crate::trade::position::PositionTrade; +use crate::trade::position::Position; +use crate::trade::position::PositionState; use anyhow::anyhow; use anyhow::bail; use anyhow::Context; @@ -28,6 +27,7 @@ use std::sync::Arc; use std::time::Duration; use tokio::runtime::Runtime; use trade::ContractSymbol; +use trade::Direction; use trade::TradeParams; static NODE: Storage> = Storage::new(); @@ -149,7 +149,7 @@ pub fn run(data_dir: String) -> Result<()> { { // TODO: Load position data from database and fill in the values; the collateral // can be taken from the DLC - event::publish(&EventInternal::PositionUpdateNotification(PositionTrade { + event::publish(&EventInternal::PositionUpdateNotification(Position { leverage: 0.0, quantity: 0.0, contract_symbol: ContractSymbol::BtcUsd, @@ -157,7 +157,7 @@ pub fn run(data_dir: String) -> Result<()> { average_entry_price: 0.0, liquidation_price: 0.0, unrealized_pnl: 0, - position_state: PositionStateTrade::Open, + position_state: PositionState::Open, collateral: *offer_collateral, })); } diff --git a/mobile/native/src/trade/order/api.rs b/mobile/native/src/trade/order/api.rs index 63b62a6e1..e7b161c79 100644 --- a/mobile/native/src/trade/order/api.rs +++ b/mobile/native/src/trade/order/api.rs @@ -1,9 +1,7 @@ -use crate::common::api::Direction; -use crate::trade::order::OrderStateTrade; -use crate::trade::order::OrderTrade; -use crate::trade::order::OrderTypeTrade; +use crate::trade::order; use flutter_rust_bridge::frb; use trade::ContractSymbol; +use trade::Direction; use uuid::Uuid; #[frb] @@ -53,19 +51,19 @@ pub struct Order { pub execution_price: Option, } -impl From for OrderTypeTrade { - fn from(value: OrderType) -> Self { +impl From for OrderType { + fn from(value: order::OrderType) -> Self { match value { - OrderType::Market => OrderTypeTrade::Market, - OrderType::Limit { price } => OrderTypeTrade::Limit { price }, + order::OrderType::Market => OrderType::Market, + order::OrderType::Limit { price } => OrderType::Limit { price }, } } } -impl From for Order { - fn from(value: OrderTrade) -> Self { +impl From for Order { + fn from(value: order::Order) -> Self { let execution_price = match value.status { - OrderStateTrade::Filled { execution_price } => Some(execution_price), + order::OrderState::Filled { execution_price } => Some(execution_price), _ => None, }; @@ -81,41 +79,41 @@ impl From for Order { } } -impl From for OrderType { - fn from(value: OrderTypeTrade) -> Self { +impl From for order::OrderType { + fn from(value: OrderType) -> Self { match value { - OrderTypeTrade::Market => OrderType::Market, - OrderTypeTrade::Limit { price } => OrderType::Limit { price }, + OrderType::Market => order::OrderType::Market, + OrderType::Limit { price } => order::OrderType::Limit { price }, } } } -impl From for OrderState { - fn from(value: OrderStateTrade) -> Self { +impl From for OrderState { + fn from(value: order::OrderState) -> Self { match value { - OrderStateTrade::Open => OrderState::Open, - OrderStateTrade::Filled { .. } => OrderState::Filled, - OrderStateTrade::Failed => OrderState::Failed, - OrderStateTrade::Initial => unimplemented!( + order::OrderState::Open => OrderState::Open, + order::OrderState::Filled { .. } => OrderState::Filled, + order::OrderState::Failed => OrderState::Failed, + order::OrderState::Initial => unimplemented!( "don't expose orders that were not submitted into the orderbook to the frontend!" ), - OrderStateTrade::Rejected => unimplemented!( + order::OrderState::Rejected => unimplemented!( "don't expose orders that were rejected by the orderbook to the frontend!" ), } } } -impl From for OrderTrade { +impl From for order::Order { fn from(value: NewOrder) -> Self { - OrderTrade { + order::Order { id: Uuid::new_v4(), leverage: value.leverage, quantity: value.quantity, contract_symbol: value.contract_symbol, direction: value.direction, order_type: (*value.order_type).into(), - status: OrderStateTrade::Open, + status: order::OrderState::Open, } } } diff --git a/mobile/native/src/trade/order/handler.rs b/mobile/native/src/trade/order/handler.rs index 3e425d5a7..ced7ba5bc 100644 --- a/mobile/native/src/trade/order/handler.rs +++ b/mobile/native/src/trade/order/handler.rs @@ -1,20 +1,20 @@ -use crate::common::api::Direction; use crate::event; use crate::event::EventInternal; use crate::ln_dlc; -use crate::trade::order::OrderStateTrade; -use crate::trade::order::OrderTrade; -use crate::trade::order::OrderTypeTrade; +use crate::trade::order::Order; +use crate::trade::order::OrderState; +use crate::trade::order::OrderType; use crate::trade::position; use anyhow::Context; use anyhow::Result; use std::str::FromStr; use std::time::Duration; use trade::ContractSymbol; +use trade::Direction; use trade::TradeParams; use uuid::Uuid; -pub async fn submit_order(order: OrderTrade) -> Result<()> { +pub async fn submit_order(order: Order) -> Result<()> { // TODO: Save in DB and pass on to orderbook tokio::time::sleep(Duration::from_secs(5)).await; @@ -45,19 +45,19 @@ pub async fn submit_order(order: OrderTrade) -> Result<()> { Ok(()) } -pub async fn get_order(id: String) -> Result { +pub async fn get_order(id: String) -> Result { // TODO: Fetch from database let id = Uuid::from_str(id.as_str()).context("Failed to parse UUID")?; - let dummy_order = OrderTrade { + let dummy_order = Order { id, leverage: 2.0, quantity: 1000.0, contract_symbol: ContractSymbol::BtcUsd, direction: Direction::Long, - order_type: OrderTypeTrade::Market, - status: OrderStateTrade::Filled { + order_type: OrderType::Market, + status: OrderState::Filled { execution_price: 25000.0, }, }; @@ -65,17 +65,17 @@ pub async fn get_order(id: String) -> Result { Ok(dummy_order) } -pub async fn get_orders() -> Result> { +pub async fn get_orders() -> Result> { // TODO: Fetch from database - let dummy_order = OrderTrade { + let dummy_order = Order { id: Uuid::new_v4(), leverage: 2.0, quantity: 1000.0, contract_symbol: ContractSymbol::BtcUsd, direction: Direction::Long, - order_type: OrderTypeTrade::Market, - status: OrderStateTrade::Filled { + order_type: OrderType::Market, + status: OrderState::Filled { execution_price: 25000.0, }, }; diff --git a/mobile/native/src/trade/order/mod.rs b/mobile/native/src/trade/order/mod.rs index f75248c0a..524a52bf2 100644 --- a/mobile/native/src/trade/order/mod.rs +++ b/mobile/native/src/trade/order/mod.rs @@ -1,5 +1,5 @@ -use crate::common::api::Direction; use trade::ContractSymbol; +use trade::Direction; use uuid::Uuid; pub mod api; @@ -9,13 +9,13 @@ pub mod handler; // `trade::OrderType` and contains errors, hence different name is used. // This is likely a bug in frb. #[derive(Debug, Clone, Copy)] -pub enum OrderTypeTrade { +pub enum OrderType { Market, Limit { price: f64 }, } #[derive(Debug, Clone, Copy)] -pub enum OrderStateTrade { +pub enum OrderState { /// Not submitted to orderbook yet /// /// In order to be able to track how many failed orders we have we store the order in the @@ -60,12 +60,12 @@ pub enum OrderStateTrade { } #[derive(Debug, Clone, Copy)] -pub struct OrderTrade { +pub struct Order { pub id: Uuid, pub leverage: f64, pub quantity: f64, pub contract_symbol: ContractSymbol, pub direction: Direction, - pub order_type: OrderTypeTrade, - pub status: OrderStateTrade, + pub order_type: OrderType, + pub status: OrderState, } diff --git a/mobile/native/src/trade/position/api.rs b/mobile/native/src/trade/position/api.rs index 2f3828e64..c698a5ef5 100644 --- a/mobile/native/src/trade/position/api.rs +++ b/mobile/native/src/trade/position/api.rs @@ -1,8 +1,7 @@ -use crate::common::api::Direction; -use crate::trade::position::PositionStateTrade; -use crate::trade::position::PositionTrade; +use crate::trade::position; use flutter_rust_bridge::frb; use trade::ContractSymbol; +use trade::Direction; #[frb] #[derive(Debug, Clone)] @@ -41,17 +40,17 @@ pub struct Position { pub collateral: u64, } -impl From for PositionState { - fn from(value: PositionStateTrade) -> Self { +impl From for PositionState { + fn from(value: position::PositionState) -> Self { match value { - PositionStateTrade::Open => PositionState::Open, - PositionStateTrade::Closing => PositionState::Closing, + position::PositionState::Open => PositionState::Open, + position::PositionState::Closing => PositionState::Closing, } } } -impl From for Position { - fn from(value: PositionTrade) -> Self { +impl From for Position { + fn from(value: position::Position) -> Self { Position { leverage: value.leverage, quantity: value.quantity, diff --git a/mobile/native/src/trade/position/handler.rs b/mobile/native/src/trade/position/handler.rs index 74e5c1990..5df814d19 100644 --- a/mobile/native/src/trade/position/handler.rs +++ b/mobile/native/src/trade/position/handler.rs @@ -1,9 +1,9 @@ -use crate::common::api::Direction; use crate::ln_dlc; -use crate::trade::position::PositionStateTrade; -use crate::trade::position::PositionTrade; +use crate::trade::position::Position; +use crate::trade::position::PositionState; use anyhow::Result; use trade::ContractSymbol; +use trade::Direction; use trade::TradeParams; /// Sets up a trade with the counterparty @@ -29,10 +29,10 @@ pub async fn trade(trade_params: TradeParams) -> Result<()> { } /// Fetch the positions from the database -pub async fn get_positions() -> Result> { +pub async fn get_positions() -> Result> { // TODO: Fetch from database - let dummy_position = PositionTrade { + let dummy_position = Position { leverage: 2.0, quantity: 10000.0, contract_symbol: ContractSymbol::BtcUsd, @@ -40,7 +40,7 @@ pub async fn get_positions() -> Result> { average_entry_price: 20000.0, liquidation_price: 14000.0, unrealized_pnl: -400, - position_state: PositionStateTrade::Open, + position_state: PositionState::Open, collateral: 2000, }; diff --git a/mobile/native/src/trade/position/mod.rs b/mobile/native/src/trade/position/mod.rs index e1c000ce6..9803c5bcc 100644 --- a/mobile/native/src/trade/position/mod.rs +++ b/mobile/native/src/trade/position/mod.rs @@ -1,12 +1,12 @@ -use crate::common::api::Direction; use trade::ContractSymbol; +use trade::Direction; pub mod api; pub mod handler; pub mod subscriber; #[derive(Debug, Clone)] -pub enum PositionStateTrade { +pub enum PositionState { /// The position is open /// /// Open in the sense, that there is an active position that is being rolled-over. @@ -27,7 +27,7 @@ pub enum PositionStateTrade { } #[derive(Debug, Clone)] -pub struct PositionTrade { +pub struct Position { pub leverage: f64, pub quantity: f64, pub contract_symbol: ContractSymbol, @@ -36,6 +36,6 @@ pub struct PositionTrade { pub liquidation_price: f64, /// The unrealized PL can be positive or negative pub unrealized_pnl: i64, - pub position_state: PositionStateTrade, + pub position_state: PositionState, pub collateral: u64, }