Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trader_pnl to trade_params table #2394

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE trades
ADD COLUMN is_complete BOOLEAN NOT NULL DEFAULT true;

ALTER TABLE trade_params
DROP COLUMN IF EXISTS trader_pnl_sat;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE trades
DROP COLUMN IF EXISTS "is_complete";

ALTER TABLE trade_params
ADD COLUMN trader_pnl_sat BIGINT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TYPE "Protocol_Type_Type"
RENAME VALUE 'open-channel' TO 'open';

ALTER TYPE "Protocol_Type_Type"
RENAME VALUE 'open-position' TO 'renew';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Must use `IF NOT EXISTS` because enum values cannot be removed on "down" migrations.
ALTER TYPE "Protocol_Type_Type"
ADD VALUE IF NOT EXISTS 'resize-position';

ALTER TYPE "Protocol_Type_Type"
RENAME VALUE 'open' TO 'open-channel';

ALTER TYPE "Protocol_Type_Type"
RENAME VALUE 'renew' TO 'open-position';
10 changes: 6 additions & 4 deletions coordinator/src/db/custom_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,13 @@ impl FromSql<ProtocolStateType, Pg> for DlcProtocolState {
impl ToSql<ProtocolTypeType, Pg> for DlcProtocolType {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
match *self {
DlcProtocolType::Open => out.write_all(b"open")?,
DlcProtocolType::OpenChannel => out.write_all(b"open-channel")?,
DlcProtocolType::Settle => out.write_all(b"settle")?,
DlcProtocolType::Renew => out.write_all(b"renew")?,
DlcProtocolType::OpenPosition => out.write_all(b"open-position")?,
DlcProtocolType::Rollover => out.write_all(b"rollover")?,
DlcProtocolType::Close => out.write_all(b"close")?,
DlcProtocolType::ForceClose => out.write_all(b"force-close")?,
DlcProtocolType::ResizePosition => out.write_all(b"resize-position")?,
}
Ok(IsNull::No)
}
Expand All @@ -195,12 +196,13 @@ impl ToSql<ProtocolTypeType, Pg> for DlcProtocolType {
impl FromSql<ProtocolTypeType, Pg> for DlcProtocolType {
fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> {
match bytes.as_bytes() {
b"open" => Ok(DlcProtocolType::Open),
b"open-channel" => Ok(DlcProtocolType::OpenChannel),
b"settle" => Ok(DlcProtocolType::Settle),
b"renew" => Ok(DlcProtocolType::Renew),
b"open-position" => Ok(DlcProtocolType::OpenPosition),
b"rollover" => Ok(DlcProtocolType::Rollover),
b"close" => Ok(DlcProtocolType::Close),
b"force-close" => Ok(DlcProtocolType::ForceClose),
b"resize-position" => Ok(DlcProtocolType::ResizePosition),
_ => Err("Unrecognized enum variant for ProtocolTypeType".into()),
}
}
Expand Down
2 changes: 1 addition & 1 deletion coordinator/src/db/dlc_channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub(crate) fn set_dlc_channel_open(
.execute(conn)
}

pub(crate) fn update_channel_on_renew(
pub(crate) fn update_channel(
conn: &mut PgConnection,
channel_id: &DlcChannelId,
coordinator_reserve: Amount,
Expand Down
22 changes: 14 additions & 8 deletions coordinator/src/db/dlc_protocols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ impl QueryId for ProtocolStateType {
#[derive(Debug, Clone, Copy, PartialEq, FromSqlRow, AsExpression, Eq, Hash)]
#[diesel(sql_type = ProtocolTypeType)]
pub(crate) enum DlcProtocolType {
Open,
Renew,
OpenChannel,
OpenPosition,
Settle,
Close,
ForceClose,
Rollover,
ResizePosition,
}

impl QueryId for ProtocolTypeType {
Expand Down Expand Up @@ -83,13 +84,13 @@ pub(crate) fn get_dlc_protocol(
.first(conn)?;

let protocol_type = match dlc_protocol.protocol_type {
DlcProtocolType::Open => {
DlcProtocolType::OpenChannel => {
let trade_params = db::trade_params::get(conn, protocol_id)?;
dlc_protocol::DlcProtocolType::Open { trade_params }
dlc_protocol::DlcProtocolType::OpenChannel { trade_params }
}
DlcProtocolType::Renew => {
DlcProtocolType::OpenPosition => {
let trade_params = db::trade_params::get(conn, protocol_id)?;
dlc_protocol::DlcProtocolType::Renew { trade_params }
dlc_protocol::DlcProtocolType::OpenPosition { trade_params }
}
DlcProtocolType::Settle => {
let trade_params = db::trade_params::get(conn, protocol_id)?;
Expand All @@ -104,6 +105,10 @@ pub(crate) fn get_dlc_protocol(
DlcProtocolType::Rollover => dlc_protocol::DlcProtocolType::Rollover {
trader: PublicKey::from_str(&dlc_protocol.trader_pubkey).expect("valid public key"),
},
DlcProtocolType::ResizePosition => {
let trade_params = db::trade_params::get(conn, protocol_id)?;
dlc_protocol::DlcProtocolType::ResizePosition { trade_params }
}
};

let protocol = dlc_protocol::DlcProtocol {
Expand Down Expand Up @@ -209,12 +214,13 @@ impl From<DlcProtocolState> for dlc_protocol::DlcProtocolState {
impl From<dlc_protocol::DlcProtocolType> for DlcProtocolType {
fn from(value: dlc_protocol::DlcProtocolType) -> Self {
match value {
dlc_protocol::DlcProtocolType::Open { .. } => DlcProtocolType::Open,
dlc_protocol::DlcProtocolType::Renew { .. } => DlcProtocolType::Renew,
dlc_protocol::DlcProtocolType::OpenChannel { .. } => DlcProtocolType::OpenChannel,
dlc_protocol::DlcProtocolType::OpenPosition { .. } => DlcProtocolType::OpenPosition,
dlc_protocol::DlcProtocolType::Settle { .. } => DlcProtocolType::Settle,
dlc_protocol::DlcProtocolType::Close { .. } => DlcProtocolType::Close,
dlc_protocol::DlcProtocolType::ForceClose { .. } => DlcProtocolType::ForceClose,
dlc_protocol::DlcProtocolType::Rollover { .. } => DlcProtocolType::Rollover,
dlc_protocol::DlcProtocolType::ResizePosition { .. } => DlcProtocolType::ResizePosition,
}
}
}
3 changes: 3 additions & 0 deletions coordinator/src/db/trade_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::orderbook::db::custom_types::Direction;
use crate::schema::trade_params;
use bitcoin::secp256k1::PublicKey;
use bitcoin::Amount;
use bitcoin::SignedAmount;
use diesel::ExpressionMethods;
use diesel::PgConnection;
use diesel::QueryDsl;
Expand All @@ -25,6 +26,7 @@ pub(crate) struct TradeParams {
pub average_price: f32,
pub direction: Direction,
pub matching_fee: i64,
pub trader_pnl: Option<i64>,
}

pub(crate) fn insert(
Expand Down Expand Up @@ -71,6 +73,7 @@ impl From<TradeParams> for dlc_protocol::TradeParams {
average_price: value.average_price,
direction: trade::Direction::from(value.direction),
matching_fee: Amount::from_sat(value.matching_fee as u64),
trader_pnl: value.trader_pnl.map(SignedAmount::from_sat),
}
}
}
24 changes: 0 additions & 24 deletions coordinator/src/db/trades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ struct Trade {
timestamp: OffsetDateTime,
order_matching_fee_sat: i64,
trader_realized_pnl_sat: Option<i64>,
is_complete: bool,
}

#[derive(Insertable, Debug, Clone)]
Expand All @@ -37,7 +36,6 @@ struct NewTrade {
average_price: f32,
order_matching_fee_sat: i64,
trader_realized_pnl_sat: Option<i64>,
is_complete: bool,
}

pub fn insert(
Expand All @@ -51,26 +49,6 @@ pub fn insert(
Ok(trade.into())
}

pub fn mark_as_completed(conn: &mut PgConnection, position_id: i32) -> QueryResult<()> {
let trade = trades::table
.filter(trades::position_id.eq(position_id))
.order_by(trades::id.desc())
.first::<Trade>(conn)
.optional()?
.ok_or(diesel::result::Error::NotFound)?;

let affected_rows = diesel::update(trades::table)
.filter(trades::id.eq(trade.id))
.set(trades::is_complete.eq(true))
.execute(conn)?;

if affected_rows == 0 {
return Err(diesel::result::Error::NotFound);
}

Ok(())
}

pub fn get_latest_for_position(
conn: &mut PgConnection,
position_id: i32,
Expand Down Expand Up @@ -112,7 +90,6 @@ impl From<crate::trade::models::NewTrade> for NewTrade {
average_price: value.average_price,
order_matching_fee_sat: value.order_matching_fee.to_sat() as i64,
trader_realized_pnl_sat: value.trader_realized_pnl_sat,
is_complete: value.is_complete,
}
}
}
Expand All @@ -132,7 +109,6 @@ impl From<Trade> for crate::trade::models::Trade {
timestamp: value.timestamp,
order_matching_fee: Amount::from_sat(value.order_matching_fee_sat as u64),
trader_realized_pnl_sat: value.trader_realized_pnl_sat,
is_complete: value.is_complete,
}
}
}
Loading
Loading