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

# getBlock

> 返回已确认区块的身份和交易信息

## 参数

<ParamField query="slot" type="number" required>
  要检索的区块的 slot 编号
</ParamField>

<ParamField query="config" type="object" optional>
  包含以下可选字段的配置对象：

  <Expandable title="config fields">
    <ParamField query="encoding" type="string" optional default="json">
      交易数据的编码格式：

      * `json` - JSON 格式
      * `jsonParsed` - 带有解析交易数据的 JSON 格式
      * `base58` - base-58 编码的二进制数据
      * `base64` - base-64 编码的二进制数据
    </ParamField>

    <ParamField query="transactionDetails" type="string" optional default="full">
      返回的交易详情级别：

      * `full` - 完整交易数据
      * `signatures` - 仅交易签名
      * `none` - 不包含交易数据
    </ParamField>

    <ParamField query="rewards" type="boolean" optional default={true}>
      是否填充奖励数组
    </ParamField>

    <ParamField query="commitment" type="string" optional>
      要查询的银行状态：

      * `processed`：最新区块（未确认）
      * `confirmed`：超级多数确认
      * `finalized`：超级多数最终确认
    </ParamField>

    <ParamField query="maxSupportedTransactionVersion" type="number" optional>
      响应中返回的最大交易版本
    </ParamField>
  </Expandable>
</ParamField>

## 响应

<ResponseField name="result" type="object | null">
  如果未找到区块，返回 `null`。否则返回包含以下内容的对象：

  <Expandable title="result fields">
    <ResponseField name="blockhash" type="string">
      此区块的 blockhash（base-58）
    </ResponseField>

    <ResponseField name="previousBlockhash" type="string">
      上一个区块的 blockhash（base-58）
    </ResponseField>

    <ResponseField name="parentSlot" type="number">
      父区块的 slot 索引
    </ResponseField>

    <ResponseField name="transactions" type="array">
      交易信息数组（如果 `transactionDetails` != "none"）：

      * 对于 `full`：完整的交易对象
      * 对于 `signatures`：仅交易签名
    </ResponseField>

    <ResponseField name="rewards" type="array">
      奖励对象数组（如果 `rewards` = true）：

      * `pubkey`: string - 公钥（base-58）
      * `lamports`: number - 奖励 lamports 数量
      * `postBalance`: number - 奖励后的账户余额
      * `rewardType`: string - 奖励类型
      * `commission`: number - 投票账户佣金（仅投票类型）
    </ResponseField>

    <ResponseField name="blockTime" type="number | null">
      估计的生产时间（Unix 时间戳）
    </ResponseField>

    <ResponseField name="blockHeight" type="number | null">
      此区块下方的区块数量
    </ResponseField>
  </Expandable>
</ResponseField>

## 代码示例

### 基本请求

```bash theme={null}
curl https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY -X POST -H "Content-Type: application/json" -d '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "getBlock",
  "params": [
    430,
    {
      "encoding": "json",
      "transactionDetails": "full",
      "rewards": true
    }
  ]
}'
```

### 带解析交易数据的请求

```bash theme={null}
curl https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY -X POST -H "Content-Type: application/json" -d '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "getBlock",
  "params": [
    430,
    {
      "encoding": "jsonParsed",
      "transactionDetails": "full",
      "rewards": true,
      "maxSupportedTransactionVersion": 0
    }
  ]
}'
```

### 使用 web3.js

```typescript theme={null}
import { Connection } from '@solana/web3.js';

const connection = new Connection('https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY');
const slot = 430;

const block = await connection.getBlock(
  slot,
  {
    maxSupportedTransactionVersion: 0
  }
);
```

## 注意事项

1. 区块生产时间是基于创世区块时间和已经过的 slot 的估计值。
2. 并非所有区块都包含奖励。
3. `jsonParsed` 编码会尝试根据已知的程序布局解析交易指令数据。
4. 某些区块可能被跳过（未分配领导者或区块生产失败）。
5. 区块数据可能会根据账本配置从节点中被修剪。

## 最佳实践

1. 如果只需要交易签名，请使用 `transactionDetails: "signatures"`。
2. 如果不需要奖励数据，请设置 `rewards: false`。
3. 如果需要最新区块，请考虑先使用 `getBlockHeight`。
4. 对于实时更新，请考虑使用 WebSocket 订阅。

## 常见错误

| 错误码    | 消息                              | 解决方案                                |
| ------ | ------------------------------- | ----------------------------------- |
| -32004 | Block not available for slot    | 区块已被修剪或跳过                           |
| -32602 | Invalid param: WrongSize        | 验证 slot 编号是否有效                      |
| -32602 | Invalid param: Too large        | 请求一个更近期的区块                          |
| -32009 | Transaction version unsupported | 指定 `maxSupportedTransactionVersion` |
