Mae*_*ros 5 scripting automation blockchain ethereum hardhat
我正在使用安全帽来编译、部署和验证以太坊智能合约。目前,我正在手动执行这些命令:
npx hardhat compile
npx hardhat run scripts/deploy.js
等待部署脚本返回合约地址。然后手动将返回的地址复制到verify命令中:
npx hardhat verify 0x<contract-address>
将编译后的合约 json (abi) 文件复制到文件系统中我的项目目录。
有什么方法可以自动运行所有这些命令吗?我正在考虑使用 shell/bash/powershell 脚本来自动化它,但我不确定这是否是实现这一目标的最佳方法。
非常感谢!
刚刚看到这个,所以只是发布我的答案以供将来参考。
运行npx hardhat run scripts/deploy.js自动编译未编译的代码。所以你不需要每次都运行它。
对于您提到的自动化(在一个脚本中部署+验证),您只需在脚本中添加以下代码行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],
});
Run Code Online (Sandbox Code Playgroud)
现在您可以调用常用的部署命令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
Run Code Online (Sandbox Code Playgroud)
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;
});
Run Code Online (Sandbox Code Playgroud)
请记住调整 wait(n) 参数,以根据您要部署到的网络上的流量来调整等待时间。
有关以编程方式验证的更多信息,请查看Hardhat-Etherscan 文档中的此链接