4.0.10 • Published 6 months ago

ethereum-waffle v4.0.10

Weekly downloads
13,086
License
MIT
Repository
github
Last release
6 months ago

Build Status

Ethereum Waffle

Library for writing and testing smart contracts.

Sweeter and simpler than truffle.

Works with ethers-js. Tastes best with ES6.

Philosophy

  • Simpler: minimalistic, a couple of helpers, matchers and tasks rather than a framework, few dependencies.
  • Sweeter: Nice syntax, fast, easy to extend.

Versions and ethers compatibility

  • Use version 0.2.3+ with ethers 3.*
  • Use version 1.0.0+ with ethers 4.*

Install:

To start using with npm, type:

npm i ethereum-waffle

or with Yarn:

yarn add ethereum-waffle

Step by step guide

Example contract

Below is example contract written in Solidity. Place it in contracts directory of your project:

pragma solidity ^0.5.1;

import "../BasicToken.sol";

contract BasicTokenMock is BasicToken {

  constructor(address initialAccount, uint256 initialBalance) public {
    balances[initialAccount] = initialBalance;
    totalSupply_ = initialBalance;
  }

}

Example test

Belows is example test written for the contract above written with Waffle. Place it in test directory of your project:

import chai from 'chai';
import {createMockProvider, deployContract, getWallets, solidity} from 'ethereum-waffle';
import BasicTokenMock from './build/BasicTokenMock';

chai.use(solidity);

const {expect} = chai;

describe('Example', () => {
  let provider;
  let token;
  let wallet;
  let walletTo;

  beforeEach(async () => {
    provider = createMockProvider();
    [wallet, walletTo] = await getWallets(provider);
    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(wallet.address)).to.eq(993);
    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 from empty account', async () => {
    const tokenFromOtherWallet = contractWithWallet(token, walletTo);
    await expect(tokenFromOtherWallet.transfer(wallet.address, 1))
      .to.be.revertedWith('Not enough balance on sender account');
  });
});

Compile

To compile contracts simply type:

npx waffle

To compile using custom configuration file:

npx waffle config.json

Example configuration file looks like this:

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

Run tests

To run test type in the console:

mocha

Adding a task

For convince, you can add a task to your package.json. In the sections scripts, add following line:

  "test": "waffle && test"

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

npm test

Features walkthrough

Import contracts from npms

Import solidity files from solidity files form npm modules that you added to your project, e.g:

import "openzeppelin-solidity/contracts/math/SafeMath.sol";

Create a mock provider

Create a mock provider (no need to run any tests!) and test your contracts against it, e.g.:

provider = createMockProvider();

Get example wallets

Get wallets you can use to sign transactions:

[wallet, walletTo] = await getWallets(provider);

Deploy contract

Deploy a contract:

token = await deployContract(wallet, BasicTokenMock, [wallet.address, 1000]);

Link a library:

myLibrary = await deployContract(wallet, MyLibrary, []);
link(LibraryConsumer, 'path/to/file:MyLibrary.sol:MyLibrary', myLibrary.address);
libraryConsumer = await deployContract(wallet, LibraryConsumer, []);

Chai matchers

A set of sweet chai matchers, makes your test easy to write and read. Below couple of examples.

  • Testing big numbers:
expect(await token.balanceOf(wallet.address)).to.eq(993);

Available matchers for BigNumbers are: equal, eq, above, below, least, most.

  • Testing what events where emitted with what arguments:
await expect(token.transfer(walletTo.address, 7))
  .to.emit(token, 'Transfer')
  .withArgs(wallet.address, walletTo.address, 7);
  • Testing if transaction was reverted:
await expect(token.transfer(walletTo.address, 1007)).to.be.reverted;
  • Testing if transaction was reverted with certain message:
await expect(token.transfer(walletTo.address, 1007)).to.be.revertedWith('Insufficient funds');
  • Testing if string is a proper address:
