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

# 通过 SDK 模拟交易

> 了解如何使用 Tenderly SDK 执行一次简单的交易模拟。

在本教程中，您将学习如何使用 Tenderly SDK 运行一次交易模拟。模拟让您可以在不将交易发送到链上的情况下测试其行为。要了解更多关于 Tenderly [交易模拟器的信息，请点击此处](/simulations/quickstart)。

### 前提条件

* 您的机器上已安装 Node.js
* [具有 API 密钥的 Tenderly 账户](/platform/account/projects/api-tokens)
* [已在 Tenderly Dashboard 中设置项目](/platform/account/projects/slug)

<Info>
  使用此链接[注册 Tenderly](https://dashboard.tenderly.co/register?utm_source=homepage)。
</Info>

### 第 1 步：初始化新的 Node.js 项目

为您的项目创建一个新目录，在终端中导航到该目录，并初始化一个新的 Node.js 项目：

```bash title="example" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
mkdir tenderly-simulation
cd tenderly-simulation
npm init -y
```

### 第 2 步：安装 Tenderly SDK

在 Node.js 项目中将 [Tenderly SDK npm 包](https://www.npmjs.com/package/@tenderly/sdk) 作为依赖安装：

```bash title="example" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
npm install @tenderly/sdk
```

### 第 3 步：导入并配置 Tenderly SDK

在项目目录中创建一个名为 **`index.js`** 的新文件，并添加以下代码以导入 Tenderly SDK 并使用您的 Tenderly 账户详情进行配置：

```javascript title="example.js" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const { Network, Tenderly } = require('@tenderly/sdk');

const tenderly = new Tenderly({
  accountName: 'your-account-name',
  projectName: 'your-project-name',
  accessKey: 'your-access-key',
  network: Network.MAINNET, // Replace with the appropriate network
});
```

请务必将 **`your-account-name`**、**`your-project-name`** 和 **`your-access-key`** 替换为您实际的 Tenderly 账户信息。

<Note>
  按照以下指南[获取配置详情](/platform/sdk/introduction#how-to-get-the-account-name-project-slug-and-secret-key)。
</Note>

### 第 4 步：定义交易详情

接下来，为模拟定义交易详情。

将以下代码添加到 **`index.js`**：

```js title="example.js" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const transactionParameters = {
  from: '0x0000000000000000000000000000000000000000',
  to: '0x1f98431c8ad98523631ae4a59f267346ea31f984',
  input: '0xa1671295000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000152649ea73beab28c5b49b26eb48f7ead6d4c8980000000000000000000000000000000000000000000000000000000000000bb8',
  value: '0',
  gas: 8000000,
  gas_price: '0',
};

const simulation = {
  transaction: transactionParameters,
  blockNumber: 18813657, // simulation's target block number
  //overrides: {...} // optional state overrides field
};

```

将 **`from`**、**`to`**、**`input`**、**`value`**、**`gas`** 和 **`gas_price`** 字段替换为您所需的交易详情。**`override`** 字段为可选字段，允许您覆盖模拟中特定合约的状态。

<Warning>
  请确保指定的区块号确实存在于所选网络上。
</Warning>

### 第 5 步：运行交易模拟

现在，使用 Tenderly SDK 的 **`simulateTransaction`** 方法来运行交易模拟。

将以下代码添加到 **`index.js`**：

```js title="example.js" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
(async () => {
  try {
    const simulationResult = await tenderly.simulator.simulateTransaction(simulation);
    console.log('Simulation result:', simulationResult);
  } catch (error) {
    console.error('Error running simulation:', error);
  }
})();
```

此代码运行一个异步函数，该函数调用 **`simulateTransaction`** 方法并将模拟结果记录到控制台。

### 第 6 步：执行脚本

保存 **`index.js`** 文件并使用以下命令运行脚本：

```bash title="example" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
node index.js
```

如果交易模拟成功，您将在控制台中看到打印出的模拟结果。

就是这样！您现在已经学会了如何使用 Tenderly SDK 运行交易模拟。
