Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

验证智能合约

验证会将合约的源代码上传到区块浏览器,并证明它编译后与已部署的字节码一致。一旦完成验证,用户就可以在 Stablescan 上读取状态、调用函数并审计源代码,而无需重新托管你的代码。本指南将带你完成在 Stable 上验证由 Foundry 部署的合约。

前置条件

  • 一个已部署在 Stable 测试网或主网上的合约。如果你尚未部署,请参阅部署智能合约
  • 已安装 Foundry(PATH 中可使用 forge)。
  • 来自 forge create 输出的已部署合约地址。

1. 确认已部署的地址

确保你拥有之前部署时的 Deployed to 地址。在部署智能合约流程中,这是 forge create 之后打印出的值。

cast code 0xDeployedContractAddress --rpc-url https://rpc.testnet.stable.xyz | head -c 20
0x6080604052600436...

非空的字节码确认该合约已部署在该地址。

2. 运行 forge verify-contract

Foundry 的验证流程会将你的源代码提交给 Stablescan 验证器。

forge verify-contract \
  0xDeployedContractAddress \
  src/Counter.sol:Counter \
  --chain-id 2201 \
  --verifier blockscout \
  --verifier-url https://testnet.stablescan.xyz/api \
  --watch
Start verifying contract `0xDeployedContractAddress` deployed on 2201

Submitting verification of contract: Counter
Submitted contract for verification:
        Response: `OK`
        GUID: `abc123...`
        URL: https://testnet.stablescan.xyz/address/0xDeployedContractAddress
Contract verification status:
Response: `OK`
Details: `Pass - Verified`
Contract successfully verified

--watch 会阻塞直到验证完成,这样你就无需轮询。在主网上,将 chain ID 换成 988,验证器 URL 换成 https://stablescan.xyz/api

3. 在 Stablescan 上确认验证

在浏览器中打开合约页面。

https://testnet.stablescan.xyz/address/0xDeployedContractAddress

此时 Contract 标签页应该会显示源代码、一个绿色的 "Verified" 徽章以及完整的 ABI。用户可以在 Read Contract 下读取状态,并在 Write Contract 下发送交易。

故障排查

  • "Bytecode does not match":你的源代码编译出的字节码与已部署的不同。最常见的原因是 Solidity 版本或优化器设置不匹配。显式传入 --compiler-version--optimizer-runs 以匹配你的 foundry.toml
  • "GUID not found":验证器尚未注册你的提交。使用 --watch 重新运行,或手动检查响应中打印的 URL。
  • 合约使用了库:为每个链接的库添加 --libraries src/Lib.sol:Lib:0xDeployedLibAddress

推荐的后续步骤

  • 索引合约事件 — 使用 ethers.js 订阅链上事件并构建实时事件流。
  • 部署智能合约 — 搭建一个全新的 Foundry 项目并部署到 Stable 测试网。
  • JSON-RPC 参考 — 查看 Stable 支持哪些 eth_* 方法用于链上交互。