How to Debug Solidity contracts in Webstorm + Hardhat
This is a continuation of my learnings from debugging Solidity contracts with Truffle. If you are using Truffle for local development environment, you might want to start there.
Hardhat is now my preferred tool for local Solidity development. Although, there is not a fancy GUI, the CLI has does more than enough to support serious Solidity development. It just works. The speed at which you can develop and test is really unmatched (from what I’ve seen). On top of that, there are lots of open-source plugins for many different use-cases.
Alright, so if this is your first bout with Hardhat you’ll need to prepare somethings. Grab the latest NodeJS LTS version installed (I tested with v.14.17.5) and then open a Terminal.
With that open terminal, let’s call our first Hardhat task to start our simple project. Move to a new and clean directory then run the following command.
mkdir -p ~/code/hardhat/welcome && cd "$_" && npx hardhat init
After running that, NPM will take care downloading, installing, and then running Hardhat with the supplied argument. Which in turn, should present you with a Hardhat welcome screen.
From this welcome screen, Hardhat starts the project survey. Respond with the following answers (it should be enough to hit Enter 4 times). Upon answering the final question, Hardhat will create your project at the specified root file path with some sample files and then install the dependencies. This part could take a few minutes depending on your Internet speed + CPU.
✔ What do you want to do? · Create a sample project
✔ Hardhat project root: · hardhat-sample
✔ Do you want to add a .gitignore? (Y/n) · y
✔ Do you want to install the sample project's dependencies with npm (hardhat @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers)? (Y/n) · y
Now, let’s point our terminal at the new project and take a peek. If everything looks similar to this, we can continue.
~/code/hardhat/welcome ⌚ 6:22:38
$ ls
contracts node_modules package.json test
hardhat.config.js package-lock.json scripts
From here, we need to switch over to Webstorm. Open this new project in Webstorm. Then, open up the package.json
file. The only thing missing from having Webstorm build run/debug configurations is the scripts
field. Let’s add a new NPM run script for executing our Hardhat tests. Your package.json
should look something like this.
{
"name": "hardhat-project",
"scripts": {
"test": "hardhat test"
},
"devDependencies": {
"@nomiclabs/hardhat-ethers": "2.0.2",
"@nomiclabs/hardhat-waffle": "2.0.1",
"chai": "4.3.4",
"ethereum-waffle": "3.4.0",
"ethers": "5.4.4",
"hardhat": "2.6.0"
}
}
If everything went according to plan, after you save the file, Webstorm should automatically recognize the new configurations, and give you a neat play icon in the left gutter of the editor pane. Let’s click that and then in the pop-up menu, click Debug "test"
.
Once you do that, Webstorm takes care of starting a NodeJS debugger process and you can start setting breakpoints in your test scripts. If you open up the sample-test.js
file and start setting breakpoints to try it out.
From here, there are a ton of awesome things you can test. This setup can take you pretty far in your Solidity development. I know it’s not a true debug of Solidity. I am still looking for a plugin to help debug Solidity code directly from Webstorm. Remix IDE seems to be the only option AFAIK. But, I leave that for another day. — Allen