SECRET OF CSS

Create a Smart Contract DevelopmentWorkflow Using Hardhat Tasks | by Kevin Janada | Jul, 2022


How to automate smart contract deployment scripts

1*FZlCCltoKM4HP5cCGFpqiQ
Photo by Ricardo Gomez Angel on Unsplash

Smart Contract development can involve tasks in addition to testing and deployment. Maybe you want to interact with the contract right after deployment or update a list of deployed contract addresses in a file.

Usually, you write scripts for each task and run them individually, which is fine, but when you have to run multiple scripts every time, this becomes tedious.

Hardhat tasks are a way to add automation to your smart contract development workflow. It provides a more structured way to create scripts. It also allows for combining multiple scripts into one. Hardhat tasks also make parsing parameters easier as it provides parameter type validations.

In this article, you will create a workflow using hardhat tasks. You will create and deploy an NFT Smart Contract to the Goerli test net, validate the address on Etherscan, and airdrop a few tokens.

This tutorial will use node js version 16. If you don’t have NodeJS yet, I recommend installing node version manager (nvm). Once you have nvm, run the following command:

Once you have the node installed, create the project directory. Move into that directory and run npx hardhat to generate hardhat boilerplate.

Hardhat will prompt you to select the type of project to use. In this case, use TypeScript.

1*RZnmImcvMkk NKeFn dX9w

Once you selected the type of project, answer all the prompts, answer ‘y’ to install the project’s dependencies, and wait for hardhat to generate the boilerplate.

1*v suogglQ5G8BttbUHcqvg

Once you are done, your project directory will look like this:

.env .example .eslintignore .eslintrc.js .gitignore .npmignore .prettierignore .prettierrc .solhint.json .solhintignore readme.md contracts greeter.sol nft.sol hardhat.config.ts package-lock.json package.json scripts deploy.ts test index.ts tsconfig.json

Add your contract deployer private key, Etherscan API key, and Alchemy key to the .env.example file. You will use the Goerli Ethereum test network for this tutorial.

After you add your keys, rename .env.example to .env .

You can get some if you need eth for the contract deployment. For the Etherscan API key, you can register here. To get your alchemy key, you can register here.

Your env file should now look like this:

Take a look at hardhat.config.ts . This is the file where you can set which network and private keys to use, among other things. The config file reads from the .env file to get the necessary environment variables.

You need to set the Goerli network configuration in the networks property of the config variable.

Add these to the .eslintrc.js files to remove warnings in VSCode. This step is optional as the project should still run without these configs.

For this tutorial, you will create an NFT contract using the standard ERC721 from Open Zeppelin. To do that, you have to install Open Zeppelin.

Create a new file called NFT.sol. Our contract will inherit Open Zeppelin’s ERC721 contract to get standard ERC721 functionalities.

Now that you have your smart contract ready, the first task you will create is the deploy task. Create a directory called tasks, and create a file called compile.ts, deploy.ts, and index.ts.

In deploy.ts create the deploy task to compile in the directory ./tasks/deploy.ts, and deploy our contract to a test network.

Add the exports in ./tasks.index.ts

Then add the tasks directory into your tsconfig.json file to make TypeScript include the tasks folder.

Now create the verify task that will verify the deployed contract on Etherscan. Create the file ./tasks/verify-etherscan.ts.

The task expects a string parameter you can insert contractAddress. Don’t forget to add the exports in ./tasks/index.ts.

Then add the exports in ./tasks/index.ts using this code:

Now for the final task, you will airdrop tokens to a few wallet addresses. Create the file ./tasks/airdrop.ts with the following code:

Add the exports in ./tasks/index.ts

Now for the final task, you will combine all the tasks you have created into one single task. Let’s create ./tasks/deploy-verify-airdrop.ts using the following code:

Again, don’t forget to add the exports in ./tasks/index.ts

Now that you have created the tasks, you can take a look at all the available tasks by running npx hardhat.

Now let’s run the deploy-verify-airdrop task by running

I am using one of the wallet addresses I generated as the airdrop address here, and you can change it to whatever wallet address you like.

1*rOPs33RNbcI1fugk01UtGg

From the output of the logs, you can see the NFT contract address deployed, the link of deployed contract that you can see on Etherscan, and the successful airdrop to the given address.

Congratulations, you have successfully deployed a smart contract, verified the deployment on Etherscan, and interacted with it in one single command. The scripts that you have written are kept small and modular. In addition, the list of tasks that you created can be viewed easily using the npx hardhat module.

Hardhat tasks are one of the most useful tools that any smart contract developer and you should utilize as it increases your productivity by keeping the codebase more organized.



News Credit

%d bloggers like this: