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

# Lido 验证者退出通知

> 学习如何使用 Tenderly Web3 Actions 监控 Lido 验证者退出请求，并使用开源监控模板通过 Telegram 发送通知。

在本教程中，我们将向您展示如何设置 **Tenderly Web3 Action 来监控 Lido 验证者退出请求** 并通过 Telegram 发送通知。本项目演示了如何使用 Web3 Actions 与智能合约和外部 API 交互以创建自定义通知系统。

使用此 GitHub 项目模板来设置您自己的 Lido 验证者退出请求监控：

<Card title="验证者监控 Web3 Action 模板" href="" />

## 项目概览

本项目的目的是设置一个系统来监控 Lido 验证者退出请求，并在满足特定条件时发送通知。

以下是项目流程的简要说明：

1. **订阅相关链上事件**：Web3 Action 监听以太坊主网上 Lido 合约的 `ValidatorExitRequest` 事件。
2. **使用 Tenderly API 解码事件**：检测到事件时，使用 Tenderly API 获取事务详情。
3. **过滤特定感兴趣的事件**：进一步评估 `ValidatorExitRequest` 事件，检查 `stakingModuleId` 是否为 1 以及 `nodeOperatorId` 是否为 14。
4. **响应**：如果感兴趣的 `ValidatorExitRequest` 发生，Web3 Action 将向指定的 Telegram 频道发送包含验证者退出请求详情的通知。

## 前提条件

在开始之前，请确保您已具备：

