Port the simulator to a Rust workspace and document each crate

This commit is contained in:
2026-12-03 02:53:46 +08:00
parent b7579603f9
commit 2f3c699e9c
25 changed files with 1320 additions and 371 deletions
+123
View File
@@ -0,0 +1,123 @@
use crate::{
book,
command::Command,
ledger,
validation::{amount, identifier, phase},
};
use anyhow::{ensure, Result};
use matching::Phase;
use rusqlite::{params, Transaction};
use serde_json::{json, Value};
pub(crate) fn apply(tx: &Transaction, command: Command) -> Result<Value> {
match command {
Command::Fund { account, cents } => {
identifier(&account)?;
amount(cents)?;
tx.execute("INSERT INTO accounts VALUES(?,?)", params![account, cents])?;
Ok(json!({"funded":cents}))
}
Command::List { market, rules } => {
identifier(&market)?;
ensure!(!rules.trim().is_empty(), "rules required");
tx.execute(
"INSERT INTO markets(id,rules,phase) VALUES(?,?,'preopen')",
params![market, rules],
)?;
Ok(json!({"phase":"preopen"}))
}
Command::Split {
market,
account,
qty,
} => {
identifier(&account)?;
amount(qty)?;
phase(tx, &market, &["preopen", "continuous"])?;
ledger::cash(tx, &account, -100 * qty)?;
tx.execute(
"UPDATE markets SET collateral=collateral+? WHERE id=?",
params![100 * qty, market],
)?;
for side in ["YES", "NO"] {
ledger::position(tx, &account, &market, side, qty)?;
}
Ok(json!({"pairs":qty}))
}
Command::Order {
market,
account,
outcome,
side,
price,
qty,
tif,
} => book::submit(
tx,
book::NewOrder {
market,
account,
outcome,
side,
price,
qty,
tif,
},
),
Command::Cancel { order, account } => {
identifier(&account)?;
let qty = book::release(tx, order, &account)?;
Ok(json!({"cancelled":qty}))
}
Command::Open { market } => {
phase(tx, &market, &["preopen"])?;
for outcome in ["YES", "NO"] {
book::match_book(tx, &market, outcome, Phase::Opening)?;
}
tx.execute("UPDATE markets SET phase='continuous' WHERE id=?", [market])?;
Ok(json!({"phase":"continuous"}))
}
Command::Close { market } => {
phase(tx, &market, &["preopen", "continuous"])?;
let mut stmt =
tx.prepare("SELECT seq,account FROM orders WHERE market=? AND status='live'")?;
let orders = stmt
.query_map([&market], |r| {
Ok((r.get::<_, i64>(0)?, r.get::<_, String>(1)?))
})?
.collect::<rusqlite::Result<Vec<_>>>()?;
for (seq, account) in orders {
book::release(tx, seq, &account)?;
}
tx.execute("UPDATE markets SET phase='closed' WHERE id=?", [market])?;
Ok(json!({"phase":"closed"}))
}
Command::Resolve { market, winner } => {
phase(tx, &market, &["closed"])?;
ensure!(
matches!(winner.as_str(), "YES" | "NO" | "VOID"),
"invalid winner"
);
let mut stmt =
tx.prepare("SELECT account,outcome,quantity FROM positions WHERE market=?")?;
let mut rows = stmt.query([&market])?;
let mut total = 0i64;
while let Some(r) = rows.next()? {
let outcome: String = r.get(1)?;
let cents = if winner == "VOID" {
50
} else if outcome == winner {
100
} else {
0
};
let payout = cents * r.get::<_, i64>(2)?;
ledger::cash(tx, &r.get::<_, String>(0)?, payout)?;
total += payout;
}
ensure!(tx.execute("UPDATE markets SET phase='resolved',collateral=0,winner=? WHERE id=? AND collateral=?",params![winner,market,total])?==1,"collateral mismatch");
tx.execute("UPDATE positions SET quantity=0 WHERE market=?", [market])?;
Ok(json!({"phase":"resolved","paid":total}))
}
}
}