-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Periodically update
positions
to Closed
based on DLC
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use crate::db; | ||
use crate::node::Node; | ||
use anyhow::Context; | ||
use anyhow::Result; | ||
use bitcoin::hashes::hex::ToHex; | ||
|
||
pub fn sync(node: Node) -> Result<()> { | ||
let mut conn = node.pool.get()?; | ||
let open_and_closing_positions = | ||
db::positions::Position::get_all_open_or_closing_positions(&mut conn) | ||
.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) | ||
{ | ||
Ok(Some(closed_contract)) => closed_contract, | ||
Ok(None) => { | ||
tracing::trace!(position_id=%position.id, "Position not closed yet, skipping"); | ||
continue; | ||
} | ||
Err(e) => { | ||
tracing::error!(position_id=%position.id, "Failed to get closed contract from DLC manager storage: {e:#}"); | ||
continue; | ||
} | ||
}; | ||
|
||
if let Err(e) = db::positions::Position::set_position_to_closed( | ||
&mut conn, | ||
position.trader, | ||
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:#}") | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |