aboutsummaryrefslogtreecommitdiffstats
path: root/packages/dev-utils
diff options
context:
space:
mode:
Diffstat (limited to 'packages/dev-utils')
-rw-r--r--packages/dev-utils/CHANGELOG.json9
-rw-r--r--packages/dev-utils/src/blockchain_lifecycle.ts55
-rw-r--r--packages/dev-utils/src/web3_factory.ts10
-rw-r--r--packages/dev-utils/test/blockchain_lifecycle_test.ts2
-rw-r--r--packages/dev-utils/test/rpc_test.ts2
5 files changed, 64 insertions, 14 deletions
diff --git a/packages/dev-utils/CHANGELOG.json b/packages/dev-utils/CHANGELOG.json
index ecb43a42e..4e1647cd8 100644
--- a/packages/dev-utils/CHANGELOG.json
+++ b/packages/dev-utils/CHANGELOG.json
@@ -1,5 +1,14 @@
[
{
+ "version": "0.4.3",
+ "changes": [
+ {
+ "note": "Add optional parameter shouldUseFakeGasEstimate to Web3Config",
+ "pr": 622
+ }
+ ]
+ },
+ {
"version": "0.4.2",
"changes": [
{
diff --git a/packages/dev-utils/src/blockchain_lifecycle.ts b/packages/dev-utils/src/blockchain_lifecycle.ts
index 3e35de861..4bb136097 100644
--- a/packages/dev-utils/src/blockchain_lifecycle.ts
+++ b/packages/dev-utils/src/blockchain_lifecycle.ts
@@ -1,5 +1,10 @@
-import { Web3Wrapper } from '@0xproject/web3-wrapper';
-import * as Web3 from 'web3';
+import { uniqueVersionIds, Web3Wrapper } from '@0xproject/web3-wrapper';
+import { includes } from 'lodash';
+
+enum NodeType {
+ Geth = 'GETH',
+ Ganache = 'GANACHE',
+}
export class BlockchainLifecycle {
private _web3Wrapper: Web3Wrapper;
@@ -8,17 +13,47 @@ export class BlockchainLifecycle {
this._web3Wrapper = web3Wrapper;
this._snapshotIdsStack = [];
}
- // TODO: In order to run these tests on an actual node, we should check if we are running against
- // TestRPC, if so, use snapshots, otherwise re-deploy contracts before every test
public async startAsync(): Promise<void> {
- const snapshotId = await this._web3Wrapper.takeSnapshotAsync();
- this._snapshotIdsStack.push(snapshotId);
+ const nodeType = await this._getNodeTypeAsync();
+ switch (nodeType) {
+ case NodeType.Ganache:
+ const snapshotId = await this._web3Wrapper.takeSnapshotAsync();
+ this._snapshotIdsStack.push(snapshotId);
+ break;
+ case NodeType.Geth:
+ const blockNumber = await this._web3Wrapper.getBlockNumberAsync();
+ this._snapshotIdsStack.push(blockNumber);
+ break;
+ default:
+ throw new Error(`Unknown node type: ${nodeType}`);
+ }
}
public async revertAsync(): Promise<void> {
- const snapshotId = this._snapshotIdsStack.pop() as number;
- const didRevert = await this._web3Wrapper.revertSnapshotAsync(snapshotId);
- if (!didRevert) {
- throw new Error(`Snapshot with id #${snapshotId} failed to revert`);
+ const nodeType = await this._getNodeTypeAsync();
+ switch (nodeType) {
+ case NodeType.Ganache:
+ const snapshotId = this._snapshotIdsStack.pop() as number;
+ const didRevert = await this._web3Wrapper.revertSnapshotAsync(snapshotId);
+ if (!didRevert) {
+ throw new Error(`Snapshot with id #${snapshotId} failed to revert`);
+ }
+ break;
+ case NodeType.Geth:
+ const blockNumber = this._snapshotIdsStack.pop() as number;
+ await this._web3Wrapper.setHeadAsync(blockNumber);
+ break;
+ default:
+ throw new Error(`Unknown node type: ${nodeType}`);
+ }
+ }
+ private async _getNodeTypeAsync(): Promise<NodeType> {
+ const version = await this._web3Wrapper.getNodeVersionAsync();
+ if (includes(version, uniqueVersionIds.geth)) {
+ return NodeType.Geth;
+ } else if (includes(version, uniqueVersionIds.ganache)) {
+ return NodeType.Ganache;
+ } else {
+ throw new Error(`Unknown client version: ${version}`);
}
}
}
diff --git a/packages/dev-utils/src/web3_factory.ts b/packages/dev-utils/src/web3_factory.ts
index 12872c122..47eef4cbd 100644
--- a/packages/dev-utils/src/web3_factory.ts
+++ b/packages/dev-utils/src/web3_factory.ts
@@ -7,10 +7,8 @@ import ProviderEngine = require('web3-provider-engine');
import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
import { EmptyWalletSubprovider, FakeGasEstimateSubprovider, GanacheSubprovider } from '@0xproject/subproviders';
-import { Provider } from 'ethereum-types';
import * as fs from 'fs';
import * as _ from 'lodash';
-import * as process from 'process';
import { constants } from './constants';
import { env, EnvVars } from './env';
@@ -19,16 +17,22 @@ export interface Web3Config {
hasAddresses?: boolean; // default: true
shouldUseInProcessGanache?: boolean; // default: false
rpcUrl?: string; // default: localhost:8545
+ shouldUseFakeGasEstimate?: boolean; // default: true
}
export const web3Factory = {
getRpcProvider(config: Web3Config = {}): ProviderEngine {
const provider = new ProviderEngine();
const hasAddresses = _.isUndefined(config.hasAddresses) || config.hasAddresses;
+ config.shouldUseFakeGasEstimate =
+ _.isUndefined(config.shouldUseFakeGasEstimate) || config.shouldUseFakeGasEstimate;
if (!hasAddresses) {
provider.addProvider(new EmptyWalletSubprovider());
}
- provider.addProvider(new FakeGasEstimateSubprovider(constants.GAS_LIMIT));
+
+ if (config.shouldUseFakeGasEstimate) {
+ provider.addProvider(new FakeGasEstimateSubprovider(constants.GAS_LIMIT));
+ }
const logger = {
log: (arg: any) => {
fs.appendFileSync('ganache.log', `${arg}\n`);
diff --git a/packages/dev-utils/test/blockchain_lifecycle_test.ts b/packages/dev-utils/test/blockchain_lifecycle_test.ts
index 3d7d2e0ef..18e245aa3 100644
--- a/packages/dev-utils/test/blockchain_lifecycle_test.ts
+++ b/packages/dev-utils/test/blockchain_lifecycle_test.ts
@@ -1,6 +1,5 @@
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as chai from 'chai';
-import { BlockParamLiteral } from 'ethereum-types';
import 'make-promises-safe';
import 'mocha';
@@ -18,6 +17,7 @@ describe('BlockchainLifecycle tests', () => {
await blockchainLifecycle.startAsync();
await web3Wrapper.mineBlockAsync();
const blockNumberAfter = await web3Wrapper.getBlockNumberAsync();
+ // tslint:disable-next-line:restrict-plus-operands
expect(blockNumberAfter).to.be.equal(blockNumberBefore + 1);
await blockchainLifecycle.revertAsync();
const blockNumberAfterRevert = await web3Wrapper.getBlockNumberAsync();
diff --git a/packages/dev-utils/test/rpc_test.ts b/packages/dev-utils/test/rpc_test.ts
index cff946e23..68842ed3a 100644
--- a/packages/dev-utils/test/rpc_test.ts
+++ b/packages/dev-utils/test/rpc_test.ts
@@ -16,6 +16,7 @@ describe('RPC tests', () => {
const blockNumberBefore = await web3Wrapper.getBlockNumberAsync();
await web3Wrapper.mineBlockAsync();
const blockNumberAfter = await web3Wrapper.getBlockNumberAsync();
+ // tslint:disable-next-line:restrict-plus-operands
expect(blockNumberAfter).to.be.equal(blockNumberBefore + 1);
});
});
@@ -26,6 +27,7 @@ describe('RPC tests', () => {
await web3Wrapper.increaseTimeAsync(TIME_DELTA);
await web3Wrapper.mineBlockAsync();
const blockTimestampAfter = await web3Wrapper.getBlockTimestampAsync(BlockParamLiteral.Latest);
+ // tslint:disable-next-line:restrict-plus-operands
expect(blockTimestampAfter).to.be.at.least(blockTimestampBefore + TIME_DELTA);
});
});