> 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/canton-zk-evm/build-on-ostia.md). Agents can also search and read this documentation through its [MCP server](https://docs.rome.builders/~gitbook/mcp).

# 在 Ostia 上构建

使用 Foundry 或 Hardhat 在 Ostia TestNet 上部署 Solidity 合约、验证合约，并通过 viem 或 ethers 进行连接。

Ostia 是一条 EVM 链，因此你已经在使用的工具也适用于它。部分方面有所不同；请参阅 [与 Ethereum 的差异](https://docs.rome.builders/zh/canton-zk-evm/evm-differences.md)。本页介绍如何部署和验证合约、如何读取事件，以及在哪里查看交易在 Canton 上最终确认。你需要一个持有少量 tROME 的地址。 [水龙头](https://docs.rome.builders/zh/canton-zk-evm/testnet.md#get-trome) 会发放 1 tROME，而 [Ostia 测试网](https://docs.rome.builders/zh/canton-zk-evm/testnet.md) 提供网络详情。

## 使用 Foundry 部署

这些命令已使用 Foundry 1.8 进行测试。运行 `foundryup` （或 `brew upgrade foundry`）先更新。请在 Foundry 项目中操作（`forge init ostia-counter` 会创建一个项目，其中包含一个 `Counter` 合约），并将已注资的密钥保存在 `PRIVATE_KEY`. `forge init` 已写入一个 `[profile.default]` 表格 `foundry.toml`。在其下方添加一行，以便部署和验证使用相同的设置进行编译：

```toml
[profile.default]
# ...保留 forge init 写入的行，不要更改
evm_version = "london"
```

```sh
export RPC=https://ostia.testnet.romeprotocol.xyz/rpc
forge create src/Counter.sol:Counter \
  --rpc-url $RPC --private-key $PRIVATE_KEY --broadcast
```

目前已在 Ostia 上运行的项目如下：

| 项目                                                                                | 编译器                 | EVM 版本     |
| --------------------------------------------------------------------------------- | ------------------- | ---------- |
| 该 [混合负载套件](https://github.com/rome-protocol/canton-zk-evm/tree/v0.1.0/load/mixed) | solc 0.8.17         | `london`   |
| Uniswap V2                                                                        | solc 0.5.16 和 0.6.6 | `istanbul` |
| TACEO 的 Merces 合约                                                                 | solc 0.8.37         | `osaka`    |

自 2026-10-28 起，Ostia 链已运行至大阪的所有分叉，因此该链接受为任何不晚于以下 EVM 目标构建的代码： `osaka`。表中的目标均已运行； `london` 是一个安全的选择。大阪之后新增的操作码将无法运行。

## 使用 Foundry 验证

该浏览器使用 Etherscan API，因此 `forge verify-contract` 可与 Etherscan 验证器配合使用。API 密钥可以是任意文本。

```sh
forge verify-contract <address> src/Counter.sol:Counter \
  --chain-id 770101 \
  --verifier etherscan \
  --verifier-url https://ostia.testnet.romeprotocol.xyz/api \
  --etherscan-api-key any \
  --watch
```

该浏览器每小时接受来自同一客户端的 30 次合约验证请求。超过此限制时，请稍候再重新发送请求。验证通过的合约会在浏览器的合约页面上显示其源代码。

## 验证大型合约

包含许多源文件的合约可能会因 HTTP `413`而被拒绝。浏览器的前置代理每个请求的上限约为 300 KB，而 forge 会对输入进行 URL 编码，因此编码后的内容比文件本身更大。 `POST /api/verify` 接收与标准 JSON 相同的输入，体积更小。代理限制适用于所有请求，因此超过 300 KB 的 JSON 请求正文也会被拒绝。工具包中的 [`verify-large.mjs`](https://github.com/rome-protocol/canton-zk-evm/blob/v0.1.0/load/mixed/scripts/verify-large.mjs) 脚本可以处理这种情况。它成功发送了 265 KB 的 Merces 合约标准 JSON，而 forge 无法发送该内容。要使用它，先从 Foundry 项目获取标准 JSON，并将其复制到工具包的文件夹中（在仓库克隆副本中为`load/mixed` ）：

```sh
forge verify-contract <address> <path:Name> --show-standard-json-input > std.json
cp std.json <your clone of canton-zk-evm>/load/mixed/
```

然后，在工具包文件夹中使用 `npm ci` 安装所需软件包并运行脚本。脚本会从工具包的 `config.json`中读取浏览器地址。（使用 Docker 时， `./run.sh node scripts/verify-large.mjs ...` 也能完成相同的操作。）

```sh
node scripts/verify-large.mjs <address> <path:Name> std.json <compiler> <runs> <evm> <constructor-args|->
```

编译器版本请填写完整版本字符串。对于一个 `Counter` 合约，使用 solc 0.8.28 编译，启用优化器并设置为 200 次运行，EVM 版本为 `london` 且没有构造函数参数时：

```sh
node scripts/verify-large.mjs 0xC249f8828EAe40Ad6a77178e4de88049ed5e962f \
  src/Counter.sol:Counter std.json v0.8.28+commit.7893614a 200 london -
```

浏览器会使用 std.json 中的设置进行编译。forge 会根据你的 foundry.toml 生成该文件，因此无论合约构建时使用了哪种优化器设置，都可以据此验证。脚本会提交验证请求，然后反复查询 `GET /api/verify/<guid>` 直到返回 `Pass` 或 `Fail`。每小时 30 次的限制同样适用。

## 使用 Hardhat 部署

以下步骤适用于 Hardhat 2（Hardhat 3 需要 ESM 配置）。这些步骤从空文件夹开始，先在 Ostia 上运行，随后在 DevNet 上运行；使用的是 Node 22 上的 Hardhat 2.29。请将已注资账户的密钥保存在 `PRIVATE_KEY`中，并包含其 `0x`.

```sh
mkdir ostia-hardhat && cd ostia-hardhat
npm init -y
npm i -D hardhat@2 @nomicfoundation/hardhat-toolbox@hh2
mkdir contracts scripts
```

`npx hardhat init` 会在终端中提示你回答问题，并自行创建一个示例合约，因此请手动创建这三个文件。首先是合约：

```solidity
// contracts/Counter.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract Counter {
    uint256 public number;

    function setNumber(uint256 newNumber) public {
        number = newNumber;
    }

    function increment() public {
        number++;
    }
}
```

配置中的 `solidity` 版本与 pragma 指令一致：

```js
// hardhat.config.js
require("@nomicfoundation/hardhat-toolbox");

module.exports = {
  solidity: { version: "0.8.17", settings: { evmVersion: "london" } },
  networks: {
    ostia: {
      url: "https://ostia.testnet.romeprotocol.xyz/rpc",
      chainId: 770101,
      accounts: [process.env.PRIVATE_KEY],
      timeout: 120000,
    },
  },
};
```

部署脚本：

```js
// scripts/deploy.js
const hre = require("hardhat");

async function main() {
  const counter = await hre.ethers.deployContract("Counter");
  await counter.waitForDeployment();
  console.log("Counter deployed to", await counter.getAddress());
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});
```

```sh
npx hardhat run scripts/deploy.js --network ostia
```

它会输出 `Counter deployed to 0x...`。 `timeout` 适用于每个请求。部署还会等待交易回执；只有 Canton 达成最终性后才会生成回执（见下文）。在 2026-10-25 的一次测试中，该命令耗时 20 至 23 秒，其中大部分时间都在等待区块达到最终状态。

## 使用 Hardhat 验证

在上面的配置中，将区块浏览器作为自定义链添加到 `@nomicfoundation/hardhat-verify`中；该插件由 `hardhat-toolbox` 提供。API 密钥可以是任意文本。我们使用 Hardhat 2.29.1 和 hardhat-verify 2.1.3，对一个已验证的合约进行了检查：插件找到了自定义链，查询了区块浏览器的 API，并返回“已验证”。尚未尝试提交新合约，因此如果无法使用，请告知我们。该插件会针对按网络配置 API 密钥发出弃用警告。此警告是预期行为：请保留下面按网络配置的 `apiKey` ，因为在此链上，单字符串形式会失败（“所选网络 (ostia) 错误”）。

```js
// hardhat.config.js，与 solidity 和 networks 同级
etherscan: {
  apiKey: { ostia: "any" },
  customChains: [
    {
      network: "ostia",
      chainId: 770101,
      urls: {
        apiURL: "https://ostia.testnet.romeprotocol.xyz/api",
        browserURL: "https://ostia.testnet.romeprotocol.xyz",
      },
    },
  ],
},
```

```sh
npx hardhat verify --network ostia <address> <constructor arguments>
```

保持相同的 `solidity` 设置，包括 `evmVersion`，与部署时使用的设置一致。区块浏览器会使用提供的设置进行编译。每小时最多验证 30 次的限制仍然适用。

## viem 和 ethers

viem 链定义示例：

```ts
import { createPublicClient, defineChain, http } from "viem";

export const ostia = defineChain({
  id: 770101,
  name: "Ostia TestNet",
  nativeCurrency: { name: "tROME", symbol: "tROME", decimals: 18 },
  rpcUrls: { default: { http: ["https://ostia.testnet.romeprotocol.xyz/rpc"] } },
  blockExplorers: {
    default: { name: "Ostia explorer", url: "https://ostia.testnet.romeprotocol.xyz" },
  },
});

const client = createPublicClient({ chain: ostia, transport: http() });
```

ethers 提供程序示例：

```js
import { JsonRpcProvider } from "ethers";

const provider = new JsonRpcProvider(
  "https://ostia.testnet.romeprotocol.xyz/rpc",
  { chainId: 770101, name: "ostia-testnet" },
  { polling: true },
);
```

该 RPC 不支持 WebSocket 和过滤器，因此 ethers v6 需要 `{ polling: true }` 来监听事件（`contract.on`或 `provider.on` 搭配过滤器使用）。只有在区块最终确认后才会生成收据，因此设置超时时间时，请预留约 10 到 30 秒。请按顺序发送同一账户的交易；参见 [与以太坊的差异](https://docs.rome.builders/zh/canton-zk-evm/evm-differences.md#nonces-and-the-order-of-sends).

在 viem 中， `multicall` 需要链上部署 Multicall3。Ostia 在常用地址部署了该合约，因此请将其添加到链定义中： `contracts: { multicall3: { address: "0xcA11bde05977b3631167028862bE2a173976CA11" } }` （参见 [与以太坊的差异](https://docs.rome.builders/zh/canton-zk-evm/evm-differences.md#the-standard-contracts)).

## 收据和事件

RPC 仅显示在 Canton 上已最终确认的区块。发送交易后， `eth_getTransactionReceipt` 在其所在区块最终确认前不会返回任何内容，这通常需要约 10 到 30 秒。在工具中设置收据超时时，请为证明过程和 Canton 确认留出时间。

不支持 WebSocket，也不支持 `eth_subscribe`，也不支持 `eth_newFilter`。若要监听事件，请轮询 `eth_getLogs` 并将区块范围设为自上次轮询以来的范围。每隔几秒轮询一次就足够了。在 DevNet 上，截至 2026-10-25 01:34 UTC 的过去 24 小时内，链繁忙时区块之间的中位间隔为 16.6 秒；链空闲时则每五分钟生成一个空区块。请将调用频率控制在每分钟 300 次以内。

## 在 Canton 上查看您的交易

1. 在区块浏览器中打开您的交易，网址为 `/tx/<hash>`.
2. 点击其区块链接， `/block/<number>`.
3. 区块页面会显示包含该区块记录的 Canton 更新、Canton 记录该更新的时间，以及与区块浏览器预期值并列的证明事实。
4. 点击 **验证**。您的浏览器会直接对区块浏览器提供的证明、区块头和交易执行证明检查。区块仍在生成证明时，该按钮会显示为灰色。

相同的数据也可通过 API 获取，例如 `GET /api/blocks/<number>` 和 `GET /api/canton/updates/<update id>`.

## 更大型的示例

&#x20;[`load/mixed`](https://github.com/rome-protocol/canton-zk-evm/tree/v0.1.0/load/mixed)  文件夹会部署一组合约，并通过这些合约发送混合流量：ERC-20、ERC-721、ERC-1155、ERC-3643 许可型代币、Uniswap V2，以及 TACEO 的 Merces 私密转账合约（其证明会在链上验证；不进行结算）。它的 [README](https://github.com/rome-protocol/canton-zk-evm/blob/v0.1.0/load/mixed/README.md) 中列出了相关命令。该工具包还会在区块浏览器上验证它部署的每个合约。按默认配置运行时，它需要约 12,000 tROME，远超水龙头提供的数量，因此请在您自己的 Ostia 上运行（参见 [运行您自己的 Ostia](https://docs.rome.builders/zh/canton-zk-evm/run-it-locally.md#load-it-with-a-realistic-mix)）。其编译器下载程序仅支持 linux x64。2026-10-24 的一次运行在一小时内发送了 13,496 笔交易，且全部最终确认。 [结果](https://github.com/rome-protocol/canton-zk-evm/blob/v0.1.0/load/mixed/RESULTS-2026-10-24.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/canton-zk-evm/build-on-ostia.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.
