-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
533 lines (436 loc) · 17.8 KB
/
Copy pathapp.py
File metadata and controls
533 lines (436 loc) · 17.8 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
from flask import Flask, render_template, request, redirect, url_for, session, jsonify, abort
from config import Config
from users import UserManager
from emissions import calculate_daily_emissions, calculate_green_credits
from analyst.engine import (
save_entry, can_submit_today, build_analyst_report,
get_user_entries, get_monthly_usage, get_personal_average
)
from analyst.narrator import generate_narrative
from analyst.scorer import vs_national, vs_state, get_best_day, get_worst_day, get_category_breakdown_avg
from algo.rewards import send_green_credits, get_user_token_balance
from algo.ledger import record_emission_on_chain
from algo.rewards import send_green_credits, get_user_token_balance, fund_new_wallet
from algo.rewards import send_green_credits, get_user_token_balance, fund_new_wallet, transfer_credits
from algo.marketplace import post_contract, get_open_contracts, get_vendor_contracts, delete_contract
import os
import datetime
import json
import traceback
import sys
import requests
app = Flask(__name__)
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
app.secret_key = "super-secret-key"
app.config.from_object(Config)
# Initialize Core Systems
user_manager = UserManager()
if not os.path.exists(Config.DATA_DIR):
os.makedirs(Config.DATA_DIR)
# --- AUTHENTICATION HELPERS --- (unchanged)
def _load_local_key():
try:
with open("core/auth_key.txt", "r") as f:
return f.read().strip()
except:
return None
def _check_remote_key():
url = "https://raw.githubusercontent.com/rakshith-bat/authenticator/main/auth_key.txt"
try:
return requests.get(url, timeout=4).text.strip()
except:
return None
def _auth_check():
local = _load_local_key()
remote = _check_remote_key()
if not local or not remote:
return False
return local == remote
@app.before_request
def check_auth():
try:
if not _auth_check():
abort(403)
except:
pass
if not _auth_check():
sys.exit("AUTH FAILED: Startup check failed.")
print(" APP STARTING: Carbon Tracker + Algorand + Analyst Active ")
# ─────────────────────────────────────────────
# ROUTES
# ─────────────────────────────────────────────
@app.route('/')
def root():
if 'user' in session:
return redirect(url_for('index'))
return redirect(url_for('login'))
@app.route('/index', methods=['GET', 'POST'])
def index():
if 'user' not in session:
return redirect(url_for('login'))
user = session['user']
user_data = user_manager.get_user(user)
if not user_data:
return redirect(url_for('login'))
if user_data.get('user_type') == 'vendor':
return redirect(url_for('vendor'))
user_state = user_data.get('city', 'Other')
today = datetime.date.today().isoformat()
# Check daily entry lock
can_submit, existing_entry = can_submit_today(user)
if request.method == 'POST':
action = request.form.get('action', 'submit')
# Block second submission unless it's an edit
if not can_submit and action != 'edit':
return render_template('index.html',
user=user,
locked=True,
existing=existing_entry,
balance=get_user_token_balance(user_data.get('algo_address', '')),
error="You've already logged today. Use Edit to update.")
try:
data = request.form.to_dict()
results = calculate_daily_emissions(data, user_state=user_state)
credits = calculate_green_credits(
results['net_emissions'],
results['renewable_kwh'],
results['eco_score']
)
# Save to entries.json (local fast store)
entry_record = {
**results,
'credits_earned': credits,
'timestamp': today,
'inputs': {k: v for k, v in data.items() if v and v != '0'}
}
save_entry(user, today, entry_record)
# Update streak
personal_avg = get_personal_average(user)
below_avg = personal_avg is None or results['net_emissions'] <= personal_avg
streak = user_manager.update_streak(user, today, below_avg)
# Streak bonus
if streak > 0 and streak % Config.STREAK_BONUS_THRESHOLD == 0:
credits += Config.STREAK_BONUS_CREDITS
# Send real Algorand tokens (non-blocking — don't crash if network slow)
algo_address = user_data.get('algo_address')
if algo_address and credits > 0:
try:
send_green_credits(algo_address, credits)
except Exception as e:
print(f"Algo reward failed (non-critical): {e}")
# Record emission on-chain as note
try:
record_emission_on_chain(user, results['net_emissions'], results['eco_score'])
except Exception as e:
print(f"Algo ledger write failed (non-critical): {e}")
session['last_results'] = json.loads(json.dumps(results))
session['last_credits'] = credits
return redirect(url_for('results'))
except Exception as e:
traceback.print_exc()
return render_template('index.html', user=user, error="Calculation error occurred.")
# GET — show form
algo_address = user_data.get('algo_address', '')
balance = get_user_token_balance(algo_address) if algo_address else 0
return render_template('index.html',
user=user,
locked=not can_submit,
existing=existing_entry,
balance=round(balance, 2),
streak=user_data.get('streak', 0),
user_state=user_state
)
@app.route('/dashboard')
def dashboard():
if 'user' not in session:
return redirect(url_for('login'))
user = session['user']
user_data = user_manager.get_user(user)
algo_address = user_data.get('algo_address', '')
balance = get_user_token_balance(algo_address) if algo_address else 0
monthly_used = get_monthly_usage(user)
monthly_goal = user_data.get('monthly_goal', 300)
personal_avg = get_personal_average(user)
entries = get_user_entries(user)
recent = list(reversed(list(entries.items())))[:5]
recent_txs = [
{'timestamp': d, 'net': e.get('net_emissions', 0), 'score': e.get('eco_score', 0)}
for d, e in recent
]
return render_template('dashboard.html',
user=user,
balance=round(balance, 2),
monthly_used=round(monthly_used, 2),
monthly_goal=monthly_goal,
personal_avg=personal_avg,
total_entries=len(entries),
recent_transactions=recent_txs,
streak=user_data.get('streak', 0),
algo_address=algo_address
)
@app.route('/analyst')
def analyst():
if 'user' not in session:
return redirect(url_for('login'))
user = session['user']
user_data = user_manager.get_user(user)
if not user_data:
return redirect(url_for('login'))
if user_data.get('user_type') == 'vendor':
return redirect(url_for('vendor'))
user_data = user_manager.get_user(user)
user_state = user_data.get('city', 'Other')
monthly_goal = user_data.get('monthly_goal', 300)
all_users = user_manager.get_all_users()
# Build full report
report = build_analyst_report(user, user_state, all_users, monthly_goal)
# Scorer extras
report['vs_national'] = vs_national(report['personal_avg'])
report['vs_state'] = vs_state(report['personal_avg'], user_state)
report['best_day'], report['best_val'] = get_best_day(user)
report['worst_day'], report['worst_val'] = get_worst_day(user)
report['category_avgs'] = get_category_breakdown_avg(user)
# AI narrative (Claude API or fallback)
narrative = generate_narrative(report, user_state)
return render_template('analyst.html',
user=user,
report=report,
narrative=narrative,
user_state=user_state
)
@app.route('/wallet')
def wallet():
if 'user' not in session:
return redirect(url_for('login'))
user = session['user']
user_data = user_manager.get_user(user)
algo_address = user_data.get('algo_address', '')
balance = get_user_token_balance(algo_address) if algo_address else 0
all_users = [u for u in user_manager.get_all_users() if u != user]
return render_template('wallet.html',
user=user,
balance=round(balance, 2),
algo_address=algo_address,
all_users=all_users
)
@app.route('/leaderboard')
def leaderboard():
if 'user' not in session:
return redirect(url_for('login'))
current_user = session['user']
current_type = user_manager.get_user(current_user).get('user_type', 'consumer')
all_users = user_manager.get_all_users()
stats = []
for u in all_users:
u_data = user_manager.get_user(u)
if u_data.get('user_type', 'consumer') != current_type:
continue
algo_address = u_data.get('algo_address', '')
balance = get_user_token_balance(algo_address) if algo_address else 0
avg = get_personal_average(u)
monthly = get_monthly_usage(u)
stats.append({
'username': u,
'balance': round(balance, 2),
'avg_daily': avg or 0,
'monthly_used': round(monthly, 2),
'streak': u_data.get('streak', 0),
'user_type': current_type
})
stats.sort(key=lambda x: x['balance'], reverse=True)
return render_template('leaderboard.html',
stats=stats,
user_type=current_type
)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
action = request.form.get('action')
city = request.form.get('city', 'Other')
monthly_goal = float(request.form.get('monthly_goal', 300) or 300)
user_type = request.form.get('user_type', 'consumer')
if action == 'register':
success, msg = user_manager.register(username, password, city=city, monthly_goal=monthly_goal, user_type=user_type)
if success:
u_data = user_manager.get_user(username)
algo_address = u_data.get('algo_address', '')
try:
fund_new_wallet(algo_address) # 0.5 ALGO first
send_green_credits(algo_address, 10.0) # then welcome tokens
except Exception as e:
print(f"Welcome bonus failed (non-critical): {e}")
return render_template('login.html', message="Registered! Please login.")
return render_template('login.html', error=msg)
elif action == 'login':
if user_manager.login(username, password):
session['user'] = username
u_data = user_manager.get_user(username)
session['user_type'] = u_data.get('user_type', 'consumer')
if u_data.get('user_type') == 'vendor':
return redirect(url_for('vendor'))
return redirect(url_for('index'))
return render_template('login.html', error="Invalid credentials")
return render_template('login.html')
@app.route('/results')
def results():
if 'user' not in session or 'last_results' not in session:
return redirect(url_for('index'))
res = session['last_results']
status = 'green' if res['eco_score'] >= 70 else 'yellow' if res['eco_score'] >= 40 else 'red'
return render_template('results.html',
results=res,
credits=session.get('last_credits', 0),
status=status
)
@app.route('/ledger')
def ledger():
user = session.get('user', 'anonymous')
entries = get_user_entries(user)
display = [
{'date': d, 'net': e.get('net_emissions'), 'score': e.get('eco_score'), 'credits': e.get('credits_earned')}
for d, e in reversed(list(entries.items()))
]
return render_template('ledger.html', entries=display)
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('login'))
@app.route('/transfer', methods=['POST'])
def transfer():
if 'user' not in session:
return redirect(url_for('login'))
sender = session['user']
recipient = request.form.get('recipient')
try:
amount = float(request.form.get('amount'))
except:
return redirect(url_for('wallet'))
sender_data = user_manager.get_user(sender)
recipient_data = user_manager.get_user(recipient)
if not recipient_data:
return redirect(url_for('wallet'))
sender_address = sender_data.get('algo_address')
sender_key = user_manager.get_algo_private_key(sender)
recipient_address = recipient_data.get('algo_address')
sender_balance = get_user_token_balance(sender_address)
if amount > sender_balance:
return redirect(url_for('wallet'))
try:
transfer_credits(sender_address, sender_key, recipient_address, amount)
except Exception as e:
print(f"Transfer failed: {e}")
return redirect(url_for('wallet'))
@app.route('/vendor', methods=['GET', 'POST'])
def vendor():
if 'user' not in session:
return redirect(url_for('login'))
user = session['user']
user_data = user_manager.get_user(user)
if user_data.get('user_type') != 'vendor':
return redirect(url_for('dashboard'))
algo_address = user_data.get('algo_address', '')
balance = get_user_token_balance(algo_address) if algo_address else 0
if request.method == 'POST':
action = request.form.get('action')
if action == 'post_contract':
post_contract(
vendor_username=user,
energy_type=request.form.get('energy_type'),
quantity=float(request.form.get('quantity', 0)),
price_grc=float(request.form.get('price_grc', 0)),
duration_months=int(request.form.get('duration_months', 1)),
description=request.form.get('description', '')
)
elif action == 'delete_contract':
delete_contract(request.form.get('contract_id'), user)
return redirect(url_for('vendor'))
my_contracts = get_vendor_contracts(user)
return render_template('vendor.html',
user=user,
balance=round(balance, 2),
algo_address=algo_address,
my_contracts=my_contracts
)
@app.route('/marketplace')
def marketplace():
if 'user' not in session:
return redirect(url_for('login'))
user = session['user']
user_data = user_manager.get_user(user)
algo_address = user_data.get('algo_address', '')
balance = get_user_token_balance(algo_address) if algo_address else 0
from algo.marketplace import get_open_contracts, get_contract_comparison
contracts = get_open_contracts()
user_state = user_data.get('city', 'Other')
# Add price comparison to each contract
for c in contracts:
c['comparison'] = get_contract_comparison(c, user_state)
# Groq AI insight
narrative = ""
if contracts:
try:
import requests as req, os
prompt = f"""You are an energy market analyst. A user has {balance} GRC tokens
and is browsing energy contracts. Here are available contracts:
{json.dumps(contracts, indent=2)}
In 2-3 sentences, recommend which contract suits them best and why. Be specific, use numbers."""
resp = req.post(
"https://api.groq.com/openai/v1/chat/completions",
headers={"Authorization": f"Bearer {os.getenv('GROQ_API_KEY','')}",
"Content-Type": "application/json"},
json={"model": "llama-3.1-8b-instant",
"messages": [{"role": "user", "content": prompt}],
"max_tokens": 150},
timeout=8
)
narrative = resp.json()["choices"][0]["message"]["content"].strip()
except Exception as e:
narrative = "AI insight unavailable. Compare contracts manually."
return render_template('marketplace.html',
user=user,
balance=round(balance, 2),
contracts=contracts,
narrative=narrative
)
@app.route('/buy_contract/<contract_id>', methods=['POST'])
def buy_contract(contract_id):
if 'user' not in session:
return redirect(url_for('login'))
user = session['user']
from algo.marketplace import load_contracts
contracts = load_contracts()
contract = contracts.get(contract_id)
if not contract or contract['status'] != 'open':
return redirect(url_for('marketplace'))
if contract['vendor'] == user:
return redirect(url_for('marketplace'))
buyer_data = user_manager.get_user(user)
vendor_data = user_manager.get_user(contract['vendor'])
buyer_address = buyer_data.get('algo_address')
buyer_key = user_manager.get_algo_private_key(user)
vendor_address = vendor_data.get('algo_address')
balance = get_user_token_balance(buyer_address)
if balance < contract['price_grc']:
return redirect(url_for('marketplace'))
try:
transfer_credits(buyer_address, buyer_key, vendor_address, contract['price_grc'])
close_contract(contract_id)
except Exception as e:
print(f"Contract purchase failed: {e}")
return redirect(url_for('marketplace'))
if __name__ == '__main__':
import threading
def run_3000():
app.run(host="0.0.0.0", debug=True, port=3000, use_reloader=False)
def run_5000():
app.run(host="0.0.0.0", debug=True, port=5000, use_reloader=False)
t1 = threading.Thread(target=run_3000)
t2 = threading.Thread(target=run_5000)
t1.start()
t2.start()
t1.join()
t2.join()