124 lines
3.8 KiB
Python
Executable File
124 lines
3.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Currency Ledger Validation Script
|
|
Ensures ledger integrity and policy compliance
|
|
"""
|
|
|
|
import csv
|
|
import yaml
|
|
from datetime import datetime
|
|
import os
|
|
|
|
def load_policies():
|
|
"""Load policy configuration files"""
|
|
policies = {}
|
|
policy_dir = os.path.join(os.path.dirname(__file__), '..', 'policies')
|
|
|
|
with open(os.path.join(policy_dir, 'credit-rates.yml'), 'r') as f:
|
|
policies['credit_rates'] = yaml.safe_load(f)
|
|
|
|
with open(os.path.join(policy_dir, 'karma-rules.yml'), 'r') as f:
|
|
policies['karma_rules'] = yaml.safe_load(f)
|
|
|
|
with open(os.path.join(policy_dir, 'validation-rules.yml'), 'r') as f:
|
|
policies['validation'] = yaml.safe_load(f)
|
|
|
|
return policies
|
|
|
|
def validate_sc_ledger():
|
|
"""Validate Smartup Credits ledger"""
|
|
print("🔍 Validating Smartup Credits ledger...")
|
|
|
|
total_sc = 0
|
|
ledger_file = os.path.join(os.path.dirname(__file__), '..', 'ledger', 'smartup-credits', 'transactions.csv')
|
|
|
|
try:
|
|
with open(ledger_file, 'r') as f:
|
|
reader = csv.DictReader(f)
|
|
transaction_count = 0
|
|
|
|
for row in reader:
|
|
if row['type'] == 'SC':
|
|
total_sc += int(row['amount'])
|
|
transaction_count += 1
|
|
elif row['type'] == 'REDEEM':
|
|
total_sc -= int(row['amount'])
|
|
transaction_count += 1
|
|
|
|
print(f"✅ Total SC Outstanding: {total_sc}")
|
|
print(f"✅ Transactions Processed: {transaction_count}")
|
|
|
|
except FileNotFoundError:
|
|
print("❌ SC ledger file not found")
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ Error validating SC ledger: {e}")
|
|
return False
|
|
|
|
return True
|
|
|
|
def validate_treasury():
|
|
"""Validate treasury balance and 3x rule"""
|
|
print("🔍 Validating treasury balance...")
|
|
|
|
treasury_file = os.path.join(os.path.dirname(__file__), '..', 'ledger', 'treasury', 'balance.csv')
|
|
|
|
try:
|
|
with open(treasury_file, 'r') as f:
|
|
reader = csv.DictReader(f)
|
|
rows = list(reader)
|
|
|
|
if rows:
|
|
latest = rows[-1]
|
|
eur_balance = float(latest['total_eur_balance'])
|
|
sc_outstanding = int(latest['sc_outstanding'])
|
|
ratio = float(latest['ratio'])
|
|
|
|
print(f"✅ EUR Treasury: €{eur_balance}")
|
|
print(f"✅ SC Outstanding: {sc_outstanding} SC")
|
|
print(f"✅ Current Ratio: {ratio:.2f}")
|
|
|
|
# Check 3x rule
|
|
if sc_outstanding <= (eur_balance * 3):
|
|
print("✅ 3x Rule: COMPLIANT")
|
|
else:
|
|
print("❌ 3x Rule: VIOLATION - Too much SC outstanding!")
|
|
return False
|
|
else:
|
|
print("⚠️ Treasury initialized but no transactions yet")
|
|
|
|
except FileNotFoundError:
|
|
print("❌ Treasury file not found")
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ Error validating treasury: {e}")
|
|
return False
|
|
|
|
return True
|
|
|
|
def main():
|
|
"""Main validation function"""
|
|
print("🚀 Smartup Zero Currency Ledger Validation")
|
|
print("=" * 50)
|
|
|
|
# Load policies
|
|
try:
|
|
policies = load_policies()
|
|
print("✅ Policy files loaded successfully")
|
|
except Exception as e:
|
|
print(f"❌ Error loading policies: {e}")
|
|
return
|
|
|
|
# Validate ledgers
|
|
sc_valid = validate_sc_ledger()
|
|
treasury_valid = validate_treasury()
|
|
|
|
print("=" * 50)
|
|
if sc_valid and treasury_valid:
|
|
print("🎉 All validations PASSED - Ledger is healthy!")
|
|
else:
|
|
print("🚨 Validation FAILED - Check errors above")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|