Writing Truffle tests with async/await

The documentation and sample projects with Truffle use promise chaining to write the test code.  As someone from the C# world, coming into the JS world it was getting really confusing to keep track (as well as all the variables I’d have to declare outside of the promise chain to use later).

I did a bit of research and found a way to do async/await in Truffle. I have put up a small simple repo by taking the truffle init project sample, and then doing a straight 1:1 conversion of their promise chaining tests over to the async/await format to help illustrate the difference. I really like the async/await version, as it is cleaner to read and much more compact (as you can see in the before/after screenshots below).

You can find the repo at https://github.com/DavidBurela/TruffleAsyncTests

 

before – promise chaining https://github.com/DavidBurela/TruffleAsyncTests/blob/master/test/metacoin.js

image

 

after – with async/await https://github.com/DavidBurela/TruffleAsyncTests/blob/master/test/asyncmetacoin.js

image

 

A shout out to Jim McDonald for pointing me to his GitHub repo where he had implemented async tests to get me started.

Advertisements

New releases of Truffle & TestRPC. Now easier to install.

I have mentioned previously that Truffle & TestRPC are my development tools of choice for Ethereum based development.

truffle logo

It is exciting to see that there have been new releases of both Truffle & TestRPC. I have been hanging out in the Truffle Gitter channel, and been fielding lots of questions about installation issues on Windows. As Tim has previously blogged, there have been a number of issues with installation. The installation issues were mostly due to compiling C based libraries, and requiring a number of build tools on the local machine. These issues have now been removed as Truffle & TestRPC are both now built, and then released as a completely pre-packaged release. This also means that installation times have dropped from 10 minutes, to 10 seconds! (Which means I’ll need to go back and update some of my old tutorials )

 

Truffle

Highlights: More reliable installation. Makes it easier for everyone to jump in and start playing!

https://github.com/trufflesuite/truffle/releases/tag/v3.3.0

TestRPC

Highlights: Installs quicker. Reduced memory consumption. Ability to persist the blockchain to disk, meaning you can suspend and then resume if you like  (I’ll still be using it mostly as a throw away in memory style dev environment).

https://github.com/ethereumjs/testrpc/releases/tag/v4.0.0

Whats next?

I’ve been waiting in suspense for these updated installers to drop for many months now. There have been a couple of projects on my backlog that had been blocked until this came out. A short list of what I’ll be working on in the near term are:

Reghack “Down Under” event roundup

Reghack was a 3 day hackfest that ran in Melbourne, Australia https://reghack.org/

The problem statement asked:
Do you have an interest in helping solve regulatory issues in the Financial Services and the Energy Sector in Australia?
How do we use RegTech to make regulatory compliance a strategic advantage that’s a win for the regulator, market participants and the consumer?

The focus of the event was to try and invigorate local innovation in the energy & financial services sectors, by allowing people to come together and explore how Blockchain could be utilised in these areas. The event was the brainchild of Chami Akmeemana https://www.linkedin.com/in/chami1/ who is a director of ConsenSys, the largest Blockchain focused consulting company in the world. Chami came to Melbourne and asked for local community support to help him organise and run the event. I was lucky enough to be tapped by Chami and invited to assist. I helped out by providing sponsorship for the meals via Microsoft, and delivered training to help upskill the community beforehand (more about that below).

The event had around 90 participants, with many more volunteers on the day. In the end 14 teams pitched their ideas which ranged from energy trading systems, ways to authenticate documents, to ways to eliminate GST during B2B transactions.
A big thanks to Chami for organising it all, and to all the volunteers that helped make the event a resounding success.

20170512_204517

Note: The roundup of the teams pitches are at the bottom of this post.

Continue reading

How to install Truffle & TestRPC on Ubuntu or Windows 10 with “Windows subsystem for Linux”

