Skip to main content
Glama
stocksentry

stocksentry

by yayguru
README.md
# StockSentry

**Is it safe to price this tokenized stock right now?**

StockSentry is an autonomous research and alert agent for the Coinbase tokenized stocks on Base. Before an app, a lending market, or another AI agent prices, lends against, liquidates, or settles a tokenized stock, it asks StockSentry. StockSentry answers with a verdict and the evidence behind it.

- **Live demo:** https://stocksentry.merjkerm.workers.dev
- **MCP endpoint:** `https://stocksentry.merjkerm.workers.dev/mcp`
- Built for **Base Stocks Maxxing Season**, Agentic Stock Finance track.

> Research only. StockSentry reads public data. It has no wallet, signs nothing, sends no transactions, and does not enable trading for anyone. Not investment advice. Not affiliated with Coinbase, Base, or Chainlink.

## Why this exists

The Base docs are blunt about tokenized-stock prices:

- Chainlink NAV feeds run 24/5, **hold the last value** on nights, weekends and holidays, and **freeze during corporate actions**.
- "Always read `updatedAt` and apply staleness bounds before relying on the price; **never settle or liquidate against a frozen feed**."
- One token is not always one share. The **multiplier** changes on dividends and splits, sometimes on a schedule (ERC-8056 `effectiveAt`).

So every protocol that touches tokenized stocks has to rebuild the same guard: market calendar, heartbeat math, multiplier and pause checks, API vs chain reconciliation. StockSentry does it once, continuously, for all of them, and exposes it to humans (dashboard), code (REST, webhooks, Atom) and agents (MCP).

## What it does every 5 minutes

1. **Observe.** Pulls all tokenized-stock records from the public Coinbase Tokenized Stocks API (435 listed at the time of writing) and the NAV feed list.
2. **Verify onchain.** Reads Base mainnet through one Multicall3 `eth_call`: Chainlink `latestRoundData` for every feed, plus B20 `totalSupply`, `uiMultiplier`, `newUIMultiplier`, `effectiveAt` and `pausedFeatures` for every minted token.
3. **Judge.** Gives each token a price-safety verdict:

| Verdict | Meaning |
|---|---|
| `OK_TO_PRICE` | Feed fresh inside the 24/5 window, API and chain agree, no pause, no flip pending |
| `CAUTION` | Something disagrees (API NAV vs onchain, API supply vs onchain, multiplier) |
| `DISPLAY_ONLY` | Market window closed, so the feed is holding its last value. Show it, don't settle on it |
| `DO_NOT_SETTLE` | Heartbeat missed in *open* market hours, transfers paused, or a multiplier flip within 24h |
| `NO_FEED` | Minted on Base but no Chainlink NAV feed is listed |

   Staleness is measured in **open market seconds** (24/5, NYSE holidays and early closes from nyse.com), so a Friday close read on Sunday is not a false alarm, and a feed that missed its 24h heartbeat on a Tuesday is.
4. **Track Authorized-Participant flows.** Scans B20 mint (`from = 0x0`) and burn (`to = 0x0`) Transfer logs for every minted token and nets creations vs redemptions over 24h, valued at the onchain NAV where a feed exists.
5. **Watch corporate actions.** Scans `Announcement` and `UIMultiplierUpdated` events and resolves the corporate action through the API (`/corporate-actions/{uuid}`).
6. **Pre-listing radar.** Finds NAV feeds whose token is not in the public API yet and reads the B20 contract's `symbol()` / `name()` / `totalSupply()` onchain.
7. **Diff and alert.** Compares with the previous snapshot and emits typed events: `NEW_LISTING`, `FIRST_MINT`, `CREATION`, `REDEMPTION`, `MULTIPLIER_SCHEDULED`, `MULTIPLIER_APPLIED`, `PAUSE_CHANGED`, `SAFETY_CHANGED`, `ANNOUNCEMENT`, `FEED_BEFORE_LISTING`. Events go to webhooks and an Atom feed.
8. **Brief.** Writes a research brief from the computed facts. An optional Workers AI rewrite is **rejected if it contains any number that is not in the facts**, so the agent cannot invent figures.

## What it found on day one (27 Nov 2026, live data)

- 435 tokenized stocks listed by the API, 138 with supply on Base, 14 NAV feeds.
- **125 minted tokens have no Chainlink NAV feed.** Any app pricing those from an oracle has nothing to read.
- **COINc (Coinbase Global Inc.)** already has a B20 contract and a live Chainlink feed, but zero supply and no entry in the public API.
- The API's `total_supply` briefly lagged the chain for GOOGLc during a run of redemptions, which StockSentry flagged as `CAUTION`.
- 24h AP flows on the tracked set: GOOGLc was the largest net redemption (about $73k of burns), NVDAc and SPCXc the largest net creations.

These numbers change every cycle. The dashboard shows the current ones.

## Use it

### From an agent (MCP, Streamable HTTP)

```json
{ "mcpServers": { "stocksentry": { "url": "https://stocksentry.merjkerm.workers.dev/mcp" } } }
```

Tools: `price_safety`, `list_tokens`, `ap_flows`, `recent_events`, `daily_brief`, `ask`.

```bash
curl -s https://stocksentry.merjkerm.workers.dev/mcp -H 'content-type: application/json' \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"price_safety","arguments":{"symbol":"NVDAc"}}}'
```

### REST

| Endpoint | What |
|---|---|
| `GET /api/safety/{symbol}` | Live verdict for one token (fresh onchain read). `?at=ISO` re-judges the same live data on a simulated clock, e.g. a Saturday |
| `GET /api/safety` | Latest verdicts for every tracked token |
| `GET /api/flows` | 24h net AP creations and redemptions |
| `GET /api/events` | Detected events (`?type=`, `?since=`) |
| `GET /api/brief` | Brief, its facts, and whether the LLM text passed the number check |
| `POST /api/ask` | `{"q":"Biggest redemptions today?"}` |
| `POST /api/subscribe` | `{"url":"https://…","symbols":["NVDAc"],"minSeverity":"high"}` webhook alerts |
| `GET /feed.xml` | Atom feed of events |

## Architecture

```
Cloudflare Worker (cron */5)
 ├─ Coinbase Tokenized Stocks API (public, read-only)
 ├─ Base mainnet RPC: Multicall3 eth_call (feeds + B20 state)
 ├─ Base mainnet logs: mint / burn / Announcement / UIMultiplierUpdated
 ├─ D1: snapshot, events, flows, subscribers
 ├─ Workers AI (optional): brief prose, number-grounded
 └─ Surfaces: dashboard · REST · MCP · webhooks · Atom
```

No dependencies at runtime. ABI encoding, Multicall3 and log decoding are small hand-written helpers in `src/abi.js`, covered by tests that check every selector against keccak256.

## Run it

```bash
npm install
npm test                 # 22 unit + integration tests, no network
npm run live             # one read-only pass against the real API and Base mainnet
node scripts/live-cycle.mjs   # three full agent cycles locally, writes preview/index.html
npx wrangler dev         # local Worker (needs a D1 binding, see wrangler.toml)
```

## Built during the Quest

Everything in this repository was written during Stocks Maxxing Season (first commit 27 Nov 2026). No earlier code.

## Compliance notes

- Read-only: public HTTP API, `eth_call` and `eth_getLogs`. No keys, no wallet, no transactions.
- Does not offer, route or execute trades. Coinbase tokenized stocks are not available to US persons; StockSentry doesn't change that or enable trading for anyone.
- Does not claim any rights in the underlying companies.

## License

MIT