Port the simulator to a Rust workspace and document each crate
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
use anyhow::{ensure, Result};
|
||||
use rusqlite::{params, Transaction};
|
||||
|
||||
pub(crate) fn cash(tx: &Transaction, account: &str, delta: i64) -> Result<()> {
|
||||
ensure!(
|
||||
tx.execute(
|
||||
"UPDATE accounts SET cash=cash+? WHERE id=? AND cash+?>=0",
|
||||
params![delta, account, delta]
|
||||
)? == 1,
|
||||
"unknown account or insufficient cash"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn position(
|
||||
tx: &Transaction,
|
||||
account: &str,
|
||||
market: &str,
|
||||
outcome: &str,
|
||||
delta: i64,
|
||||
) -> Result<()> {
|
||||
tx.execute(
|
||||
"INSERT OR IGNORE INTO positions VALUES(?,?,?,0)",
|
||||
params![account, market, outcome],
|
||||
)?;
|
||||
ensure!(tx.execute("UPDATE positions SET quantity=quantity+? WHERE account=? AND market=? AND outcome=? AND quantity+?>=0",params![delta,account,market,outcome,delta])?==1,"insufficient position");
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user