Building your own Custom Hardhat Plugins from scratch

Nonthakon Jitchiranant
Laika Labs
Published in
5 min readFeb 17, 2022

--

If you’ve been using Hardhat for a while, there might be some situations where you have some tasks that you perform regularly (eg. remove console.log from your code). In that case, it would be really helpful to create some tasks or some plugins to reuse later.

So, in this article, we’re going to walk through how we can create a new custom hardhat plugin from scratch (really really from scratch).

What we going to create

In this article, I’m going to walk through how I write a custom plugin called hardhat-laika which is the plugin that going to sync ABI of artifacts generate from Hardhat to an application called Laika

If you don’t know what Laika is, Laika is a Blockchain development tool that helps you request smart contracts without having the hassle of writing a single line of code — a postman for web3!

How does it work?

How hardhat-laika is going to work is really simple. It works as follows.

  1. get the compiled artifacts from specific contracts
  2. send its ABI to https://api.getlaika.app
  3. get a response from it then generate a new URL so user can access their own contract through Laika

Alright, as you can see the purpose of it is pretty specific (it is not too simple or too complicated so I think it would be a pretty good example to walk through together).

So I want to encourage you guys to think about something you want to do yourself and follow along.

And by all means, I’m no expert in building a custom Hardhat plugin so if you want to add some suggestions I’m really welcome for it!

What exactly are Hardhat plugins?

According to https://hardhat.org/advanced/building-plugins.html

Hardhat plugins are reusable configurations or anything you can add to your config file you can pack it together to create another plugin.

Yep, so the easiest way to build a Hardhat plugin is to get started on your hardhat.config.js!

So what can you do with the Hardhat plugin?

As said in the documentation, you can do whatever you want on your config file. Let’s list them out so we can get the idea.

and that’s about it!

Let’s scaffold our plugin on the config file

Before anything else, let’s create a new Hardhat project by using the command

npx hardhat init

Then, set it up however you like, but for me, I will use this configuration.

√ What do you want to do? · Create a basic sample project
√ Hardhat project root: · /path/to/project/
√ Do you want to add a .gitignore? (Y/n) · y
√ Do you want to install this sample project's dependencies with npm (...)? (Y/n) · y

Because I want my hardhat-laika plugin that when I install it I would be able to do something like.

npx hardhat laika-sync --contract <name> --address <address>

Absolutely, The first thing I have to do is to create a new task for that.

The things that we are doing with this task are really simple and the thing is that this is all we have to do for the task that I want. We get the ABI and contract address upload it to Laika’s backend then generate a new URL so users can access it through the browsers.

Notice that I’ve been using “open” (to open up the browser) and node-fetch so don’t forget to install it!

Alright, let’s try it :))

First I use the command

Then the plugin would open up the browser and we’ll see the collection that is going to be imported.

Cool!

Now Let’s move it to Plugin

When building a new plugin, I really recommend you to use boilerplate provided by Nomiclabs which you can find here https://github.com/nomiclabs/hardhat-ts-plugin-boilerplate/

Try looking around the repo to see how things link together.

Another great way to explore how things link together is by looking around other plugins if you’re really getting started there are a lot of small plugins that you could go look around.

eg.

Alright so first thing first let’s clone the boilerplate to our Hardhat project (You can also click use as a template and just clone that repo)

git clone https://github.com/nomiclabs/hardhat-ts-plugin-boilerplate.git

Then let’s head to type-extensions.ts (I find it easier for me to set things up what it should look like first)

I’m going to put this code in that file.

This chunk of code is where I describe the interface of the task I created (laikaSync)

Next, let’s move our task to the plugin. I’m going to create a new folder call tasks and create a new file in that folder name laika-sync.ts (So when we have more and more tasks every task will not be mixed up in index.ts)

I move the task from hardhat.config.js to here. You might notice that I have changed the code structure a little bit to make it more versatile (hopefully lol). Also, you might notice that I have imported endpoint URL from config

So let’s create it.

Yep, this is quite simple. Now let’s move to our index.ts

Also, quite simple haha. In this file, I just extend the environment by adding laikaSync to the hre. And this is about it! your plugin is ready 👀

Let’s test it & publish

Because our plugin is written in Typescript so don’t forget to build it!

After I clone the boilerplate I change the name to hardhat-laika

So when I’m going to use it I would have to add require(”./hardhat-laika”) to my hardhat.config.js (the root of project one)

Yep! It’s still working like expected! Anyway, It would be a great idea if we write a test script for it but that’s the topic for another article! 👀

If you want to publish to npm and have never done it before just head to https://docs.npmjs.com/cli/v8/commands/npm-publish for more information.

Phew~, That was a lot to cover. Thanks a lot for reading up until this point! Hope you get a grasp of how to build a new Hardhat plugin.

Anyway if you have time I really encourage you to try hardhat-laika with Laika out here: https://www.npmjs.com/package/hardhat-laika

We will surely have more articles about how to use hardhat-laika out soon. Stay tuned!

See ya next time 😃

--

--