← Back
β˜†
import { useState, useEffect } from 'react';

/**
 * For the copied leaders, fetch their live Polymarket holdings (Data API) and
 * index them by conditionId. Lets the Portfolio show "which whales hold how much
 * in this market" on each position card β€” i.e. every slon you're riding, so a
 * multi-whale market reads as a consensus signal.
 *
 * Attribution is approximate (matches ALL followed leaders holding the same market,
 * sorted by holding size). Exact copy→leader attribution will come later from the
 * daemon ledger β€” see project TODO.
 */
export interface LeaderHolding {
  address: string;
  value: number;   // leader's current $ value in this market
  initial: number; // leader's ENTRY bet (survives resolution)
  price: number;   // leader's current mark price
  size: number;    // leader's share count
}

export function useLeaderHoldings(leaders: string[], getToken?: () => Promise<string | null>) {
  // Stable key so the effect only re-runs when the actual followed set changes.
  const key = leaders.map(a => a.toLowerCase()).sort().join(',');
  const [map, setMap] = useState<Record<string, LeaderHolding[]>>({});

  useEffect(() => {
    if (!key || !getToken) return;
    let alive = true;
    (async () => {
      try {
        // Server-side fan-out (Variant A): the backend fetches every active leader's holdings
        // from its own IP with retry + a 30s cache, so the browser no longer fires ~20 parallel
        // Data API calls that Polymarket rate-limited (dropping ~2 cards' whale volume per load).
        const token = await getToken();
        if (!token) return;
        const r = await fetch('/api/copy/leader-holdings', { headers: { authorization: `Bearer ${token}` } });
        const d = await r.json();
        if (alive && d?.success && d.data) setMap(d.data as Record<string, LeaderHolding[]>);
      } catch { /* keep last good map */ }
    })();
    return () => { alive = false; };
  }, [key, getToken]);

  // When there are no leaders, expose an empty map without a synchronous reset
  // inside the effect (the stale map is harmless β€” it's never read with no key).
  return key ? map : {};
}

πŸ“œ Git History

b9c19bfchore(poli): reconcile local Flow/Insider/manual-trade work with deployed state3 days ago
6c47fa4chore: local Polikopi project home + Phase 1 redesign artifacts12 days ago
Show last diff
Loading...