Skip to content

Commit

Permalink
Merge #970
Browse files Browse the repository at this point in the history
970: Fix outdated schema and correctly handle temporary contract id r=da-kami a=da-kami

Unfortunately #966 merged with an outdated `schema.rs` version in the coordinator. 
This PR fixes this!

Co-authored-by: Daniel Karzel <[email protected]>
  • Loading branch information
bors[bot] and da-kami authored Jul 21, 2023
2 parents 6b05827 + 0520259 commit 8d33367
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
7 changes: 4 additions & 3 deletions coordinator/src/db/positions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct Position {
pub expiry_timestamp: OffsetDateTime,
pub update_timestamp: OffsetDateTime,
pub trader_pubkey: String,
pub temporary_contract_id: String,
pub temporary_contract_id: Option<String>,
pub realized_pnl: Option<i64>,
}

Expand Down Expand Up @@ -158,8 +158,9 @@ impl From<Position> for crate::position::models::Position {
expiry_timestamp: value.expiry_timestamp,
update_timestamp: value.update_timestamp,
trader: value.trader_pubkey.parse().expect("to be valid public key"),
temporary_contract_id: ContractId::from_hex(value.temporary_contract_id.as_str())
.expect("contract id to decode"),
temporary_contract_id: value.temporary_contract_id.map(|contract_id| {
ContractId::from_hex(contract_id.as_str()).expect("contract id to decode")
}),
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions coordinator/src/node/closed_positions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ pub fn sync(node: Node) -> Result<()> {
.context("Failed to load open and closing positions")?;

for position in open_and_closing_positions {
let contract = match node
.inner
.get_closed_contract(position.temporary_contract_id)
{
let temporary_contract_id = match position.temporary_contract_id {
None => {
tracing::trace!(position_id=%position.id, "Position does not have temporary contract id, skipping");
continue;
}
Some(temporary_contract_id) => temporary_contract_id,
};

let contract = match node.inner.get_closed_contract(temporary_contract_id) {
Ok(Some(closed_contract)) => closed_contract,
Ok(None) => {
tracing::trace!(position_id=%position.id, "Position not closed yet, skipping");
Expand All @@ -29,7 +34,7 @@ pub fn sync(node: Node) -> Result<()> {
if let Err(e) =
db::positions::Position::set_position_to_closed(&mut conn, position.id, contract.pnl)
{
tracing::error!(position_id=%position.id, temporary_contract_id=%position.temporary_contract_id.to_hex(), pnl=%contract.pnl, "Failed to set position to closed: {e:#}")
tracing::error!(position_id=%position.id, temporary_contract_id=%temporary_contract_id.to_hex(), pnl=%contract.pnl, "Failed to set position to closed: {e:#}")
}
}

Expand Down
4 changes: 3 additions & 1 deletion coordinator/src/position/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ pub struct Position {
/// We use the temporary contract id because the actual contract id might not be known at that
/// point. The temporary contract id is propagated to all states until the contract is
/// closed.
pub temporary_contract_id: ContractId,
/// This field is optional for backwards compatibility because we cannot deterministically
/// associate already existing contracts with positions.
pub temporary_contract_id: Option<ContractId>,
}
2 changes: 1 addition & 1 deletion coordinator/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ diesel::table! {
expiry_timestamp -> Timestamptz,
update_timestamp -> Timestamptz,
trader_pubkey -> Text,
temporary_contract_id -> Text,
temporary_contract_id -> Nullable<Text>,
realized_pnl -> Nullable<Int8>,
}
}
Expand Down

0 comments on commit 8d33367

Please sign in to comment.