|
| 1 | +import argparse |
| 2 | +import datetime |
| 3 | +import json |
| 4 | +from decimal import Decimal |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import pandas as pd |
| 8 | + |
| 9 | +from fee_allocator.votemarket_analytics import get_aura_share_per_gauge |
| 10 | +from fee_allocator.logger import logger |
| 11 | + |
| 12 | +PROJECT_ROOT = Path(__file__).parent |
| 13 | +SUMMARIES_DIR = PROJECT_ROOT / "fee_allocator" / "summaries" |
| 14 | +INCENTIVES_DIR = PROJECT_ROOT / "fee_allocator" / "allocations" / "incentives" |
| 15 | +BRIBES_DIR = PROJECT_ROOT / "fee_allocator" / "allocations" / "output_for_msig" |
| 16 | + |
| 17 | + |
| 18 | +def _ts_to_date_str(ts: int) -> str: |
| 19 | + return datetime.datetime.fromtimestamp(ts, tz=datetime.timezone.utc).strftime("%Y-%m-%d") |
| 20 | + |
| 21 | + |
| 22 | +def _load_gauge_data_from_incentives_csv(csv_path: Path) -> pd.DataFrame: |
| 23 | + df = pd.read_csv(csv_path) |
| 24 | + if "gauge_address" in df.columns and "voting_pool_override" in df.columns: |
| 25 | + return df |
| 26 | + return None |
| 27 | + |
| 28 | + |
| 29 | +def _load_gauge_data_from_bribe_csv(csv_path: Path) -> pd.DataFrame: |
| 30 | + if not csv_path.exists(): |
| 31 | + return None |
| 32 | + df = pd.read_csv(csv_path) |
| 33 | + if "target" not in df.columns: |
| 34 | + return None |
| 35 | + bribe_rows = df[df["platform"].isna()] if "platform" in df.columns else df |
| 36 | + bribe_rows = bribe_rows[bribe_rows["amount"] > 0].copy() |
| 37 | + if bribe_rows.empty: |
| 38 | + return None |
| 39 | + bribe_rows = bribe_rows.rename(columns={"target": "gauge_address"}) |
| 40 | + if "voting_pool_override" not in bribe_rows.columns: |
| 41 | + bribe_rows["voting_pool_override"] = "" |
| 42 | + bribe_rows["voting_pool_override"] = bribe_rows["voting_pool_override"].fillna("") |
| 43 | + return bribe_rows |
| 44 | + |
| 45 | + |
| 46 | +def _compute_aura_split(gauge_data: pd.DataFrame, gauge_aura_shares: dict) -> pd.DataFrame: |
| 47 | + aura_list = [] |
| 48 | + bal_list = [] |
| 49 | + for _, row in gauge_data.iterrows(): |
| 50 | + total = Decimal(str(row.get("total_incentives", 0))) |
| 51 | + override = str(row.get("voting_pool_override", "")).strip() |
| 52 | + gauge = str(row.get("gauge_address", "")).strip().lower() |
| 53 | + |
| 54 | + if override == "aura": |
| 55 | + aura_share = Decimal(1) |
| 56 | + elif override == "bal": |
| 57 | + aura_share = Decimal(0) |
| 58 | + elif gauge: |
| 59 | + aura_share = gauge_aura_shares.get(gauge, Decimal(0)) |
| 60 | + else: |
| 61 | + aura_share = Decimal(0) |
| 62 | + |
| 63 | + aura_list.append(float(round(total * aura_share, 4))) |
| 64 | + bal_list.append(float(round(total - total * aura_share, 4))) |
| 65 | + |
| 66 | + gauge_data = gauge_data.copy() |
| 67 | + gauge_data["aura_incentives"] = aura_list |
| 68 | + gauge_data["bal_incentives"] = bal_list |
| 69 | + return gauge_data |
| 70 | + |
| 71 | + |
| 72 | +def _get_total_incentives(entry: dict) -> float: |
| 73 | + if "totalIncentives" in entry: |
| 74 | + return entry["totalIncentives"] |
| 75 | + aura = entry.get("auraIncentives", 0) or 0 |
| 76 | + bal = entry.get("balIncentives", 0) or 0 |
| 77 | + return aura + bal |
| 78 | + |
| 79 | + |
| 80 | +def backfill(dry_run: bool = False): |
| 81 | + for version in ["v2", "v3"]: |
| 82 | + recon_path = SUMMARIES_DIR / f"{version}_recon.json" |
| 83 | + if not recon_path.exists(): |
| 84 | + logger.info(f"No recon file for {version}, skipping") |
| 85 | + continue |
| 86 | + |
| 87 | + with open(recon_path) as f: |
| 88 | + data = json.load(f) |
| 89 | + |
| 90 | + modified = False |
| 91 | + for entry in data: |
| 92 | + total_incentives = _get_total_incentives(entry) |
| 93 | + aura_incentives = entry.get("auraIncentives", 0) or 0 |
| 94 | + |
| 95 | + if aura_incentives != 0 or total_incentives == 0: |
| 96 | + continue |
| 97 | + |
| 98 | + period_start = entry["periodStart"] |
| 99 | + period_end = entry["periodEnd"] |
| 100 | + start_str = _ts_to_date_str(period_start) |
| 101 | + end_str = _ts_to_date_str(period_end) |
| 102 | + |
| 103 | + logger.info(f"[{version}] Processing period {start_str} to {end_str}") |
| 104 | + |
| 105 | + gauge_aura_shares = get_aura_share_per_gauge(period_start, period_end) |
| 106 | + if not gauge_aura_shares: |
| 107 | + logger.info(f"[{version}] No VoteMarket data for {start_str}_{end_str}, skipping") |
| 108 | + continue |
| 109 | + |
| 110 | + incentives_csv = INCENTIVES_DIR / f"{version}_incentives_{start_str}_{end_str}.csv" |
| 111 | + gauge_data = None |
| 112 | + |
| 113 | + if incentives_csv.exists(): |
| 114 | + gauge_data = _load_gauge_data_from_incentives_csv(incentives_csv) |
| 115 | + |
| 116 | + if gauge_data is None: |
| 117 | + end_date = _ts_to_date_str(period_end) |
| 118 | + bribe_csv = BRIBES_DIR / f"{version}_bribes_{end_date}.csv" |
| 119 | + gauge_data = _load_gauge_data_from_bribe_csv(bribe_csv) |
| 120 | + |
| 121 | + if gauge_data is None: |
| 122 | + logger.warning(f"[{version}] No gauge data found for {start_str}_{end_str}, skipping") |
| 123 | + continue |
| 124 | + |
| 125 | + gauge_data = _compute_aura_split(gauge_data, gauge_aura_shares) |
| 126 | + |
| 127 | + total_aura = sum(gauge_data["aura_incentives"]) |
| 128 | + total_bal = sum(gauge_data["bal_incentives"]) |
| 129 | + |
| 130 | + logger.info(f"[{version}] {start_str}_{end_str}: aura={total_aura:.2f} bal={total_bal:.2f}") |
| 131 | + |
| 132 | + if not dry_run: |
| 133 | + if incentives_csv.exists(): |
| 134 | + full_df = pd.read_csv(incentives_csv) |
| 135 | + if "gauge_address" in full_df.columns: |
| 136 | + updated = _compute_aura_split(full_df, gauge_aura_shares) |
| 137 | + updated.to_csv(incentives_csv, index=False) |
| 138 | + logger.info(f"[{version}] Updated incentives CSV: {incentives_csv.name}") |
| 139 | + |
| 140 | + entry["auraIncentives"] = round(total_aura, 2) |
| 141 | + entry["balIncentives"] = round(total_bal, 2) |
| 142 | + combined = total_aura + total_bal |
| 143 | + entry["auravebalShare"] = round(total_aura / combined, 2) if combined > 0 else 0 |
| 144 | + total_distributed = entry.get("totalDistributed", entry.get("incentivesDistributed", 0)) |
| 145 | + entry["auraIncentivesPct"] = round(total_aura / total_distributed, 4) if total_distributed > 0 else 0.0 |
| 146 | + entry["balIncentivesPct"] = round(total_bal / total_distributed, 4) if total_distributed > 0 else 0.0 |
| 147 | + modified = True |
| 148 | + |
| 149 | + if modified and not dry_run: |
| 150 | + with open(recon_path, "w") as f: |
| 151 | + json.dump(data, f, indent=2) |
| 152 | + logger.info(f"[{version}] Wrote updated recon to {recon_path.name}") |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + parser = argparse.ArgumentParser(description="Backfill aura/bal split into recon JSON") |
| 157 | + parser.add_argument("--dry_run", action="store_true", help="Print what would be done without writing") |
| 158 | + args = parser.parse_args() |
| 159 | + backfill(dry_run=args.dry_run) |
0 commit comments