Skip to content

Commit

Permalink
chore(coordinator): Remove collateral from trades table
Browse files Browse the repository at this point in the history
A trade only has a collateral associated with it once it's applied to
a position. Since it's not used for anything, we remove it.
  • Loading branch information
luckysori committed Apr 9, 2024
1 parent 703fd0f commit 0f0ae67
Show file tree
Hide file tree
Showing 7 changed files with 2 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE trades ADD COLUMN collateral;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE trades DROP COLUMN collateral;
4 changes: 0 additions & 4 deletions coordinator/src/db/trades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ struct Trade {
trader_pubkey: String,
quantity: f32,
trader_leverage: f32,
collateral: i64,
direction: Direction,
average_price: f32,
timestamp: OffsetDateTime,
Expand All @@ -34,7 +33,6 @@ struct NewTrade {
trader_pubkey: String,
quantity: f32,
trader_leverage: f32,
collateral: i64,
direction: Direction,
average_price: f32,
order_matching_fee_sat: i64,
Expand Down Expand Up @@ -110,7 +108,6 @@ impl From<crate::trade::models::NewTrade> for NewTrade {
trader_pubkey: value.trader_pubkey.to_string(),
quantity: value.quantity,
trader_leverage: value.trader_leverage,
collateral: value.coordinator_margin,
direction: value.trader_direction.into(),
average_price: value.average_price,
order_matching_fee_sat: value.order_matching_fee.to_sat() as i64,
Expand All @@ -130,7 +127,6 @@ impl From<Trade> for crate::trade::models::Trade {
.expect("public key to decode"),
quantity: value.quantity,
trader_leverage: value.trader_leverage,
collateral: value.collateral,
direction: value.direction.into(),
average_price: value.average_price,
timestamp: value.timestamp,
Expand Down
17 changes: 0 additions & 17 deletions coordinator/src/dlc_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use std::fmt::Formatter;
use std::str::from_utf8;
use time::OffsetDateTime;
use tokio::sync::broadcast::Sender;
use trade::cfd::calculate_margin;
use trade::cfd::calculate_pnl;
use trade::Direction;
use uuid::Uuid;
Expand Down Expand Up @@ -392,13 +391,6 @@ impl DlcProtocolExecutor {
closing_price,
)?;

let coordinator_margin = calculate_margin(
closing_price,
trade_params.quantity,
crate::trade::coordinator_leverage_for_trade(&trade_params.trader)
.map_err(|_| RollbackTransaction)?,
);

let order_matching_fee = trade_params.matching_fee;

let new_trade = NewTrade {
Expand All @@ -407,7 +399,6 @@ impl DlcProtocolExecutor {
trader_pubkey: trade_params.trader,
quantity: trade_params.quantity,
trader_leverage: trade_params.leverage,
coordinator_margin: coordinator_margin as i64,
trader_direction: trade_params.direction,
average_price: trade_params.average_price,
order_matching_fee,
Expand Down Expand Up @@ -460,13 +451,6 @@ impl DlcProtocolExecutor {
PositionState::Open,
)?;

let coordinator_margin = calculate_margin(
Decimal::try_from(trade_params.average_price).expect("to fit into decimal"),
trade_params.quantity,
crate::trade::coordinator_leverage_for_trade(&trade_params.trader)
.map_err(|_| RollbackTransaction)?,
);

let order_matching_fee = trade_params.matching_fee;

if matches!(original.position_state, PositionState::Resizing) {
Expand All @@ -482,7 +466,6 @@ impl DlcProtocolExecutor {
trader_pubkey: trade_params.trader,
quantity: trade_params.quantity,
trader_leverage: trade_params.leverage,
coordinator_margin: coordinator_margin as i64,
trader_direction: trade_params.direction,
average_price: trade_params.average_price,
order_matching_fee,
Expand Down
1 change: 0 additions & 1 deletion coordinator/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@ diesel::table! {
trader_pubkey -> Text,
quantity -> Float4,
trader_leverage -> Float4,
collateral -> Int8,
direction -> DirectionType,
average_price -> Float4,
timestamp -> Timestamptz,
Expand Down
2 changes: 0 additions & 2 deletions coordinator/src/trade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,8 +748,6 @@ impl TradeExecutor {
trader_pubkey: position.trader,
quantity: trade_params.quantity,
trader_leverage: position.trader_leverage,
// TODO: To be removed.
coordinator_margin: 0,
trader_direction: trade_params.direction,
average_price: trade_params
.average_execution_price()
Expand Down
4 changes: 0 additions & 4 deletions coordinator/src/trade/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ pub struct NewTrade {
pub trader_pubkey: PublicKey,
pub quantity: f32,
pub trader_leverage: f32,
// TODO: Consider removing this since it doesn't make sense with all kinds of trades.
pub coordinator_margin: i64,
pub trader_direction: Direction,
pub average_price: f32,
pub order_matching_fee: Amount,
Expand All @@ -28,8 +26,6 @@ pub struct Trade {
pub trader_pubkey: PublicKey,
pub quantity: f32,
pub trader_leverage: f32,
// TODO: Consider removing this since it doesn't make sense with all kinds of trades.
pub collateral: i64,
pub direction: Direction,
pub average_price: f32,
pub timestamp: OffsetDateTime,
Expand Down

0 comments on commit 0f0ae67

Please sign in to comment.