> For the complete documentation index, see [llms.txt](https://docs.rome.builders/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rome.builders/zh/rome-xue-yuan/09-build-a-dual-lane-app.md).

# 构建双通道应用

构建一个金库合约，让 MetaMask 用户和 Solana 钱包用户都能向其中存入和提取，并从双方看到相同的余额。

这是构建课程（[2. 你可以构建什么](https://docs.rome.builders/zh/rome-xue-yuan/02-what-you-can-build.md) 列出了使用场景）。你编写一个小型 USDC 金库，只部署一次，并通过两个钱包使用它：MetaMask 和一个 Solana 钱包。最后，两个用户在同一个合约状态中都有余额。

## 你将学到什么

* 双通道合约的形态：一个普通的 ERC-20 金库。
* 每条通道进行存款和取款所遵循的有序步骤。
* 为什么 Solana 通道有资金转入环节和清扫环节，而 EVM 通道没有。
* 如何在 Solidity 核心和 Solana 程序核心之间选择。
* 如何衡量每一步的成本。

## 开始之前

* 完成 [8. 向 EVM 用户开放你的 Solana 程序](https://docs.rome.builders/zh/rome-xue-yuan/08-open-your-solana-program-to-evm-users.md)。第 7 课讲解了本构建所使用的 Solana 通道，所以请先阅读 [7. 向 Solana 钱包开放你的 EVM 应用](https://docs.rome.builders/zh/rome-xue-yuan/07-open-to-solana-wallets.md) ，如果你跳过了的话。
* 在 Hadrian 上准备好 MetaMask，并在 Solana devnet 上准备好一个可签名 v1 交易的 Solana 钱包，二者都像第 3 课中那样已充值，并且 [水龙头](https://docs.rome.builders/zh/zi-yuan/faucets.md).
* 已安装 TypeScript SDK（[Rome SDK](https://docs.rome.builders/zh/chan-pin/rome-sdk.md#typescript-sdk-rome-protocol-sdk)）以及 Foundry 或 Hardhat（[部署 Solidity 合约](https://docs.rome.builders/zh/kai-fa-zhe-zhi-nan/deploy-solidity.md)).

## 本课

### 步骤 1。选择你的核心

双通道应用将核心逻辑保留在一侧。核心要么是 Solidity，Solana 用户通过其合成地址访问它；要么是原生 Solana 程序，EVM 用户通过一个轻量路由器访问它。两者都不是硬性规则。请根据你已有的代码、你的团队以及应用的功能来选择。 [选择你的核心](https://docs.rome.builders/zh/he-xin-gai-nian/choose-your-core.md) 列出了权衡取舍。

本课程构建的是第一种形态。这条路径更短，并使用标准的 EVM 工具链。如果你持有的是 Solana 程序，则第 8 课和 [集成 Solana 程序](https://docs.rome.builders/zh/kai-fa-zhe-zhi-nan/integrate-solana-program.md) 会讲解谁来签名、代币如何移动以及单线规则。其核心观点同样适用于本课：合约无法自行移动用户的代币，因此用户必须先授予权限。

### 步骤 2。编写金库

金库通过 `transferFrom` 提取代币，并通过 `transfer`将其返还。这里面没有任何内容特定于 Rome。

```solidity
function deposit(uint256 amount) external {
    require(token.transferFrom(msg.sender, address(this), amount));
    balanceOf[msg.sender] += amount;
}
function withdraw(uint256 amount) external {
    balanceOf[msg.sender] -= amount;
    require(token.transfer(msg.sender, amount));
}
```

构造函数接收 wUSDC 包装器地址。按照第 3 课中的方式将其部署到 Hadrian，然后验证源代码（[验证合约](https://docs.rome.builders/zh/kai-fa-zhe-zhi-nan/verify-contracts.md)）。完整合约位于 [构建双通道应用](https://docs.rome.builders/zh/kai-fa-zhe-zhi-nan/dual-lane-app.md).

### 步骤 3。运行 EVM 通道

MetaMask 用户持有的 USDC 可作为原生 gas，而金库接受 wUSDC。因此该通道包含四个步骤，每一步都是普通交易：

1. 使用 `withdraw_to_ata` 在 Withdraw 预编译合约上将部分 gas 包装成 wUSDC。
2. 如果需要，只需向包装器授予一次权限， `isEnabled(user)` 为 false。
3. `approve` 以便金库能够操作 wUSDC。
4. `存款` 存入金库。

没有资金转入或清扫环节。包装后的代币已经位于用户的 EVM 地址中。代码位于 [来自 MetaMask 的同一应用](https://docs.rome.builders/zh/kai-fa-zhe-zhi-nan/dual-lane-app.md#the-same-app-from-metamask)。预编译合约地址见 [预编译合约](https://docs.rome.builders/zh/can-kao/precompiles.md).

### 步骤 4。运行 Solana 通道

Solana 用户持有 SOL 和 SPL USDC。每一步都是由该钱包签名的 v1 交易：

1. 使用 `create_pda`为合成地址开通权限，一次即可。
2. 资金转入环节：将 USDC 从钱包的代币账户转入合成地址的代币账户。
3. 如果需要，则向包装器授予权限， `isEnabled` 为 false。
4. `approve` 供金库使用。
5. `存款`.

取款按相反顺序执行。 `withdraw` 将代币返还给合成地址，而清扫环节会将其转移到用户自己的钱包中。完成一次完整的取款和清扫后，合成地址中的已存入代币余额会回到零。这些是独立的交易，因此如果存款或清扫失败，请读取合成地址的代币余额，并让用户重试或将剩余部分清扫回来。逐步代码位于 [输入值](https://docs.rome.builders/zh/kai-fa-zhe-zhi-nan/dual-lane-app.md#value-in-deposit-step-by-step) 和 [输出值](https://docs.rome.builders/zh/kai-fa-zhe-zhi-nan/dual-lane-app.md#value-out-withdraw-step-by-step).

在应用的首次存款流程中，对两条通道都保留权限检查。一个代币账户只有一个委托方，因此后续向另一个程序（例如桥或路由器）授予权限，会替换包装器的授权。请在每次存款前检查 `isEnabled` ，而不要假设先前的授权仍然存在（[每个钱包一次性授权](https://docs.rome.builders/zh/he-xin-gai-nian/token-interop.md#one-time-grant-per-wallet)).

### 步骤 5。衡量每一步

阅读 `computeUnitsConsumed` 像第 7 课那样，从每个 Solana 通道步骤的 Solana 收据中获取数据。该指南列出了每一步在 Hadrian 上使用的计算单元、字节和账户。该表中的每一步都远低于原子预算。请将这些数字作为基线，再与你自己的数据进行比较。

如果某一步接近 140 万计算单元或 64 个账户，就通过中继路由它，或者简化它。参见 [约束](https://docs.rome.builders/zh/he-xin-gai-nian/constraints.md) 和 [计算预算](https://docs.rome.builders/zh/he-xin-gai-nian/compute-budget.md).

## 自测

请在 Hadrian 上执行此操作并保留链接。

1. 从 MetaMask 存款。从 Solana 钱包存款。
2. 阅读 `balanceOf` 在金库中对应两个地址。每个地址都会显示各自的存款。对于 Solana 用户，请在合成地址读取。
3. 在 Hadrian 区块浏览器中打开每笔交易（[Hadrian](https://docs.rome.builders/zh/wang-luo/hadrian.md) 提供了链接； [Via](https://docs.rome.builders/zh/rome-shang-de-ying-yong/via.md) 说明了它所展示的内容）。对于 Solana 通道的存款，请通过 Via 的“结算于”链接跳转到 Solana 交易；Solana 区块浏览器会显示其指令和计算单元。
4. 两个通道都执行取款。Solana 用户的 USDC 已返回其自己的钱包。

如果 Solana 通道的存款因 SPL 错误而回滚 `0x4`，则缺少对包装器的授权。

然后回答：

* 应用的哪一侧需要资金转入环节，为什么？
* 完成一次完整往返后，Solana 用户的合成地址持有什么？

<details>

<summary>答案</summary>

* Solana 通道。用户的 USDC 起始于其钱包的代币账户。资金转入环节将其移动到合成地址的代币账户，因此包装器余额会显示它。MetaMask 用户的代币则已经位于其 EVM 地址。
* 没有已存入的代币。清扫环节会将它们返还到用户的钱包。

</details>

## 下一步

[10. 从其他链引入用户和资金](https://docs.rome.builders/zh/rome-xue-yuan/10-from-other-chains.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.rome.builders/zh/rome-xue-yuan/09-build-a-dual-lane-app.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