* 已安装 [Tenderly CLI](https://github.com/Tenderly/tenderly-cli)
* [Node.js](https://nodejs.org/)（当前支持 v20）
* [npm](https://www.npmjs.com/)
* 一个 Telegram bot token 和频道 ID
* Tenderly 账户和项目

### 设置

让我们一步步设置您的 Lido 验证者退出通知系统：

<Steps>
  ### 克隆仓库

  首先，克隆项目仓库并导航到项目目录：

  ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  git clone https://github.com/Tenderly/tenderly-lido-validator-monitoring-solution
  cd tenderly-lido-validator-monitoring-solution
  ```

  ### 安装依赖

  安装所需的 npm 包：

  ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install
  ```

  ### 配置 Tenderly 项目

  按照以下步骤设置您的 Tenderly 项目：

  1. 登录您的 Tenderly 账户
  2. 创建新项目或使用现有项目
  3. 记下您的 [**Account Name 和 Project Slug**](/platform/account/projects/slug)

  ### 配置 tenderly.yaml

  使用您的 Tenderly 项目信息更新 `tenderly.yaml` 文件：

  ```yaml showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  account_id: "<YOUR_ACCOUNT_ID>"
  actions:
    <YOUR_ACCOUNT_ID>/<YOUR_PROJECT_SLUG>:
      runtime: v2
      sources: actions
      specs:
        action_name:
          description: Get a notification when condition matches on Lido tx
          function: lidoEvents:subscribeToLidoValidatorExitRequestFn
          execution_type: parallel
          trigger:
            type: transaction
            transaction:
              status:
                - mined
              filters:
                - network: 1
                  eventEmitted:
                    contract:
                      address: 0x0de4ea0184c2ad0baca7183356aea5b8d5bf5c6e
                    name: ValidatorExitRequest
  project_slug: "<YOUR_PROJECT_SLUG>"
  ```

  请务必将 `<YOUR_ACCOUNT_ID>` 和 `<YOUR_PROJECT_SLUG>` 替换为您实际的 Tenderly **Account Name** 和 **Project Slug**。

  ### 将合约地址添加到项目

  为了能够部署 action 并监控目标合约，您需要将该合约添加到项目。

  在本例中，我们要添加 **`0x0de4ea0184c2ad0baca7183356aea5b8d5bf5c6e`**。

  ### 设置 Secrets

  将以下 secrets 添加到您的 Web3 Actions：

  * `BEARER`：您的 Tenderly API bearer token
  * `BOT-TOKEN`：您的 Telegram bot token
  * `CHANNEL-ID`：您的 Telegram 频道 ID

  要添加 secrets，前往您项目内的 Web3 Actions 并导航到 [Secrets](/monitoring/web3-actions/references/context#secrets) 部分。

  ### 部署

  要部署您的 Web3 Action，请使用 Tenderly CLI：

  ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  tenderly actions deploy
  ```

  此命令将部署您 `tenderly.yaml` 文件中定义的 action。
</Steps>

## 工作原理

让我们分解 `lidoEvents.ts` 文件的主要组件：

```typescript showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}

const subscribeToLidoValidatorExitRequestFn = async (
  context: Context,
  transactionEvent: TransactionEvent,
) => {
  // Event handling logic here
};

// Helper functions for API calls and notifications
```

`subscribeToLidoValidatorExitRequestFn` 函数是我们 Web3 Action 的主入口。它处理以下任务：

1. 使用 Tenderly API 获取事务详情
2. 检查 `stakingModuleId` 是否为 1 且 `nodeOperatorId` 是否为 14
3. 如果条件满足，则向指定的 Telegram 频道发送通知

### 用于事务 Trace 的 Tenderly API

此 Web3 Action 的关键部分是使用 Tenderly API 获取事务 trace。以下是所使用的 API 端点：

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const url = `https://api.tenderly.co/api/v1/public-contract/${network}/trace/${txHash}`;
```

此 API 端点返回事务的详细 trace，其中包括事务执行期间发出的所有日志和事件。以下是 URL 各部分的含义：

* `https://api.tenderly.co/api/v1/public-contract/`：Tenderly 公共合约 API 的基础 URL
* `${network}`：网络标识符（例如以太坊主网为 `1`）
* `${txHash}`：我们感兴趣的事务的哈希

此 API 的响应包含关于事务的大量信息，包括：

* 所有发出的事件及其参数
* 事务期间发生的内部调用
* 发生的状态变化

在我们的 Web3 Action 中，我们使用此 trace 来查找 `ValidatorExitRequest` 事件并提取相关信息：

```typescript showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const validatorExitRequestLog = result.logs.find(
  (log: any) => log.name === "ValidatorExitRequest"
);

if (validatorExitRequestLog) {
  const stakingModuleId = validatorExitRequestLog.inputs.find(
    (input: any) => input.soltype.name === "stakingModuleId"
  )?.value;
  const nodeOperatorId = validatorExitRequestLog.inputs.find(
    (input: any) => input.soltype.name === "nodeOperatorId"
  )?.value;
  // ... other parameters
}
```

这使我们能够精确识别感兴趣的验证者退出请求，并提取发送通知所需的必要信息。

<Note>
  使用 Tenderly API 获取事务 trace 提供了一种强大的方式来访问链上事件的详细信息，而无需直接与区块链交互或设置您自己的节点。
</Note>

## 自定义

对于 Lido 验证者退出，感兴趣的事件由参数 `stakingModuleId` 值为 1 且 `nodeOperatorId` 值为 14 来识别。

要修改这些过滤条件或添加更多要监控的事件：

1. 编辑 `lidoEvents.ts` 文件
2. 更新 `if` 语句中的条件检查以匹配您的要求
3. 根据需要修改消息格式或添加额外信息

例如，要更改 `stakingModuleId` 和 `nodeOperatorId` 条件：

```typescript showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
if (stakingModuleId == 1 && nodeOperatorId == 14) {
  // Your custom logic here
}
```

## 监控和故障排除

为确保您的 Web3 Action 正常工作：

* 在 Tenderly dashboard 中监控您的 Web3 Action 执行
* 检查 Tenderly 日志以查看任何错误消息或执行详情
* 确保您的 Telegram bot 有权向指定频道发送消息

<Warning>
  此设置专门用于监控以太坊主网上的 Lido 验证者退出请求。
</Warning>

## 总结

您现在已使用 Tenderly Web3 Actions 设置了一个自定义通知系统，用于监控 Lido 验证者退出请求。