I previously wrote how you can install Truffle & TestRPC on a windows machine, by using the Windows installers for Node & npm.
However I have found it is a much nicer experience to use the Windows Subsystem for Linux which provides a native Ubuntu shell with bash, which allows me to run all the Linux tools natively without issues. These install steps will work on an Ubuntu machine as well.

Prerequisite if running on Windows

  1. Enable Windows subsystem for Linux.
    Follow this short guide on how to enable it https://msdn.microsoft.com/en-us/commandline/wsl/install_guide
    image
  2. After following the steps in the guide above. Simply start the Ubuntu bash shell
    image

Install steps

# Takes a clean Ubuntu image, up to being Ethereum dev ready.
# installing npm from official repo
https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions as Ubuntu default apt-get has a very old version (NodeJS v4.1)
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash –
sudo apt-get update -y && sudo apt-get upgrade -y

# install system basics
sudo apt-get install -y build-essential nodejs

# upgrade node/npm/npx and then install dev tools
sudo npx dist-upgrade
sudo npm install -g ethereumjs-testrpc truffle

# fix up permission issues. npm5 creates these as root, so user can’t modify by default
sudo chown -R $(whoami) ~/.npm
sudo chown -R $USER:$(id -gn $USER) ~/.config

now you should be able to create a folder, go into it and run `truffle init` to create a new project template.

imageimage

Additional things to do

Now that your machine has it installed, I recommend you follow my other guide to get Visual Studio code configured for Truffle development.

Configuring Visual Studio code for Ethereum Blockchain development

Visual Studio code is a great tool for editing Solidity smart contracts, and is available on Windows, Mac & Linux. There is a great plugin that enables Syntax highlighting, snippets, and compiling of the current contract (if you aren’t using an external tool) https://github.com/juanfranblanco/vscode-solidity/

This configuration works really well with Truffle (as shown in the final screenshot). You can read how to install Truffle on Windows in my previous post https://davidburela.wordpress.com/2016/11/18/how-to-install-truffle-testrpc-on-windows-for-blockchain-development/

Step 1: Install Visual Studio code
https://code.visualstudio.com/

Easy option on Windows: Just install via https://chocolatey.org/ by using the command
choco install VisualstudioCode –y

Step 2: Install Visual Studio extensions
Go into the extensions section, then install these plugins:

  • solidity
  • Material Icon Theme

image

Step 3: Enable icon theme
File –> Preferences –> File Icon Themeimage

Final result: Sexy workspace
image

How to install Truffle & TestRPC on Windows for Blockchain development

UPDATE 2017/08/25:
Now that Truffle has made installation easier, I have cleaned up and streamlined the windows install steps below.
However I recommend that instead look at using the new feature in Windows 10 that lets you run Linux tools directly in Windows. This is how I’ve been doing my development for the last few months
https://davidburela.wordpress.com/2017/05/12/how-to-install-truffle-testrpc-on-ubuntu-or-windows-10-with-windows-subsystem-for-linux/

I have been doing a bunch of Blockchain development work, one of which was recently featured on the front page of the Australian Financial Review and on Microsoft’s news website.
One of the trickiest things has been trying to get Windows environments correctly configured, as the tools are npm based expect to be running on a Linux machine. Here is the simplest install steps I have found

Step 1. Install windows tools with chocolatey:

Open a PowerShell prompt as Administrator

image

Step 2. Install the tools via npm:


Open a NEW PowerShell prompt as Administrator (to ensure that it reloads the path)

  • npm install -g npm
  • npm install -g ethereumjs-testrpc truffle

image

You may see some informational notes that optional components were skipped. You can run the commands truffle and testrpc.
image
image

Bonus Step: combining with Visual Studio Code

This is how I use Truffle & Visual Studio code together. Make sure you install and configure Visual Studio Code with the Solidity extensions.
If you navigate into the folder where your files are, typing “code .” will open Visual Studio Code in the current folder. Running the command after a truffle init gets you up and running quickly

image
image