Match complementary binary orders and settle paired issuance or merge

This commit is contained in:
2026-12-03 03:49:42 +08:00
parent 4eb80fa458
commit 47d49cb8c9
10 changed files with 238 additions and 38 deletions
+11 -5
View File
@@ -6,7 +6,7 @@ use crate::{
validation::{amount, identifier, phase},
};
use anyhow::{ensure, Result};
use rusqlite::{params, Transaction};
use rusqlite::{params, OptionalExtension, Transaction};
use serde_json::{json, Value};
pub(crate) fn apply(tx: &Transaction, books: &Books, command: Command) -> Result<Value> {
@@ -133,10 +133,16 @@ pub(crate) fn apply(tx: &Transaction, books: &Books, command: Command) -> Result
}
Command::Open { market } => {
phase(tx, &market, &["preopen"])?;
for outcome in ["YES", "NO"] {
if let Some(book) = books.get(&market, outcome) {
book::settle(tx, &market, outcome, book.opening())?;
}
// Run one auction. Using the earliest order's outcome as the quote unit
// preserves the existing low-tick tie break for a one-outcome auction.
let first:Option<String>=tx.query_row(
"SELECT outcome FROM orders WHERE market=? AND status='live' ORDER BY seq LIMIT 1",
[&market],|r|r.get(0)).optional()?;
if let Some(outcome) = first {
let book = books
.get(&market, &outcome)
.ok_or_else(|| anyhow::anyhow!("missing auction book"))?;
book::settle(tx, &market, &outcome, book.opening())?;
}
tx.execute("UPDATE markets SET phase='continuous' WHERE id=?", [market])?;
Ok(json!({"phase":"continuous"}))