expect('0x28FAA621c3348823D6c6548981a19716bcDc740e').to.be.properAddress;
  • Testing if string is a proper secret:
expect('0x706618637b8ca922f6290ce1ecd4c31247e9ab75cf0530a0ac95c0332173d7c5').to.be.properPrivateKey;
  • Testing if string is a proper hex value of given length:
expect('0x70').to.be.properHex(2);
  • Testing whether the transaction changes balance
await expect(() => myContract.transferWei(receiverWallet.address, 2)).to.changeBalance(receiverWallet, 2);

Note: transaction call should be passed to the expect as a callback (we need to check the balance before the call). The matcher can accept numbers, strings and BigNumbers as a balance change, while the address should be specified as a wallet.

changeBalance calls should not be chained. If you need to chain it, you probably want to use changeBalances matcher.

  • Testing whether the transaction changes balance for multiple accounts
await expect(() => myContract.transferWei(receiverWallet.address, 2)).to.changeBalances([senderWallet, receiverWallet], [-2, 2]);

Fast complication

By default, Waffle uses solcjs. Solcjs is solidity complier cross-complied to javascript. It is slow, but easy to install. As an alternative, you can use the original Solidity compiler, which is faster, but requires more complex installation procedure, which is different for each operating system.

You can find installation instructions here.

Once installed you can use the following command for faster complication:

npx waffle --fast-compile

That should speed up complication time by a factor of x10.

