Compile Solidity Smart Contracts Faster in Truffle Projects

Mudit Gupta
HackerNoon.com
4 min readNov 15, 2018

--

The Truffle Suite is the most widely used development framework for Solidity development but it is not perfect. We at Polymath, deal with a large set of interconnected smart contracts. The sheer number of smart contracts cause the compilation time to go over 80 seconds on my daily driver.

And adding more contracts everyday :)

Lucky for us, we can make the compilation go faster.

Truffle uses solc.js by default rather than native binaries for the compilation of smart contracts. solc.js offers greater portability but is significantly slower than natively compiled binaries.

How much faster are the native binaries, you may ask? I managed to cut down Polymath-core’s compilation time from 80 seconds to just under 15 seconds!

It’s that fast!

Why doesn’t everyone already use native binaries? Managing native binaries especially when you need to use different versions of Solidity in different projects is hard but don’t sweat, we can easily use docker images as a sweet alternate solution. I’ll guide you through the process.

Truffle 4.X does not allow us to set a custom compiler but Truffle 5.X supports this feature. However, Truffle 5.X uses Web3 1.X while Truffle 4.X uses Web3 0.X which makes migrating to Truffle 5.X harder. We don’t actually need to migrate completely to Truffle 5.X just to get faster compilation though. I’ll show how we can keep using Truffle 4.X for tests and migrations while using Truffle 5.X for faster compilation.

Let’s start by setting up the Truffle 5.X. Later, I’ll explain how we can use Truffle 5.X in conjunction with 4.X to get best of both worlds.

Setting up Truffle to use Docker Image for solc

  1. Firstly, you need to install Docker. The easiest way is to follow the Instructions on https://docs.docker.com/install/#supported-platforms
  2. You need to pull the docker image that you want to use by using the following command:

Replace the solc version with the one you need.

3. Head to your project’s root directory and Install truffle 5.X using:

4. Edit your truffle config (present in the root directory of your project) to specify the version of solidity to use. For using docker image, you’ll need to add:

Your truffle config file should look something like

5. Try out the new speedy compiler by executing the below command:

Bonus: To make the command shorter, add it under scripts in your package.json like:

and then you can directly use the command by running

Note: We are not directly calling truffle compile as we will be installing Truffle 4.x globally. If you want to use Truffle 5.x only then you can install it globally and directly use truffle commands

Using Truffle 4.X along with Truffle 5.X

  1. Install Truffle 4.X globally using

2. Run ./node_modules/.bin/truffle compile or npm run compile before truffle test/migration commands so that you enjoy faster compilation.

3. Use truffle commands normally. For example, to run tests, use:

The above command will run tests using Truffle 4.X as it is globally installed.

Note: I am using paths supported by *nix systems in this article. If you are using windows, replace / with \\ in the paths and make any other relevant changes if required. Also, consider dual booting a Linux system for development :)

Final Thoughts

Solc.js is Slow and Serious while native binaries are Fast and Furious! Perhaps using a combination of both Truffle stable and beta is the best way to ensure a good mix of stability, features and speed.

If you need any help, feel free to leave a comment here or ask in Truffle’s Gitter channel. If you are interested in joining me at Polymath, please apply through our website.

--

--

Mudit Gupta
HackerNoon.com

Blockchain @ polymath.network | Engineer | Security Researcher | Loves coding and Gaming | github.com/maxsam4