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

# 模拟交易

> 在 Virtual Environment 上模拟一笔交易，在发送之前预览其结果、gas 使用情况、状态变化和发出的事件。

Virtual Environments 支持交易模拟。您可以从 Dashboard UI 或通过 RPC 编程方式模拟交易。

<CardGroup cols={2}>
  <Card title="Simulator UI" href="/simulator-ui/overview" />

  <Card title="Simulation RPC" href="/node-rpc/rpc-reference?network=ethereum-mainnet&method=tenderly_simulateTransaction" />

  <Card title="Bundle Simulation RPC" href="/node-rpc/rpc-reference?network=ethereum-mainnet&method=tenderly_simulateBundle" />

  <Card title="Simulation API on Virtual Environments" href="/api-reference" />
</CardGroup>

## 从 Dashboard 模拟

要从 Dashboard 模拟交易，请前往 **Virtual Environments**，然后单击左侧菜单中的 **Simulate**。模拟的交易仅在 Virtual Environment 仪表板中可见，不会出现在[公共浏览器](/virtual-environments/explorer#public-explorer)中。
您可以按以下方式使用 Simulator：

* 使用 **Edit Source** 调试智能合约错误以检查您的 bug 修复。
* 调用合约以了解状态，而不将交易持久化到网络。
* 使用 **State Overrides** 设置期望的合约状态，以评估在链上不存在且不应存在的条件下的执行情况。
* 构建概念验证交易以验证测试场景。

## 通过 RPC 模拟

Virtual Environments 上的交易也可以使用我们的 RPC 方法 [`tenderly_simulate`](/node-rpc/rpc-reference?network=ethereum-mainnet\&method=tenderly_simulateTransaction) 和 [`tenderly_simulateBundle`](/node-rpc/rpc-reference?network=ethereum-mainnet\&method=tenderly_simulateBundle) 进行模拟。

<Note>
  我们建议集成 Simulation RPC，但也可以使用 [Simulation REST API](/api-reference)。
</Note>

下面的示例代码运行了一个使用 state overrides 铸造 DAI 的模拟。

* 发送方地址：`0xe2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2`。
* DAI 合约地址：`0x6b175474e89094c44da98b954eedeac495271d0f`
* 状态覆盖参数：`DAI(0x6b175474e89094c44da98b954eedeac495271d0f).wards[0xe2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2] = 1`

阅读本指南了解更多关于[使用状态覆盖进行模拟](/simulations/state-overrides)的内容。

```bash showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# simulate DAI minting by 0xe2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2
# 0xf1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1 gets the minted DAI
# Simulated on the latest block

VIRTUAL_TESTNET_RPC=RPC_LINK_HERE

curl $VIRTUAL_TESTNET_RPC \
-X POST \
-H "Content-Type: application/json" \
-d \
'{
  "id":0,
  "jsonrpc":"2.0",
  "method":"tenderly_simulateTransaction",
  "params":[
    {
      "from":"0xe2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2",
      "to":"0x6b175474e89094c44da98b954eedeac495271d0f",
      "gas":"0x7a1200",
      "data":"0x40c10f19000000000000000000000000f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f10000000000000000000000000000000000000000000000001bc16d674ec80000"
    },
    "latest",
    {
      "0x6b175474e89094c44da98b954eedeac495271d0f":{
        "stateDiff":{
          "0xedd7d04419e9c48ceb6055956cbb4e2091ae310313a4d1fa7cbcfe7561616e03":"0x0000000000000000000000000000000000000000000000000000000000000001"
        },
        "balance": "0xFFFFFF"
      }
    }
  ]
}'
```

## 通过客户端库模拟交易

要通过 Ethers 和 Viem 模拟交易，请查看以下示例：

<Card title="Simulating with Viem" href="" />
