Hardhat发布合约源码到etherscan.io
etherscan
- 在该网站注册账号登陆
- 切换到主网络上



- 复制创建好的API Key
hardhat-verify
- 安装
hardhat-verify
npm install --save-dev @nomicfoundation/hardhat-verify
npm install --save-dev undici
- 在hardhat.config.js文件中添加代码如下
require("@nomicfoundation/hardhat-toolbox");
require("@nomicfoundation/hardhat-verify");
const { ProxyAgent, setGlobalDispatcher } = require("undici");
const proxyAgent = new ProxyAgent("http://127.0.0.1:7890");
setGlobalDispatcher(proxyAgent);
module.exports = {
defaultNetwork: "sepolia",
networks: {
sepolia: {
url: "https://sepolia.infura.io/v3/自己的API key",
accounts: ["使用哪个账户部署(填写私钥)"]
}
},
etherscan: {
apiKey: "etherscan网站上面创建的Key"
},
sourcify: {
enabled: true
},
solidity: {
compilers: [{ version: "0.8.24" }, { version: "0.5.16" }, { version: "0.6.6" }, { version: "0.8.7" },],
},
};
公开代码
npx hardhat verify --network sepolia 自己部署合约的地址