Account for fractional outcome sets from split through redemption

This commit is contained in:
2026-12-03 06:53:37 +08:00
parent 7b26767a97
commit 2c4179a64e
17 changed files with 492 additions and 108 deletions
+25
View File
@@ -273,3 +273,28 @@ CREATE TABLE IF NOT EXISTS tick_changes (
UNIQUE(market,at_ms)
);
CREATE INDEX IF NOT EXISTS tick_changes_by_seq ON tick_changes(market,seq);
CREATE TABLE IF NOT EXISTS position_remainders (
account TEXT NOT NULL, market TEXT NOT NULL, outcome TEXT NOT NULL,
micros INTEGER NOT NULL CHECK(micros BETWEEN 0 AND 999999),
PRIMARY KEY(account,market,outcome),
FOREIGN KEY(account,market,outcome) REFERENCES positions(account,market,outcome)
);
CREATE TABLE IF NOT EXISTS collateral_remainders (
market TEXT PRIMARY KEY REFERENCES markets(id), micros INTEGER NOT NULL CHECK(micros BETWEEN 0 AND 9999)
);
CREATE TABLE IF NOT EXISTS settlement_dust (
market TEXT PRIMARY KEY REFERENCES markets(id), micros INTEGER NOT NULL CHECK(micros>0)
);
CREATE TABLE IF NOT EXISTS precise_movements (
request_id TEXT NOT NULL, account TEXT NOT NULL, shares_micros INTEGER NOT NULL, cash_micros INTEGER NOT NULL,
PRIMARY KEY(request_id,account),
FOREIGN KEY(request_id,account) REFERENCES wallet_movements(request_id,account) DEFERRABLE INITIALLY DEFERRED
);
CREATE VIEW IF NOT EXISTS position_units AS
SELECT p.account,p.market,p.outcome,p.available*1000000+COALESCE(r.micros,0) AS available,
p.reserved*1000000 AS reserved,p.phase,p.payout_per_share
FROM position_sizes p LEFT JOIN position_remainders r USING(account,market,outcome);
CREATE VIEW IF NOT EXISTS market_backing AS
SELECT m.id AS market,m.collateral*10000+COALESCE(r.micros,0) AS micros
FROM markets m LEFT JOIN collateral_remainders r ON r.market=m.id;