{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chart-order-book",
  "title": "Order Book",
  "description": "Cumulative bid and ask depth around the mid price in your chart colors, updated every second.",
  "dependencies": [
    "@wick-charts/react@^0.9.2"
  ],
  "registryDependencies": [
    "card",
    "https://wick-charts.eeff.io/r/wick-chart.json"
  ],
  "files": [
    {
      "path": "blocks/chart-order-book.tsx",
      "content": "\"use client\"\n\nimport { useEffect, useState } from \"react\"\n\nimport {\n  Crosshair,\n  type DepthLevel,\n  DepthSeries,\n  NumberFlow,\n  Tooltip,\n  XAxis,\n  YAxis,\n  formatSignificant,\n} from \"@wick-charts/react\"\n\nimport {\n  Card,\n  CardAction,\n  CardContent,\n  CardDescription,\n  CardHeader,\n  CardTitle,\n} from \"@/components/ui/card\"\nimport { WickChart } from \"@/components/ui/wick-chart\"\n\nconst LEVELS = 40\nconst TICK_SIZE = 0.5\nconst TICK_MS = 1000\n\ninterface Book {\n  mid: number\n  bids: DepthLevel[]\n  asks: DepthLevel[]\n}\n\n// The mid price format; change it for your market. Below 1, `formatSignificant` keeps\n// the digits a small price needs (0.0₆95), as the library default does.\nconst formatPrice = (value: number) =>\n  Math.abs(value) < 1\n    ? formatSignificant(value)\n    : value.toLocaleString(\"en-US\", {\n        minimumFractionDigits: 2,\n        maximumFractionDigits: 2,\n      })\n\n// Seeded, so the server render and the browser start from the same book.\nfunction seededRandom(seed: number): () => number {\n  let state = seed\n\n  return () => {\n    state = (state * 1_664_525 + 1_013_904_223) % 4_294_967_296\n\n    return state / 4_294_967_296\n  }\n}\n\n// Demo feed: the mid drifts and every level gets a fresh size each tick.\nfunction levelSize(distance: number, random: () => number): number {\n  return (0.4 + distance * 0.08) * (0.5 + random())\n}\n\nfunction makeBook(mid: number, random: () => number = Math.random): Book {\n  const bids: DepthLevel[] = []\n  const asks: DepthLevel[] = []\n\n  for (let i = 1; i <= LEVELS; i++) {\n    bids.push([mid - i * TICK_SIZE, levelSize(i, random)])\n    asks.push([mid + i * TICK_SIZE, levelSize(i, random)])\n  }\n\n  return { mid, bids, asks }\n}\n\nfunction nextBook(book: Book): Book {\n  const step = Math.round((Math.random() - 0.5) * 2) * TICK_SIZE\n\n  return makeBook(book.mid + step)\n}\n\nexport function ChartOrderBook() {\n  const [book, setBook] = useState(() => makeBook(3_450, seededRandom(3_450)))\n\n  useEffect(() => {\n    const timer = setInterval(() => setBook(nextBook), TICK_MS)\n\n    return () => clearInterval(timer)\n  }, [])\n\n  return (\n    <Card>\n      <CardHeader>\n        <CardTitle>ETH / USD order book</CardTitle>\n        <CardDescription>\n          Cumulative depth, {LEVELS} levels a side\n        </CardDescription>\n        <CardAction className=\"text-right\">\n          <NumberFlow\n            value={book.mid}\n            format={formatPrice}\n            className=\"text-2xl font-semibold tabular-nums\"\n          />\n          <div className=\"text-xs text-muted-foreground\">mid</div>\n        </CardAction>\n      </CardHeader>\n      <CardContent>\n        <WickChart\n          className=\"aspect-auto h-[280px]\"\n          axis={{ x: { type: \"numeric\" }, y: { min: 0 } }}\n        >\n          <DepthSeries bids={book.bids} asks={book.asks} />\n          <XAxis />\n          <YAxis />\n          <Crosshair />\n          <Tooltip />\n        </WickChart>\n      </CardContent>\n    </Card>\n  )\n}\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "charts",
    "finance"
  ],
  "type": "registry:block"
}