【问题标题】:Hardhat compile, deploy and verify in a single scriptHardhat 在单个脚本中编译、部署和验证
【发布时间】:2022-11-08 21:22:59
【问题描述】:

我正在使用安全帽来编译、部署和验证以太坊智能合约。目前,我手动执行这些命令:

  1. npx hardhat compile

  2. npx hardhat run scripts/deploy.js

    等到部署脚本返回合约地址。然后手动将返回的地址复制到验证命令中:

    1. npx hardhat verify 0x<contract-address>

    2. 将编译好的合约 json (abi) 文件复制到文件系统中我的项目目录中。

    有什么方法可以自动运行所有这些命令?我正在考虑使用 shell/bash/powershell 脚本来自动化它,但我不确定这是否是实现这一目标的最佳方式。

    非常感谢!

【问题讨论】:

    标签: automation scripting blockchain ethereum hardhat


    【解决方案1】:

    刚刚看到这个所以只是发布我的答案以供将来参考。

    1. 运行npx hardhat run scripts/deploy.js 会自动编译未编译的代码。所以你不需要每次都运行它。

    2. 为了自动化你提到(在一个脚本中部署 + 验证) 您可以在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 的此链接

    【讨论】:

      【解决方案2】:

      您还可以创建一个名为 utils 的文件夹,并以更有条理的方式将其导入您的部署文件夹。

      const { run } = require("hardhat")
      
      const verify = async (contractAddress, args) => {
          console.log("Verifying contract...")
          try {
              await run("verify:verify", {
                  address: contractAddress,
                  constructorArguments: args,
              })
          } catch (e) {
              if (e.message.toLowerCase().includes("already verified")) {
                  console.log("Already verified!")
              } else {
                  console.log(e)
              }
          }
      }
      
      module.exports = {
          verify,
      }
      

      【讨论】:

        猜你喜欢
        • 2022-11-27
        • 2022-12-24
        • 2021-11-18
        • 2022-09-29
        • 1970-01-01
        • 1970-01-01
        • 2022-11-17
        • 2022-08-17
        • 2020-04-18
        相关资源
        最近更新 更多