Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

5.4 KB
Raw Blame
import { Panel } from './Panel';
import { escapeHtml } from '@/utils/sanitize';

interface ExploitEntry {
  date: number;
  name: string;
  amount: number | null;
  classification: string | null;
  technique: string | null;
  chains: string[];
  targetType: string | null;
  bridgeHack: boolean;
  returnedFunds: number | null;
}

interface ExploitLedgerResult {
  timestamp: string;
  summary: {
    totalHacks: number;
    count90d: number;
    lost90d: number;
    lost1y: number;
    lostAllTime: number;
    biggest: { name: string; amount: number; date: number } | null;
  };
  hacks: ExploitEntry[];
}

type LedgerFilter = 'all' | 'bridges';

function formatUsd(n: number): string {
  if (n >= 1e9) return `$${(n / 1e9).toFixed(2)}B`;
  if (n >= 1e6) return `$${(n / 1e6).toFixed(1)}M`;
  if (n >= 1e3) return `$${(n / 1e3).toFixed(0)}K`;
  return `$${n.toFixed(0)}`;
}

function formatDate(unixS: number): string {
  return new Date(unixS * 1000).toLocaleDateString(undefined, { month: 'short', day: 'numeric', year: 'numeric' });
}

export class ExploitLedgerPanel extends Panel {
  private data: ExploitLedgerResult | null = null;
  private loading = true;
  private error: string | null = null;
  private filter: LedgerFilter = 'all';
  private refreshInterval: ReturnType<typeof setInterval> | null = null;
  private abortController: AbortController | null = null;

  constructor() {
    super({ id: 'exploit-ledger', title: 'Exploit Ledger', showCount: true });
    void this.fetchData();
    this.refreshInterval = setInterval(() => this.fetchData(), 30 * 60 * 1000); // curated DB, refresh slowly
  }

  public destroy(): void {
    if (this.refreshInterval) { clearInterval(this.refreshInterval); this.refreshInterval = null; }
    this.abortController?.abort();
    this.abortController = null;
    super.destroy();
  }

  private async fetchData(): Promise<void> {
    this.abortController?.abort();
    this.abortController = new AbortController();
    const { signal } = this.abortController;

    try {
      const res = await fetch('/api/exploit-ledger?limit=50', { signal });
      if (!res.ok) throw new Error(`HTTP ${res.status}`);
      this.data = await res.json();
      this.error = null;

      const cacheStatus = res.headers.get('X-Cache');
      this.setDataBadge(cacheStatus === 'HIT' ? 'cached' : 'live');
      this.setErrorState(false);
    } catch (err) {
      if (signal.aborted) return;
      this.error = err instanceof Error ? err.message : 'Failed to fetch';
      this.setDataBadge('unavailable');
      this.setErrorState(true, this.error);
    } finally {
      if (!signal.aborted) {
        this.loading = false;
        this.renderPanel();
      }
    }
  }

  private renderPanel(): void {
    if (this.loading) { this.showLoading('Fetching exploit history...'); return; }
    if (this.error || !this.data) { this.showError(this.error || 'No data'); return; }

    const d = this.data;
    const rows = this.filter === 'bridges' ? d.hacks.filter((h) => h.bridgeHack) : d.hacks;
    this.setCount(rows.length);

    const html = `
      <div class="exploit-summary">
        <div class="exploit-stat">
          <span class="exploit-stat-value negative">${formatUsd(d.summary.lost90d)}</span>
          <span class="exploit-stat-label">Lost (90d)</span>
        </div>
        <div class="exploit-stat">
          <span class="exploit-stat-value negative">${formatUsd(d.summary.lost1y)}</span>
          <span class="exploit-stat-label">Lost (1y)</span>
        </div>
        <div class="exploit-stat">
          <span class="exploit-stat-value">${d.summary.totalHacks}</span>
          <span class="exploit-stat-label">Tracked Hacks</span>
        </div>
      </div>
      <div class="exploit-filter" role="tablist" aria-label="Exploit filter">
        <button class="exploit-filter-btn ${this.filter === 'all' ? 'active' : ''}" data-filter="all" role="tab" aria-selected="${this.filter === 'all'}">All</button>
        <button class="exploit-filter-btn ${this.filter === 'bridges' ? 'active' : ''}" data-filter="bridges" role="tab" aria-selected="${this.filter === 'bridges'}">Bridges</button>
      </div>
      <div class="exploit-list">
        ${rows.length ? rows.map((h) => `
          <div class="exploit-row" title="${escapeHtml(h.technique || h.classification || '')}">
            <div class="exploit-row-main">
              <span class="exploit-name">${escapeHtml(h.name)}</span>
              <span class="exploit-amount">${h.amount != null ? formatUsd(h.amount) : '-'}</span>
            </div>
            <div class="exploit-row-meta">
              <span>${formatDate(h.date)}</span>
              ${h.chains.length ? `<span>${escapeHtml(h.chains.join(', '))}</span>` : ''}
              ${h.classification ? `<span class="exploit-class">${escapeHtml(h.classification)}</span>` : ''}
              ${h.returnedFunds ? `<span class="positive">${formatUsd(h.returnedFunds)} returned</span>` : ''}
            </div>
          </div>`).join('') : '<div class="panel-loading-text">No bridge hacks in the recent window. Widen the filter to All.</div>'}
      </div>
    `;
    this.setContent(html);

    this.content.querySelectorAll<HTMLButtonElement>('.exploit-filter-btn').forEach((btn) => {
      btn.addEventListener('click', () => {
        const next = btn.dataset.filter as LedgerFilter;
        if (next && next !== this.filter) {
          this.filter = next;
          this.renderPanel();
        }
      });
    });
  }
}