# 使用Hardhat验证已部署的智能合约 **Published by:** [cutepanda](https://paragraph.com/@catpad/) **Published on:** 2023-04-04 **URL:** https://paragraph.com/@catpad/hardhat ## Content 要在BscScan上验证你的合约,你需要安装并配置hardhat-etherscan插件。以下是一些配置和验证的步骤: 1. 安装hardhat-etherscan插件:npm install --save-dev @nomiclabs/hardhat-etherscan 2. 在hardhat.config.js文件中,导入hardhat-etherscan插件,并添加你的BscScan API密钥:javascriptCopy coderequire("@nomiclabs/hardhat-etherscan"); const { alchemyApiKey, privateKey, bscscanApiKey } = require("./secrets.json"); // ... 其他配置代码 ... module.exports = { // ... 其他配置选项 ... etherscan: { apiKey: bscscanApiKey, }, }; 3. 更新secrets.json文件以包含你的BscScan API密钥:jsonCopy code{ "alchemyApiKey": "your_alchemy_api_key", "privateKey": "your_private_key", "bscscanApiKey": "your_bscscan_api_key" } 将your_bscscan_api_key替换为你的BscScan API密钥。要获取API密钥,请登录到BscScan,然后访问API-Keys页面。 4. 部署你的合约:npx hardhat run --network bscTestNet scripts/deploy.js 在部署合约后,记下部署日志中的合约地址。 5. 在Hardhat项目中验证合约:npx hardhat verify --network bscTestNet DEPLOYED_CONTRACT_ADDRESS "ConstructorArgument1" "ConstructorArgument2" --force 将DEPLOYED_CONTRACT_ADDRESS替换为你在第4步中记录的合约地址。将ConstructorArgument1,ConstructorArgument2等替换为你的合约构造函数的实际参数(如果有的话)。如果你的合约没有构造函数参数,可以删除这些参数。--force标志可用于跳过任何本地版本检查,强制验证。 按照这些步骤操作后,你的合约将在BscScan上进行验证。验证后,你可以在BscScan上查看合约的源代码和ABI,同时也会提高合约的透明度和可信度。 ## Publication Information - [cutepanda](https://paragraph.com/@catpad/): Publication homepage - [All Posts](https://paragraph.com/@catpad/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@catpad): Subscribe to updates - [Twitter](https://twitter.com/cutepandaweb3): Follow on Twitter