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

# Viem - Node RPC 集成

> 将 viem 连接到 Tenderly Node RPC，通过 HTTPS 或 WebSocket 在 100 多个 EVM 网络上进行读取、发送、跟踪和模拟交易。

[Viem](https://viem.sh/) 是一个 Ethereum 的 TypeScript 接口，为与 Ethereum 交互提供底层无状态原语。

您可以将 Tenderly 与 Viem 一起使用，通过标准 RPC 方法以及 [Node RPC 上可用的自定义方法](/node-rpc/rpc-reference) 与区块链交互。

调用 Viem 的 `createPublicClient` 并传入以下参数：

* `chain`：来自 `viem/chains` 的预定义链之一
* `transport`：对 `http` 或 `webSocket` 的调用，并使用 `https` 或 `wss` RPC URL 初始化。

<Card title="代码示例：将 Viem 与 Node RPC 结合使用" href="" />

```js title="node/samples/viem/0-a-call.ts" showLineNumbers {16-18} theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}

const client = createPublicClient({
  chain: mainnet,
  transport: http(
    `https://mainnet.gateway.tenderly.co/${process.env.TENDERLY_NODE_ACCESS_KEY}`
  ),
});


console.log("Block Number", await client.getBlockNumber());
```

## 批量请求

Viem 支持发送[批量请求](https://viem.sh/docs/clients/transports/http#batch-json-rpc)。通过批处理，多个 JSON-RPC 调用会在一次 HTTPS 请求中处理。多个 Viem 操作将被批处理并在当前 JavaScript 消息队列末尾执行。默认情况下没有时间延迟，但可以通过可选的 `wait` 参数进行配置。

这意味着多个 viem 操作将被批处理，并在当前 JavaScript 消息队列末尾执行。默认情况下没有时间延迟，但可以通过可选的 `wait` 参数进行配置。

```js title="node/samples/viem/0-batch-calls.ts" showLineNumbers {16-21} theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}

const client = createPublicClient({
  chain: mainnet,
  transport: http(
    `https://mainnet.gateway.tenderly.co/${process.env.TENDERLY_NODE_ACCESS_KEY}`,
    {
      batch: true,
    }
  ),
});

const [blockNumber, balance, ensName] = await Promise.all([
  client.getBlockNumber(),
  client.getBalance({ address: "0xd2135CfB216b74109775236E36d4b433F1DF507B" }),
  client.getEnsName({ address: "0xd2135CfB216b74109775236E36d4b433F1DF507B" }),
  client.getChainId(),
]);
console.log("Results are in");
console.log({ blockNumber, balance, ensName });

//  So will this
const bnPromise = client.getBlockNumber();

const balancePromise = client.getBalance({
  address: "0xd2135CfB216b74109775236E36d4b433F1DF507B",
});

console.log(await bnPromise);
console.log(await balancePromise);
```

## 使用 viem 模拟交易

要使用[模拟 RPC 方法](/node-rpc/rpc-reference?network=ethereum-mainnet\&method=tenderly_simulateTransaction)，只需使用 `client.request` 方法：

```js title="node/samples/viem/1-simulate.ts" showLineNumbers {2-15} theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const simulation = await client.request({
  method: "tenderly_simulateTransaction",
  params: [
    // transaction object
    {
      from: "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
      to: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
      gas: "0x0",
      gasPrice: "0x0",
      value: "0x0",
      data: "0xa9059cbb00000000000000000000000020a5814b73ef3537c6e099a0d45c798f4bd6e1d60000000000000000000000000000000000000000000000000000000000000001",
    },
    // the block
    "latest",
  ],
});

console.log("Trace");
console.log(JSON.stringify(simulation.trace, null, 2));
console.log("=======================================================");
console.log("Logs");
console.log(JSON.stringify(simulation.logs, null, 2));
console.log("=======================================================");
console.log("Asset Changes");
console.log(JSON.stringify(simulation.assetChanges, null, 2));
console.log("=======================================================");
console.log("Balance Changes");
console.log(JSON.stringify(simulation.balanceChanges, null, 2));
```

## 使用 Viem 跟踪交易

使用 [`tenderly_traceTransaction`](/node-rpc/rpc-reference?network=ethereum-mainnet\&method=tenderly_traceTransaction) 获取完全解码的交易跟踪。

```ts showLineNumbers title="node/samples/viem/2-transaction-trace.ts" {2-6} theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const txTrace = await client.request({
  method: "tenderly_traceTransaction",
  params: [
    // transaction hash
    "0x6b2264fa8e28a641d834482d250080b39cbbf39251344573c7504d6137c4b793"
  ],
});


console.log("Logs");
console.log(JSON.stringify(txTrace.logs, null, 2));
console.log("=======================================================");
console.log("Asset Changes");
console.log(JSON.stringify(txTrace.assetChanges, null, 2));
console.log("=======================================================");
console.log("Balance Changes");
console.log(JSON.stringify(txTrace.balanceChanges, null, 2));
console.log("=======================================================");
```
