Socket
Socket
Sign inDemoInstall

ethereum-waffle

Package Overview
Dependencies
Maintainers
1
Versions
238
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ethereum-waffle

Sweeter, faster and simpler than truffle.


Version published
Weekly downloads
39K
decreased by-29.11%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status

Ethereum Waffle

Library for writing and testing smart contracts.

Sweeter, simpler and faster than Truffle.

Philosophy

  • Simpler: Minimalistic, few dependencies.
  • Sweeter: Nice syntax, easy to extend.
  • Faster: Strong focus on the speed of test execution.

Features:

  • Sweet set of chai matchers, e.g.:
    • expect(...).to.be.revertedWith('Error message')
    • expect(...).to.emitEvent(contract, 'EventName).withArgs(...))
  • Importing contracts from npm modules working out of the box, e.g.:
    • import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
  • Fixtures that help write fast and maintainable test suites, e.g.:
    • const {token} = await loadFixture(standardTokenWithBalance);
  • Sub-second compilation with native and dockerized solc
  • Support for TypeScript
  • Documentation

Documentation

Documentation is available here.

Installation:

To get started using npm, type:

npm i ethereum-waffle -D

or with Yarn:

yarn add ethereum-waffle -D

Step by step guide

Add external dependency:

To add an external library install it using npm:

npm i openzeppelin-solidity -D

or with yarn:

yarn add openzeppelin-solidity -D

Example contract

Below is an example contract written in Solidity. Place it in contracts/BasicTokenMock.sol file of your project:

pragma solidity ^0.5.1;

import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";


// Example class - a mock class using delivering from ERC20
contract BasicTokenMock is ERC20 {
  constructor(address initialAccount, uint256 initialBalance) public {
    super._mint(initialAccount, initialBalance);
  }
}

Example test

Below is an example test written for the contract above compiled with Waffle. Place it under test/basicTokenMock.js file of your project:

const chai = require('chai');
const {createMockProvider, deployContract, getWallets, solidity} = require('ethereum-waffle');
const BasicTokenMock = require('../build/BasicTokenMock');


chai.use(solidity);
const {expect} = chai;

describe('INTEGRATION: Example', () => {
  let provider = createMockProvider();
  let [wallet, walletTo] = getWallets(provider);
  let token;

  beforeEach(async () => {
    token = await deployContract(wallet, BasicTokenMock, [wallet.address, 1000]);
  });

  it('Assigns initial balance', async () => {
    expect(await token.balanceOf(wallet.address)).to.eq(1000);
  });

  it('Transfer adds amount to destination account', async () => {
    await token.transfer(walletTo.address, 7);
    expect(await token.balanceOf(walletTo.address)).to.eq(7);
  });

  it('Transfer emits event', async () => {
    await expect(token.transfer(walletTo.address, 7))
      .to.emit(token, 'Transfer')
      .withArgs(wallet.address, walletTo.address, 7);
  });

  it('Can not transfer above the amount', async () => {
    await expect(token.transfer(walletTo.address, 1007)).to.be.reverted;
  });

  it('Can not transfer from empty account', async () => {
    const tokenFromOtherWallet = token.connect(walletTo);
    await expect(tokenFromOtherWallet.transfer(wallet.address, 1))
      .to.be.reverted;
  });

});

Note: You will also need to install following dependencies with npm to run the example above:

npm i chai -D
npm i mocha -D

Or with yarn:

yarn add mocha -D
yarn add chai -D

Compiling

To compile your smart contracts run:

npx waffle

To compile using a custom configuration file run:

npx waffle config.json

Example configuration file looks like this (all fields optional):

{
  "sourcesPath": "./custom_contracts",
  "targetPath": "./custom_build",
  "npmPath": "./custom_node_modules"
}

Running tests

To run the tests run the following command:

mocha

Adding an npm script

For convinience, you can add the following to your package.json:

{
  ...,
  "scripts": {
    "test": "waffle && mocha"
  }
}

Now you can build and test your contracts with one command:

npm test

Documentation

For detailed feature walkthrough checkout documentation.

Contributing

Contributions are always welcome, no matter how large or small. Before contributing, please read the code of conduct and contribution policy.

Before you issue pull request:

Make sure all tests and linters pass. Make sure you have test coverage for any new features.

Running tests

Note: To make end-to-end test pass, you need to:

  • have Docker installed, up and running
  • have Ethereum stable docker image pulled, if not run docker pull ethereum/solc:stable
  • have native solidity 0.5.* installed

To run tests type:

yarn test

To run linter type:

yarn lint

Building documentation:

cd docs
make html

Roadmap

Waffle 2.0

  • New matcher: changeBalance (see #9)
  • Faster compilation with native and dockerized solc (aside from solcjs)
  • Documentation
  • TypeScript rewrite

Waffle 2.1

  • Faster testing with native geth (aside from ganache)
  • New matcher: changeBalance for ERC20 tokens

Waffle 2.2

  • Debugging and profiling

License

Waffle is released under the MIT License.

FAQs

Package last updated on 04 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc