Just saw this so just posting my answer for future reference.
Running npx hardhat run scripts/deploy.js
automatically compiles uncompiled code. So you don't need to run it every time.
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