Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename 'environment' config option & add singletons setup #96

Merged
merged 17 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.5.4 (unreleased)
* Fixed some RPC calls not having an `id` field. ([#92](https://github.com/OpenZeppelin/openzeppelin-test-helpers/pull/92))
* Added `singletons` configuration and renamed the `environment` option to `singletons.abstraction`. ([#96](https://github.com/OpenZeppelin/openzeppelin-test-helpers/pull/96))

## 0.5.3 (2019-10-16)
* Fixed a bug in the `chai-bn` setup. ([#85](https://github.com/OpenZeppelin/openzeppelin-test-helpers/pull/85))
Expand Down
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,35 @@ contract('ERC20', function ([sender, receiver]) {

### 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`.
This library supports both web3 and truffle contract instances. Where possible, helpers will automatically detect what you're using and work with both. For details about each helper see their documentation entries below.

While automatic detection should cover most use cases, both the environment and provider can be manually supplied:
#### provider
nventuro marked this conversation as resolved.
Show resolved Hide resolved

In a truffle environment, the web3 provider will be pulled from truffle's global web3 instance. Otherwise, it defaults to `http://localhost:8545`. You can override this behavior and configure your own via the `provider` key:

```javascript
require('@openzeppelin/test-helpers/configure')({ environment: 'web3', provider: 'http://localhost:8080' });
require('@openzeppelin/test-helpers/configure')({ provider: 'http://localhost:8080' });
````

#### singletons
nventuro marked this conversation as resolved.
Show resolved Hide resolved

const { expectEvent } = require('openzeppelin-test-helpers');
The `singletons` helper returns contract objects, which have multiple values that can be configured:
* `abstraction`: the underlying contract abstraction type, `'web3'` for `web3-eth-contract` and `'truffle'` for `@truffle/contract` instances. Defaults to `'web3'` unless a truffle environment is detected.
* `defaultGas`: how much gas to allocate when a transaction's `gas` field is not specified. Defaults to 8 million.
nventuro marked this conversation as resolved.
Show resolved Hide resolved
* `defaultSender`: the sender address to use when a transaction's `from` field is not specified. No default.

While automatic detection and defaults should cover most use cases, all values can be manually supplied:

```javascript
require('@openzeppelin/test-helpers/configure')({ singletons: { abstraction: 'web3', defaultGas: 6e6, defaultSender: '0x5a0b5...' } });
```

#### truffle migrations
#### About truffle migrations

Automatic environment detection does not work inside truffle migrations, so the helpers must be manually configured.
Automatic truffle environment detection does not work inside truffle migrations, so the helpers must be manually configured.

```javascript
require('@openzeppelin/test-helpers/configure')({ environment: 'truffle', provider: web3.currentProvider });
require('@openzeppelin/test-helpers/configure')({ provider: web3.currentProvider, singletons: { abstraction: 'truffle' } });
```

## Reference
Expand Down
14 changes: 11 additions & 3 deletions configure.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const { setWeb3Provider } = require('./src/config/web3');
const { setEnvironment } = require('./src/config/environment');
const { setSingletonsConfig } = require('./src/config/singletons');

const { deprecate } = require('util');

const setEnvironment = deprecate((environment) => setSingletonsConfig({ abstraction: environment }),
'The \'environment\' configuration option is deprecated, use \'singletons.abstraction\' instead.'
);

let configLoaded = false;

Expand All @@ -18,7 +24,7 @@ function configure (config) {

function defaultConfigure () {
setWeb3Provider.default();
setEnvironment.default();
setSingletonsConfig.default();
}

function customConfigure (config) {
Expand All @@ -29,7 +35,9 @@ function customConfigure (config) {
setWeb3Provider(config.provider);
}

if ('environment' in config) {
if ('singletons' in config) {
setSingletonsConfig(config.singletons);
} else if ('environment' in config) {
setEnvironment(config.environment);
}
}
Expand Down
33 changes: 15 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"homepage": "https://github.com/OpenZeppelin/openzeppelin-test-helpers#readme",
"dependencies": {
"@openzeppelin/contract-loader": "^0.4.0",
"@truffle/contract": "^4.0.35",
"ansi-colors": "^3.2.3",
"chai": "^4.2.0",
Expand All @@ -46,7 +47,6 @@
"web3-utils": "^1.2.1"
},
"devDependencies": {
"@openzeppelin/contract-loader": "^0.1.0",
"eslint": "^5.9.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.14.0",
Expand Down
39 changes: 0 additions & 39 deletions src/config/environment.js

This file was deleted.

31 changes: 31 additions & 0 deletions src/config/singletons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* global web3 */

let singletonsConfig;

function setSingletonsConfig (config) {
singletonsConfig = config;
}

function setDefaultSingletonsConfig () {
setSingletonsConfig({
abstraction: isTruffleEnvironment() ? 'truffle' : 'web3',
defaultGas: 8e6,
defaultSender: '',
frangio marked this conversation as resolved.
Show resolved Hide resolved
});
}

function getSingletonsConfig () {
return singletonsConfig;
}

function isTruffleEnvironment () {
// Truffle environments are detected by the presence of (truffle-injected) global web3 and artifacts variables
return (typeof web3 !== 'undefined' && typeof artifacts !== 'undefined');
}

setSingletonsConfig.default = setDefaultSingletonsConfig;

module.exports = {
setSingletonsConfig,
getSingletonsConfig,
};
8 changes: 4 additions & 4 deletions src/config/web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const Web3 = require('web3');

const DEFAULT_PROVIDER_URL = 'http://localhost:8545';

const configWeb3 = new Web3();
const localWeb3 = new Web3();

function setWeb3Provider (provider) {
configWeb3.setProvider(provider);
localWeb3.setProvider(provider);
}

function setDefaultWeb3Provider () {
Expand All @@ -20,11 +20,11 @@ function setDefaultWeb3Provider () {
}

function getWeb3 () {
if (configWeb3.currentProvider === null) {
if (localWeb3.currentProvider === null) {
throw new Error('web3 provider is not configured');
}

return configWeb3;
return localWeb3;
}

setWeb3Provider.default = setDefaultWeb3Provider;
Expand Down
5 changes: 3 additions & 2 deletions src/expectEvent.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { web3, BN } = require('./setup');
const { expect } = require('chai');
const flatten = require('lodash.flatten');
const util = require('util');

const { deprecate } = require('util');

function expectEvent (receipt, eventName, eventArgs = {}) {
// truffle contract receipts have a 'logs' object, with an array of objects
Expand Down Expand Up @@ -134,7 +135,7 @@ function isTruffleContract (contract) {
return 'abi' in contract && typeof contract.abi === 'object';
}

expectEvent.inLogs = util.deprecate(inLogs, 'expectEvent.inLogs() is deprecated. Use expectEvent() instead.');
expectEvent.inLogs = deprecate(inLogs, 'expectEvent.inLogs() is deprecated. Use expectEvent() instead.');
expectEvent.inConstruction = inConstruction;
expectEvent.inTransaction = inTransaction;
module.exports = expectEvent;
25 changes: 17 additions & 8 deletions src/singletons.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ const { web3 } = require('./setup');
const ether = require('./ether');
const send = require('./send');

const { getSingletonsConfig } = require('./config/singletons');

const { setupLoader } = require('@openzeppelin/contract-loader');

const {
ERC1820_REGISTRY_ABI,
ERC1820_REGISTRY_ADDRESS,
Expand All @@ -26,20 +30,25 @@ async function ERC1820Registry (funder) {
}

async function getDeployedERC1820Registry () {
const environment = require('./config/environment').getEnviroment();
const config = getSingletonsConfig();
const loader = setupLoader({
nventuro marked this conversation as resolved.
Show resolved Hide resolved
provider: web3.currentProvider,
defaultGas: config.defaultGas,
defaultSender: config.defaultSender,
});

if (environment === 'truffle') {
const truffleContract = require('@truffle/contract');
const contractAbstraction = truffleContract({ abi: ERC1820_REGISTRY_ABI });
contractAbstraction.setProvider(web3.currentProvider);
if (config.abstraction === 'truffle') {
const registry = loader.truffle.fromABI(ERC1820_REGISTRY_ABI);
return registry.at(ERC1820_REGISTRY_ADDRESS);

return contractAbstraction.at(ERC1820_REGISTRY_ADDRESS);
} else if (config.abstraction === 'web3') {
const registry = loader.web3.fromABI(ERC1820_REGISTRY_ABI);
registry.options.address = ERC1820_REGISTRY_ADDRESS;

} else if (environment === 'web3') {
return new web3.eth.Contract(ERC1820_REGISTRY_ABI, ERC1820_REGISTRY_ADDRESS);

} else {
throw new Error(`Unknown environment: '${environment}'`);
throw new Error(`Unknown contract abstraction: '${config.abstraction}'`);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = function(deployer) {
deployer.deploy(Migrations);

try {
require('@openzeppelin/test-helpers/configure')({ provider: web3.currentProvider, environment: 'truffle' });
require('@openzeppelin/test-helpers/configure')({ provider: web3.currentProvider, singletons: { abstraction: 'truffle' } });

console.error('Successfully configured Web3 instance');
} catch (e) {
Expand Down
12 changes: 5 additions & 7 deletions test/src/expectEvent.web3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ const { expect } = require('chai');
const assertFailure = require('../helpers/assertFailure');
const expectEvent = require('../../src/expectEvent');

require('@openzeppelin/contract-loader/lib/configure').set(web3);
const { load } = require('@openzeppelin/contract-loader');
const { setupLoader } = require('@openzeppelin/contract-loader');

const EventEmitter = load('EventEmitter');
const IndirectEventEmitter = load('IndirectEventEmitter');
const web3Loader = setupLoader({ provider: web3.eth.currentProvider }).web3;

const EventEmitter = web3Loader.fromArtifacts('EventEmitter');
const IndirectEventEmitter = web3Loader.fromArtifacts('IndirectEventEmitter');

contract('expectEvent (web3 contracts) ', function ([deployer]) {
before(function () {
EventEmitter.options.from = deployer;
EventEmitter.options.gas = 2e6;

IndirectEventEmitter.options.from = deployer;
IndirectEventEmitter.options.gas = 2e6;
});

beforeEach(async function () {
Expand Down
Loading