# Crypto — Cookbook

> Short, copy-pasteable tasks for the Solana BDN: subscribe to feeds, submit transactions, target leaders, and tune dedup over QUIC.

Task-sized snippets for the QUIC-only Solana BDN. Feeds use the raw MoQT client
(`@clutchcall/sdk/moqt`); transactions use the HTTP/3 submit API. There is no
typed `Crypto` client yet — see [SDK methods](./sdk-methods).

## Connect to the feed relay

Dial the feed relay once and reuse the client for every track. It auto-reconnects.

  <Tab title="TypeScript">
```ts
import { MoqtClient } from "@clutchcall/sdk/moqt";

const client = await MoqtClient.connect(
  "quic://feeds.clutchcall.dev",
  process.env.CLUTCHCALL_CREDENTIALS,
);
```
  </Tab>
  <Tab title="Python">
```python
from clutchcall.moqt import MoqtClient

client = MoqtClient.connect("quic://feeds.clutchcall.dev", auth_token="…")
```
  </Tab>

## Subscribe to a feed track

Each public mainnet track lives under `sol/feed/mainnet`. Decode the frame
payload as the track's schema.

```ts
const sub = client.subscribeFrame("sol/feed/mainnet", "slots", (_ts, _p, data) => {
  const { slot } = JSON.parse(new TextDecoder().decode(data));
  console.log("slot", slot);
});
```

## Stream recent blockhash

The `blockhash` track carries `{ slot, blockhash, lastValidBlockHeight }` — keep
the latest to build transactions against a fresh blockhash.

```ts
let recent = { blockhash: "", lastValidBlockHeight: 0 };
client.subscribeFrame("sol/feed/mainnet", "blockhash", (_ts, _p, data) => {
  recent = JSON.parse(new TextDecoder().decode(data));
});
```

## Track the priority-fee market

The `priorityFee` track carries `{ slot, p50, max }` — size your compute-unit
price off the live p50 instead of guessing.

```ts
client.subscribeFrame("sol/feed/mainnet", "priorityFee", (_ts, _p, data) => {
  const { p50, max } = JSON.parse(new TextDecoder().decode(data));
  console.log("priority fee µ-lamports — p50", p50, "max", max);
});
```

## Submit a transaction with BDN fan-out

Hand the signed tx to the BDN once; it dedups and lands it on every edge's
nearby leaders. `leader=auto` resolves the schedule for you.

```bash
curl --http3 \
  "https://crypto.clutchcall.dev/bdn/submit?leader=auto" \
  -H "Authorization: Bearer $CLUTCHCALL_CREDENTIALS" \
  -H "Content-Type: application/octet-stream" \
  --data-binary @tx.bin
```

## Submit straight to the TPU (no fan-out)

Skip the mesh and land on the current + next leader from this node directly.

```bash
curl --http3 \
  "https://crypto.clutchcall.dev/tpu?host=auto" \
  -H "Authorization: Bearer $CLUTCHCALL_CREDENTIALS" \
  -H "Content-Type: application/octet-stream" \
  --data-binary @tx.bin
```

## Target a specific validator leader

Pin the land to one validator's TPU instead of auto-resolving the schedule.

```bash
curl --http3 \
  "https://crypto.clutchcall.dev/tpu?host=<validator-host>&port=<quic-port>" \
  -H "Authorization: Bearer $CLUTCHCALL_CREDENTIALS" \
  -H "Content-Type: application/octet-stream" \
  --data-binary @tx.bin
```

## Submit from code over HTTP/3

The body is the raw signed transaction bytes — no JSON envelope.

```python
import os, httpx
res = httpx.post(
    "https://crypto.clutchcall.dev/bdn/submit",
    params={"leader": "auto"},
    headers={
        "Authorization": f"Bearer {os.environ['CLUTCHCALL_CREDENTIALS']}",
        "Content-Type": "application/octet-stream",
    },
    content=signed_tx,
)
print(res.json())   # {"ok": true, "sig": "…"}
```

## Deduplicate your own re-submits

The BDN dedups on the **first 64 bytes** (the Solana signature). Re-sending the
same signed tx across edges is safe — it lands at most once per leader within the
dedup TTL, so a retry loop won't double-land.

```ts
async function submitWithRetry(tx: Uint8Array, attempts = 3) {
  for (let i = 0; i < attempts; i++) {
    const r = await fetch(`https://crypto.clutchcall.dev/bdn/submit?leader=auto`, {
      method: "POST",
      headers: {
        Authorization: `Bearer ${process.env.CLUTCHCALL_CREDENTIALS}`,
        "Content-Type": "application/octet-stream",
      },
      body: tx,
    });
    if ((await r.json()).ok) return;          // dedup makes the retry idempotent
  }
  throw new Error("tx did not land");
}
```

## Subscribe to your private feed

A private feed (your own gossip/Geyser ingest) is published under
`sol/feed/<orgId>`. Subscribe to its `slots` track with an org-scoped token.

```ts
const orgNs = `sol/feed/${orgId}`;
client.subscribeFrame(orgNs, "slots", (_ts, _p, data) => {
  console.log("private slot", JSON.parse(new TextDecoder().decode(data)).slot);
});
```

## Subscribe before the feed exists

The relay holds a subscribe for a namespace that hasn't announced yet, so you can
subscribe to a private feed before your ingest has published its first slot —
frames start flowing the moment it does. No special call needed; just subscribe
early.

## React to relay reconnects

The client replays subscriptions automatically; use `onState` only for logging
or to pause your own work while the link is down.

```ts
const client = await MoqtClient.connect(
  "quic://feeds.clutchcall.dev",
  process.env.CLUTCHCALL_CREDENTIALS,
  (state, reason) => {
    if (state /* Reconnecting */) console.warn("feed relay flap:", reason);
  },
);
```

## Tear down cleanly

Close a single track, or the whole client.

```ts
const sub = client.subscribeFrame("sol/feed/mainnet", "slots", () => {});
sub.close();      // unsubscribe one track
client.close();   // tear down the session + all subscriptions
```
