diff options
Diffstat (limited to 'packages/dev-utils/src')
-rw-r--r-- | packages/dev-utils/src/blockchain_lifecycle.ts | 98 | ||||
-rw-r--r-- | packages/dev-utils/src/callback_error_reporter.ts | 77 | ||||
-rw-r--r-- | packages/dev-utils/src/constants.ts | 6 | ||||
-rw-r--r-- | packages/dev-utils/src/env.ts | 26 | ||||
-rw-r--r-- | packages/dev-utils/src/globals.d.ts | 6 | ||||
-rw-r--r-- | packages/dev-utils/src/index.ts | 5 | ||||
-rw-r--r-- | packages/dev-utils/src/web3_factory.ts | 71 |
7 files changed, 0 insertions, 289 deletions
diff --git a/packages/dev-utils/src/blockchain_lifecycle.ts b/packages/dev-utils/src/blockchain_lifecycle.ts deleted file mode 100644 index e9687787f..000000000 --- a/packages/dev-utils/src/blockchain_lifecycle.ts +++ /dev/null @@ -1,98 +0,0 @@ -import { logUtils } from '@0x/utils'; -import { NodeType, Web3Wrapper } from '@0x/web3-wrapper'; -import * as _ from 'lodash'; - -// HACK(albrow): 🐉 We have to do this so that debug.setHead works correctly. -// (Geth does not seem to like debug.setHead(0), so by sending some transactions -// we increase the current block number beyond 0). Additionally, some tests seem -// to break when there are fewer than 3 blocks in the chain. (We have no idea -// why, but it was consistently reproducible). -const MINIMUM_BLOCKS = 3; - -export class BlockchainLifecycle { - private readonly _web3Wrapper: Web3Wrapper; - private readonly _snapshotIdsStack: number[]; - private _addresses: string[] = []; - private _nodeType: NodeType | undefined; - constructor(web3Wrapper: Web3Wrapper) { - this._web3Wrapper = web3Wrapper; - this._snapshotIdsStack = []; - } - public async startAsync(): Promise<void> { - const nodeType = await this._getNodeTypeAsync(); - switch (nodeType) { - case NodeType.Ganache: - const snapshotId = await this._web3Wrapper.takeSnapshotAsync(); - this._snapshotIdsStack.push(snapshotId); - break; - case NodeType.Geth: - let blockNumber = await this._web3Wrapper.getBlockNumberAsync(); - if (blockNumber < MINIMUM_BLOCKS) { - // If the minimum block number is not met, force Geth to - // mine some blocks by sending some dummy transactions. - await this._mineMinimumBlocksAsync(); - blockNumber = await this._web3Wrapper.getBlockNumberAsync(); - } - this._snapshotIdsStack.push(blockNumber); - // HACK(albrow) It's possible that we applied a time offset but - // the transaction we mined to put that time offset into the - // blockchain was reverted. As a workaround, we mine a new dummy - // block so that the latest block timestamp accounts for any - // possible time offsets. - await this._mineDummyBlockAsync(); - break; - default: - throw new Error(`Unknown node type: ${nodeType}`); - } - } - public async revertAsync(): Promise<void> { - 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 _mineMinimumBlocksAsync(): Promise<void> { - logUtils.warn('WARNING: minimum block number for tests not met. Mining additional blocks...'); - while ((await this._web3Wrapper.getBlockNumberAsync()) < MINIMUM_BLOCKS) { - logUtils.warn('Mining block...'); - await this._mineDummyBlockAsync(); - } - logUtils.warn('Done mining the minimum number of blocks.'); - } - private async _getNodeTypeAsync(): Promise<NodeType> { - if (_.isUndefined(this._nodeType)) { - this._nodeType = await this._web3Wrapper.getNodeTypeAsync(); - } - return this._nodeType; - } - // Sends a transaction that has no real effect on the state and waits for it - // to be mined. - private async _mineDummyBlockAsync(): Promise<void> { - if (this._addresses.length === 0) { - this._addresses = await this._web3Wrapper.getAvailableAddressesAsync(); - if (this._addresses.length === 0) { - throw new Error('No accounts found'); - } - } - await this._web3Wrapper.awaitTransactionMinedAsync( - await this._web3Wrapper.sendTransactionAsync({ - from: this._addresses[0], - to: this._addresses[0], - value: '0', - }), - 0, - ); - } -} diff --git a/packages/dev-utils/src/callback_error_reporter.ts b/packages/dev-utils/src/callback_error_reporter.ts deleted file mode 100644 index b993bf22a..000000000 --- a/packages/dev-utils/src/callback_error_reporter.ts +++ /dev/null @@ -1,77 +0,0 @@ -import * as chai from 'chai'; -import * as _ from 'lodash'; - -import { DoneCallback } from '@0x/types'; - -const expect = chai.expect; - -export const callbackErrorReporter = { - reportNoErrorCallbackErrors( - done: DoneCallback, - expectToBeCalledOnce: boolean = true, - ): <T>(f?: ((value: T) => void) | undefined) => (value: T) => void { - const callback = <T>(f?: (value: T) => void) => { - const wrapped = (value: T) => { - if (_.isUndefined(f)) { - done(); - return; - } - try { - f(value); - if (expectToBeCalledOnce) { - done(); - } - } catch (err) { - done(err); - } - }; - return wrapped; - }; - return callback; - }, - reportNodeCallbackErrors( - done: DoneCallback, - expectToBeCalledOnce: boolean = true, - ): <T>(f?: ((value: T) => void) | undefined) => (error: Error | null, value: T | undefined) => void { - const callback = <T>(f?: (value: T) => void) => { - const wrapped = (error: Error | null, value: T | undefined) => { - if (!_.isNull(error)) { - done(error); - } else { - if (_.isUndefined(f)) { - done(); - return; - } - try { - f(value as T); - if (expectToBeCalledOnce) { - done(); - } - } catch (err) { - done(err); - } - } - }; - return wrapped; - }; - return callback; - }, - assertNodeCallbackError( - done: DoneCallback, - errMsg: string, - ): <T>(error: Error | null, value: T | undefined) => void { - const wrapped = <T>(error: Error | null, _value: T | undefined) => { - if (_.isNull(error)) { - done(new Error('Expected callback to receive an error')); - } else { - try { - expect(error.message).to.be.equal(errMsg); - done(); - } catch (err) { - done(err); - } - } - }; - return wrapped; - }, -}; diff --git a/packages/dev-utils/src/constants.ts b/packages/dev-utils/src/constants.ts deleted file mode 100644 index 56e87d764..000000000 --- a/packages/dev-utils/src/constants.ts +++ /dev/null @@ -1,6 +0,0 @@ -export const constants = { - RPC_URL: 'http://localhost:8545', - RPC_PORT: 8545, - GAS_LIMIT: 7000000, - TESTRPC_FIRST_ADDRESS: '0x5409ed021d9299bf6814279a6a1411a7e866a631', -}; diff --git a/packages/dev-utils/src/env.ts b/packages/dev-utils/src/env.ts deleted file mode 100644 index 024162c2f..000000000 --- a/packages/dev-utils/src/env.ts +++ /dev/null @@ -1,26 +0,0 @@ -import * as _ from 'lodash'; -import * as process from 'process'; - -export enum EnvVars { - SolidityCoverage = 'SOLIDITY_COVERAGE', - SolidityProfiler = 'SOLIDITY_PROFILER', - SolidityRevertTrace = 'SOLIDITY_REVERT_TRACE', - VerboseGanache = 'VERBOSE_GANACHE', -} - -export const env = { - parseBoolean(key: string): boolean { - let isTrue: boolean; - const envVarValue = process.env[key]; - if (envVarValue === 'true') { - isTrue = true; - } else if (envVarValue === 'false' || _.isUndefined(envVarValue)) { - isTrue = false; - } else { - throw new Error( - `Failed to parse ENV variable ${key} as boolean. Please make sure it's either true or false. Defaults to false`, - ); - } - return isTrue; - }, -}; diff --git a/packages/dev-utils/src/globals.d.ts b/packages/dev-utils/src/globals.d.ts deleted file mode 100644 index 94e63a32d..000000000 --- a/packages/dev-utils/src/globals.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -declare module '*.json' { - const json: any; - /* tslint:disable */ - export default json; - /* tslint:enable */ -} diff --git a/packages/dev-utils/src/index.ts b/packages/dev-utils/src/index.ts deleted file mode 100644 index d4c19f1bf..000000000 --- a/packages/dev-utils/src/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -export { BlockchainLifecycle } from './blockchain_lifecycle'; -export { web3Factory } from './web3_factory'; -export { constants as devConstants } from './constants'; -export { env, EnvVars } from './env'; -export { callbackErrorReporter } from './callback_error_reporter'; diff --git a/packages/dev-utils/src/web3_factory.ts b/packages/dev-utils/src/web3_factory.ts deleted file mode 100644 index 5f8981a46..000000000 --- a/packages/dev-utils/src/web3_factory.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { - EmptyWalletSubprovider, - FakeGasEstimateSubprovider, - GanacheSubprovider, - RPCSubprovider, - Web3ProviderEngine, -} from '@0x/subproviders'; -import * as fs from 'fs'; -import * as _ from 'lodash'; - -import { constants } from './constants'; -import { env, EnvVars } from './env'; - -export interface Web3Config { - hasAddresses?: boolean; // default: true - shouldUseInProcessGanache?: boolean; // default: false - shouldThrowErrorsOnGanacheRPCResponse?: boolean; // default: true - rpcUrl?: string; // default: localhost:8545 - shouldUseFakeGasEstimate?: boolean; // default: true - ganacheDatabasePath?: string; // default: undefined, creates a tmp dir -} - -export const web3Factory = { - getRpcProvider(config: Web3Config = {}): Web3ProviderEngine { - const provider = new Web3ProviderEngine(); - const hasAddresses = _.isUndefined(config.hasAddresses) || config.hasAddresses; - config.shouldUseFakeGasEstimate = - _.isUndefined(config.shouldUseFakeGasEstimate) || config.shouldUseFakeGasEstimate; - if (!hasAddresses) { - provider.addProvider(new EmptyWalletSubprovider()); - } - - if (config.shouldUseFakeGasEstimate) { - provider.addProvider(new FakeGasEstimateSubprovider(constants.GAS_LIMIT)); - } - const logger = { - log: (arg: any) => { - fs.appendFileSync('ganache.log', `${arg}\n`); - }, - }; - const shouldUseInProcessGanache = !!config.shouldUseInProcessGanache; - if (shouldUseInProcessGanache) { - if (!_.isUndefined(config.rpcUrl)) { - throw new Error('Cannot use both GanacheSubrovider and RPCSubprovider'); - } - const shouldThrowErrorsOnGanacheRPCResponse = - _.isUndefined(config.shouldThrowErrorsOnGanacheRPCResponse) || - config.shouldThrowErrorsOnGanacheRPCResponse; - if (!_.isUndefined(config.ganacheDatabasePath)) { - // Saving the snapshot to a local db. Ganache requires this directory to exist - fs.mkdirSync(config.ganacheDatabasePath); - } - provider.addProvider( - new GanacheSubprovider({ - vmErrorsOnRPCResponse: shouldThrowErrorsOnGanacheRPCResponse, - db_path: config.ganacheDatabasePath, - gasLimit: constants.GAS_LIMIT, - logger, - verbose: env.parseBoolean(EnvVars.VerboseGanache), - port: 8545, - network_id: 50, - mnemonic: 'concert load couple harbor equip island argue ramp clarify fence smart topic', - } as any), // TODO remove any once types are merged in DefinitelyTyped - ); - } else { - provider.addProvider(new RPCSubprovider(config.rpcUrl || constants.RPC_URL)); - } - provider.start(); - return provider; - }, -}; |