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

# The Graph 与 Virtual Environments

> 设置和使用 Tenderly Virtual Environments 与 The Graph 协同工作的指南。

本指南详细介绍了设置和使用 Tenderly Virtual Environments 与 The Graph 协同工作的步骤。

要在 Virtual Environments 中使用 The Graph，您需要运行自己的 Graph 节点。如果您正在暂存 dapp，则需要在您选择的云提供商上托管 The Graph 实例。

## 前提条件

请确保您的系统上已安装以下前提条件：

* Node.js 和 npm
* Docker
* 一个 [Tenderly 账户](https://dashboard.tenderly.co/login?redirectTo=testnets)
* 一个 Virtual Environment RPC
* 一个使用 [Hardhat](/virtual-environments/develop/deploy-contracts#hardhat) 或 [Foundry](/virtual-environments/develop/deploy-contracts#foundry) 部署到 Virtual Environment 的智能合约

## 设置 The Graph

<Steps>
  ### 全局安装 Graph CLI

  使用 npm 在您的系统上全局安装 Graph CLI：

  ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install -g @graphprotocol/graph-cli
  ```

  有关更多详细信息，请参阅官方 [Graph CLI 文档](https://thegraph.com/docs/en/developer/cli/)。

  ### 设置 The Graph 项目

  在您的项目目录中，初始化 The Graph 项目。

  注意事项：

  * 调整 `--abi` 使其指向本地系统上的合约 ABI
  * 使用合约名称和地址修改 `--contract-name` 和 `--from-contract`
  * 设置 `--start-block`
  * 更改 `--network`，使其引用您的 Virtual Environment 所基于的网络（例如 `mainnet`、`base` 等）。

  <Note>
    我们建议让 `network` 与原始网络的名称匹配，而不将其标记为 `virtual_`。这使得在部署到不同的 staging 和演示环境时可以轻松切换 RPC。
  </Note>

  ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  graph init your-subgraph-name subgraph-dir \
  --protocol ethereum \
  --studio \
  --abi path/to/abi/ERC20Token.json \
  --contract-name ERC20TokenFactory \
  --from-contract 0x7F2Fa32C991fD2F8dF3EB23C394f550376e9b30d \
  --start-block 0 \
  --network mainnet
  ```

  <Card title="Graph Initialization documentation" href="" />

  ### 更新 `docker-compose.yml`

  编辑 `docker-compose.yml` 文件（如下），并更新 `environment.ethereum` 使其引用 Virtual Environment RPC。

  ```yaml showLineNumbers lines={15} theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  version: '3'
  services:
    graph-node:
      image: graphprotocol/graph-node
      ports:
        - '8020:8020'
        - '8000:8000'
        - '5001:5001'
      environment:
        postgres_host: postgres
        postgres_user: graph-node
        postgres_pass: let-me-in
        postgres_db: graph-node
        ipfs: 'ipfs:5001'
        ethereum: 'mainnet:https://virtual.mainnet.rpc.tenderly.co/d0a6...-40..2c-81..0f-a5..90'
  ```

  <Card title="Graph Docker documentation" href="" />

  ### 在本地运行 Docker Compose

  设置 docker-compose 配置后，运行以下命令：

  ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  docker-compose up
  ```

  ### 创建并部署您的 subgraph

  运行以下命令，将 `your-subgraph-name` 替换为您希望的 subgraph 名称。

  ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  graph codegen
  graph create --node http://localhost:8020 your-subgraph-name
  graph deploy --node http://localhost:8020 --ipfs http://localhost:5001 your-subgraph-name
  ```

  <Card title="Graph Deployment documentation" href="" />

  ### 访问 subgraph

  部署后，通过下面的 URL 访问您的 subgraph。在浏览器中打开此 URL 以访问 subgraph 并运行查询：

  `Deployed to http://localhost:8000/subgraphs/name/your-subgraph-name`

  <Card title="Graph Query documentation" href="" />

  ### 部署到云端

  对于 dapp staging，请将 The Graph 节点部署到您选择的云提供商，并将您的 dapp UI/后端连接到该节点以进行查询。
</Steps>
