Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
ethereum-waffle
Advanced tools
Library for writing and testing smart contracts.
Sweeter, simpler and faster than Truffle.
expect(...).to.be.revertedWith('Error message')
expect(...).to.emitEvent(contract, 'EventName).withArgs(...)
)import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
const {token} = await loadFixture(standardTokenWithBalance);
Documentation is available here.
To get started using npm, type:
npm i ethereum-waffle -D
or with Yarn:
yarn add ethereum-waffle -D
To add an external library install it using npm:
npm i openzeppelin-solidity -D
or with yarn:
yarn add openzeppelin-solidity -D
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);
}
}
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
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"
}
To run the tests run the following command:
mocha
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
For detailed feature walkthrough checkout documentation.
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.
Note: To make end-to-end test pass, you need to:
docker pull ethereum/solc:stable
To run tests type:
yarn test
To run linter type:
yarn lint
Building documentation:
cd docs
make html
Waffle is released under the MIT License.
FAQs
Sweeter, faster and simpler than truffle.
The npm package ethereum-waffle receives a total of 31,725 weekly downloads. As such, ethereum-waffle popularity was classified as popular.
We found that ethereum-waffle demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.