-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.sql
More file actions
178 lines (168 loc) · 5.35 KB
/
init.sql
File metadata and controls
178 lines (168 loc) · 5.35 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
-- CrossPay Database Schema
-- Payments table
CREATE TABLE IF NOT EXISTS payments (
id SERIAL PRIMARY KEY,
blockchain_id BIGINT NOT NULL,
sender_address VARCHAR(42) NOT NULL,
recipient_address VARCHAR(42) NOT NULL,
sender_ens VARCHAR(255),
recipient_ens VARCHAR(255),
token_address VARCHAR(42) NOT NULL,
amount DECIMAL(36, 18) NOT NULL,
fee DECIMAL(36, 18) NOT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'pending',
tx_hash VARCHAR(66),
chain_id INTEGER NOT NULL,
metadata_uri TEXT,
receipt_cid VARCHAR(100),
oracle_price VARCHAR(50),
random_seed VARCHAR(66),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
completed_at TIMESTAMP,
INDEX idx_sender (sender_address),
INDEX idx_recipient (recipient_address),
INDEX idx_status (status),
INDEX idx_chain (chain_id),
INDEX idx_tx_hash (tx_hash)
);
-- Receipts table
CREATE TABLE IF NOT EXISTS receipts (
id SERIAL PRIMARY KEY,
payment_id BIGINT REFERENCES payments(blockchain_id),
receipt_cid VARCHAR(100) NOT NULL UNIQUE,
metadata_cid VARCHAR(100),
format VARCHAR(10) NOT NULL DEFAULT 'json',
language VARCHAR(5) NOT NULL DEFAULT 'en',
content_hash VARCHAR(66),
signature TEXT,
verified BOOLEAN DEFAULT FALSE,
compliance_fields TEXT,
creator_address VARCHAR(42) NOT NULL,
is_public BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
verified_at TIMESTAMP,
INDEX idx_payment (payment_id),
INDEX idx_creator (creator_address),
INDEX idx_cid (receipt_cid),
INDEX idx_verified (verified)
);
-- Oracle requests table
CREATE TABLE IF NOT EXISTS oracle_requests (
id SERIAL PRIMARY KEY,
request_id VARCHAR(100) NOT NULL UNIQUE,
request_type VARCHAR(20) NOT NULL, -- 'price', 'random', 'proof'
symbol VARCHAR(20),
requester_address VARCHAR(42),
payment_id BIGINT,
status VARCHAR(20) NOT NULL DEFAULT 'pending',
result TEXT,
error_message TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
fulfilled_at TIMESTAMP,
INDEX idx_request_id (request_id),
INDEX idx_type (request_type),
INDEX idx_status (status),
INDEX idx_requester (requester_address)
);
-- ENS cache table
CREATE TABLE IF NOT EXISTS ens_cache (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
address VARCHAR(42) NOT NULL,
avatar_url TEXT,
text_records JSONB,
cached_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
ttl INTEGER DEFAULT 3600,
INDEX idx_name (name),
INDEX idx_address (address),
INDEX idx_cached_at (cached_at)
);
-- Storage operations table
CREATE TABLE IF NOT EXISTS storage_operations (
id SERIAL PRIMARY KEY,
operation_id VARCHAR(100) NOT NULL UNIQUE,
operation_type VARCHAR(20) NOT NULL, -- 'upload', 'retrieve'
file_cid VARCHAR(100),
filename VARCHAR(255),
file_size BIGINT,
content_type VARCHAR(100),
storage_cost DECIMAL(18, 8),
status VARCHAR(20) NOT NULL DEFAULT 'pending',
payment_id BIGINT,
receipt_id BIGINT,
error_message TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
completed_at TIMESTAMP,
INDEX idx_operation_id (operation_id),
INDEX idx_cid (file_cid),
INDEX idx_type (operation_type),
INDEX idx_status (status)
);
-- Analytics aggregations table
CREATE TABLE IF NOT EXISTS analytics_daily (
id SERIAL PRIMARY KEY,
date DATE NOT NULL,
chain_id INTEGER NOT NULL,
payment_count BIGINT DEFAULT 0,
payment_volume DECIMAL(36, 18) DEFAULT 0,
completed_payments BIGINT DEFAULT 0,
receipts_generated BIGINT DEFAULT 0,
receipts_verified BIGINT DEFAULT 0,
oracle_requests BIGINT DEFAULT 0,
ens_resolutions BIGINT DEFAULT 0,
storage_operations BIGINT DEFAULT 0,
unique_senders BIGINT DEFAULT 0,
unique_recipients BIGINT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(date, chain_id),
INDEX idx_date (date),
INDEX idx_chain_date (chain_id, date)
);
-- Service health table
CREATE TABLE IF NOT EXISTS service_health (
id SERIAL PRIMARY KEY,
service_name VARCHAR(50) NOT NULL,
status VARCHAR(20) NOT NULL, -- 'healthy', 'unhealthy', 'degraded'
last_check TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
response_time_ms INTEGER,
error_count INTEGER DEFAULT 0,
metadata JSONB,
INDEX idx_service (service_name),
INDEX idx_status (status),
INDEX idx_last_check (last_check)
);
-- Insert initial data
INSERT INTO service_health (service_name, status) VALUES
('payment-processor', 'healthy'),
('storage-worker', 'healthy'),
('oracle-service', 'healthy'),
('ens-resolver', 'healthy')
ON CONFLICT (service_name) DO NOTHING;
-- Create views for common queries
CREATE OR REPLACE VIEW payment_summary AS
SELECT
p.id,
p.blockchain_id,
p.sender_address,
p.recipient_address,
p.sender_ens,
p.recipient_ens,
p.amount,
p.status,
p.chain_id,
p.created_at,
r.receipt_cid,
r.verified as receipt_verified
FROM payments p
LEFT JOIN receipts r ON p.blockchain_id = r.payment_id;
CREATE OR REPLACE VIEW daily_stats AS
SELECT
date,
SUM(payment_count) as total_payments,
SUM(payment_volume) as total_volume,
SUM(completed_payments) as total_completed,
SUM(receipts_generated) as total_receipts,
SUM(oracle_requests) as total_oracle_requests
FROM analytics_daily
GROUP BY date
ORDER BY date DESC;