-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path002_add_functions.sql
More file actions
217 lines (194 loc) · 6.04 KB
/
002_add_functions.sql
File metadata and controls
217 lines (194 loc) · 6.04 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
-- Additional database functions for MemeCoinGen
-- Function to check user's deployment quota
CREATE OR REPLACE FUNCTION check_deployment_quota(user_id UUID)
RETURNS BOOLEAN AS $$
DECLARE
user_tier subscription_tier;
monthly_limit INTEGER;
current_count INTEGER;
BEGIN
-- Get user's subscription tier
SELECT subscription_tier INTO user_tier
FROM users
WHERE id = user_id;
-- Set monthly limits based on tier
CASE user_tier
WHEN 'free' THEN monthly_limit := 3;
WHEN 'pro' THEN monthly_limit := 10;
WHEN 'enterprise' THEN monthly_limit := 100;
ELSE monthly_limit := 3;
END CASE;
-- Get current month's deployment count
SELECT coins_created_this_month INTO current_count
FROM users
WHERE id = user_id;
RETURN current_count < monthly_limit;
END;
$$ LANGUAGE plpgsql;
-- Function to increment deployment count
CREATE OR REPLACE FUNCTION increment_deployment_count(user_id UUID)
RETURNS void AS $$
BEGIN
UPDATE users
SET coins_created_this_month = coins_created_this_month + 1,
total_coins_created = total_coins_created + 1
WHERE id = user_id;
END;
$$ LANGUAGE plpgsql;
-- Function to get deployment priority based on user tier
CREATE OR REPLACE FUNCTION get_deployment_priority(user_id UUID)
RETURNS INTEGER AS $$
DECLARE
user_tier subscription_tier;
BEGIN
SELECT subscription_tier INTO user_tier
FROM users
WHERE id = user_id;
CASE user_tier
WHEN 'enterprise' THEN RETURN 100;
WHEN 'pro' THEN RETURN 50;
ELSE RETURN 0;
END CASE;
END;
$$ LANGUAGE plpgsql;
-- Function to calculate gas fee estimates
CREATE OR REPLACE FUNCTION estimate_gas_fee(
blockchain blockchain_type,
include_liquidity BOOLEAN DEFAULT FALSE
)
RETURNS TABLE(
estimated_gas_units NUMERIC,
estimated_fee_native NUMERIC,
estimated_fee_usd DECIMAL
) AS $$
BEGIN
CASE blockchain
WHEN 'ethereum' THEN
estimated_gas_units := CASE
WHEN include_liquidity THEN 500000
ELSE 300000
END;
estimated_fee_native := estimated_gas_units * 30; -- 30 gwei
estimated_fee_usd := (estimated_fee_native / 1e18) * 2500; -- Assuming $2500 ETH
WHEN 'bsc' THEN
estimated_gas_units := CASE
WHEN include_liquidity THEN 400000
ELSE 250000
END;
estimated_fee_native := estimated_gas_units * 5; -- 5 gwei
estimated_fee_usd := (estimated_fee_native / 1e18) * 300; -- Assuming $300 BNB
WHEN 'solana' THEN
estimated_gas_units := 5000; -- lamports
estimated_fee_native := estimated_gas_units;
estimated_fee_usd := (estimated_fee_native / 1e9) * 100; -- Assuming $100 SOL
END CASE;
RETURN NEXT;
END;
$$ LANGUAGE plpgsql;
-- Function to clean up old rate limit entries
CREATE OR REPLACE FUNCTION cleanup_rate_limits()
RETURNS void AS $$
BEGIN
DELETE FROM rate_limits
WHERE window_start < CURRENT_TIMESTAMP - INTERVAL '1 hour';
END;
$$ LANGUAGE plpgsql;
-- Function to get trending coins
CREATE OR REPLACE FUNCTION get_trending_coins(
time_window INTERVAL DEFAULT '24 hours',
limit_count INTEGER DEFAULT 10
)
RETURNS TABLE(
coin_id UUID,
name VARCHAR,
symbol VARCHAR,
blockchain blockchain_type,
contract_address VARCHAR,
price_change_percent DECIMAL,
volume_24h DECIMAL,
social_score INTEGER
) AS $$
BEGIN
RETURN QUERY
SELECT
mc.id,
mc.name,
mc.symbol,
mc.blockchain,
mc.contract_address,
COALESCE(
((latest.price_usd - earlier.price_usd) / NULLIF(earlier.price_usd, 0)) * 100,
0
) as price_change_percent,
COALESCE(latest.volume_24h, 0) as volume_24h,
COALESCE(social_count.count::INTEGER, 0) as social_score
FROM meme_coins mc
LEFT JOIN LATERAL (
SELECT * FROM analytics
WHERE coin_id = mc.id
ORDER BY timestamp DESC
LIMIT 1
) latest ON true
LEFT JOIN LATERAL (
SELECT * FROM analytics
WHERE coin_id = mc.id
AND timestamp <= CURRENT_TIMESTAMP - time_window
ORDER BY timestamp DESC
LIMIT 1
) earlier ON true
LEFT JOIN LATERAL (
SELECT COUNT(*) as count
FROM social_shares
WHERE coin_id = mc.id
AND created_at >= CURRENT_TIMESTAMP - time_window
) social_count ON true
WHERE mc.deployment_status = 'deployed'
AND latest.timestamp >= CURRENT_TIMESTAMP - time_window
ORDER BY
COALESCE(latest.volume_24h, 0) DESC,
price_change_percent DESC,
social_score DESC
LIMIT limit_count;
END;
$$ LANGUAGE plpgsql;
-- Function to archive old analytics data
CREATE OR REPLACE FUNCTION archive_old_analytics()
RETURNS void AS $$
BEGIN
-- Create archive table if not exists
CREATE TABLE IF NOT EXISTS analytics_archive (LIKE analytics INCLUDING ALL);
-- Move data older than 30 days to archive
INSERT INTO analytics_archive
SELECT * FROM analytics
WHERE timestamp < CURRENT_TIMESTAMP - INTERVAL '30 days';
-- Delete archived data from main table
DELETE FROM analytics
WHERE timestamp < CURRENT_TIMESTAMP - INTERVAL '30 days';
-- Vacuum the table
VACUUM ANALYZE analytics;
END;
$$ LANGUAGE plpgsql;
-- Scheduled job functions (to be called by cron or pg_cron)
CREATE OR REPLACE FUNCTION daily_maintenance()
RETURNS void AS $$
BEGIN
-- Clean up rate limits
PERFORM cleanup_rate_limits();
-- Archive old analytics
PERFORM archive_old_analytics();
-- Refresh materialized view
REFRESH MATERIALIZED VIEW CONCURRENTLY coin_rankings;
-- Update statistics
ANALYZE;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION monthly_maintenance()
RETURNS void AS $$
BEGIN
-- Reset monthly coin counts
PERFORM reset_monthly_coin_counts();
-- Clean up old audit logs
DELETE FROM audit_logs
WHERE created_at < CURRENT_TIMESTAMP - INTERVAL '6 months';
END;
$$ LANGUAGE plpgsql;