Roadmap

  • New matcher: changeBalance (see #9)
  • Faster testing with parallelization
  • Faster compilation with incremental compilation
  • Documentation
  • Debugging
@safepay0x/core@safepay0x/libraries@safepay0x/peripherydistributed-town-smart-contracts@jbox/contracts@meetusvr/smart-contracts@infinitebrahmanuniverse/nolb-ethe@everything-registry/sub-chunk-1619artkdev-integration@abacus-network/hardhatgoswap-coregiveswap-coreairnode-protocolgluwa-token-testtools@trustlessfi/protocols@tracer-protocol/contracts@sfpy/core@sfpy/libraries@sfpy/periphery@weav3npm/periphery@withtally/local-testnet-deployer@ngmpool/sdk@ourz/our-contracts@overnight-contracts/blp-rebalancergnardinitest1greenwood-borrow-aggregator-integration0confirmation-solillumquo@biconomy/hyphen-contracts@chainscore/client@chainscore/contracts@cryptoswapdex/v3-periphery@cryptoswap2/v3-peripheryjam-sdk@consensolabs/claims-test@connext/watcher@clabs/celogivecakir-web3dex-sniper-promarketplace-nftchainscorename-wrappernemosapientenft-marketplace-projectnewdex-periphery@carlos0202/local-testnet-deployerico-crowdsale12infinitymint-btdms-store-purchase-serverpancakeswap-oraclepixiu-swap-coresurviving-the-bearsortition-sum-tree-factorysoulswap-hardhat-configspreadly-jstornadio-sdktestpackage-praveen5-otestsdkv1wepublic-contractsx-domain-messenger-plus@saxle/daemonsusdcuniversal-login-opsvoluptasomnis@klaytn/kds-oracles@klaytn/kss-oracles@kiwanoswap/v2-core@kangafinance/hardhat-framework@luckyfinance/hardhat-framework@m.arefev/nft@linkdrop/safe-module-contracts@linkdrop/ui-kit@linkdrop-widget/contracts@richeroneren/fast-presale@recruiter.party/contracts@safient/contracts@safient/claims@universal-login/ops@soundxyz/protocol@unilogin/ops@sc1/contracts@usedappify/testing@squadswap/v3-periphery@tetu_io/tetu-contracts@tetu_io/tetu-contracts-v2@tetu_io/tetu-liquidatorgem-ether@pooltogether/governor-contracts@pigi/contracts@pie-dao/pie-vaults@polycity/hardhat-framework@polycrypt/erdstall@midas-capital/sdk@mimic-fi/v1-contractsbizboa-bridge-server-tempbizboa-bridge-server-tmpboomerang-ethereum-identity-sdk-relayer@devprtcl/util-ts@devprtcl/utilskitsune-wallet
4.0.10-dev.238c11c

6 months ago

4.0.10-dev.efd5f2a

6 months ago

4.0.10

1 year ago

4.0.9

1 year ago

4.0.8

1 year ago

4.0.7

2 years ago

4.0.6

2 years ago

4.0.5

2 years ago

4.0.4

2 years ago

4.0.1

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

4.0.0-alpha.28

2 years ago

4.0.0-alpha.27

2 years ago

4.0.0-alpha.26

2 years ago

4.0.0-alpha.25

2 years ago

4.0.0-alpha.24

2 years ago

4.0.0-alpha.23

2 years ago

4.0.0-alpha.9

2 years ago

4.0.0-alpha.7

2 years ago

4.0.0-alpha.8

2 years ago

4.0.0-alpha.5

2 years ago

4.0.0-alpha.6

2 years ago

4.0.0-alpha.17

2 years ago

4.0.0-alpha.16

2 years ago

4.0.0-alpha.15

2 years ago

4.0.0-alpha.14

2 years ago

4.0.0-alpha.19

2 years ago

4.0.0-alpha.18

2 years ago

4.0.0-alpha.3

2 years ago

4.0.0-alpha.4

2 years ago

4.0.0-alpha.1

2 years ago

4.0.0-alpha.2

2 years ago

4.0.0-alpha.13

2 years ago

4.0.0-alpha.12

2 years ago

4.0.0-alpha.11

2 years ago

4.0.0-alpha.10

2 years ago

4.0.0

2 years ago

4.0.0-alpha.20

2 years ago

4.0.0-alpha.22

2 years ago

4.0.0-alpha.21

2 years ago

3.4.4

2 years ago

4.0.0-alpha.17.1

2 years ago

4.0.0-alpha.17.2

2 years ago

4.0.0-alpha.17.3

2 years ago

4.0.0-alpha.17.4

2 years ago

4.0.0-alpha.0

2 years ago

3.4.0

3 years ago

3.3.0

3 years ago

3.2.2

3 years ago

3.2.1

3 years ago

3.2.0

3 years ago

3.1.2

3 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

3.0.0-beta.3

4 years ago

3.0.0-beta.2

4 years ago

2.5.1

4 years ago

3.0.0-beta.1

4 years ago

2.5.0

4 years ago

2.4.1

4 years ago

2.4.0

4 years ago

2.3.2

4 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.2.0

4 years ago

2.3.0-istanbul.0

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

5 years ago

2.0.16

5 years ago

2.0.15

5 years ago

2.0.14

5 years ago

2.0.13

5 years ago

2.0.12

5 years ago

2.0.11

5 years ago

2.0.10

5 years ago

2.0.9

5 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

2.0.0-beta.11

5 years ago

2.0.0-beta.10

5 years ago

2.0.0-beta.9

5 years ago

2.0.0-beta.8

5 years ago

2.0.0-beta.7

5 years ago

2.0.0-beta.6

5 years ago

2.0.0-beta.5

5 years ago

2.0.0-beta.4

5 years ago

2.0.0-beta.3

5 years ago

2.0.0-beta.2

5 years ago

2.0.0-beta.1

5 years ago

2.0.0-beta.0

5 years ago

1.2.0

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.0

6 years ago

0.1.24

6 years ago

0.1.23

6 years ago

0.1.21

6 years ago

0.1.20

6 years ago

0.1.19

6 years ago

0.1.18

6 years ago

0.1.17

6 years ago

0.1.16

6 years ago

0.1.15

6 years ago

0.1.14

6 years ago

0.1.12

6 years ago

0.1.11

6 years ago

0.1.10

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago