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

# Ethers.js v6 与 v5 - Node RPC 集成

> 通过 HTTPS 或 WebSocket 将 ethers v6 或 v5 连接到 Tenderly Node RPC，使用 JsonRpcProvider 在 EVM 网络上查询、跟踪和模拟交易。

[Ethers.js](https://docs.ethers.org/v5/) 是一个独立的 TypeScript/JavaScript 库，用于与 Ethereum 区块链及其生态系统交互。

要通过 Node RPC 使用 ethers v6 或 v5 连接到网络，请使用基于 HTTPS 或 WebSocket 的 JSON-RPC provider。

## 使用 JsonRpcProvider（ethers v6 与 v5）

<Tabs>
  <Tab title="Ethers 6">
    ```js title="ethers-6/json-rpc-provider.ts" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    // Installation Instructions: https://docs.ethers.io/v6/getting-started
    import { ethers } from "ethers";

    const RPC_URL = `https://mainnet.gateway.tenderly.co/${process.env.TENDERLY_NODE_ACCESS_KEY}`

    async function executeMethod() {
      // Initialize an ethers provider instance
      const provider = new ethers.JsonRpcProvider(RPC_URL);

      const blockNumber = await provider.getBlockNumber();

      console.log(blockNumber);
    }

    await executeMethod();
    ```
  </Tab>

  <Tab title="Ethers 5">
    ```js title="ethers-5/json-rpc-provider.ts" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    // Installation Instructions: https://docs.ethers.io/v5/getting-started
    import { ethers } from "ethers";

    const RPC_URL = `https://mainnet.gateway.tenderly.co/${process.env.TENDERLY_NODE_ACCESS_KEY}`

    async function executeMethod() {
      // Initialize an ethers provider instance
      const provider = new ethers.providers.JsonRpcProvider(RPC_URL);

      const blockNumber = await provider.getBlockNumber();

      console.log(blockNumber);
    }

    await executeMethod();
    ```
  </Tab>
</Tabs>

连接后，即可通过 provider 调用任何标准的 `eth_*` 方法或 Tenderly 扩展方法（例如 `tenderly_simulateTransaction`）。参见完整的 [JSON-RPC 参考](/node-rpc/rpc-reference)，了解所有受支持的方法与网络。

## 使用 WebSocket provider

<Tabs>
  <Tab title="Ethers 6">
    ```js title="ethers-6/wss-provider.ts" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    // Installation Instructions: https://docs.ethers.io/v6/getting-started
    import { ethers } from "ethers";

    const RPC_URL_WSS = `wss://mainnet.gateway.tenderly.co/${process.env.TENDERLY_NODE_ACCESS_KEY}`

    async function executeMethod() {
      // Initialize an ethers provider instance
      const provider = new ethers.WebSocketProvider(RPC_URL_WSS);

      const blockNumber = await provider.getBlockNumber();

      console.log(blockNumber);
    }

    await executeMethod();
    ```
  </Tab>

  <Tab title="Ethers 5">
    ```js title="ethers-5/wss-provider.ts" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    // Installation Instructions: https://docs.ethers.io/v5/getting-started
    import { ethers } from "ethers";

    const RPC_URL_WSS = `wss://mainnet.gateway.tenderly.co/${process.env.TENDERLY_NODE_ACCESS_KEY}`

    async function executeMethod() {
      // Initialize an ethers provider instance
      const provider = new ethers.providers.WebSocketProvider(RPC_URL_WSS);

      const blockNumber = await provider.getBlockNumber();

      console.log(blockNumber);
    }

    await executeMethod();
    ```
  </Tab>
</Tabs>
