Files
eventbook-exchange/crates/exchange/src/order_submission_schema.sql
T

21 lines
972 B
SQL

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;