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

# 索引 ERC-20 转账

> 以 USDC 为例，创建事件表，在 Notebook 中运行 Pipeline，回填一小段历史并查看转账记录。

本教程将以太坊主网的 USDC 转账整理成事件表，记录发送方、接收方、金额和交易哈希。

<div className="doc-flow">
  `chain.event.eth` → 筛选 USDC Transfer 事件 → 提取转账字段 → 转账事件表
</div>

登录 [Chaintable 控制台](https://chaintable.com/spaces)，进入自己的 Space。本文使用 `demo`，操作时替换为你的 Space ID。

## 1. 创建转账事件表

在 **Tables** 中创建表，名称填 `usdc_transfers`，类型选择 **Block Event**，链选择 **Ethereum**。创建后的表 ID 为 `demo.usdc_transfers.eth`。

<Frame>
  <img src="https://mintcdn.com/opcodelabspteltd/TOA0bb8_WqdBuVVN/images/quickstart/index-erc20-transfers/create-table.png?fit=max&auto=format&n=TOA0bb8_WqdBuVVN&q=85&s=93ac5e746fe567553a5d8a568a30a56c" alt="在 Tables 页面创建 Ethereum 区块事件表" width="1920" height="1080" data-path="images/quickstart/index-erc20-transfers/create-table.png" />
</Frame>

在表的 **Schema** 中添加以下字段，保留系统预置字段和其他默认配置：

| 字段名            | 类型        | 内容         |
| -------------- | --------- | ---------- |
| `tx_id`        | `TXID`    | 交易哈希       |
| `from_address` | `ADDRESS` | 发送方地址      |
| `to_address`   | `ADDRESS` | 接收方地址      |
| `amount`       | `UINT256` | 转账金额的原始整数值 |

<Frame>
  <img src="https://mintcdn.com/opcodelabspteltd/TOA0bb8_WqdBuVVN/images/quickstart/index-erc20-transfers/table-schema.png?fit=max&auto=format&n=TOA0bb8_WqdBuVVN&q=85&s=2c2e30160cd63d91e97891cd831ac538" alt="新建转账表的字段结构，保留默认配置" width="1920" height="1080" data-path="images/quickstart/index-erc20-transfers/table-schema.png" />
</Frame>

## 2. 运行 Pipeline

### 配置 Pipeline

在 **Notebooks** 中创建 `backfill_usdc_transfers`，输入以下代码，将 `SPACE` 改为你的 Space ID：

```python theme={null}
from blockdb import Column, filter
from blockx import Pipeline, Trigger, SOURCE_ROW

SPACE = 'demo'


def parse_usdc_transfer(event):
    sender, recipient, amount = event['params']
    return {
        'id': event['id'],
        'tx_id': event['tx_id'],
        'from_address': sender,
        'to_address': recipient,
        'amount': amount,
    }


pipeline = Pipeline(
    triggers=[
        Trigger(
            table='chain.event.eth',
            func=parse_usdc_transfer,
            params=[SOURCE_ROW],
            operator=filter(
                (Column('contract_id') == '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48')
                & (Column('name') == 'Transfer')
            ),
        ),
    ],
    target_table=f'{SPACE}.usdc_transfers.eth',
)

pipeline.backfill(block_start=25939925, block_end=25939934)
```

这段代码的处理流程是：

* `Trigger` 筛选 USDC 的 Transfer 事件。
* `SOURCE_ROW` 将筛选出的事件传给函数。
* `parse_usdc_transfer` 从已解码的 `params` 提取发送方、接收方和金额。
* Pipeline 将函数返回结果写入 `target_table` 指定的转账表。

这里直接在 Notebook 中定义函数，也可以将 `func` 设为已有 Function 的 ID。

### 运行回填

保存并点击 **Run**，在 **Output** 中查看回填进度和处理结果。

`backfill()` 处理指定的 10 个区块，包含起止高度，完成后自动结束。

<Frame>
  <img src="https://mintcdn.com/opcodelabspteltd/TOA0bb8_WqdBuVVN/images/quickstart/index-erc20-transfers/notebook-backfill.png?fit=max&auto=format&n=TOA0bb8_WqdBuVVN&q=85&s=bb8e1975722c208d8981ac90f225b8fd" alt="backfill_usdc_transfers Notebook 中的回填调用和完成结果" width="1920" height="1080" data-path="images/quickstart/index-erc20-transfers/notebook-backfill.png" />
</Frame>

## 3. 查看转账数据

回到 `usdc_transfers.eth`，在 **Overview** 查看已处理区块。本次只回填了一个局部区间，更早的历史尚未补齐。

<Frame>
  <img src="https://mintcdn.com/opcodelabspteltd/TOA0bb8_WqdBuVVN/images/quickstart/index-erc20-transfers/table-overview.png?fit=max&auto=format&n=TOA0bb8_WqdBuVVN&q=85&s=cb27c8fd2402dc50afde6b666c04945f" alt="默认配置下转账表的处理进度和 event 子表入口" width="1920" height="1080" data-path="images/quickstart/index-erc20-transfers/table-overview.png" />
</Frame>

打开 **Subtables** 下的 **event**，查看转账记录。

<Frame>
  <img src="https://mintcdn.com/opcodelabspteltd/TOA0bb8_WqdBuVVN/images/quickstart/index-erc20-transfers/table-result.png?fit=max&auto=format&n=TOA0bb8_WqdBuVVN&q=85&s=69f3c25c7c1096d40e9f919418f24ff5" alt="event 子表中的真实 USDC 转账记录" width="1920" height="1080" data-path="images/quickstart/index-erc20-transfers/table-result.png" />
</Frame>

每条记录使用源事件的 `id`，区块字段由 Pipeline 自动补齐。

`amount` 保留原始整数值，除以 `10^6` 即为 USDC 数量。

## 可选：持续更新

新建 Notebook `update_usdc_transfers`，复制前面的代码，将最后一行 `pipeline.backfill(...)` 替换为以下调用，保存并运行：

```python theme={null}
pipeline.update()
```

`update()` 默认从最小未完成区块开始补齐历史，追上最新区块后持续更新。

历史区块较多时，也可以在另一个 Notebook 中同时运行 `backfill()`，加快回填。

<Frame>
  <img src="https://mintcdn.com/opcodelabspteltd/TOA0bb8_WqdBuVVN/images/quickstart/index-erc20-transfers/notebook-update.png?fit=max&auto=format&n=TOA0bb8_WqdBuVVN&q=85&s=e1e48ca7c22c67ece066b21aa329b124" alt="update_usdc_transfers Notebook 的持续更新调用和历史区块处理日志" width="1920" height="1080" data-path="images/quickstart/index-erc20-transfers/notebook-update.png" />
</Frame>

选择 **Switch to Background** 可转为后台运行。在账户的 **Instances** 中取消实例即可停止，已写入的数据会保留。
