#!/usr/bin/env bash
# fetch-datasets.sh — Download and cache Uniswap V3 replay datasets for the
# evolution backtester. Re-running is safe: fetch-events.ts has resume support.
#
# Usage:
#   INFURA_API_KEY=<key> ./fetch-datasets.sh
#   # or export the variable beforehand:
#   export INFURA_API_KEY=<key>
#   ./fetch-datasets.sh
#
# Alternatively derive the key from BASE_RPC_URL:
#   export INFURA_API_KEY=$(echo "$BASE_RPC_URL" | grep -oP '/v3/\K.*')

set -euo pipefail

: "${INFURA_API_KEY:?INFURA_API_KEY is required. Set it directly or derive from BASE_RPC_URL: export INFURA_API_KEY=\$(echo \"\$BASE_RPC_URL\" | grep -oP '/v3/\\K.*')}"

export INFURA_API_KEY

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

cd "$SCRIPT_DIR"

mkdir -p cache

echo "=== Fetching HIGHER/WETH (last 30 days) ==="
npx tsx fetch-events.ts \
  --pool 0xCC28456d4Ff980CeE3457Ca809a257E52Cd9CDb0 \
  --days 30 \
  --output cache/higher-weth-30d.jsonl

echo "=== Fetching DEGEN/WETH (last 30 days) ==="
npx tsx fetch-events.ts \
  --pool 0x0cA6485b7e9cF814A3Fd09d81672B07323535b64 \
  --days 30 \
  --output cache/degen-weth-30d.jsonl

echo "=== Fetching TOSHI/WETH (last 30 days) ==="
npx tsx fetch-events.ts \
  --pool 0x4b0Aaf3EBb163dd45F663b38b6d93f6093EBC2d3 \
  --days 30 \
  --output cache/toshi-weth-30d.jsonl

echo ""
echo "=== Done. Event counts: ==="
for f in cache/higher-weth-30d.jsonl cache/degen-weth-30d.jsonl cache/toshi-weth-30d.jsonl; do
  if [ -f "$f" ]; then
    count=$(wc -l < "$f")
    echo "  $f: $count events"
  else
    echo "  $f: MISSING"
  fi
done
