Setting up a professional blockchain development environment
By: Steve Higham
Tags: Integrated Development, Blockchain Development, Integration Testing, Developer Tools, Truffle

Intro

This article focusses on my experience of setting up a good working environment for blockchain development. Specifically I am using zsh, git, truffle, WebStorm, mocha and chai. The raison d’être of this article is to integrate JetBrains WebStorm into the truffle development environment so you can run a single file of truffle tests from an IDE with an integrated debugger. Unfortunately the blockchain toolset is not 100% reliable and backwards compatible. Therefore the ability to set a breakpoint, then examine the various JavaScript variables is invaluable and well worth the £ 10 a month license fee for WebStorm. I suspect this will work equally well from the IntelliJ IDEA product but you will need the ultimate edition at a whopping £ 40 per month. Still, if you already use this product, then this how-to should work just fine. The critical point as that you need the node extensions which are (unfortunately) not available for the community edition.

I’m aware that there are a number of similar how-to’s available. But they go out of date really fast. I found it challenging to get this environment up and running so hopefully others will benefit from my experience.

I haven’t included blow by blow accounts of how to install the individual components. Many web resources provide detailed instructions which are often platform specific.

I am running this environment on OSX (Big Sur) and Ubuntu 18. It should run on most platforms although you may need to do some additional work if you want ZSH, Git and the ZSH Git plugin to work well together on windows.

My Environment

I develop on a reasonably recent iMac (27") and a large screen laptop running Ubuntu 18. I have a QNAP NAS running Gogs and MariaDB in (separate) docker containers. Therefore my work-in-progress is held on the NAS, I can rapidly switch between the iMac and my laptop. Anything I want to publish goes to my GitHub account.

I have recently upgraded to ZSH on OSX and Ubuntu. I use Oh-my-zsh with the Git and History plugins. On the Mac I use the iTerm2 terminal and on Ubuntu I still use the standard terminal.

I currently use node versions 8, 10 and 12. Therefore all my node installations are managed by NVM.

Installing the individual components is reasonably straight forward and well covered by other resources. Therefore I will focus on the integration points.

Integrating the Components

The first challenge was the WebStorm terminal which continued to use BASH even though the host OS’s had been reconfigured to use Zsh. Simply running chsh from the WebStorm terminal (followed by a WebStorm restart) was sufficient to get the zsh prompt back with all the associated highlighting and plugin magic. WebStorm already has great integration with Git. You need to add a few plugins for things like Solidity support but this is straight forwards. The complicated part is to integrate with the node / truffle development environment.

Integrating Node / Truffle into WebStorm

The WebStorm IDE provides Edit Configurations under the Run menu. These configurations can be utilised by the Run and Debug commands. These commands can be fired from the Run Menu or buttons on the button bar. The “Edit Configurations” dialog (also on the Run menu) provides templates to integrate with numerous technologies including Mocha, Node.js, Nodeunit npm and Shell Scripts. I experimented with all of these points. However I achieved my best and simplest integration using the npm template.

First of all I configured a test.sh bash shell script to run from npm. This can be done within the package.json file as follows: -

Normally you can run commands like “npx truffle test test/unit/*.js” from your OS terminal or the WebStorm terminal and they work fine. However getting npx to run correctly from the IDE integration points is something of a challenge. By using this integration point you can write npx commands in the ./scripts/test.sh file and they work as expected. Once this integration is in place you can test it with “npm run” which should list your configured scripts. The script can now be run by “npm run test”.

My current project also has similar integration points to run the linter and coverage reports.

I created a BASH script (based on an inherited script) which takes a JavaScript test file as a parameter and runs that test file via truffle. If no parameter is provided it runs all the *.js test files under the test/unit directory. This is as follows:

As you can see, this scripts relies on npx installed scripts start-chain and stop-chain. These utilise ganache-cli to start and stop the blockchain with whatever environment you need for your project.

There is also a dependency on a script get-network-config to return the port number. In this case this script simply returns the Ganache default port 8545.

This script can easily be tested as it is developed by executing “npm run test [test/sometest.js]” from the OS or WebStorm console.

Once this script is running successfully from the console we need to integrate it into the IDE. This is done by creating an Edit Configuration as follows using the npm template:

And that’s it. You can now work on a JavaScript test file, set a break point, select the test “Run Configuration” and press the WebStorm debug button. The script will run as intended running your current file (via $FileDir$/$FileName$). WebStorm will set up a remote debugging session to node. Execution pauses at your breakpoint and you have all the facilities of the excellent JetBrains debugger at your disposal.

The biggest downside of this configuration is that you need to setup separate configurations for each version of node that you use. I would like the configurations to automatically pick up the terminal provided versions from the path. I haven’t figured out how to do this yet.

Happy hunting …