> 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/03-first-contract-on-hadrian.md).

# 3. 你在 Hadrian 上的第一个合约

将钱包连接到 Hadrian，向其注资，使用 Hardhat 部署合约，并跟踪你的交易，直到最终结算它的 Solana 交易。

你将一个小型 Solidity 合约部署到 Hadrian，罗马的公共测试网。部署方式是标准的 Hardhat。新的是你用 USDC 支付 gas，而且浏览器会把你引导到你所做操作对应的 Solana 侧。

## 你将学到什么

* 如何将钱包和 Hardhat 指向 Hadrian。
* 为什么 gas 以 USDC 计价，以及钱包如何获取它。
* 为什么在 Rome 上写入操作的 gas 成本比以太坊习惯预期的更高。
* 如何在 Via 浏览器中查看你的交易，并跟踪到结算它的 Solana 交易。
* 如何验证你的源码，以便浏览器显示它。

## 开始之前

你已阅读 [第 2 课](https://docs.rome.builders/zh/rome-xue-yuan/02-what-you-can-build.md)，其中说明了这个合约迈向什么目标的第一步。请准备好：

* Node.js 版本为 [快速开始](https://docs.rome.builders/zh/kuai-su-ru-men/quickstart.md) 中列出。
* 已安装 MetaMask。
* 一个新账户，或一个一次性密钥。你会将它的私钥导出到你的 shell 中，因此不要使用持有真实资金的密钥。
* 一个设置为 Devnet 的 Solana 钱包，仅在你从 Solana 端注资时需要。

## 路径

### 步骤 1. 将 Hadrian 添加到你的钱包

Hadrian 是一个标准的 EVM 网络。你需要手动将其添加到 MetaMask，填写名称、RPC URL、链 ID、货币符号和浏览器 URL。所有值都取自 [快速入门，第 1 步](https://docs.rome.builders/pages/c6fe54b3d8602242fa6c57a2f041d5a855b35cca#id-1.-add-hadrian-to-metamask)。这些值也在 [Hadrian](https://docs.rome.builders/zh/wang-luo/hadrian.md)上。将货币符号设置为 `USDC`.

Hardhat 也需要相同的链 ID 和 RPC URL。你会在第 3 步将它们写入其配置中。

### 步骤 2. 为钱包注资

Hadrian 上的 gas 使用 USDC，以 Circle 的 Solana Devnet USDC 为支撑。SOL 不是 gas 代币。Rome 没有水龙头。你需要在别处获取测试 USDC，并通过 Rome Bridge 转入。通过桥接到达的存款默认会作为 gas 到账。请将桥接页面的接收选项保持为 gas。

有两个来源：

* **来自 Solana Devnet。** 从 Circle 水龙头获取 devnet USDC 和少量 devnet SOL 以支付 Solana 费用。使用其 Solana 路由桥接 USDC，并将你的 EVM 地址作为接收方。桥接会要求你的 EVM 钱包在 Hadrian 上签署一笔认领交易，而这会将 USDC 变成 gas。
* **来自 EVM 测试网。** 在 Ethereum Sepolia 上铸造测试 USDC，然后将其桥接进来。你需要少量 Sepolia ETH 来支付源端交易费用。

步骤和水龙头链接在 [快速入门，第 2 步](https://docs.rome.builders/pages/c6fe54b3d8602242fa6c57a2f041d5a855b35cca#id-2.-fund-your-wallet), [在 Hadrian 上为钱包注资](https://docs.rome.builders/zh/wang-luo/hadrian.md#fund-a-wallet) 和 [水龙头](https://docs.rome.builders/zh/zi-yuan/faucets.md)。桥本身在 [Rome Bridge](https://docs.rome.builders/zh/rome-shang-de-ying-yong/bridge-api.md).

要注入多少？快速入门给出了最低金额和较为充裕的金额。在开始之前请先阅读。

继续之前，请先在 MetaMask 中检查余额。如果显示为零，则转账尚未完成。桥接页面会显示每一步及其预计耗时。

### 步骤 3. 创建 Hardhat 项目

使用 TypeScript 和 viem 模板创建一个 Hardhat 3 项目。然后添加一个 `hadrian` 条目位于 `网络` 下，使用 Hadrian 页面上的链 ID 和 RPC URL。私钥来自环境变量，或者来自 Hardhat 的加密密钥库。除了一次性密钥之外，建议使用密钥库。

准确的命令和配置在快速入门的 [第 3 步](https://docs.rome.builders/pages/c6fe54b3d8602242fa6c57a2f041d5a855b35cca#id-3.-create-a-hardhat-project) 和 [第 4 步](https://docs.rome.builders/pages/c6fe54b3d8602242fa6c57a2f041d5a855b35cca#id-4.-configure-hardhat-for-rome). [部署 Solidity 合约](https://docs.rome.builders/zh/kai-fa-zhe-zhi-nan/deploy-solidity.md#hardhat) 显示完整配置文件，而 [在 Foundry 中的相同流程](https://docs.rome.builders/zh/kai-fa-zhe-zhi-nan/deploy-solidity.md#foundry).

### 步骤 4. 编写并部署合约

快速入门中的合约 `HelloRome`刻意保持朴素。它存储一个问候语，统计对 `greet()`的调用次数，并发出一个事件。其中没有任何内容是 Rome 所特有的。

这正是这一步的目的： `HelloRome` 无需修改即可部署。在迁移其他合约之前，请检查 [已知限制](https://docs.rome.builders/zh/an-quan/known-limitations.md) ，以查看是否存在不支持的操作码和 gas 差异。先写好它，然后针对 `hadrian`运行部署脚本。脚本会部署，调用 `greet()` 一次，并将计数器和问候语读回。两者都复制自快速入门的 [第 5 步](https://docs.rome.builders/pages/c6fe54b3d8602242fa6c57a2f041d5a855b35cca#id-5.-write-a-contract) 和 [第 6 步](https://docs.rome.builders/pages/c6fe54b3d8602242fa6c57a2f041d5a855b35cca#id-6.-deploy).

成功后，你会得到一个合约地址和计数器值 1。

### 步骤 5. 理解其成本

这次部署消耗的 gas 远高于同一合约在以太坊上的成本。Rome 上一次写入的大部分 gas 都用于创建 Solana 账户。已部署的合约会为其代码和存储创建新账户。向一个从未使用过的接收方转账也是如此，新代币账户也一样。

因此有两条规则：

* 让你的工具使用 `eth_estimateGas`来估算 gas。Hardhat， `forge create` 和 `cast send` 会。 `forge script` 需要 `--skip-simulation` 也应这样做。
* 绝不要硬编码 21,000。会创建账户的写入操作，例如部署或向从未使用过的地址转账，在如此低的限制下会失败。

Rome 按交易实际使用的 gas 收费，因此较高的估算不会给你带来额外成本。保留工具添加的安全余量：对于调用 Solana 程序，估算值可能偏低。 [JSON-RPC 支持](https://docs.rome.builders/zh/can-kao/json-rpc.md#gas-is-solana-derived-estimate-dont-hardcode) 已经记录了真实写入的测量数值。

### 步骤 6. 在浏览器中查看

打开 Hadrian 的 Via 浏览器，查找你的合约地址。其页面会链接创建交易，并列出 `greet()` 调用在其交易中。浏览器链接在 [Hadrian](https://docs.rome.builders/zh/wang-luo/hadrian.md).

Via 会显示常规的 EVM 视图：区块、发送方、已用 gas、事件日志。其“Settled in”一行保存着结算你交易的 Solana 交易签名。打开该链接。Solana 浏览器会显示该交易的指令、它触及的程序以及它消耗的计算单元。 [通过 Explorer](https://docs.rome.builders/zh/rome-shang-de-ying-yong/via.md) 说明 Via 还会显示什么。

在那里读取计算单元，不要从 `gasUsed`读取。在 Rome 上， `gasUsed` 并不是 Solana 计算量的可靠度量。

### 步骤 7. 验证源码

验证会发布你的源码和 ABI，随后浏览器会显示“源码已验证”徽章。Rome 运行的是托管的 Sourcify 服务，因此标准的 Hardhat 和 Foundry 验证流程都可用。你只需将它们指向 Rome 的验证器并提供链 ID。

端点位于 [验证器端点](https://docs.rome.builders/zh/kai-fa-zhe-zhi-nan/verify-contracts.md#verifier-endpoint)，而配置和命令在 [Hardhat](https://docs.rome.builders/zh/kai-fa-zhe-zhi-nan/verify-contracts.md#hardhat) 部分。对 Hadrian 使用测试网验证器。主网验证器是另一个 URL，这在第 11 课中很重要。

## 自测

你的成果：在 Hadrian 浏览器中打开你的合约，并看到以下三项全部内容。

* 你的部署交易，其“Settled in”后的 Solana 签名，以及该 Solana 交易在 Solana 浏览器中的指令。
* 第二笔交易，即 `greet()` 调用，带有来自你的合约地址的一条事件日志。该日志是 `Greeted` 事件；其第一个 topic 是 `Greeted(address,uint256)`.
* 合约上的“源码已验证”徽章。

如果你遇到困难，常见错误列在 [快速开始](https://docs.rome.builders/zh/kuai-su-ru-men/quickstart.md#common-errors)中。最常见的是 `余额不足`余额不足

。然后回答这些问题。

1. 为什么首次部署在 gas 上比以太坊贵得多？
2. 你在哪里读取交易的 Solana 计算单元： `gasUsed`，还是 Via 链接到的 Solana 交易？
3. 你使用的测试网 USDC 有价值吗？

<details>

<summary>答案</summary>

1. 大部分 gas 用于创建 Solana 账户：合约、其存储，以及任何新的代币账户。
2. Solana 交易。Via 会在“Settled in”一行中链接它，而 Solana 浏览器会显示其计算单元。 `gasUsed` 并不是一个可靠的替代指标。
3. 没有。Hadrian 在 Solana Devnet 上结算，因此其 USDC 和 SOL 不是现实货币。

</details>

## 下一步

[在 Arena 中注册你的应用](https://arena.mainnet.romeprotocol.xyz/apps/new) 并将这个 Hadrian 合约关联到它。该应用会迁移到 Building。

[4. Rome 如何运行你的代码](https://docs.rome.builders/zh/rome-xue-yuan/04-how-rome-runs-your-code.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/03-first-contract-on-hadrian.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.
