Hardhat compile, deploy and verify in a single script
Asked Answered
A

2

6

I am using hardhat to compile, deploy and verify an Ethereum Smart Contract. Currently, I am mannually peforming these commands:

  1. npx hardhat compile

  2. npx hardhat run scripts/deploy.js

Wait until the deployment script returns the contract address. Then manually copy the returned address to the verify command:

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

  2. Copy the compiled contract json (abi) file to my project's directory in the filesystem.

Is there any way that I can run all these commands automatically? I am thinking to automate it using shell/bash/powershell scripting but I am not sure if this is the best way to achieve this.

Many thanks!

Athletic answered 8/7, 2022 at 20:31 Comment(0)
S
2

Just saw this so just posting my answer for future reference.

  1. Running npx hardhat run scripts/deploy.js automatically compiles uncompiled code. So you don't need to run it every time.

  2. For the Automation you mentioned (deploy + verify in one script) you can just add the below lines of code in your deploy.js script to automatically verify it after it deploys:

       //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],
    });
    

Now you can just call your usual deploy command npx hardhat run scripts/deploy.js and the terminal will log the deployment + the verification like:

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

Here's a sample of my overall deploy.js script

    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;
    });

Remember to adjust the wait(n) parameter to adjust the wait time depending on the traffic on the network you're deploying to.

For more info about programatically verifying, check this link from Hardhat-Etherscan docs

Scarrow answered 6/9, 2022 at 18:11 Comment(0)
L
0

You can also create a folder called utils and import it into your deploy folder in a much more organized manner.

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,
}
Lasonde answered 8/11, 2022 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.