# 编程日记：Hardhat合约验证（2022-08-30）

By [Corror](https://paragraph.com/@corror) · 2022-08-30

---

编程学习
----

### 做了什么？

1.  成功写出了验证智能合约的代码，但是在运行时出现了报错。
    

### 收获了什么？

#### 尝试解决验证失败的问题

我的 `verify` 函数声明：

    async function verify(contractAddress, args) {
        try {
            /**About "verify:verify":
             *  The first verify is a name in the type of string
             *  The second verify is the task of verify */
            await run("verify:verify", {
                address: contractAddress,
                constructorArgument: args,
            })
        } catch (e) {
            /**Why we need a try... catch.. here?
             * To throw the error if the verification get wrong. */
            if (e.message.toLowerCase().includes("already verified")) {
                console.log("Already Verified!")
            } else {
                console.log(e)
            }
        }
    }
    

我的 `verify` 函数调用：

    await verify(simpleStorage.address, [])
    

我的报错：

🔻第一次报错

第一次验证时，出现了如下报错：

    ENOENT: no such file or directory
    

🔻第二次报错

谷歌搜索后，根据[最高赞回答](https://github.com/NomicFoundation/hardhat/issues/1117#issuecomment-779213915)，尝试了 `npx hardhat clean` 命令，擦除了 cache, 结果直接导致无法科学上网，并出现了以下报错：

    Endpoint URL: https://api-rinkeby.etherscan.io/api
    Reason: Connect Timeout Error
    

根据[登链社区的回答](https://learnblockchain.cn/question/2939)以及[CSDN的回答](https://blog.csdn.net/qq_36838406/article/details/121208890)，是因为我们无法访问 EtherScan 的 API, 个人推测是域名污染问题。

同时，由于 WSL 也读取的是 Windows 当中的 hosts 文件，因此，我们需要在本地的 \[hosts\]([https://etherscan.io/address/](https://etherscan.io/address/)) 文件中，针对 `etherscan.io` 这个域名添加一行映射：

    104.22.14.57  api-rinkeby.etherscan.io  
    

在配置过后，`ping api-rinkeby.etherscan.io` 确实可以成功访问，但是问题依然存在...

---

*Originally published on [Corror](https://paragraph.com/@corror/hardhat-2022-08-30)*
