> ## 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 Environments 发送交易

> 通过 RPC、Dashboard 或合约页面的 Write 标签页向 Virtual Environment 发送已签名和未签名交易，并用无限水龙头为账户充值。

Virtual Environments 允许您向虚拟网络发送交易。发送交易的过程与在真实网络上发送交易完全相同。

交易可以通过 Dashboard UI 或 RPC 发送。本页面向您展示如何使用这两种方法进行操作。

## 通过 RPC 发送

使用 `eth_sendRawTransaction` RPC 方法向您的 Virtual Environment 发送交易。

下面的示例展示了如何使用 Viem、Ethers.js 和 cURL 命令发送交易。这些代码片段无需修改即可运行。

> [tab] Shell
>
> ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
> curl $TENDERLY_VIRTUAL_TESTNET_RPC \
>     -X POST \
>     -H "Content-Type: application/json" \
>     -d '{
>           "jsonrpc": "2.0",
>           "id": 0,
>           "method": "eth_sendRawTransaction",
>           "params": [
>             "0x095ea7b300000000000000000000000040bdb4497614bae1a67061ee20aade3c2067ac9e00000000000000000000000000000000000000000000000000000000000186a0"
>           ]
>     }'
> ```

> [tab] virtual-environments/src/viem-send-tx.ts
>
> 阅读[本指南](https://docs.tenderly.co/virtual-environments/libraries/viem.md)以获取有关 Viem 集成的更多详细信息。
>
> ```typescript showLineNumbers title="virtual-environments/src/viem-send-tx.ts" lines={2,4, 17, 23} theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
>
> const EXPLORER_BASE_URL = vMainnet.blockExplorers.default;
>
> const account = privateKeyToAccount(generatePrivateKey());
>
> (async () => {
>   const client = createWalletClient({
>     account,
>     chain: vMainnet,
>     transport: http(vMainnet.rpcUrls.default.http[0]),
>   });
>
>   await tenderlySetBalance(client, [
>     [account.address],
>     "0xDE0B6B3A7640000",
>   ]);
>
>
>   const tx = await client.sendTransaction({
>     to: "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC",
>     value: parseEther("0.01"),
>   });
>
>   console.log(`${EXPLORER_BASE_URL}/tx/${tx}`);
> })();
> ```

> [tab] send-tx.ts
>
> 阅读[本指南](https://docs.tenderly.co/virtual-environments/libraries/ethers.md)以获取有关 Ethers 集成的更多详细信息。
>
> ```ts title="send-tx.ts" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
>
> await (async () => {
>   const provider = new ethers.JsonRpcProvider(process.env.TENDERLY_VIRTUAL_TESTNET_RPC!);
>
>   const signer = Wallet.fromPhrase(Mnemonic.fromEntropy(ethers.randomBytes(24)).phrase, provider);
>
>   await provider.send("tenderly_setBalance", [
>     signer.address,
>     "0xDE0B6B3A7640000",
>   ]);
>
>   const tx = await signer.sendTransaction({
>     to: "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC",
>     value: ethers.parseEther("0.01"),
>   });
>
>   console.log(`${process.env.VIRTUAL_MAINNET_EXPLORER!}/tx/${tx.hash}`);
> })();
> ```

## 从 Dashboard 发送

从 Dashboard 发送的交易将记录在 Virtual Environment 的状态中。这些交易**无需签名**，可以在 Virtual Environment 上的任意地址之间发送。

在 Explorer 视图中，单击右上角的 **Send 图标**以访问 **Send Transaction** 输入表单。

### 选择合约

在 **address** 字段中，粘贴 DAI 稳定币合约的地址：`0x6B175474E89094C44Da98b954EedeAC495271d0F`。

接收合约可以是以下任意一种：

* [部署在 Virtual Environment 上](https://docs.tenderly.co/virtual-environments/develop/deploy-contracts.md)的 Virtual Contract
* 添加到您 [Tenderly 项目](https://docs.tenderly.co/platform/account/projects/overview.md)中的合约
* 网络上任何已有的合约

### 指定调用数据

对于[在 Tenderly 上验证过](https://docs.tenderly.co/contract-verification/overview.md)（私有或公开）或在 Etherscan 或 Blockscout 上公开验证过的合约，您可以使用 **Choose function and parameters**。

1. 选择 **`approve`** 函数
2. 粘贴您希望批准 DAI 的地址（`0x40BdB4497614bAe1A67061EE20AAdE3c2067AC9e`）
3. 设置您想要批准的 DAI 以 wei 计的数量。例如，`10000000000000000000`。

> [!NOTE]
>
> 对于未验证的合约，您需要以十六进制形式**输入原始输入数据/calldata**。

### 填写交易参数

您可以填写以下选项：

* **From**：任何账户地址，因为 Virtual Environments 上的交易无需签名。使用 `0xbd8daa414fda8a8a129f7035e7496759c5af8570` 或您为其注资过 DAI 的任何地址。
* **Gas**：默认为 `800000`，或设置期望的 gas（上限）
* **Gas Price**：默认为 `0`，或任意 gas 价格
* **Value**：默认为 `0`，或指定任意交易 value，将从发送方（**from**）余额中扣除。

> [!NOTE]
>
> 发送账户需要有足够的资金来发送交易。在 Virtual Environments 上发送交易之前，请使用[无限水龙头](https://docs.tenderly.co/virtual-environments/unlimited-faucet.md)为任意账户充值任意余额。

### 发送交易

单击 **Send Transaction**。

已发送的交易将出现在 [Virtual Environment Explorer](https://docs.tenderly.co/virtual-environments/explorer.md) 中。单击该交易可以检查调用跟踪，使用 Debugger 进行调试，或使用不同参数重新模拟。

![](https://mintcdn.com/tenderly/XsEZlaGXYskrtN68/images/testnets/dai-send-tx.png?fit=max\&auto=format\&n=XsEZlaGXYskrtN68\&q=85\&s=e87158c2e275276447fb71683b0f03e2)

## 从合约页面发送

在 Virtual Environment 内的合约页面上，**Read/Write** 标签页会通过该 Virtual Environment 的 RPC 将 write 函数作为交易发送。这些交易无需签名，并会像其他交易一样记录到 Virtual Environment 的状态中。

![Write sub-tab of the Read/Write tab on the WETH9 contract inside a Virtual Environment, with a selected sender address, Fund and View Portfolio buttons, and the expanded approve function showing its parameters and the Write and Simulate buttons](https://mintcdn.com/tenderly/iIERNR2m0qr1Dtyk/images/testnets/vnet-contract-read-write.webp?fit=max\&auto=format\&n=iIERNR2m0qr1Dtyk\&q=85\&s=53314912ba295bef654a6a1c888f56ab)

### 打开 Write 标签页

从 Virtual Environment 的 **Contracts** 页面或通过搜索地址打开合约，然后选择 **Read/Write** 和 **Write** 子标签页。代理合约还有 **Write as Proxy** 子标签页，通过代理调用实现合约的函数。

### 选择发送方

点击 **Impersonate address**，从 **Environment accounts** 或 **Project accounts** 中选择地址，或输入任意地址。所选地址会显示在子标签页旁边，并附带 **Fund** 按钮，可通过 [Unlimited Faucet](https://docs.tenderly.co/virtual-environments/unlimited-faucet.md) 为其充值。

### 填写参数并发送

展开函数，填写参数，然后点击 **Write**。交易发送后，函数下方会显示 **Transaction created** 以及 **View transaction** 链接。**Simulate** 会以模拟方式运行同一调用，而不发送交易。

已发送的交易也会出现在 Virtual Environment 的 **Activity** 视图中。

## 后续步骤

* [模拟交易](https://docs.tenderly.co/virtual-environments/interact/simulate-transactions.md)：从 Dashboard 或通过 RPC 运行交易模拟
* [部署智能合约](https://docs.tenderly.co/virtual-environments/develop/deploy-contracts.md)：开始部署和监控智能合约
* [暂存 dapp](https://docs.tenderly.co/virtual-environments/ci-cd/stage-dapps.md)：了解如何将 Virtual Environment 连接到您 dapp 的 UI
* 使用 Tenderly Debugger [调试交易](https://docs.tenderly.co/virtual-environments/develop/debug-transactions.md)。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.