# Cookbook

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

This page has task-sized snippets for the QUIC-only Solana BDN. Feeds use the
raw MoQT client (`clutchcall_connect/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. Reuse the client for every track. The client
reconnects automatically.

  
```ts
import { MoqtClient } from "clutchcall_connect/moqt";

const client = await MoqtClient.connect(
  "quic://feeds.firstbyte.fast",
  process.env.CLUTCHCALL_CREDENTIALS,
);
```
  
  
```python
from clutchcall.moqt import MoqtClient

client = MoqtClient.connect("quic://feeds.firstbyte.fast", auth_token="…")
```
  

## 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 object 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 }`. Set your compute-unit
price from the live p50. Do not guess.

```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

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

```bash
curl --http3 \
  "https://crypto.firstbyte.fast/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. Land on the current + next leader directly from this node.

```bash
curl --http3 \
  "https://crypto.firstbyte.fast/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.firstbyte.fast/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. There is no JSON envelope.

```python
import os, httpx
res = httpx.post(
    "https://crypto.firstbyte.fast/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). It is safe to
send the same signed tx again across edges. The tx lands at most once per
leader within the dedup TTL. Thus a retry loop does not double-land.

```ts
async function submitWithRetry(tx: Uint8Array, attempts = 3) {
  for (let i = 0; i < attempts; i++) {
    const r = await fetch(`https://crypto.firstbyte.fast/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

The BDN publishes a private feed (your own gossip/Geyser ingest) 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 is not announced yet. Thus you
can subscribe to a private feed before your ingest publishes its first slot.
Frames start to flow the moment the ingest publishes. You do not need a special
call. 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.firstbyte.fast",
  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
```
