为了自动化你提到(在一个脚本中部署 + 验证) 您可以在deploy.js 脚本中添加以下代码行,以便在部署后自动验证它:
//wait for 5 block transactions to ensure deployment before verifying
await myContract.deployTransaction.wait(5);
//verify
await hre.run("verify:verify", {
address: myContract.address,
contract: "contracts/MyContract.sol:MyContract", //Filename.sol:ClassName
constructorArguments: [arg1, arg2, arg3],
});
现在你可以调用你常用的部署命令npx hardhat run scripts/deploy.js,终端将记录部署+验证,如:
MyContract deployed to "0xTheDeployedContractAddress" constructor arguments: arg1, arg2, arg3
Nothing to compile
Successfully submitted source code for contract
contracts/MyContract.sol:Contrac at 0xTheDeployedContractAddress
for verification on the block explorer. Waiting for verification result...
Successfully verified contract HoloVCore on Etherscan.
https://goerli.etherscan.io/address/0xTheDeployedContractAddress#code
这是我的整体deploy.js 脚本示例
const hre = require("hardhat");
async function main() {
const arg1 = "Contract Name";
const arg2 = "TKN";
const arg3 = 100000;
const MyContract = await hre.ethers.getContractFactory("MyContract");
const myContract = await MyContract.deploy(arg1, arg2, arg3);
await myContract.deployed();
console.log(`VLX Token deployed to ${myContract.address}`);
//wait for 5 block transactions to ensure deployment before verifying
await myContract.deployTransaction.wait(5);
//verify (source: https://hardhat.org/hardhat-runner/plugins/nomiclabs-hardhat-etherscan#using-programmatically)
await hre.run("verify:verify", {
address: myContract.address,
contract: "contracts/MyContract.sol:MyContract", //Filename.sol:ClassName
constructorArguments: [arg1, arg2, arg3],
});
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
请记住根据您要部署的网络上的流量调整 wait(n) 参数以调整等待时间。
有关以编程方式验证的更多信息,请查看来自 Hardhat-Etherscan docs 的此链接