-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathschema.sql
More file actions
54 lines (50 loc) · 1.7 KB
/
Copy pathschema.sql
File metadata and controls
54 lines (50 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- polymarket 15m markets schema
-- markets (one per 15-min window per coin)
CREATE TABLE IF NOT EXISTS markets (
id BIGSERIAL PRIMARY KEY,
coin TEXT NOT NULL,
window_ts BIGINT NOT NULL,
slug TEXT NOT NULL,
up_token TEXT NOT NULL,
down_token TEXT NOT NULL,
spot_start DOUBLE PRECISION,
spot_end DOUBLE PRECISION,
outcome TEXT,
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(coin, window_ts)
);
-- snapshots (order book + price at a point in time)
CREATE TABLE IF NOT EXISTS snapshots (
id BIGSERIAL PRIMARY KEY,
ts TIMESTAMPTZ NOT NULL,
market_id BIGINT REFERENCES markets(id) ON DELETE CASCADE,
spot_price DOUBLE PRECISION,
up_bid DOUBLE PRECISION,
up_ask DOUBLE PRECISION,
down_bid DOUBLE PRECISION,
down_ask DOUBLE PRECISION,
up_depth JSONB,
down_depth JSONB
);
-- indexes
CREATE INDEX IF NOT EXISTS idx_snapshots_ts ON snapshots(ts);
CREATE INDEX IF NOT EXISTS idx_snapshots_market ON snapshots(market_id);
CREATE INDEX IF NOT EXISTS idx_markets_window ON markets(window_ts);
CREATE INDEX IF NOT EXISTS idx_markets_coin ON markets(coin);
-- useful views
CREATE OR REPLACE VIEW v_market_summary AS
SELECT
m.coin,
m.window_ts,
m.outcome,
m.spot_start,
m.spot_end,
CASE WHEN m.spot_start > 0
THEN ((m.spot_end - m.spot_start) / m.spot_start * 100)
ELSE NULL END as spot_change_pct,
COUNT(s.id) as snapshot_count,
MIN(s.ts) as first_snapshot,
MAX(s.ts) as last_snapshot
FROM markets m
LEFT JOIN snapshots s ON s.market_id = m.id
GROUP BY m.id;