> ## Documentation Index
> Fetch the complete documentation index at: https://docs.solanatracker.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 通过 gRPC 订阅 Solana slot 与区块

> 选择 slot 更新、区块元数据或筛选后的区块数据，跟踪确认状态并处理数据间隔。

Slot 更新用于进度和状态，区块元数据用于哈希与计数；需要交易或账户写入时再使用完整区块。

先将[快速入门中的辅助代码](/cn/yellowstone-grpc/quickstart)保存为同一目录的 `stream.mts`。以下示例使用相同的环境变量与依赖。

## 订阅 slot 与区块元数据

```typescript slots.mts theme={null}
import { subscribe } from "./stream.mts";

for await (const update of subscribe({
  slots: { slots: { filterByCommitment: true } },
  blocksMeta: { blocks: {} },
})) {
  if (update.slot) {
    console.log("slot", update.slot.slot, update.slot.parent, update.slot.status);
  }
  if (update.blockMeta) {
    console.log("block", update.blockMeta.slot, update.blockMeta.blockhash,
      update.blockMeta.executedTransactionCount);
  }
}
```

```bash theme={null}
npx tsx slots.mts
```

`filterByCommitment: true` 将 slot 通知与请求的确认级别对齐。一个 slot 可有多个状态更新，记录变化时同时保存 slot 与状态。

## 请求筛选后的区块

```typescript filtered-blocks.mts theme={null}
import { subscribe } from "./stream.mts";

const address = process.env.WATCH_ADDRESS;
if (!address) throw new Error("Set WATCH_ADDRESS");

for await (const update of subscribe({
  blocks: {
    selected: {
      accountInclude: [address],
      includeTransactions: true,
      includeAccounts: false,
      includeEntries: false,
    },
  },
})) {
  if (!update.block) continue;
  console.log(update.block.slot, update.block.transactions.length);
}
```

三个 include 标志分别控制交易、账户和 entry。筛选后的交易数组长度可能小于整个区块的已执行交易数。

## 处理缺失的 slot

Slot 是出块机会，部分 slot 不产生区块。断线、筛选或延迟也可能造成间隔。将确认级别与 RPC 可用区块进行比较后，再判断是否丢失数据。测量实际间隔，不要假设固定交付时间。

## 相关指南

<CardGroup cols={2}>
  <Card title="Entry 元数据" href="/cn/yellowstone-grpc/entry-monitoring" />

  <Card title="可靠性与性能" href="/cn/yellowstone-grpc/best-practices" />
</CardGroup>
