Repository navigation
Expand file tree
/
Copy pathsupabase-schema.sql
More file actions
655 lines (549 loc) · 29.5 KB
/
Copy pathsupabase-schema.sql
File metadata and controls
655 lines (549 loc) · 29.5 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
-- =============================================================================
-- Zyfi DeFi Yield Optimization Platform — Supabase Schema
-- Target: Base mainnet (chain ID 8453)
-- Convention: ALL addresses stored lowercase, amounts in raw units (wei/smallest unit)
-- =============================================================================
-- Enable required extensions
CREATE EXTENSION IF NOT EXISTS "pgcrypto"; -- gen_random_uuid()
CREATE EXTENSION IF NOT EXISTS "pg_trgm"; -- text search optimization (optional)
-- =============================================================================
-- 1. TOKENS — Registry of supported tokens with decimal info
-- =============================================================================
CREATE TABLE tokens (
address TEXT PRIMARY KEY CHECK (address = lower(address)),
symbol TEXT NOT NULL,
name TEXT NOT NULL,
decimals SMALLINT NOT NULL CHECK (decimals >= 0 AND decimals <= 18),
chain_id INTEGER NOT NULL DEFAULT 8453,
is_collateral BOOLEAN NOT NULL DEFAULT false, -- can be used as collateral
is_borrowable BOOLEAN NOT NULL DEFAULT false, -- can be borrowed
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
COMMENT ON TABLE tokens IS 'Supported tokens with decimal and capability info';
COMMENT ON COLUMN tokens.address IS 'Token contract address (lowercase)';
COMMENT ON COLUMN tokens.decimals IS 'Token decimals (WETH=18, USDC=6)';
-- Seed Base mainnet tokens
INSERT INTO tokens (address, symbol, name, decimals, is_collateral, is_borrowable) VALUES
('0x4200000000000000000000000000000000000006', 'WETH', 'Wrapped Ether', 18, true, false),
('0x833589fcd6edb6e08f4c7c32d4f71b54bda02913', 'USDC', 'USD Coin', 6, false, true),
('0x2ae3f1ec7f1f5012cfeab0185bfc7aa3cf0dec22', 'cbETH', 'Coinbase ETH', 18, true, false),
('0xc1cba3fcea344f92d9239c08c0568f6f2f0ee452', 'wstETH','Wrapped stETH', 18, true, false),
('0x50c5725949a6f0c72e6c4a641f24049a917db0cb', 'DAI', 'Dai Stablecoin', 18, false, true),
('0xd9aaec86b65d86f6a7b5b1b0c42ffa531710b6ca', 'USDbC', 'USD Base Coin', 6, false, true)
ON CONFLICT (address) DO NOTHING;
-- =============================================================================
-- 2. PROTOCOLS — Supported lending protocols
-- =============================================================================
CREATE TABLE protocols (
id TEXT PRIMARY KEY, -- 'aave_v3', 'morpho_blue'
name TEXT NOT NULL,
pool_address TEXT NOT NULL CHECK (pool_address = lower(pool_address)),
flashloan_fee NUMERIC NOT NULL DEFAULT 0, -- 0 for Morpho, 0.0005 for Aave (0.05%)
chain_id INTEGER NOT NULL DEFAULT 8453,
is_active BOOLEAN NOT NULL DEFAULT true,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
COMMENT ON TABLE protocols IS 'Supported lending protocols';
COMMENT ON COLUMN protocols.flashloan_fee IS 'Flashloan fee as decimal (0.0005 = 0.05%)';
INSERT INTO protocols (id, name, pool_address, flashloan_fee) VALUES
('aave_v3', 'Aave V3', '0xa238dd80c259a72e81d7e4664a9801593f98d1c5', 0.0005),
('morpho_blue', 'Morpho Blue', '0xbbbbbbbbbb9cc5e90e3b3af64bdaf62c37eeffcb', 0)
ON CONFLICT (id) DO NOTHING;
-- =============================================================================
-- 3. USERS — Authenticated users (EOA -> Safe mapping)
-- =============================================================================
CREATE TABLE users (
address TEXT PRIMARY KEY CHECK (address = lower(address)), -- EOA (MetaMask)
safe_address TEXT UNIQUE CHECK (safe_address = lower(safe_address)),
onboarding_step SMALLINT NOT NULL DEFAULT 0 CHECK (onboarding_step BETWEEN 0 AND 3),
-- 0 = new user (just authenticated)
-- 1 = Safe deployed
-- 2 = Modules installed (GuardedExecModule + SmartSessions + UnifiedFlashloanModule)
-- 3 = Session key created (fully onboarded)
is_active BOOLEAN NOT NULL DEFAULT true,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
COMMENT ON TABLE users IS 'Authenticated users with Safe smart wallet mapping';
COMMENT ON COLUMN users.address IS 'User EOA address from MetaMask (lowercase)';
COMMENT ON COLUMN users.safe_address IS 'Deployed Safe smart wallet address (lowercase)';
COMMENT ON COLUMN users.onboarding_step IS '0=new, 1=safe deployed, 2=modules installed, 3=session key created';
CREATE INDEX idx_users_safe ON users(safe_address) WHERE safe_address IS NOT NULL;
CREATE INDEX idx_users_onboarding ON users(onboarding_step);
-- =============================================================================
-- 4. SESSION_KEYS — Encrypted session keys (separate table for security)
-- =============================================================================
CREATE TABLE session_keys (
user_address TEXT PRIMARY KEY REFERENCES users(address) ON DELETE CASCADE
CHECK (user_address = lower(user_address)),
key_address TEXT NOT NULL CHECK (key_address = lower(key_address)), -- Public address of session key
encrypted_key JSONB NOT NULL, -- { iv, authTag, ciphertext } (AES-256-GCM)
is_active BOOLEAN NOT NULL DEFAULT true,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
rotated_at TIMESTAMPTZ -- NULL until first rotation
);
COMMENT ON TABLE session_keys IS 'AES-256-GCM encrypted session keys. NEVER store plaintext.';
COMMENT ON COLUMN session_keys.encrypted_key IS 'JSON: { iv: hex, authTag: hex, ciphertext: hex }. Decrypted only in backend memory.';
COMMENT ON COLUMN session_keys.key_address IS 'Derived public address of session key (for on-chain reference)';
-- =============================================================================
-- 5. POSITIONS — Active collateral/debt positions
-- =============================================================================
CREATE TABLE positions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_address TEXT NOT NULL REFERENCES users(address)
CHECK (user_address = lower(user_address)),
safe_address TEXT NOT NULL CHECK (safe_address = lower(safe_address)),
-- Protocol info
current_protocol TEXT NOT NULL REFERENCES protocols(id), -- 'aave_v3' or 'morpho_blue'
-- Collateral side
collateral_token TEXT NOT NULL REFERENCES tokens(address), -- e.g., WETH
collateral_amount NUMERIC NOT NULL DEFAULT 0, -- Raw units (wei for WETH = 18 decimals)
-- Debt side
debt_token TEXT NOT NULL REFERENCES tokens(address), -- e.g., USDC
debt_amount NUMERIC NOT NULL DEFAULT 0, -- Raw units (6 decimals for USDC)
-- Health & monitoring
health_factor NUMERIC, -- Last computed HF (e.g., 1.85)
liquidation_threshold NUMERIC, -- Protocol LT (e.g., 0.83 for Aave WETH)
supply_apy NUMERIC, -- Current supply APY at last check
borrow_apy NUMERIC, -- Current borrow APY at last check
net_apy NUMERIC, -- supply_apy - borrow_apy
-- Status
status TEXT NOT NULL DEFAULT 'active'
CHECK (status IN ('active', 'migrating', 'closing', 'closed')),
migration_count INTEGER NOT NULL DEFAULT 0, -- Times this position was migrated
last_checked_at TIMESTAMPTZ, -- Last health check timestamp
next_check_at TIMESTAMPTZ DEFAULT now(), -- When daemon should check next (tiered)
-- Morpho-specific (NULL for Aave positions)
morpho_market_id TEXT, -- Morpho market hash
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
COMMENT ON TABLE positions IS 'Active user positions across protocols';
COMMENT ON COLUMN positions.collateral_amount IS 'Stored in raw token units (wei). Use tokens.decimals to convert.';
COMMENT ON COLUMN positions.debt_amount IS 'Stored in raw token units. USDC = 6 decimals, so 1000 USDC = 1000000000.';
COMMENT ON COLUMN positions.health_factor IS 'collateralValue * liquidationThreshold / debtValue. < 1.0 = liquidatable.';
COMMENT ON COLUMN positions.next_check_at IS 'Tiered monitoring: HF<1.3=15s, 1.3-1.5=30s, 1.5-2.0=2min, >2.0=10min.';
-- Indexes for daemon queries
CREATE INDEX idx_positions_user ON positions(user_address);
CREATE INDEX idx_positions_status ON positions(status) WHERE status = 'active';
CREATE INDEX idx_positions_next_check ON positions(next_check_at ASC) WHERE status = 'active';
CREATE INDEX idx_positions_health ON positions(health_factor ASC) WHERE status = 'active';
CREATE INDEX idx_positions_protocol ON positions(current_protocol) WHERE status = 'active';
CREATE INDEX idx_positions_safe ON positions(safe_address);
-- =============================================================================
-- 6. TRANSACTION_LOGS — Comprehensive log of ALL operations
-- =============================================================================
CREATE TABLE transaction_logs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_address TEXT NOT NULL REFERENCES users(address)
CHECK (user_address = lower(user_address)),
position_id UUID REFERENCES positions(id), -- NULL for onboarding txs
safe_address TEXT CHECK (safe_address = lower(safe_address)),
-- Transaction type
tx_type TEXT NOT NULL CHECK (tx_type IN (
'deposit', -- Supply collateral to protocol
'withdraw', -- Withdraw collateral
'borrow', -- Borrow asset
'repay', -- Repay debt
'flashloan_migrate', -- Atomic collateral swap between protocols
'liquidation_protect', -- Emergency position adjustment
'safe_deploy', -- Safe smart wallet deployment
'module_install', -- Module installation
'session_key_create', -- Session key creation
'approve', -- Token approval
'transfer' -- Token transfer (e.g., USDC to EOA after borrow)
)),
-- Protocol info
protocol TEXT REFERENCES protocols(id), -- Which protocol (NULL for non-protocol txs)
-- Token info
token_address TEXT REFERENCES tokens(address) -- Which token
CHECK (token_address = lower(token_address)),
-- Amounts (raw units — use tokens.decimals to convert for display)
amount NUMERIC, -- Primary amount in raw token units
amount_usd NUMERIC, -- USD value at time of tx (optional)
-- On-chain reference
tx_hash TEXT, -- On-chain transaction hash
user_op_hash TEXT, -- ERC-4337 UserOp hash
block_number BIGINT,
-- Status
status TEXT NOT NULL DEFAULT 'pending'
CHECK (status IN ('pending', 'submitted', 'confirmed', 'failed', 'reverted')),
error_message TEXT, -- Error details if failed
-- Migration-specific metadata
metadata JSONB DEFAULT '{}',
-- For flashloan_migrate:
-- { "from_protocol": "aave_v3", "to_protocol": "morpho_blue",
-- "flashloan_provider": "morpho_blue", "flashloan_fee": "0",
-- "collateral_amount": "1000000000000000000",
-- "debt_amount": "1000000000",
-- "rate_diff_bps": 75 }
--
-- For borrow:
-- { "recipient_eoa": "0x...", "interest_rate_mode": 2 }
--
-- For deposit:
-- { "detected_via": "listener" | "api_call" }
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
COMMENT ON TABLE transaction_logs IS 'Immutable log of every operation. Never update, only insert.';
COMMENT ON COLUMN transaction_logs.amount IS 'Raw token units. Join tokens table for decimals. WETH: 1e18 = 1 WETH. USDC: 1e6 = 1 USDC.';
COMMENT ON COLUMN transaction_logs.metadata IS 'Structured extra data per tx_type. See schema comments for shapes.';
-- Indexes for querying
CREATE INDEX idx_logs_user ON transaction_logs(user_address);
CREATE INDEX idx_logs_user_type ON transaction_logs(user_address, tx_type);
CREATE INDEX idx_logs_user_created ON transaction_logs(user_address, created_at DESC);
CREATE INDEX idx_logs_position ON transaction_logs(position_id) WHERE position_id IS NOT NULL;
CREATE INDEX idx_logs_tx_hash ON transaction_logs(tx_hash) WHERE tx_hash IS NOT NULL;
CREATE INDEX idx_logs_created ON transaction_logs(created_at DESC);
CREATE INDEX idx_logs_status ON transaction_logs(status) WHERE status IN ('pending', 'submitted');
CREATE INDEX idx_logs_type ON transaction_logs(tx_type);
-- =============================================================================
-- 7. RATE_SNAPSHOTS — Historical rate data for decision making
-- =============================================================================
CREATE TABLE rate_snapshots (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
protocol TEXT NOT NULL REFERENCES protocols(id),
token_address TEXT NOT NULL REFERENCES tokens(address)
CHECK (token_address = lower(token_address)),
supply_apy NUMERIC NOT NULL, -- Annual percentage yield (decimal: 0.035 = 3.5%)
borrow_apy NUMERIC NOT NULL, -- Annual borrow rate (decimal: 0.042 = 4.2%)
collateral_factor NUMERIC, -- Protocol collateral factor (0.83 = 83%)
liquidity NUMERIC, -- Available liquidity in raw units
utilization NUMERIC, -- Pool utilization rate (0-1)
snapshot_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
COMMENT ON TABLE rate_snapshots IS 'Periodic rate snapshots for trend analysis and migration decisions.';
COMMENT ON COLUMN rate_snapshots.supply_apy IS 'Decimal form: 0.035 = 3.5% APY';
CREATE INDEX idx_rates_protocol_token ON rate_snapshots(protocol, token_address, snapshot_at DESC);
CREATE INDEX idx_rates_snapshot_at ON rate_snapshots(snapshot_at DESC);
-- Partition-friendly: delete old snapshots periodically
-- Retention policy: keep 30 days of rate data
-- Run weekly: DELETE FROM rate_snapshots WHERE snapshot_at < now() - interval '30 days';
-- =============================================================================
-- 8. MIGRATION_HISTORY — Dedicated table for migration analytics
-- =============================================================================
CREATE TABLE migration_history (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
position_id UUID NOT NULL REFERENCES positions(id),
user_address TEXT NOT NULL REFERENCES users(address)
CHECK (user_address = lower(user_address)),
from_protocol TEXT NOT NULL REFERENCES protocols(id),
to_protocol TEXT NOT NULL REFERENCES protocols(id),
-- Amounts at time of migration (raw units)
collateral_token TEXT NOT NULL REFERENCES tokens(address),
collateral_amount NUMERIC NOT NULL,
debt_token TEXT NOT NULL REFERENCES tokens(address),
debt_amount NUMERIC NOT NULL,
-- Flashloan details
flashloan_provider TEXT NOT NULL, -- 'morpho_blue' or 'aave_v3'
flashloan_token TEXT NOT NULL REFERENCES tokens(address),
flashloan_amount NUMERIC NOT NULL,
flashloan_fee NUMERIC NOT NULL DEFAULT 0, -- Fee paid in raw units
-- Rate justification
old_supply_apy NUMERIC,
new_supply_apy NUMERIC,
old_borrow_apy NUMERIC,
new_borrow_apy NUMERIC,
rate_diff_bps INTEGER, -- Basis points improvement
-- Execution
tx_hash TEXT,
gas_used BIGINT,
gas_cost_usd NUMERIC,
execution_time_ms INTEGER, -- How long the migration took
-- Result
status TEXT NOT NULL DEFAULT 'completed'
CHECK (status IN ('completed', 'failed', 'reverted')),
error_message TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
COMMENT ON TABLE migration_history IS 'Detailed record of every position migration for analytics and auditing.';
CREATE INDEX idx_migration_position ON migration_history(position_id);
CREATE INDEX idx_migration_user ON migration_history(user_address);
CREATE INDEX idx_migration_created ON migration_history(created_at DESC);
-- =============================================================================
-- 9. UPDATED_AT TRIGGER — Auto-update timestamps
-- =============================================================================
CREATE OR REPLACE FUNCTION update_updated_at()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER trg_users_updated_at
BEFORE UPDATE ON users
FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER trg_positions_updated_at
BEFORE UPDATE ON positions
FOR EACH ROW EXECUTE FUNCTION update_updated_at();
-- =============================================================================
-- 10. ROW LEVEL SECURITY (RLS) — Defense in depth
-- =============================================================================
--
-- HOW IT WORKS:
-- - Backend uses SUPABASE_SERVICE_KEY (service_role) which BYPASSES RLS.
-- This is the only role that should touch session_keys or write data.
-- - If frontend ever connects directly to Supabase (realtime, direct queries),
-- it uses the anon key + JWT. These policies restrict what it can access.
-- - Supabase sets auth.uid() from the JWT sub claim. We store the user's
-- lowercase EOA address as their Supabase auth UID during JWT creation.
--
-- ROLES:
-- service_role → full access (bypasses RLS) — used by Express backend
-- authenticated → scoped to own data via auth.uid() — used by frontend direct queries
-- anon → read-only on public tables (tokens, protocols, rates)
-- -----------------------------------------------------------------------------
-- 10.1 ENABLE RLS ON ALL TABLES
-- -----------------------------------------------------------------------------
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
ALTER TABLE session_keys ENABLE ROW LEVEL SECURITY;
ALTER TABLE positions ENABLE ROW LEVEL SECURITY;
ALTER TABLE transaction_logs ENABLE ROW LEVEL SECURITY;
ALTER TABLE migration_history ENABLE ROW LEVEL SECURITY;
ALTER TABLE tokens ENABLE ROW LEVEL SECURITY;
ALTER TABLE protocols ENABLE ROW LEVEL SECURITY;
ALTER TABLE rate_snapshots ENABLE ROW LEVEL SECURITY;
-- -----------------------------------------------------------------------------
-- 10.2 SERVICE ROLE — Full access (backend)
-- service_role bypasses RLS by default in Supabase, no policies needed.
-- These explicit policies ensure the backend can always operate even if
-- Supabase changes defaults.
-- -----------------------------------------------------------------------------
CREATE POLICY service_all_users ON users
FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_all_session_keys ON session_keys
FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_all_positions ON positions
FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_all_transaction_logs ON transaction_logs
FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_all_migration_history ON migration_history
FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_all_tokens ON tokens
FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_all_protocols ON protocols
FOR ALL TO service_role USING (true) WITH CHECK (true);
CREATE POLICY service_all_rate_snapshots ON rate_snapshots
FOR ALL TO service_role USING (true) WITH CHECK (true);
-- -----------------------------------------------------------------------------
-- 10.3 TOKENS & PROTOCOLS — Public read-only (reference data)
-- Anyone can read token/protocol info. Only service_role can insert/update.
-- -----------------------------------------------------------------------------
CREATE POLICY anon_read_tokens ON tokens
FOR SELECT TO anon, authenticated USING (true);
CREATE POLICY anon_read_protocols ON protocols
FOR SELECT TO anon, authenticated USING (true);
CREATE POLICY anon_read_rate_snapshots ON rate_snapshots
FOR SELECT TO anon, authenticated USING (true);
-- -----------------------------------------------------------------------------
-- 10.4 USERS — Authenticated users can only read their own row
-- They CANNOT update/delete (only backend can modify user records)
-- -----------------------------------------------------------------------------
CREATE POLICY auth_read_own_user ON users
FOR SELECT TO authenticated
USING (address = lower(auth.uid()::text));
-- No INSERT/UPDATE/DELETE for authenticated — backend handles all mutations
-- -----------------------------------------------------------------------------
-- 10.5 SESSION_KEYS — FULLY LOCKED from frontend
-- Session keys must NEVER be readable from the frontend.
-- Only service_role (backend) can read/write.
-- No policies for anon or authenticated = complete block.
-- -----------------------------------------------------------------------------
-- (no anon/authenticated policies = denied by default when RLS is enabled)
-- -----------------------------------------------------------------------------
-- 10.6 POSITIONS — Authenticated users can read their own positions
-- Only backend can create/update/delete positions
-- -----------------------------------------------------------------------------
CREATE POLICY auth_read_own_positions ON positions
FOR SELECT TO authenticated
USING (user_address = lower(auth.uid()::text));
-- -----------------------------------------------------------------------------
-- 10.7 TRANSACTION_LOGS — Authenticated users can read their own logs
-- Only backend can insert logs (immutable — no update/delete ever)
-- -----------------------------------------------------------------------------
CREATE POLICY auth_read_own_logs ON transaction_logs
FOR SELECT TO authenticated
USING (user_address = lower(auth.uid()::text));
-- -----------------------------------------------------------------------------
-- 10.8 MIGRATION_HISTORY — Authenticated users can read their own migrations
-- Only backend can insert
-- -----------------------------------------------------------------------------
CREATE POLICY auth_read_own_migrations ON migration_history
FOR SELECT TO authenticated
USING (user_address = lower(auth.uid()::text));
-- -----------------------------------------------------------------------------
-- 10.9 ANON — Deny everything except public reference tables
-- anon can only SELECT on tokens, protocols, rate_snapshots (policies above)
-- All other tables: no anon policy = denied by default
-- -----------------------------------------------------------------------------
-- (no anon policies on users, session_keys, positions, transaction_logs,
-- migration_history = all denied for anonymous access)
-- -----------------------------------------------------------------------------
-- 10.10 SECURITY AUDIT CHECKLIST
-- -----------------------------------------------------------------------------
--
-- [x] session_keys: NO frontend access at all (no anon/authenticated policies)
-- [x] users: authenticated can only SELECT own row (no write)
-- [x] positions: authenticated can only SELECT own rows (no write)
-- [x] transaction_logs: authenticated can only SELECT own rows (no write, immutable)
-- [x] migration_history: authenticated can only SELECT own rows (no write)
-- [x] tokens/protocols/rates: public read-only (reference data)
-- [x] All writes go through service_role (Express backend) only
-- [x] auth.uid() matched against lowercase address for row scoping
-- [x] RLS enabled on EVERY table (no exceptions)
-- =============================================================================
-- 11. USEFUL VIEWS
-- =============================================================================
-- View: Active positions with token details
CREATE VIEW v_positions_detail AS
SELECT
p.id,
p.user_address,
p.safe_address,
p.current_protocol,
pr.name AS protocol_name,
ct.symbol AS collateral_symbol,
ct.decimals AS collateral_decimals,
p.collateral_amount,
-- Human-readable collateral: raw / 10^decimals
(p.collateral_amount / power(10, ct.decimals))::NUMERIC(36, 18) AS collateral_human,
dt.symbol AS debt_symbol,
dt.decimals AS debt_decimals,
p.debt_amount,
-- Human-readable debt: raw / 10^decimals
(p.debt_amount / power(10, dt.decimals))::NUMERIC(36, 6) AS debt_human,
p.health_factor,
p.supply_apy,
p.borrow_apy,
p.net_apy,
p.status,
p.migration_count,
p.last_checked_at,
p.next_check_at,
p.created_at,
p.updated_at
FROM positions p
JOIN tokens ct ON p.collateral_token = ct.address
JOIN tokens dt ON p.debt_token = dt.address
JOIN protocols pr ON p.current_protocol = pr.id;
-- View: Transaction logs with token details
CREATE VIEW v_transaction_logs AS
SELECT
tl.id,
tl.user_address,
tl.position_id,
tl.tx_type,
tl.protocol,
pr.name AS protocol_name,
t.symbol AS token_symbol,
t.decimals AS token_decimals,
tl.amount,
-- Human-readable amount
CASE WHEN t.decimals IS NOT NULL
THEN (tl.amount / power(10, t.decimals))::NUMERIC(36, 18)
ELSE tl.amount
END AS amount_human,
tl.amount_usd,
tl.tx_hash,
tl.user_op_hash,
tl.status,
tl.error_message,
tl.metadata,
tl.created_at
FROM transaction_logs tl
LEFT JOIN tokens t ON tl.token_address = t.address
LEFT JOIN protocols pr ON tl.protocol = pr.id;
-- View: Positions due for health check (daemon query)
-- IMPORTANT: This view joins session_keys — only use via service_role (backend).
-- Never expose this view to frontend/anon/authenticated roles.
CREATE VIEW v_positions_due_check
WITH (security_invoker = true)
AS
SELECT
p.id,
p.user_address,
p.safe_address,
p.current_protocol,
p.collateral_token,
p.collateral_amount,
p.debt_token,
p.debt_amount,
p.health_factor,
p.next_check_at,
sk.encrypted_key,
sk.key_address
FROM positions p
JOIN session_keys sk ON p.user_address = sk.user_address AND sk.is_active = true
WHERE p.status = 'active'
AND p.next_check_at <= now()
ORDER BY p.next_check_at ASC;
-- =============================================================================
-- 11.1 VIEW ACCESS CONTROL
-- =============================================================================
-- v_positions_due_check contains encrypted session keys — lock it down
REVOKE ALL ON v_positions_due_check FROM anon, authenticated;
GRANT SELECT ON v_positions_due_check TO service_role;
-- Other views are safe for authenticated users (RLS still applies on underlying tables)
GRANT SELECT ON v_positions_detail TO authenticated, service_role;
GRANT SELECT ON v_transaction_logs TO authenticated, service_role;
-- =============================================================================
-- 12. HELPER FUNCTIONS
-- =============================================================================
-- Function: Calculate next check time based on health factor (tiered monitoring)
CREATE OR REPLACE FUNCTION calc_next_check_at(hf NUMERIC)
RETURNS TIMESTAMPTZ AS $$
BEGIN
IF hf IS NULL THEN
RETURN now() + interval '1 minute';
ELSIF hf < 1.3 THEN
RETURN now() + interval '15 seconds'; -- Critical
ELSIF hf < 1.5 THEN
RETURN now() + interval '30 seconds'; -- Elevated risk
ELSIF hf < 2.0 THEN
RETURN now() + interval '2 minutes'; -- Normal
ELSE
RETURN now() + interval '10 minutes'; -- Healthy
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE;
-- Function: Update position health and set next check time
CREATE OR REPLACE FUNCTION update_position_health(
p_id UUID,
p_health_factor NUMERIC,
p_collateral_amount NUMERIC,
p_debt_amount NUMERIC,
p_supply_apy NUMERIC DEFAULT NULL,
p_borrow_apy NUMERIC DEFAULT NULL
)
RETURNS VOID AS $$
BEGIN
UPDATE positions SET
health_factor = p_health_factor,
collateral_amount = p_collateral_amount,
debt_amount = p_debt_amount,
supply_apy = COALESCE(p_supply_apy, supply_apy),
borrow_apy = COALESCE(p_borrow_apy, borrow_apy),
net_apy = COALESCE(p_supply_apy, supply_apy) - COALESCE(p_borrow_apy, borrow_apy),
last_checked_at = now(),
next_check_at = calc_next_check_at(p_health_factor)
WHERE id = p_id;
END;
$$ LANGUAGE plpgsql;
-- =============================================================================
-- 13. EXAMPLE QUERIES
-- =============================================================================
-- Get all logs for a specific address (with human-readable amounts)
-- SELECT * FROM v_transaction_logs WHERE user_address = '0x...' ORDER BY created_at DESC;
-- Get active positions needing health check
-- SELECT * FROM v_positions_due_check LIMIT 500;
-- Get migration history with rate improvement
-- SELECT * FROM migration_history WHERE user_address = '0x...' ORDER BY created_at DESC;
-- Get rate comparison between protocols for WETH
-- SELECT protocol, supply_apy, borrow_apy, collateral_factor, snapshot_at
-- FROM rate_snapshots
-- WHERE token_address = '0x4200000000000000000000000000000000000006'
-- ORDER BY snapshot_at DESC LIMIT 10;
-- Count active positions per protocol
-- SELECT current_protocol, count(*) FROM positions WHERE status = 'active' GROUP BY current_protocol;
-- Positions with health factor below threshold
-- SELECT * FROM v_positions_detail WHERE status = 'active' AND health_factor < 1.5 ORDER BY health_factor ASC;