Skip to content

Commit

Permalink
chore: Remove taken from orders
Browse files Browse the repository at this point in the history
The taken flag is not needed anymore as it is replaced by a more elaborate order state.
  • Loading branch information
holzeis committed Sep 9, 2023
1 parent 837b9d6 commit a09364a
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- This file should undo anything in `up.sql`
ALTER TABLE "orders"
ADD COLUMN "taken" BOOLEAN NOT NULL DEFAULT FALSE;

UPDATE orders SET taken = true WHERE order_state = 'Taken';
3 changes: 3 additions & 0 deletions coordinator/migrations/2023-09-08-090156_remove_taken/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- This file should undo anything in `up.sql`
ALTER TABLE "orders"
DROP COLUMN "taken";
18 changes: 3 additions & 15 deletions coordinator/src/orderbook/db/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ struct Order {
pub trader_order_id: Uuid,
pub price: f32,
pub trader_id: String,
pub taken: bool,
pub direction: Direction,
pub quantity: f32,
pub timestamp: OffsetDateTime,
Expand All @@ -104,7 +103,6 @@ impl From<Order> for OrderbookOrder {
id: value.trader_order_id,
price: Decimal::from_f32(value.price).expect("To be able to convert f32 to decimal"),
trader_id: value.trader_id.parse().expect("to have a valid pubkey"),
taken: value.taken,
leverage: value.leverage,
contract_symbol: value.contract_symbol.into(),
direction: value.direction.into(),
Expand Down Expand Up @@ -143,7 +141,6 @@ struct NewOrder {
pub trader_order_id: Uuid,
pub price: f32,
pub trader_id: String,
pub taken: bool,
pub direction: Direction,
pub quantity: f32,
pub order_type: OrderType,
Expand All @@ -163,7 +160,6 @@ impl From<OrderbookNewOrder> for NewOrder {
.to_f32()
.expect("To be able to convert decimal to f32"),
trader_id: value.trader_id.to_string(),
taken: false,
direction: value.direction.into(),
quantity: value
.quantity
Expand Down Expand Up @@ -196,13 +192,13 @@ pub fn all_by_direction_and_type(
conn: &mut PgConnection,
direction: OrderbookDirection,
order_type: OrderBookOrderType,
taken: bool,
filter_expired: bool,
) -> QueryResult<Vec<OrderbookOrder>> {
let filters = orders::table
.filter(orders::direction.eq(Direction::from(direction)))
.filter(orders::order_type.eq(OrderType::from(order_type)))
.filter(orders::taken.eq(taken));
.filter(orders::order_state.eq(OrderState::Open));

let orders: Vec<Order> = if filter_expired {
filters
.filter(orders::expiry.gt(OffsetDateTime::now_utc()))
Expand Down Expand Up @@ -270,17 +266,9 @@ pub fn set_order_state(
id: Uuid,
order_state: orderbook_commons::OrderState,
) -> QueryResult<OrderbookOrder> {
let mut is_taken = false;
if order_state == orderbook_commons::OrderState::Taken {
is_taken = true;
}

let order: Order = diesel::update(orders::table)
.filter(orders::trader_order_id.eq(id))
.set((
orders::taken.eq(is_taken),
orders::order_state.eq(OrderState::from(order_state)),
))
.set((orders::order_state.eq(OrderState::from(order_state)),))
.get_result(conn)?;

Ok(OrderbookOrder::from(order))
Expand Down
3 changes: 2 additions & 1 deletion coordinator/src/orderbook/tests/sample_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::orderbook::tests::start_postgres;
use bitcoin::secp256k1::PublicKey;
use orderbook_commons::NewOrder;
use orderbook_commons::OrderReason;
use orderbook_commons::OrderState;
use orderbook_commons::OrderType;
use rust_decimal_macros::dec;
use std::str::FromStr;
Expand Down Expand Up @@ -34,7 +35,7 @@ async fn crud_test() {
.unwrap();

let order = orders::set_is_taken(&mut conn, order.id, true).unwrap();
assert!(order.taken);
assert_eq!(order.order_state, OrderState::Taken);

let deleted = orders::delete_with_id(&mut conn, order.id).unwrap();
assert_eq!(deleted, 1);
Expand Down
5 changes: 0 additions & 5 deletions coordinator/src/orderbook/trading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ impl Trading {
&mut conn,
order.direction.opposite(),
OrderType::Limit,
false,
true,
)?;

Expand Down Expand Up @@ -480,7 +479,6 @@ pub mod tests {
"027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007",
)
.unwrap(),
taken: false,
direction: Direction::Long,
leverage: 1.0,
contract_symbol: ContractSymbol::BtcUsd,
Expand Down Expand Up @@ -621,7 +619,6 @@ pub mod tests {
"027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007",
)
.unwrap(),
taken: false,
direction: Direction::Short,
leverage: 1.0,
contract_symbol: ContractSymbol::BtcUsd,
Expand Down Expand Up @@ -696,7 +693,6 @@ pub mod tests {
"027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007",
)
.unwrap(),
taken: false,
direction: Direction::Short,
leverage: 1.0,
contract_symbol: ContractSymbol::BtcUsd,
Expand Down Expand Up @@ -747,7 +743,6 @@ pub mod tests {
"027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007",
)
.unwrap(),
taken: false,
direction: Direction::Long,
leverage: 1.0,
contract_symbol: ContractSymbol::BtcUsd,
Expand Down
1 change: 0 additions & 1 deletion coordinator/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ diesel::table! {
trader_order_id -> Uuid,
price -> Float4,
trader_id -> Text,
taken -> Bool,
direction -> DirectionType,
quantity -> Float4,
timestamp -> Timestamptz,
Expand Down
2 changes: 0 additions & 2 deletions crates/orderbook-commons/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ pub struct Order {
pub leverage: f32,
pub contract_symbol: ContractSymbol,
pub trader_id: PublicKey,
pub taken: bool,
pub direction: Direction,
#[serde(with = "rust_decimal::serde::float")]
pub quantity: Decimal,
Expand Down Expand Up @@ -104,7 +103,6 @@ pub struct OrderResponse {
#[serde(with = "rust_decimal::serde::float")]
pub price: Decimal,
pub trader_id: PublicKey,
pub taken: bool,
pub direction: Direction,
#[serde(with = "rust_decimal::serde::float")]
pub quantity: Decimal,
Expand Down
30 changes: 13 additions & 17 deletions crates/orderbook-commons/src/price.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::Order;
use crate::OrderState;
use crate::ToPrimitive;
use rust_decimal::Decimal;
use serde::Deserialize;
Expand Down Expand Up @@ -55,7 +56,7 @@ fn best_price_for(
let use_max = direction == Direction::Long;
current_orders
.iter()
.filter(|order| !order.taken && order.direction == direction)
.filter(|order| order.order_state == OrderState::Open && order.direction == direction)
.map(|order| order.price.to_f64().expect("to represent decimal as f64"))
// get the best price
.fold(None, |acc, x| match acc {
Expand Down Expand Up @@ -89,16 +90,11 @@ mod test {
.unwrap()
}

fn dummy_order(price: Decimal, direction: Direction, taken: bool) -> Order {
let mut order_state = OrderState::Open;
if taken {
order_state = OrderState::Taken;
}
fn dummy_order(price: Decimal, direction: Direction, order_state: OrderState) -> Order {
Order {
id: Uuid::new_v4(),
price,
trader_id: dummy_public_key(),
taken,
direction,
leverage: 1.0,
contract_symbol: BtcUsd,
Expand All @@ -114,32 +110,32 @@ mod test {
#[test]
fn test_best_bid_price() {
let current_orders = vec![
dummy_order(dec!(10_000), Direction::Long, false),
dummy_order(dec!(30_000), Direction::Long, false),
dummy_order(dec!(500_000), Direction::Long, true), // taken
dummy_order(dec!(50_000), Direction::Short, false), // wrong direction
dummy_order(dec!(10_000), Direction::Long, OrderState::Open),
dummy_order(dec!(30_000), Direction::Long, OrderState::Open),
dummy_order(dec!(500_000), Direction::Long, OrderState::Taken), // taken
dummy_order(dec!(50_000), Direction::Short, OrderState::Open), // wrong direction
];
assert_eq!(best_bid_price(&current_orders, BtcUsd), Some(dec!(30_000)));
}

#[test]
fn test_best_ask_price() {
let current_orders = vec![
dummy_order(dec!(10_000), Direction::Short, false),
dummy_order(dec!(30_000), Direction::Short, false),
dummy_order(dec!(10_000), Direction::Short, OrderState::Open),
dummy_order(dec!(30_000), Direction::Short, OrderState::Open),
// ignored in the calculations - this order is taken
dummy_order(dec!(5_000), Direction::Short, true),
dummy_order(dec!(5_000), Direction::Short, OrderState::Taken),
// ignored in the calculations - it's the bid price
dummy_order(dec!(50_000), Direction::Long, false),
dummy_order(dec!(50_000), Direction::Long, OrderState::Open),
];
assert_eq!(best_ask_price(&current_orders, BtcUsd), Some(dec!(10_000)));
}

#[test]
fn test_no_price() {
let all_orders_taken = vec![
dummy_order(dec!(10_000), Direction::Short, true),
dummy_order(dec!(30_000), Direction::Long, true),
dummy_order(dec!(10_000), Direction::Short, OrderState::Taken),
dummy_order(dec!(30_000), Direction::Long, OrderState::Taken),
];

assert_eq!(best_ask_price(&all_orders_taken, BtcUsd), None);
Expand Down
9 changes: 0 additions & 9 deletions maker/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
// @generated automatically by Diesel CLI.

diesel::table! {
orders (id) {
id -> Int4,
price -> Int4,
maker_id -> Text,
taken -> Bool,
}
}

0 comments on commit a09364a

Please sign in to comment.