paint-brush
Compile Solidity Smart Contracts Faster in Truffle Projectsby@MuditG
3,125 reads
3,125 reads

Compile Solidity Smart Contracts Faster in Truffle Projects

by Mudit GuptaNovember 14th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The <a href="https://truffleframework.com/truffle" target="_blank">Truffle Suite</a> is the most widely used development framework for Solidity development but it is not perfect. We at <a href="https://polymath.network/" target="_blank">Polymath</a>, 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.

Company Mentioned

Mention Thumbnail
featured image - Compile Solidity Smart Contracts Faster in Truffle Projects
Mudit Gupta HackerNoon profile picture

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:

docker pull ethereum/solc:0.4.25

Replace the solc version with the one you need.

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

npm install truffle@next

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:






compilers: {solc: {version: "0.4.25", // Change this to whatever you needdocker: true,}}

Your truffle config file should look something like
























module.exports = {networks: {development: {host: 'localhost',port: 8545,network_id: '*'},},compilers: {solc: {version: "0.4.25",docker: true,settings: {optimizer: {enabled: true,runs: 200}}}},mocha: {enableTimeouts: false}};

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

./node_modules/.bin/truffle compile

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



"scripts": {"compile": "node_modules/.bin/truffle compile"}

and then you can directly use the command by running

npm run compile

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

npm install truffle -g

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:

truffle test

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.