Match complementary binary orders and settle paired issuance or merge
This commit is contained in:
+85
-16
@@ -92,6 +92,47 @@ pub(crate) fn submit(tx: &Transaction, books: &Books, order: NewOrder) -> Result
|
||||
Ok(json!({"order":seq,"filled":qty-remaining,"status":status}))
|
||||
}
|
||||
|
||||
struct ExecutedLeg {
|
||||
seq: i64,
|
||||
outcome: String,
|
||||
side: String,
|
||||
price: i64,
|
||||
}
|
||||
|
||||
fn book_leg(
|
||||
tx: &Transaction,
|
||||
market: &str,
|
||||
view: &str,
|
||||
fill: &Fill,
|
||||
seq: i64,
|
||||
) -> Result<ExecutedLeg> {
|
||||
let (account, outcome, side, limit): (String, String, String, i64) = tx.query_row(
|
||||
"SELECT account,outcome,side,price FROM orders
|
||||
WHERE seq=? AND market=? AND status='live' AND remaining>=?",
|
||||
params![seq, market, fill.quantity],
|
||||
|row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)),
|
||||
)?;
|
||||
let price = if outcome == view {
|
||||
fill.price
|
||||
} else {
|
||||
100 - fill.price
|
||||
};
|
||||
if side == "buy" {
|
||||
ensure!(price <= limit, "buy limit exceeded");
|
||||
ledger::cash(tx, &account, (limit - price) * fill.quantity)?;
|
||||
ledger::position(tx, &account, market, &outcome, fill.quantity)?;
|
||||
} else {
|
||||
ensure!(price >= limit, "sell limit exceeded");
|
||||
ledger::cash(tx, &account, price * fill.quantity)?;
|
||||
}
|
||||
Ok(ExecutedLeg {
|
||||
seq,
|
||||
outcome,
|
||||
side,
|
||||
price,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn settle(
|
||||
tx: &Transaction,
|
||||
market: &str,
|
||||
@@ -99,22 +140,50 @@ pub(crate) fn settle(
|
||||
fills: Vec<Fill>,
|
||||
) -> Result<()> {
|
||||
for f in fills {
|
||||
let (buyer, limit): (String, i64) = tx.query_row(
|
||||
"SELECT account,price FROM orders WHERE seq=?",
|
||||
[f.bid],
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)?;
|
||||
let seller: String =
|
||||
tx.query_row("SELECT account FROM orders WHERE seq=?", [f.ask], |r| {
|
||||
r.get(0)
|
||||
})?;
|
||||
ledger::cash(tx, &seller, f.price * f.quantity)?;
|
||||
ledger::cash(tx, &buyer, (limit - f.price) * f.quantity)?;
|
||||
ledger::position(tx, &buyer, market, outcome, f.quantity)?;
|
||||
tx.execute(
|
||||
"INSERT INTO fills(bid,ask,price,quantity) VALUES(?,?,?,?)",
|
||||
params![f.bid, f.ask, f.price, f.quantity],
|
||||
)?;
|
||||
let first = book_leg(tx, market, outcome, &f, f.bid)?;
|
||||
let second = book_leg(tx, market, outcome, &f, f.ask)?;
|
||||
if first.outcome == second.outcome {
|
||||
let (buy, sell) = if first.side == "buy" {
|
||||
(first, second)
|
||||
} else {
|
||||
(second, first)
|
||||
};
|
||||
ensure!(buy.side == "buy" && sell.side == "sell", "invalid transfer");
|
||||
tx.execute("INSERT INTO fills(seq,bid,ask,price,quantity) SELECT id,?1,?2,?3,?4 FROM next_execution",
|
||||
params![buy.seq,sell.seq,buy.price,f.quantity])?;
|
||||
} else {
|
||||
ensure!(first.side == second.side, "invalid complementary legs");
|
||||
let mint = first.side == "buy";
|
||||
let delta = if mint {
|
||||
100 * f.quantity
|
||||
} else {
|
||||
-100 * f.quantity
|
||||
};
|
||||
ensure!(
|
||||
tx.execute(
|
||||
"UPDATE markets SET collateral=collateral+?1 WHERE id=?2 AND collateral+?1>=0",
|
||||
params![delta, market]
|
||||
)? == 1,
|
||||
"insufficient collateral"
|
||||
);
|
||||
let (yes, no) = if first.outcome == "YES" {
|
||||
(first, second)
|
||||
} else {
|
||||
(second, first)
|
||||
};
|
||||
tx.execute(
|
||||
"INSERT INTO paired_fills(seq,market,kind,yes_order,no_order,yes_price,quantity)
|
||||
SELECT id,?1,?2,?3,?4,?5,?6 FROM next_execution",
|
||||
params![
|
||||
market,
|
||||
if mint { "mint" } else { "merge" },
|
||||
yes.seq,
|
||||
no.seq,
|
||||
yes.price,
|
||||
f.quantity
|
||||
],
|
||||
)?;
|
||||
}
|
||||
for seq in [f.bid, f.ask] {
|
||||
tx.execute("UPDATE orders SET remaining=remaining-?1,status=CASE WHEN remaining=?1 THEN 'filled' ELSE 'live' END WHERE seq=?2",params![f.quantity,seq])?;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user