File size: 5,110 Bytes
8f82127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ca0b3cde",
   "metadata": {},
   "source": [
    "# POL to Binance Netflow\n",
    "\n",
    "This notebook visualizes **cumulative netflow** of native POL on Polygon to/from Binance.\n",
    "\n",
    "**How it works**\n",
    "- Reads `netflow` table to draw a time series of the cumulative netflow.\n",
    "- Optionally shows a simple *live* refresh loop (press the stop button or interrupt kernel to stop).\n",
    "\n",
    "\n",
    "> Keep your indexer running in another terminal while you watch this notebook update."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c13974d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "import sqlite3\n",
    "from decimal import Decimal\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from IPython.display import display, clear_output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef22f569",
   "metadata": {},
   "outputs": [],
   "source": [
    "DB_PATH = \"pol_indexer.sqlite\"\n",
    "POLL_SECONDS = float(os.getenv(\"POLL_SECONDS\", \"2.0\"))  # refresh interval for live chart\n",
    "\n",
    "print(\"DB_PATH =\", DB_PATH)\n",
    "print(\"POLL_SECONDS =\", POLL_SECONDS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea246fed",
   "metadata": {},
   "outputs": [],
   "source": [
    "def _connect(db_path: str):\n",
    "    conn = sqlite3.connect(db_path, timeout=10)\n",
    "    conn.row_factory = sqlite3.Row\n",
    "    return conn\n",
    "\n",
    "def load_netflow(db_path: str) -> pd.DataFrame:\n",
    "    with _connect(db_path) as c:\n",
    "        df = pd.read_sql_query(\n",
    "            \"\"\"\n",
    "            SELECT created_at, cumulative_value\n",
    "            FROM netflow\n",
    "            ORDER BY created_at ASC\n",
    "            \"\"\", c\n",
    "        )\n",
    "    # Convert to numeric & datetime\n",
    "    if not df.empty:\n",
    "        df[\"created_at_dt\"] = pd.to_datetime(df[\"created_at\"], unit=\"s\")\n",
    "        # Convert decimal strings safely to float for plotting\n",
    "        df[\"cumulative_float\"] = df[\"cumulative_value\"].astype(float)\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88d0261a",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = load_netflow(DB_PATH)\n",
    "if df.empty:\n",
    "    print(\"No netflow data yet. Keep the indexer running; this will populate as Binance touches happen.\")\n",
    "else:\n",
    "    fig = plt.figure(figsize=(8,4))\n",
    "    plt.plot(df[\"created_at_dt\"], df[\"cumulative_float\"])\n",
    "    plt.title(\"Cumulative POL Netflow to Binance (since indexer start)\")\n",
    "    plt.xlabel(\"Time\")\n",
    "    plt.ylabel(\"Cumulative Netflow (POL)\")\n",
    "    plt.grid(True, which=\"both\", axis=\"both\")\n",
    "    display(fig)\n",
    "    plt.close(fig)\n",
    "\n",
    "    latest = df.iloc[-1]\n",
    "    print(\"Latest cumulative:\", latest[\"cumulative_value\"], \"POL at\", latest[\"created_at_dt\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b96adcf1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run this cell to start a live loop. Stop the cell to end.\n",
    "try:\n",
    "    while True:\n",
    "        df = load_netflow(DB_PATH)\n",
    "        clear_output(wait=True)\n",
    "        if df.empty:\n",
    "            print(\"No netflow data yet. Waiting for new rows...\")\n",
    "        else:\n",
    "            fig = plt.figure(figsize=(8,4))\n",
    "            plt.plot(df[\"created_at_dt\"], df[\"cumulative_float\"])\n",
    "            plt.title(\"Cumulative POL Netflow to Binance (LIVE)\")\n",
    "            plt.xlabel(\"Time\")\n",
    "            plt.ylabel(\"Cumulative Netflow (POL)\")\n",
    "            plt.grid(True, which=\"both\", axis=\"both\")\n",
    "            display(fig)\n",
    "            plt.close(fig)\n",
    "\n",
    "            latest = df.iloc[-1]\n",
    "            print(\"Latest cumulative:\", latest[\"cumulative_value\"], \"POL at\", latest[\"created_at_dt\"])\n",
    "            print(\"Rows:\", len(df))\n",
    "        time.sleep(POLL_SECONDS)\n",
    "except KeyboardInterrupt:\n",
    "    print(\"Live loop stopped.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82ebd3ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = load_netflow(DB_PATH)\n",
    "display(df.tail(10))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv (3.13.3)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}