Cancel outstanding orders when their trading key misses its heartbeat

This commit is contained in:
2026-12-03 05:57:49 +08:00
parent d01ab8f15c
commit 2306c6fd96
14 changed files with 296 additions and 9 deletions
+22 -1
View File
@@ -7,6 +7,7 @@ mod catalog;
mod estimate;
mod expiry;
mod fees;
mod heartbeat;
mod holders;
mod twap;
mod validation;
@@ -70,7 +71,27 @@ impl Exchange {
Ok(Self { db, books })
}
pub fn execute(&mut self, payload: Value) -> Result<Value> {
/// Run expiry and synchronize the resident book while the transport is idle.
pub fn maintain(&mut self) -> Result<()> {
let tx = expiry::transaction(&self.db, TransactionBehavior::Deferred)?;
let changes = self.books.changes(&tx)?;
tx.commit()?;
self.books.apply(changes);
Ok(())
}
pub fn execute(&mut self, mut payload: Value) -> Result<Value> {
if payload["op"].as_str() == Some("heartbeat") {
payload
.as_object_mut()
.context("expected object")?
.remove("op");
let beat: heartbeat::Beat = serde_json::from_value(payload)?;
let tx = expiry::transaction(&self.db, TransactionBehavior::Immediate)?;
let response = beat.accept(&tx)?;
tx.commit()?;
return Ok(response);
}
if matches!(
payload["op"].as_str(),
Some(