-- PollCelo Production Database Schema for Neon PostgreSQL -- Enable UUID extension CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; -- Polls table CREATE TABLE polls ( id TEXT PRIMARY KEY DEFAULT concat('poll-', extract(epoch from now())::text), creator_fid INTEGER NOT NULL, creator_address TEXT NOT NULL, question TEXT NOT NULL, description TEXT, emoji TEXT DEFAULT '📊', start_time BIGINT NOT NULL, end_time BIGINT NOT NULL, status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'closed', 'verified', 'distributed', 'refunded')), -- Verification configuration verification_type TEXT NOT NULL CHECK (verification_type IN ('chainlink', 'curve', 'dexscreener', 'api', 'espn', 'manual')), verification_config JSONB NOT NULL, correct_option_id TEXT, verified_at BIGINT, verification_proof TEXT, -- Financial tracking total_pot NUMERIC(20, 8) NOT NULL DEFAULT 0, protocol_fee NUMERIC(20, 8) NOT NULL DEFAULT 0, creation_fee NUMERIC(20, 8) NOT NULL DEFAULT 5, winners_count INTEGER DEFAULT 0, distributed_at BIGINT, -- Metadata created_at TIMESTAMP DEFAULT NOW(), updated_at TIMESTAMP DEFAULT NOW() ); -- Poll options table CREATE TABLE poll_options ( id TEXT PRIMARY KEY DEFAULT concat('opt-', extract(epoch from now())::text), poll_id TEXT NOT NULL REFERENCES polls(id) ON DELETE CASCADE, text TEXT NOT NULL, emoji TEXT DEFAULT '📌', votes INTEGER DEFAULT 0, total_amount NUMERIC(20, 8) DEFAULT 0, position INTEGER NOT NULL, created_at TIMESTAMP DEFAULT NOW() ); -- Votes table CREATE TABLE votes ( id TEXT PRIMARY KEY DEFAULT concat('vote-', extract(epoch from now())::text), poll_id TEXT NOT NULL REFERENCES polls(id) ON DELETE CASCADE, user_fid TEXT NOT NULL, user_address TEXT NOT NULL, option_id TEXT NOT NULL REFERENCES poll_options(id) ON DELETE CASCADE, amount NUMERIC(20, 8) NOT NULL, tx_hash TEXT NOT NULL UNIQUE, timestamp BIGINT NOT NULL, created_at TIMESTAMP DEFAULT NOW(), -- One vote per user per poll UNIQUE(poll_id, user_fid) ); -- Distributions table (payout logs) CREATE TABLE distributions ( id SERIAL PRIMARY KEY, poll_id TEXT NOT NULL REFERENCES polls(id) ON DELETE CASCADE, user_fid TEXT NOT NULL, user_address TEXT NOT NULL, amount NUMERIC(20, 8) NOT NULL, tx_hash TEXT NOT NULL, timestamp BIGINT NOT NULL, created_at TIMESTAMP DEFAULT NOW() ); -- Verification attempts log (for debugging) CREATE TABLE verification_attempts ( id SERIAL PRIMARY KEY, poll_id TEXT NOT NULL REFERENCES polls(id) ON DELETE CASCADE, attempt_time TIMESTAMP DEFAULT NOW(), success BOOLEAN NOT NULL, result_data JSONB, error_message TEXT, created_at TIMESTAMP DEFAULT NOW() ); -- User profiles cache (from Farcaster) CREATE TABLE user_profiles ( fid INTEGER PRIMARY KEY, username TEXT, display_name TEXT, pfp_url TEXT, custody_address TEXT, verified_addresses TEXT[], last_updated TIMESTAMP DEFAULT NOW() ); -- Indexes for performance CREATE INDEX idx_polls_status ON polls(status); CREATE INDEX idx_polls_end_time ON polls(end_time); CREATE INDEX idx_polls_creator ON polls(creator_fid); CREATE INDEX idx_polls_created_at ON polls(created_at DESC); CREATE INDEX idx_poll_options_poll_id ON poll_options(poll_id); CREATE INDEX idx_votes_poll_id ON votes(poll_id); CREATE INDEX idx_votes_user_fid ON votes(user_fid); CREATE INDEX idx_votes_tx_hash ON votes(tx_hash); CREATE INDEX idx_distributions_poll_id ON distributions(poll_id); CREATE INDEX idx_distributions_user_fid ON distributions(user_fid); CREATE INDEX idx_verification_attempts_poll_id ON verification_attempts(poll_id); CREATE INDEX idx_user_profiles_username ON user_profiles(username); -- Function to auto-update updated_at timestamp CREATE OR REPLACE FUNCTION update_updated_at_column() RETURNS TRIGGER AS $$ BEGIN NEW.updated_at = NOW(); RETURN NEW; END; $$ language 'plpgsql'; -- Trigger to auto-update polls.updated_at CREATE TRIGGER update_polls_updated_at BEFORE UPDATE ON polls FOR EACH ROW EXECUTE FUNCTION update_updated_at_column(); -- Views for common queries -- Active polls view CREATE VIEW active_polls AS SELECT p.*, json_agg( json_build_object( 'id', po.id, 'text', po.text, 'emoji', po.emoji, 'votes', po.votes, 'totalAmount', po.total_amount, 'position', po.position ) ORDER BY po.position ) as options FROM polls p LEFT JOIN poll_options po ON p.id = po.poll_id WHERE p.status = 'active' GROUP BY p.id ORDER BY p.created_at DESC; -- Polls pending verification CREATE VIEW polls_pending_verification AS SELECT p.*, json_agg( json_build_object( 'id', po.id, 'text', po.text, 'emoji', po.emoji, 'votes', po.votes, 'totalAmount', po.total_amount ) ORDER BY po.position ) as options FROM polls p LEFT JOIN poll_options po ON p.id = po.poll_id WHERE p.status = 'closed' AND p.end_time < extract(epoch from now()) * 1000 AND p.verification_type != 'manual' GROUP BY p.id ORDER BY p.end_time ASC; -- Polls pending distribution CREATE VIEW polls_pending_distribution AS SELECT p.*, json_agg( json_build_object( 'id', po.id, 'text', po.text, 'emoji', po.emoji, 'votes', po.votes, 'totalAmount', po.total_amount ) ORDER BY po.position ) as options FROM polls p LEFT JOIN poll_options po ON p.id = po.poll_id WHERE p.status = 'verified' AND p.correct_option_id IS NOT NULL AND p.distributed_at IS NULL GROUP BY p.id ORDER BY p.verified_at ASC; -- User statistics view CREATE VIEW user_stats AS SELECT v.user_fid, COUNT(DISTINCT v.poll_id) as polls_voted, SUM(v.amount) as total_voted, COUNT(DISTINCT CASE WHEN p.correct_option_id = v.option_id THEN v.poll_id END) as polls_won, COALESCE(SUM(d.amount), 0) as total_winnings FROM votes v LEFT JOIN polls p ON v.poll_id = p.id LEFT JOIN distributions d ON v.user_fid = d.user_fid AND v.poll_id = d.poll_id GROUP BY v.user_fid; -- Grant permissions (adjust role as needed) -- GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO your_neon_role; -- GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO your_neon_role;