OpenZeppelin Test Helpers
JavaScript testing helpers for Ethereum smart contract development. These use web3 1.2 under the hood, but include support for truffle-contract
objects. Chai bn.js assertions using chai-bn are also included.
Installation
npm install --save-dev openzeppelin-test-helpers chai
Usage
// Import all required modules from openzeppelin-test-helpersconst BN constants expectEvent expectRevert = ; // Import preferred chai flavor: both expect and should are supportedconst expect = ; const ERC20 = artifacts; ;
Configuration
This library features support for both web3 and truffle contract instances. The default environment is 'web3'
, unless a 'truffle'
environment is automatically detected. In a 'truffle
' environment, the web3 provider will be pulled from truffle's global web3 instance, otherwise, it defaults to http://localhost:8545
.
While automatic detection should cover most use cases, both the environment and provider can be manually supplied:
environment: 'web3' provider: 'http://localhost:8080' ; const expectEvent = ;
truffle migrations
Automatic environment detection does not work inside truffle migrations, so the helpers must be manually configured.
environment: 'truffle' provider: web3currentProvider ;
Reference
This documentation is a work in progress: if in doubt, head over to the tests directory to see examples of how each helper can be used.
All returned numbers are of type BN.
balance
Helpers to inspect Ether balances of a specific account.
All of these functions return BN
instances, with balances in 'wei' by default.
balance current
async balance.current(account, unit = 'wei')
Returns the current balance of an account.
const balance = await balancecurrentaccount// same as new BN(web3.eth.getBalance(account)) const balanceEth = await balancecurrentaccount 'ether'// same as new BN(web3.utils.fromWei(await web3.eth.getBalance(account), 'ether'))
balance tracker
Allows you to keep track of the changes in an account's Ether balance.
async balance.tracker(account, unit = 'wei')
Creates an instance of a balance tracker.
const tracker = await balance
async tracker.get(unit = tracker.unit)
Returns the current balance of an account.
const tracker = await balance // instantiationconst currentBalance = await tracker // returns the current balance of account
async tracker.delta(unit = tracker.unit)
Returns the change in the balance since the last time it was checked (with either get()
or delta()
).
const tracker = await balancesendawait trackershouldbebignumber;await trackershouldbebignumber;
Or using get()
:
const tracker = await balance // instantiationconst currentBalance = await tracker // returns the current balance of accountawait trackershouldbebignumber;
A tracker can also return all balances and deltas in a specific unit:
const tracker = await balance;const balanceGwei = tracker; // in gigaweiconst balanceEther = tracker; // in ether
BN
A bn.js object. Use new BN(number)
to create BN
instances.
constants
A collection of useful constants.
constants.ZERO_ADDRESS
The initial value of a type address
variable, i.e., address(0)
in Solidity.
constants.MAX_UINT256
The maximum unsigned integer 2^256 - 1
represented in BN
.
constants.MAX_INT256
The maximum signed integer 2^255 - 1
represented in BN
.
constants.MIN_INT256
The minimum signed integer -2^255
represented in BN
.
ether
Converts a value in Ether to wei.
expectEvent (receipt, eventName, eventArgs = {})
Asserts the logs in receipt
contain an entry for an event with name eventName
, for which all entries in eventArgs
match. receipt
is the object returned by either a web3 Contract or a truffle-contract call.
const web3Receipt = await MyWeb3Contractmethods;; const truffleReceipt = await MyTruffleContract;;
async inTransaction (txHash, emitter, eventName, eventArgs = {})
Same as expectEvent
, but for events emitted in an arbitrary transaction (of hash txHash
), by an arbitrary contract (emitter
, the contract instance), even if it was indirectly called (i.e. if it was called by another smart contract and not an externally owned account).
// With web3 contractsconst contract = await MyContract;const transactionHash = await contractmethods;await expectEvent; // With truffle contractsconst contract = await MyContract;const txHash = await contract;await expectEvent;
async function inConstruction (emitter, eventName, eventArgs = {})
Same as inTransaction
, but for events emitted during the construction of emitter
. Note that this is currently only supported for truffle contracts.
expectRevert
Collection of assertions for transaction errors (similar to chai's throw
).
async expectRevert (promise, message)
This helper asserts that promise
was rejected due to a reverted transaction, and it will check that the revert reason includes message
. Use expectRevert.unspecified
when the revert reason is unknown. For example:
contract Owned { address private _owner; constructor () { _owner = msg.sender; } function doOwnerOperation() public view { require(msg.sender == _owner, "Unauthorized"); .... }}
Can be tested as follows:
const expectRevert = ; const Owned = artifacts;
async expectRevert.unspecified (promise)
This helper asserts that promise
was rejected due to a reverted transaction caused by a require
or revert
statement.
async expectRevert.assertion (promise)
This helper asserts that promise
was rejected due to a reverted transaction caused by an assert
statement or an invalid opcode.
async expectRevert.outOfGas (promise)
This helper asserts that promise
was rejected due to a transaction running out of gas.
makeInterfaceId
ERC165 (interfaces = [])
Calculates the ERC165 interface ID of a contract, given a series of function signatures.
ERC1820 (name)
Calculates the ERC1820 interface hash of a contract, given its name.
send
async send.ether (from, to, value)
Sends value
Ether from from
to to
.
async function send.transaction (target, name, argsTypes, argsValues, opts = {})
Sends a transaction to contract target
, calling method name
with argValues
, which are of type argTypes
(as per the method's signature).
singletons
async singletons.ERC1820Registry (funder)
Returns an instance of an ERC1820Registry deployed as per the specification (i.e. the registry is located at the canonical address). This can be called multiple times to retrieve the same instance.
time
async time.advanceBlock ()
Forces a block to be mined, incrementing the block height.
async time.latest ()
Returns the timestamp of the latest mined block. Should be coupled with advanceBlock
to retrieve the current blockchain time.
async time.latestBlock ()
Returns the latest mined block number.
async time.increase (duration)
Increases the time of the blockchain by duration
(in seconds), and mines a new block with that timestamp.
async time.increaseTo (target)
Same as increase
, but a target time is specified instead of a duration.
time.duration
Helpers to convert different time units to seconds. Available helpers are: seconds
, minutes
, hours
, days
, weeks
and years
.
await time;