Compilation

Using third party libraries

One of the nice things about Waffle is that it enables you to import third party libraries when writing your smart contracts. All you need to do is install the library from npm.

For example you can install the popular @openzeppelin/contracts package:

yarn add @openzeppelin/contracts

Or if you prefer npm:

npm install @openzeppelin/contracts

After installing a library you can import it in your Solidity code:

pragma solidity ^0.5.0;

import "@openzeppelin/contracts/token/ERC721/ERC721Full.sol";

If you are using a custom node_modules location you can configure Waffle to recognize it. Change the npmPath setting in your .waffle.json file:

{
  "npmPath": "path/to/node_modules"
}

To read more about configuring Waffle, see Configuration.

Reducing compile times

By default, Waffle uses solc-js for compiling smart contracts. The package provides JavaScript bindings for the Solidity compiler. It is slow, but easy to use and install in the JS ecosystem.

Because we value speed and flexibility we provide some alternatives that you can use with Waffle. There are two other options:

  1. Installing solc directly on your computer, see Using native solc
  2. Using solc installed in a docker container, see Using dockerized solc

Using native solc

This is the fastest option but comes with some downsides. A system wide installation means that you are stuck with a single Solidity version across all of your projects. Additionally it might be complicated to install old versions of the compiler using this method.

We recommend this option if you only care about the latest solidity version.

You can find detailed installation instructions for native solc in the Solidity documentation.

Note

You need to install version compatible with your source files.

Change the compiler setting in your .waffle.json file:

{
  "compiler": "native"
}

To read more about configuring Waffle, see Configuration.

When compiling your smart contracts Waffle will now use the native solc installation.

Using dockerized solc

This is the recommended option if you want flexibility when it comes to the compiler version. It is pretty easy to set up, especially if you have Docker installed.

If you don’t have docker visit the Docker documentation to learn how to install it.

After you’ve installed docker you can install the Solidity compiler. Pull the docker container tagged with the version you are interested in, for example for version 0.4.24:

docker pull ethereum/solc:0.4.24

Then, change the compiler setting in your .waffle.json file:

{
  "compiler": "dockerized-solc",
  "docker-tag": "0.4.24"
}

The default value for docker-tag is latest. To read more about configuring Waffle, see Configuration.

When compiling your smart contracts Waffle will now use the docker image you pulled.