Skip to content

Commit

Permalink
Merge #231
Browse files Browse the repository at this point in the history
231: Refactor/business logic model names r=da-kami a=da-kami



Co-authored-by: Daniel Karzel <[email protected]>
  • Loading branch information
bors[bot] and da-kami authored Mar 8, 2023
2 parents 953422a + 972ca2f commit a045dc9
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 110 deletions.
6 changes: 6 additions & 0 deletions crates/trade/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,9 @@ impl ContractSymbol {
}
}
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum Direction {
Long,
Short,
}
10 changes: 9 additions & 1 deletion mobile/native/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::calculations;
use crate::common::api::Direction;
use crate::config;
use crate::config::api::Config;
use crate::db;
Expand All @@ -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<logger::LogEntry>) {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion mobile/native/src/calculations/mod.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
5 changes: 0 additions & 5 deletions mobile/native/src/common/api.rs

This file was deleted.

1 change: 0 additions & 1 deletion mobile/native/src/common/mod.rs

This file was deleted.

56 changes: 27 additions & 29 deletions mobile/native/src/db/models.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::common;
use crate::schema;
use crate::schema::last_login;
use crate::schema::orders;
Expand Down Expand Up @@ -113,8 +112,8 @@ impl Order {
}
}

impl From<crate::trade::order::OrderTrade> for Order {
fn from(value: crate::trade::order::OrderTrade) -> Self {
impl From<crate::trade::order::Order> 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();

Expand Down Expand Up @@ -153,11 +152,11 @@ pub enum Direction {
Short,
}

impl From<common::api::Direction> for Direction {
fn from(value: common::api::Direction) -> Self {
impl From<trade::Direction> 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,
}
}
}
Expand All @@ -169,11 +168,11 @@ pub enum OrderType {
Limit,
}

impl From<crate::trade::order::OrderTypeTrade> for (OrderType, Option<f64>) {
fn from(value: crate::trade::order::OrderTypeTrade) -> Self {
impl From<crate::trade::order::OrderType> for (OrderType, Option<f64>) {
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)),
}
}
}
Expand All @@ -188,14 +187,14 @@ pub enum OrderState {
Filled,
}

impl From<crate::trade::order::OrderStateTrade> for (OrderState, Option<f64>) {
fn from(value: crate::trade::order::OrderStateTrade) -> Self {
impl From<crate::trade::order::OrderState> for (OrderState, Option<f64>) {
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))
}
}
Expand All @@ -204,7 +203,6 @@ impl From<crate::trade::order::OrderStateTrade> for (OrderState, Option<f64>) {

#[cfg(test)]
pub mod test {
use crate::common;
use crate::db::models::LastLogin;
use crate::db::models::Order;
use crate::db::MIGRATIONS;
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions mobile/native/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -23,10 +23,10 @@ pub fn publish(event: &EventInternal) {
pub enum EventInternal {
Init(String),
Log(String),
OrderUpdateNotification(OrderTrade),
OrderUpdateNotification(Order),
WalletInfoUpdateNotification(WalletInfo),
OrderFilledWith(Box<TradeParams>),
PositionUpdateNotification(PositionTrade),
PositionUpdateNotification(Position),
}

impl From<EventInternal> for EventType {
Expand Down
1 change: 0 additions & 1 deletion mobile/native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions mobile/native/src/ln_dlc/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Arc<Node>> = Storage::new();
Expand Down Expand Up @@ -149,15 +149,15 @@ 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,
direction: Direction::Long,
average_entry_price: 0.0,
liquidation_price: 0.0,
unrealized_pnl: 0,
position_state: PositionStateTrade::Open,
position_state: PositionState::Open,
collateral: *offer_collateral,
}));
}
Expand Down
48 changes: 23 additions & 25 deletions mobile/native/src/trade/order/api.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -53,19 +51,19 @@ pub struct Order {
pub execution_price: Option<f64>,
}

impl From<OrderType> for OrderTypeTrade {
fn from(value: OrderType) -> Self {
impl From<order::OrderType> 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<OrderTrade> for Order {
fn from(value: OrderTrade) -> Self {
impl From<order::Order> 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,
};

Expand All @@ -81,41 +79,41 @@ impl From<OrderTrade> for Order {
}
}

impl From<OrderTypeTrade> for OrderType {
fn from(value: OrderTypeTrade) -> Self {
impl From<OrderType> 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<OrderStateTrade> for OrderState {
fn from(value: OrderStateTrade) -> Self {
impl From<order::OrderState> 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<NewOrder> for OrderTrade {
impl From<NewOrder> 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,
}
}
}
Loading

0 comments on commit a045dc9

Please sign in to comment.