---
title: "trace_filter"
description: "返回匹配区块和地址过滤条件的 trace（Ethereum）"
url: "https://blockreq.com/docs/zh-cn/build/api-reference/ethereum/trace_filter/"
---

# trace_filter

**网络：** Ethereum · 主网 · Chain ID `1`（`0x1`）

**计为 5 个 Request。** 计费规则见 [Request Counting](https://blockreq.com/docs/zh-cn/pricing/request-counting/index.md)。

`trace_filter` 返回匹配区块范围和可选地址过滤条件的 trace。它是付费 trace 方法，Free 不可用，宽范围历史查询成本较高。

## 参数

1. `Object` - 过滤对象：

| 字段          | 类型            | 说明             |
| ------------- | --------------- | ---------------- |
| `fromBlock`   | `QUANTITY\|TAG` | 起始区块。       |
| `toBlock`     | `QUANTITY\|TAG` | 结束区块。       |
| `fromAddress` | `Array<DATA>`   | 可选发送方地址。 |
| `toAddress`   | `Array<DATA>`   | 可选接收方地址。 |
| `after`       | `QUANTITY`      | 可选分页偏移。   |
| `count`       | `QUANTITY`      | 可选分页大小。   |

## 请求

```json
{
  "jsonrpc": "2.0",
  "method": "trace_filter",
  "params": [
    {
      "fromBlock": "0x1",
      "toBlock": "0x10",
      "count": "0x10"
    }
  ],
  "id": 1
}
```

## 代码示例

**cURL**

```bash
curl -X POST https://ethereum-rpc.blockreq.com/v1/rpc/public \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","method":"trace_filter","params":[{"fromBlock":"0x1","toBlock":"0x10","count":"0x10"}],"id":1}'
```

**TypeScript**

```typescript
const response = await fetch("https://ethereum-rpc.blockreq.com/v1/rpc/public", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
  "jsonrpc": "2.0",
  "method": "trace_filter",
  "params": [
    {
      "fromBlock": "0x1",
      "toBlock": "0x10",
      "count": "0x10"
    }
  ],
  "id": 1
}),
});
const data = await response.json();
console.log(data.result);
```

**Python**

```python
import requests

response = requests.post(
    "https://ethereum-rpc.blockreq.com/v1/rpc/public",
    json={
  "jsonrpc": "2.0",
  "method": "trace_filter",
  "params": [
    {
      "fromBlock": "0x1",
      "toBlock": "0x10",
      "count": "0x10"
    }
  ],
  "id": 1
}
)
print(response.json()["result"])
```

**Go**

```go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
)

func main() {
	payload := []byte(`{"jsonrpc":"2.0","method":"trace_filter","params":[{"fromBlock":"0x1","toBlock":"0x10","count":"0x10"}],"id":1}`)
	resp, err := http.Post(
		"https://ethereum-rpc.blockreq.com/v1/rpc/public",
		"application/json",
		bytes.NewBuffer(payload),
	)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	body, _ := io.ReadAll(resp.Body)
	var result map[string]interface{}
	json.Unmarshal(body, &result)
	fmt.Println(result["result"])
}
```

**viem**

```typescript
import { createPublicClient, http } from "viem";

const client = createPublicClient({
  transport: http("https://ethereum-rpc.blockreq.com/v1/rpc/public"),
});

const result = await client.request({
  method: "trace_filter",
  params: [{"fromBlock":"0x1","toBlock":"0x10","count":"0x10"}],
});
console.log(result);
```

**Foundry**

```bash
cast rpc --rpc-url https://ethereum-rpc.blockreq.com/v1/rpc/public trace_filter {"fromBlock":"0x1","toBlock":"0x10","count":"0x10"}
```

**Hardhat**

```javascript
const { ethers } = require("hardhat");

async function main() {
  const provider = new ethers.JsonRpcProvider("https://ethereum-rpc.blockreq.com/v1/rpc/public");
  const result = await provider.send("trace_filter", [{"fromBlock":"0x1","toBlock":"0x10","count":"0x10"}]);
  console.log(result);
}

main();
```
