> ## 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.

# 项目结构

> 了解通过 CLI 生成的 Web3 Action 项目的结构， 并理解 tenderly.yaml 配置文件的总体布局。

在本指南中，您将学习如何浏览通过 Tenderly CLI 生成的 Web3 Action 项目中的文件和项目结构。

了解 [如何使用 Tenderly CLI](https://github.com/Tenderly/tenderly-cli)。

## 项目概览和根目录文件结构

当您运行 `tenderly actions init` 命令时，Tenderly CLI 会创建一个 *actions root* 目录以及必要的文件。默认情况下，*actions root* 目录名为 *actions*。

不过，您可以在初始化过程中指定其他名称。初始化后，根目录的名称仍可通过修改文件夹名称以及 `tenderly.yaml` 文件中的 `sources` 字段进行调整。

一个基于 TypeScript 的 Web3 Actions 项目结构 **示例**：

```bash title="example" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
src
|--- actions 			# actions root directory, can be renamed
|--- |--- example.ts
|--- |--- package.json
|--- |--- tsconfig.json
|--- |--- node_modules
|         |--- @tenderly/actions
|         |--- typescript
|--- tenderly.yaml
|--- tenderly-schema.json
```

*actions root* 目录包含您 Web3 Actions 的 npm 项目。所有相关文件，包括 package.json、node\_modules、源代码文件以及其他依赖、库和资源，都包含在此文件夹中。

**默认文件包括：**

* `actions/example.ts`：默认情况下，此文件包含所有 Web3 Actions。
* `tenderly.yaml`：这是一个配置文件，指定项目中所有 Web3 Actions 的设置
* `tenderly-schema.json`：自动生成的 JSON Schema，可为 `tenderly.yaml` 启用 IDE 验证和自动补全，请勿手动编辑 `tenderly-schema.json`

<Warning>
  压缩后的 actions root 目录（包括 `node_modules`）大小必须小于 40MB。请
  确保在安装额外的包时保持在此限制以下。
</Warning>

## tenderly.yaml 文件结构

`tenderly.yaml` 文件包含 Tenderly 项目和所有 Web3 Actions 的配置和设置。

出于本指南的目的，完整的 `tenderly.yaml` 示例文件如下所示：

```yaml title="example.yaml" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
$schema: ./tenderly-schema.json
account_id: 'tenderly-username'
project_slug: 'our-cool-project'
actions:
  our-org/our-cool-project:
    runtime: v2
    sources: actions
    specs:
      bestActionEver:
        description: Does the best thing ever
        function: very/organized/file:bestActionEver
        trigger:
          type: block
          block:
            network:
              - 3
            blocks: 10
```

<Note>
  `$schema` 字段由 `tenderly actions init` 自动添加，并指向生成的
  `tenderly-schema.json` 文件。支持 JSON Schema 的 IDE（VS Code、JetBrains）会使用它在您编辑 `tenderly.yaml` 时提供
  自动补全和内联验证。每次运行 `tenderly actions init` 时也会重新生成该 schema 文件。如果您不希望
  提交它，请将 `tenderly-schema.json` 添加到您的 `.gitignore`。
</Note>

现在，让我们分解此文件每个部分的作用及配置方法。

为了便于理解，可以将 `tenderly.yaml` 视为包含两个主要部分：

* **通用配置**：包含 Web3 Action 所关联账户和项目名称的部分。
* **Web3 Actions 配置**：包含 Web3 Action 本身的配置，基本上告知 Tenderly 何时运行它。

### 通用配置

通用配置部分包含以下键值对：

```yaml title="example.yaml" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
account_id: 'johnDoe'
project_slug: 'our-cool-project'
```

* `account_id`：您的用户名
* `project_slug`：与 Web3 Action 关联的项目 slug（可选，可留空）

查看本指南以了解如何 [查找组织名称、用户名和项目 slug](/platform/account/projects/slug)。

### Web3 Actions 配置

`actions` 对象是您开始定义 Web3 Actions 的位置，包括项目设置（例如 runtime、sources 位置以及实际的 Web3 Action 声明）。

```yaml title="example.yaml" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
account_id: 'johnDoe'
project_slug: ''
actions:
  my-username/my-cool-project: # in case it's an individual project
    # our-cool-org/our-cool-project:   # in case it's an org-level project
    runtime: v2
    sources: actions
    specs: ...
```

### 指定项目

首先指定在 Tenderly 中唯一标识项目的复合键。

* `username/project-slug`：属于个人开发者项目的用户名和项目 slug
* `org-name/project-slug`：如果您的项目属于组织，则为组织名称和项目 slug

查看本指南以了解如何 [查找组织名称、用户名和项目 slug](/platform/account/projects/slug)。

<Note>
  可以将同一个 Web3 Actions 项目用于您在 Tenderly Dashboard 中有权访问的
  多个项目。
</Note>

### 配置运行时

接下来，您需要指定运行时版本以及包含 Web3 Action 代码的目录。Tenderly CLI 使用这些数据将您的代码打包并部署到 Tenderly 基础设施上的 Web3 Actions 运行时。

以下设置是必填的：

* `runtime`：运行时版本。目前，我们支持 **v2**，对应 Node 20。
* `sources`：Web3 Action 源代码文件的位置。其值必须是相对于包含 `tenderly.yaml` 文件的文件夹的、指向 *actions root* 目录的路径。它应与您运行 init 命令时指定的路径匹配。

**示例：** 对于下面所示的文件夹结构， **sources** 的合适值应为 `web3-actions`。

```bash title="example" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
src
|-- web3-actions 			# actions root directory, can be renamed
|   |--- example.ts
|   |--- package.json
|   |--- tsconfig.json
|   |--- node_modules
|   	 |--- @tenderly
|        |--- /typescript
|-- tenderly.yaml
```

<Note>
  如果您更改 `sources` 值，则在相对于 *actions root* 的指定路径中必须存在一个目录，
  并且该目录必须包含带有 Web3 Action 函数的源代码文件。
</Note>

### 定义 Web3 Actions 设置

单个 Web3 Actions 在 `specs` 对象下声明。您可以在单个 `tenderly.yaml` 文件中声明多个 Web3 Actions。

每个 Web3 Action 必须以一个代表 action 名称的键（`bestActionEver`）开始。该 action 名称在项目级别必须唯一，且必须以字母开头，后跟字母、数字、连字符或下划线（模式：`^[a-zA-Z][a-zA-Z0-9_-]*$`）。此键表示已部署 Web3 Action 的命名及其在 Tenderly Dashboard 中的表示方式。

`description` 键也必须包含。描述通常是一两句话，说明 Web3 Action 的作用。

```yaml title="example.yaml" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
specs:
  bestActionEver:
    description: Does the best thing ever!
```

接下来，您需要指定 `function` 键以链接到您要运行的 action 函数。Web3 Actions 的源代码可以拆分到多个文件中或嵌套在自定义目录结构中。`tenderly.yaml` 文件允许无缝引用。

例如，位于 `actions/very/organized/file.ts` 中的函数 `bestActionEver` 在 yaml 中会像下面这样被引用：

```yaml title="example.yaml" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
our-org/our-cool-project:
  runtime: v2
  sources: actions
  specs:
    bestActionEver:
      description: Does the best thing ever
      function: very/organized/file:bestActionEver
      trigger: ...
```

下一步是为 Web3 Action 选择并定义触发器。触发器是 Tenderly 监听并在满足条件时执行 Web3 Action 的事件。

有关触发器的深入参考，请浏览 [可用的 Web3 Action 触发器类型](/monitoring/web3-actions/references/functions-events-triggers)。

在为 Web3 Action 定义 **触发器** 时，首先要指定其类型。在下面的示例中，我们定义的是 *block* 触发器类型。这意味着 `bestActionEver` Web3 Action 将在 Rinkeby 上每 10 个区块被打包时运行一次。

```yaml title="example.yaml" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
bestActionEver:
  description: Does the best thing ever
  function: very/organized/file:bestActionEver
  trigger:
    type: block
    block:
      network:
        - 3
      blocks: 10
```
