Retain submission time and instructions for live and terminal orders
This commit is contained in:
@@ -46,7 +46,9 @@ impl Filter {
|
||||
(SELECT expiration FROM order_deadlines d WHERE d.seq=orders.seq),
|
||||
b.amount_cents,COALESCE((SELECT micros FROM budget_spend_micros WHERE order_seq=orders.seq),b.spent_cents*10000),
|
||||
(SELECT COALESCE(SUM(fee_micros),0) FROM execution_fees WHERE taker_order=orders.seq),
|
||||
(SELECT key_id FROM order_keys WHERE order_seq=orders.seq) FROM orders LEFT JOIN budget_buys b ON b.order_seq=orders.seq
|
||||
(SELECT key_id FROM order_keys WHERE order_seq=orders.seq),s.order_type,s.post_only,s.created_at
|
||||
FROM orders LEFT JOIN budget_buys b ON b.order_seq=orders.seq
|
||||
LEFT JOIN order_submissions s ON s.order_seq=orders.seq
|
||||
WHERE account=?1 AND (?2 IS NULL OR market=?2) AND (?3 IS NULL OR outcome=?3)
|
||||
AND seq>?4 AND {scope} ORDER BY seq LIMIT ?5"
|
||||
);
|
||||
@@ -79,6 +81,8 @@ impl Filter {
|
||||
"price_micros":row.get::<_,i64>(4)?*100,"original_size":crate::lots::display(quantity),
|
||||
"size_matched":crate::lots::display(quantity-remaining),"open_size":crate::lots::display(if status=="live"{remaining}else{0}),
|
||||
"status":status,"expiration":row.get::<_,Option<i64>>(8)?,
|
||||
"order_type":row.get::<_,Option<String>>(13)?,"post_only":row.get::<_,Option<bool>>(14)?,
|
||||
"created_at":row.get::<_,Option<i64>>(15)?,
|
||||
"amount_cents":row.get::<_,Option<i64>>(9)?,"spent_cents":row.get::<_,Option<i64>>(10)?.map(|v|crate::inventory::decimal(v.into(),10000)),"fee_micros":row.get::<_,i64>(11)?,"key_id":row.get::<_,Option<String>>(12)?}),
|
||||
);
|
||||
last_id = Some(id);
|
||||
|
||||
@@ -42,6 +42,27 @@ pub(crate) fn gross_micros(fills: &[Fill]) -> i64 {
|
||||
fills.iter().map(|f| f.price * f.quantity).sum()
|
||||
}
|
||||
|
||||
fn record_submission(
|
||||
tx: &Transaction,
|
||||
seq: i64,
|
||||
tif: TimeInForce,
|
||||
post_only: bool,
|
||||
key: Option<&str>,
|
||||
) -> Result<()> {
|
||||
let order_type = match tif {
|
||||
TimeInForce::Gtc => "GTC",
|
||||
TimeInForce::Gtd => "GTD",
|
||||
TimeInForce::Ioc => "IOC",
|
||||
TimeInForce::Fok => "FOK",
|
||||
TimeInForce::Fak => "FAK",
|
||||
};
|
||||
tx.execute(
|
||||
"INSERT INTO order_submissions(order_seq,order_type,post_only) VALUES(?,?,?)",
|
||||
params![seq, order_type, post_only],
|
||||
)?;
|
||||
crate::heartbeat::record_key(tx, seq, key)
|
||||
}
|
||||
|
||||
pub(crate) fn submit(tx: &Transaction, books: &Books, order: NewOrder) -> Result<ShareExecution> {
|
||||
let NewOrder {
|
||||
market,
|
||||
@@ -107,7 +128,7 @@ pub(crate) fn submit(tx: &Transaction, books: &Books, order: NewOrder) -> Result
|
||||
}
|
||||
tx.execute("INSERT INTO orders(account,market,outcome,side,price,quantity,remaining,status) VALUES(?,?,?,?,?,?,?,'live')",params![account,market,outcome,side,price,qty,qty])?;
|
||||
let seq = tx.last_insert_rowid();
|
||||
crate::heartbeat::record_key(tx, seq, key_id.as_deref())?;
|
||||
record_submission(tx, seq, tif, post_only, key_id.as_deref())?;
|
||||
if let Some(deadline) = deadline {
|
||||
tx.execute(
|
||||
"INSERT INTO order_deadlines(seq,expiration) VALUES(?,?)",
|
||||
@@ -216,7 +237,7 @@ pub(crate) fn market_buy(tx: &Transaction, books: &Books, order: BudgetBuy) -> R
|
||||
params![account, market, outcome, max_price],
|
||||
)?;
|
||||
let seq = tx.last_insert_rowid();
|
||||
crate::heartbeat::record_key(tx, seq, key_id.as_deref())?;
|
||||
record_submission(tx, seq, tif, false, key_id.as_deref())?;
|
||||
let mut fills = books
|
||||
.get(&market, &outcome)
|
||||
.map(|b| b.match_budget_buy(seq, max_price, amount_cents * 10000))
|
||||
|
||||
@@ -84,6 +84,13 @@ impl Exchange {
|
||||
db.busy_timeout(Duration::from_secs(10))?;
|
||||
db.execute_batch("PRAGMA foreign_keys=ON")?;
|
||||
db.execute_batch(include_str!("schema.sql"))?;
|
||||
let submissions_ready: bool = db.query_row(
|
||||
"SELECT EXISTS(SELECT 1 FROM sqlite_schema WHERE type='table' AND name='order_submissions')",
|
||||
[], |r| r.get(0),
|
||||
)?;
|
||||
if !submissions_ready {
|
||||
db.execute_batch(include_str!("order_submission_schema.sql"))?;
|
||||
}
|
||||
let directory_ready: bool = db.query_row(
|
||||
"SELECT EXISTS(SELECT 1 FROM sqlite_schema WHERE type='table' AND name='event_directory_ids')",
|
||||
[], |row| row.get(0),
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
BEGIN IMMEDIATE;
|
||||
CREATE TABLE IF NOT EXISTS order_submissions (
|
||||
order_seq INTEGER PRIMARY KEY REFERENCES orders(seq),
|
||||
order_type TEXT NOT NULL,
|
||||
post_only INTEGER NOT NULL CHECK(post_only IN (0,1)),
|
||||
created_at INTEGER DEFAULT (unixepoch())
|
||||
);
|
||||
|
||||
-- Old receipts preserve the accepted instruction but do not record when it was submitted.
|
||||
INSERT INTO order_submissions(order_seq,order_type,post_only,created_at)
|
||||
SELECT o.seq, COALESCE(json_extract(r.payload,'$.tif'),
|
||||
CASE WHEN json_extract(r.payload,'$.op') IN ('market_buy','market_sell') THEN 'FAK'
|
||||
WHEN d.seq IS NOT NULL THEN 'GTD' ELSE 'GTC' END),
|
||||
COALESCE(json_extract(r.payload,'$.post_only'),0), NULL
|
||||
FROM requests r JOIN orders o ON o.seq=json_extract(r.response,'$.order')
|
||||
LEFT JOIN order_deadlines d ON d.seq=o.seq
|
||||
WHERE json_extract(r.payload,'$.op') IN ('order','market_buy','market_sell')
|
||||
AND json_type(r.response,'$.order')='integer'
|
||||
ON CONFLICT(order_seq) DO NOTHING;
|
||||
COMMIT;
|
||||
Reference in New Issue
Block a user