# 使用Hardhat验证已部署的智能合约


By [cutepanda](https://paragraph.com/@catpad) · 2023-04-04

---

要在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，同时也会提高合约的透明度和可信度。

---

*Originally published on [cutepanda](https://paragraph.com/@catpad/hardhat)*
