Skip to content

Commit fb9e5d9

Browse files
authored
Merge pull request #2338 from get10101/feat/add-realized-pnl-to-trades
feat: Add trader realized pnl to trades
2 parents e4a83de + 5ef530c commit fb9e5d9

File tree

6 files changed

+15
-0
lines changed

6 files changed

+15
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE "trades"
2+
DROP COLUMN IF EXISTS "trader_realized_pnl_sat";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ALTER TABLE "trades"
2+
ADD COLUMN "trader_realized_pnl_sat" BIGINT;
3+
4+
UPDATE TRADES SET trader_realized_pnl_sat = (SELECT trader_realized_pnl_sat from POSITIONS where POSITIONS.ID = TRADES.POSITION_ID AND POSITIONS.TRADER_DIRECTION != TRADES.DIRECTION);

coordinator/src/db/trades.rs

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct Trade {
2222
average_price: f32,
2323
timestamp: OffsetDateTime,
2424
order_matching_fee_sat: i64,
25+
trader_realized_pnl_sat: Option<i64>,
2526
}
2627

2728
#[derive(Insertable, Debug, Clone)]
@@ -36,6 +37,7 @@ struct NewTrade {
3637
direction: Direction,
3738
average_price: f32,
3839
order_matching_fee_sat: i64,
40+
trader_realized_pnl_sat: Option<i64>,
3941
}
4042

4143
pub fn insert(
@@ -74,6 +76,7 @@ impl From<crate::trade::models::NewTrade> for NewTrade {
7476
direction: value.trader_direction.into(),
7577
average_price: value.average_price,
7678
order_matching_fee_sat: value.order_matching_fee.to_sat() as i64,
79+
trader_realized_pnl_sat: value.trader_realized_pnl_sat,
7780
}
7881
}
7982
}
@@ -93,6 +96,7 @@ impl From<Trade> for crate::trade::models::Trade {
9396
average_price: value.average_price,
9497
timestamp: value.timestamp,
9598
order_matching_fee: Amount::from_sat(value.order_matching_fee_sat as u64),
99+
trader_realized_pnl_sat: value.trader_realized_pnl_sat,
96100
}
97101
}
98102
}

coordinator/src/dlc_protocol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ impl DlcProtocolExecutor {
412412
trader_direction: trade_params.direction,
413413
average_price: trade_params.average_price,
414414
order_matching_fee,
415+
trader_realized_pnl_sat: Some(trader_realized_pnl_sat),
415416
};
416417

417418
db::trades::insert(conn, new_trade)?;
@@ -474,6 +475,7 @@ impl DlcProtocolExecutor {
474475
trader_direction: trade_params.direction,
475476
average_price: trade_params.average_price,
476477
order_matching_fee,
478+
trader_realized_pnl_sat: None,
477479
};
478480

479481
db::trades::insert(conn, new_trade)?;

coordinator/src/schema.rs

+1
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ diesel::table! {
399399
average_price -> Float4,
400400
timestamp -> Timestamptz,
401401
order_matching_fee_sat -> Int8,
402+
trader_realized_pnl_sat -> Nullable<Int8>,
402403
}
403404
}
404405

coordinator/src/trade/models.rs

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct NewTrade {
1616
pub trader_direction: Direction,
1717
pub average_price: f32,
1818
pub order_matching_fee: Amount,
19+
pub trader_realized_pnl_sat: Option<i64>,
1920
}
2021

2122
#[derive(Debug)]
@@ -32,4 +33,5 @@ pub struct Trade {
3233
pub average_price: f32,
3334
pub timestamp: OffsetDateTime,
3435
pub order_matching_fee: Amount,
36+
pub trader_realized_pnl_sat: Option<i64>,
3537
}

0 commit comments

Comments
 (0)