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
npm install @openzeppelin/contracts
After installing a library, you can import it into your Solidity code:
pragma solidity ^0.6.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 nodeModulesDirectory
setting in your .waffle.json
file:
{
"nodeModulesDirectory": "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:
Installing solc directly on your computer, see Using native solc
Using solc installed in a docker container, see Using dockerized solc
Using native solc¶
This is the fastest option but comes with some downsides. The 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 the 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 compilerType
setting in your .waffle.json
file:
{
"compilerType": "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 compilerType
setting in your .waffle.json
file:
{
"compilerType": "dockerized-solc",
"compilerVersion": "0.4.24"
}
If no compilerVersion
is specified the docker tag pulled defaults to
latest
. To read more about configuring Waffle, see Configuration.
When compiling your smart contracts, Waffle will now use the docker image you pulled.
Using dockerized vyper¶
This is the option if you have contracts in Vyper. You will need Docker installed.
To install docker visit the Docker documentation to learn how to do it.
To install dockerized Vyper pull the docker container tagged with the version you are interested in, for example for version 0.1.0:
docker pull vyperlang/vyper:0.1.0
Then, change the compilerType
setting in your .waffle.json
file:
{
"compilerType": "dockerized-vyper",
"compilerVersion": "0.1.0"
}
If no compilerVersion
is specified the docker tag pulled defaults to
latest
. To read more about configuring Waffle, see Configuration.
When compiling your smart contracts, Waffle will now use the docker image you pulled.