> 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/kai-fa-zhe-zhi-nan/deploy-solidity.md).

# 部署 Solidity 合约

本指南介绍如何使用 Hardhat 和 Foundry 在 Rome EVM 上部署 Solidity 智能合约。

## 前提条件

* 已安装 Node.js v22.13+（Hardhat）或 Foundry
* 一个已充值的 Rome EVM 地址（请参见 [快速开始](/zh/ru-men/quickstart.md))
* 已将你的私钥导出为环境变量

```bash
export PRIVATE_KEY="0xYOUR_PRIVATE_KEY"
```

## 网络配置

| 网络           | RPC URL                                     | 链 ID     |
| ------------ | ------------------------------------------- | -------- |
| 本地           | `http://localhost:9090`                     | `1001`   |
| Hadrian（开发网） | `https://hadrian.testnet.romeprotocol.xyz/` | `200010` |
| Martius（测试网） | `https://martius.testnet.romeprotocol.xyz/` | `121214` |

## Hardhat

### 设置

```bash
mkdir my-rome-project && cd my-rome-project
npx hardhat --init
```

接受默认选项（Hardhat 3、当前目录、TypeScript + viem 模板）；依赖项会自动安装。

### hardhat.config.ts

```typescript
import hardhatToolboxViemPlugin from "@nomicfoundation/hardhat-toolbox-viem";
import { configVariable, defineConfig } from "hardhat/config";

export default defineConfig({
  plugins: [hardhatToolboxViemPlugin],
  solidity: {
    profiles: {
      default: { version: "0.8.28" },
      production: {
        version: "0.8.28",
        settings: { optimizer: { enabled: true, runs: 200 } },
      },
    },
  },
  networks: {
    rome_local: {
      type: "http",
      chainType: "l1",
      chainId: 1001,
      url: "http://localhost:9090",
      accounts: [configVariable("PRIVATE_KEY")],
    },
    hadrian: {
      type: "http",
      chainType: "l1",
      chainId: 200010,
      url: "https://hadrian.testnet.romeprotocol.xyz/",
      accounts: [configVariable("PRIVATE_KEY")],
    },
    martius: {
      type: "http",
      chainType: "l1",
      chainId: 121214,
      url: "https://martius.testnet.romeprotocol.xyz/",
      accounts: [configVariable("PRIVATE_KEY")],
    },
  },
});
```

`configVariable("PRIVATE_KEY")` 从环境变量解析（或从加密密钥库解析 — `npx hardhat keystore set PRIVATE_KEY`).

### 部署

```bash
npx hardhat run scripts/deploy.ts --network hadrian
```

### 在区块浏览器上验证

```bash
npx hardhat verify --network martius 0xCONTRACT_ADDRESS
```

需要来自 [验证合约](/zh/kai-fa-zhe-zhi-nan/verify-contracts.md) 在你的 `hardhat.config.ts`.

## Foundry

### 设置

```bash
forge init my-rome-project
cd my-rome-project
```

### 部署

```bash
# 本地
forge create --rpc-url http://localhost:9090 \\
  --private-key $PRIVATE_KEY \\
  src/Counter.sol:Counter

# 开发网
forge create --rpc-url https://hadrian.testnet.romeprotocol.xyz/ \\
  --private-key $PRIVATE_KEY \\
  src/Counter.sol:Counter
```

### 调用已部署的合约

```bash
# 读取
cast call 0xCONTRACT_ADDRESS "number()" \\
  --rpc-url https://hadrian.testnet.romeprotocol.xyz/

# 写入
cast send 0xCONTRACT_ADDRESS "increment()" \\
  --rpc-url https://hadrian.testnet.romeprotocol.xyz/ \\
  --private-key $PRIVATE_KEY
```

## 使用 Rome Solidity SDK

对于与 Solana 程序交互的合约，请使用公共 [`rome-solidity`](https://github.com/rome-protocol/rome-solidity) 仓库中的接口（npm 发布待定）：

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {ICrossProgramInvocation, IHelperProgram}
    from "@rome-protocol/rome-solidity/contracts/interface.sol";

contract MyRomeContract {
    IHelperProgram constant Helper =
        IHelperProgram(0xFF00000000000000000000000000000000000009);
    ICrossProgramInvocation constant Cpi =
        ICrossProgramInvocation(0xFF00000000000000000000000000000000000008);

    // 从调用者的 PDA 转移 SPL 代币
    function moveSpl(address to, uint64 tokens, bytes32 mint) external {
        Helper.transfer_spl(to, tokens, mint);
    }

    // 通过 CPI 调用任意 Solana 程序
    function callSolanaProgram(
        bytes32 programId,
        ICrossProgramInvocation.AccountMeta[] calldata accounts,
        bytes calldata data
    ) external {
        Cpi.invoke(programId, accounts, data);
    }
}
```

## 迁移现有协议

任何 Solidity 合约都可以在 Rome 上无需修改地部署——无论是合规合约、DeFi 协议，还是已有的成熟应用。你可以使用上面的 Hardhat / Foundry 流程 fork 并部署以下两个生产级示例：

* [**compound-on-rome-comet**](https://github.com/rome-protocol/compound-on-rome-comet) — 官方的 Compound v3（Comet），标准 Foundry。
* [**rome-aave-v3**](https://github.com/rome-protocol/rome-aave-v3) — 官方的 Aave v3。

两者都无需修改合约即可获得 Solana 执行和 CPI 可组合性。

## 部署限制

| 限制项         | 上限             | 备注                               |
| ----------- | -------------- | -------------------------------- |
| 最大合约大小      | 24 KB          | 与 Ethereum 相同（EIP-170，24,576 字节） |
| 交易大小限制      | 每个持有者 80 KB    | 大型部署会透明地分拆到多个持有者账户               |
| 计算预算        | 约 1.4M CU（原子级） | 对于复杂合约，请使用迭代模式                   |
| Solidity 版本 | 推荐使用 0.8.28    | 更早版本也可使用，但 0.8.28 与 SDK 最匹配      |

## 常见错误

| 错误         | 原因              | 修复方法                                                                                            |
| ---------- | --------------- | ----------------------------------------------------------------------------------------------- |
| `gas 资金不足` | EVM 地址没有 gas 代币 | 通过 [Rome 应用](https://app.devnet.romeprotocol.xyz) 来包装 gas（请参见 [快速开始](/zh/ru-men/quickstart.md)) |
| `nonce 过低` | 钱包中的 nonce 已过期  | 重置 MetaMask 账户或手动指定 nonce                                                                       |
| `执行回退`     | 合约逻辑失败          | 使用 `eth_call` 或 `forge test --fork-url`                                                         |
| `交易费用过低`   | gas 价格低于最低值     | 提高交易中的 gas 价格                                                                                   |

## 下一步

* [从 EVM 调用 Solana](/zh/kai-fa-zhe-zhi-nan/call-solana-from-evm.md) — 使用 CPI 预编译与 Solana 程序交互
* [验证合约](/zh/kai-fa-zhe-zhi-nan/verify-contracts.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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.rome.builders/zh/kai-fa-zhe-zhi-nan/deploy-solidity.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
