-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_schema.sql
More file actions
117 lines (99 loc) · 3.64 KB
/
Copy pathdb_schema.sql
File metadata and controls
117 lines (99 loc) · 3.64 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
-- Create database for Hyperliquid data collection
-- Run with: psql postgres -f db_schema.sql
-- Create database
DROP DATABASE IF EXISTS hyperliquid_data;
CREATE DATABASE hyperliquid_data;
-- Connect to the new database
\c hyperliquid_data
-- Candles table (OHLCV data for different intervals)
CREATE TABLE candles (
id BIGSERIAL PRIMARY KEY,
coin VARCHAR(20) NOT NULL,
interval VARCHAR(10) NOT NULL, -- '5m', '1h', '4h', '1d'
timestamp BIGINT NOT NULL, -- Unix timestamp in milliseconds
open DECIMAL(20, 8) NOT NULL,
high DECIMAL(20, 8) NOT NULL,
low DECIMAL(20, 8) NOT NULL,
close DECIMAL(20, 8) NOT NULL,
volume DECIMAL(30, 8) NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
UNIQUE(coin, interval, timestamp)
);
-- Index for fast queries
CREATE INDEX idx_candles_coin_interval_timestamp ON candles(coin, interval, timestamp DESC);
CREATE INDEX idx_candles_timestamp ON candles(timestamp DESC);
-- Open Interest table
CREATE TABLE open_interest (
id BIGSERIAL PRIMARY KEY,
coin VARCHAR(20) NOT NULL,
timestamp BIGINT NOT NULL,
value DECIMAL(30, 8) NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
UNIQUE(coin, timestamp)
);
CREATE INDEX idx_oi_coin_timestamp ON open_interest(coin, timestamp DESC);
-- Orderbook snapshots (best bid/ask)
CREATE TABLE orderbook (
id BIGSERIAL PRIMARY KEY,
coin VARCHAR(20) NOT NULL,
timestamp BIGINT NOT NULL,
best_bid DECIMAL(20, 8) NOT NULL,
best_ask DECIMAL(20, 8) NOT NULL,
bid_size DECIMAL(30, 8) NOT NULL,
ask_size DECIMAL(30, 8) NOT NULL,
spread_bps DECIMAL(10, 2), -- Spread in basis points
created_at TIMESTAMP DEFAULT NOW(),
UNIQUE(coin, timestamp)
);
CREATE INDEX idx_orderbook_coin_timestamp ON orderbook(coin, timestamp DESC);
-- Funding rates
CREATE TABLE funding_rates (
id BIGSERIAL PRIMARY KEY,
coin VARCHAR(20) NOT NULL,
timestamp BIGINT NOT NULL,
rate DECIMAL(10, 8) NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
UNIQUE(coin, timestamp)
);
CREATE INDEX idx_funding_coin_timestamp ON funding_rates(coin, timestamp DESC);
-- Collector health/heartbeat table
CREATE TABLE collector_health (
id BIGSERIAL PRIMARY KEY,
service_name VARCHAR(50) NOT NULL,
last_heartbeat BIGINT NOT NULL,
status VARCHAR(20) NOT NULL, -- 'running', 'error', 'stopped'
message TEXT,
created_at TIMESTAMP DEFAULT NOW()
);
CREATE INDEX idx_health_service_heartbeat ON collector_health(service_name, last_heartbeat DESC);
-- Tokens metadata table
CREATE TABLE tokens (
id SERIAL PRIMARY KEY,
coin VARCHAR(20) NOT NULL UNIQUE,
name VARCHAR(100),
added_at TIMESTAMP DEFAULT NOW(),
is_active BOOLEAN DEFAULT true
);
-- Insert initial tokens (will be populated from API)
INSERT INTO tokens (coin, is_active) VALUES ('BTC', true), ('ETH', true), ('SOL', true)
ON CONFLICT (coin) DO NOTHING;
-- Create a view for latest orderbook data
CREATE VIEW latest_orderbook AS
SELECT DISTINCT ON (coin)
coin, timestamp, best_bid, best_ask, bid_size, ask_size, spread_bps
FROM orderbook
ORDER BY coin, timestamp DESC;
-- Create a view for latest OI
CREATE VIEW latest_oi AS
SELECT DISTINCT ON (coin)
coin, timestamp, value
FROM open_interest
ORDER BY coin, timestamp DESC;
-- Grant permissions (optional, for security)
-- CREATE USER collector_user WITH PASSWORD 'your_password';
-- GRANT CONNECT ON DATABASE hyperliquid_data TO collector_user;
-- GRANT SELECT, INSERT ON ALL TABLES IN SCHEMA public TO collector_user;
-- GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO collector_user;
-- Show summary
SELECT 'Database setup complete!' as status;
SELECT tablename FROM pg_tables WHERE schemaname = 'public';