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(forge 在您的 PATH 中可用)。
  • 来自 forge create 输出的已部署合约地址。

1. 确认已部署地址

确保您拥有之前部署的“部署到”地址。在部署智能合约流程中,这是 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 会一直阻塞直到验证完成,这样您就不需要轮询。在主网上,将链 ID 更改为 988,验证器 URL 更改为 https://stablescan.xyz/api

3. 在 Stablescan 上确认验证

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

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

合约选项卡现在应该显示源代码,一个绿色的“已验证”徽章,以及完整的 ABI。用户可以在读取合约下读取状态,并在写入合约下发送交易。

故障排除

  • “字节码不匹配”:您的源代码编译成与已部署代码不同的字节码。这通常是由于 Solidity 版本或优化器设置不匹配造成的。明确传递 --compiler-version--optimizer-runs 以匹配您的 foundry.toml
  • “GU ID 未找到”:验证器尚未注册您的提交。使用 --watch 重新运行或手动检查响应中打印的 URL。
  • 合约使用库:为每个链接的库添加 --libraries src/Lib.sol:Lib:0xDeployedLibAddress

下一步推荐