From cb596c1413938ce23901135f8235bb813cc6e784 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 8 Dec 2017 18:01:40 +0300 Subject: Move more shared utils into utils package and reuse them --- packages/0x.js/test/0x.js_test.ts | 4 +- packages/0x.js/test/ether_token_wrapper_test.ts | 4 +- .../0x.js/test/exchange_transfer_simulator_test.ts | 4 +- packages/0x.js/test/exchange_wrapper_test.ts | 4 +- packages/0x.js/test/expiration_watcher_test.ts | 5 +- packages/0x.js/test/order_state_watcher_test.ts | 4 +- packages/0x.js/test/order_validation_test.ts | 4 +- packages/0x.js/test/subscription_test.ts | 4 +- packages/0x.js/test/token_registry_wrapper_test.ts | 4 +- packages/0x.js/test/token_wrapper_test.ts | 4 +- packages/0x.js/test/utils/blockchain_lifecycle.ts | 26 ---------- packages/0x.js/test/utils/constants.ts | 3 +- packages/0x.js/test/utils/rpc.ts | 58 ---------------------- packages/0x.js/test/utils/web3_factory.ts | 3 +- 14 files changed, 23 insertions(+), 108 deletions(-) delete mode 100644 packages/0x.js/test/utils/blockchain_lifecycle.ts delete mode 100644 packages/0x.js/test/utils/rpc.ts (limited to 'packages/0x.js/test') diff --git a/packages/0x.js/test/0x.js_test.ts b/packages/0x.js/test/0x.js_test.ts index 6b7a70699..52f9566dc 100644 --- a/packages/0x.js/test/0x.js_test.ts +++ b/packages/0x.js/test/0x.js_test.ts @@ -1,3 +1,4 @@ +import {BlockchainLifecycle} from '@0xproject/dev-utils'; import BigNumber from 'bignumber.js'; import * as chai from 'chai'; import * as _ from 'lodash'; @@ -6,13 +7,12 @@ import * as Sinon from 'sinon'; import {ApprovalContractEventArgs, LogWithDecodedArgs, Order, TokenEvents, ZeroEx, ZeroExError} from '../src'; -import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import {chaiSetup} from './utils/chai_setup'; import {constants} from './utils/constants'; import {TokenUtils} from './utils/token_utils'; import {web3Factory} from './utils/web3_factory'; -const blockchainLifecycle = new BlockchainLifecycle(); +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); chaiSetup.configure(); const expect = chai.expect; diff --git a/packages/0x.js/test/ether_token_wrapper_test.ts b/packages/0x.js/test/ether_token_wrapper_test.ts index d3e4439ee..e0d738f84 100644 --- a/packages/0x.js/test/ether_token_wrapper_test.ts +++ b/packages/0x.js/test/ether_token_wrapper_test.ts @@ -1,3 +1,4 @@ +import {BlockchainLifecycle} from '@0xproject/dev-utils'; import BigNumber from 'bignumber.js'; import * as chai from 'chai'; import 'mocha'; @@ -5,14 +6,13 @@ import * as Web3 from 'web3'; import {ZeroEx, ZeroExError} from '../src'; -import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import {chaiSetup} from './utils/chai_setup'; import {constants} from './utils/constants'; import {web3Factory} from './utils/web3_factory'; chaiSetup.configure(); const expect = chai.expect; -const blockchainLifecycle = new BlockchainLifecycle(); +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); // Since the address depositing/withdrawing ETH/WETH also needs to pay gas costs for the transaction, // a small amount of ETH will be used to pay this gas cost. We therefore check that the difference between diff --git a/packages/0x.js/test/exchange_transfer_simulator_test.ts b/packages/0x.js/test/exchange_transfer_simulator_test.ts index a1d9bdade..dbd1e99bd 100644 --- a/packages/0x.js/test/exchange_transfer_simulator_test.ts +++ b/packages/0x.js/test/exchange_transfer_simulator_test.ts @@ -1,3 +1,4 @@ +import {BlockchainLifecycle} from '@0xproject/dev-utils'; import BigNumber from 'bignumber.js'; import * as chai from 'chai'; @@ -5,14 +6,13 @@ import {ExchangeContractErrs, Token, ZeroEx} from '../src'; import {BlockParamLiteral, TradeSide, TransferType} from '../src/types'; import {ExchangeTransferSimulator} from '../src/utils/exchange_transfer_simulator'; -import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import {chaiSetup} from './utils/chai_setup'; import {constants} from './utils/constants'; import {web3Factory} from './utils/web3_factory'; chaiSetup.configure(); const expect = chai.expect; -const blockchainLifecycle = new BlockchainLifecycle(); +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); describe('ExchangeTransferSimulator', () => { const web3 = web3Factory.create(); diff --git a/packages/0x.js/test/exchange_wrapper_test.ts b/packages/0x.js/test/exchange_wrapper_test.ts index 14559c706..b7628e6d2 100644 --- a/packages/0x.js/test/exchange_wrapper_test.ts +++ b/packages/0x.js/test/exchange_wrapper_test.ts @@ -1,3 +1,4 @@ +import {BlockchainLifecycle} from '@0xproject/dev-utils'; import BigNumber from 'bignumber.js'; import * as chai from 'chai'; import 'mocha'; @@ -19,7 +20,6 @@ import { } from '../src'; import {BlockParamLiteral, DoneCallback} from '../src/types'; -import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import {chaiSetup} from './utils/chai_setup'; import {constants} from './utils/constants'; import {FillScenarios} from './utils/fill_scenarios'; @@ -28,7 +28,7 @@ import {web3Factory} from './utils/web3_factory'; chaiSetup.configure(); const expect = chai.expect; -const blockchainLifecycle = new BlockchainLifecycle(); +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); const NON_EXISTENT_ORDER_HASH = '0x79370342234e7acd6bbeac335bd3bb1d368383294b64b8160a00f4060e4d3777'; diff --git a/packages/0x.js/test/expiration_watcher_test.ts b/packages/0x.js/test/expiration_watcher_test.ts index d4581259d..e72653b67 100644 --- a/packages/0x.js/test/expiration_watcher_test.ts +++ b/packages/0x.js/test/expiration_watcher_test.ts @@ -1,3 +1,4 @@ +import {BlockchainLifecycle} from '@0xproject/dev-utils'; import {Web3Wrapper} from '@0xproject/web3-wrapper'; import BigNumber from 'bignumber.js'; import * as chai from 'chai'; @@ -12,8 +13,8 @@ import {DoneCallback, Token} from '../src/types'; import {constants} from '../src/utils/constants'; import {utils} from '../src/utils/utils'; -import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import {chaiSetup} from './utils/chai_setup'; +import {constants as testConstants} from './utils/constants'; import {FillScenarios} from './utils/fill_scenarios'; import {reportCallbackErrors} from './utils/report_callback_errors'; import {TokenUtils} from './utils/token_utils'; @@ -21,7 +22,7 @@ import {web3Factory} from './utils/web3_factory'; chaiSetup.configure(); const expect = chai.expect; -const blockchainLifecycle = new BlockchainLifecycle(); +const blockchainLifecycle = new BlockchainLifecycle(testConstants.RPC_URL); describe('ExpirationWatcher', () => { let web3: Web3; diff --git a/packages/0x.js/test/order_state_watcher_test.ts b/packages/0x.js/test/order_state_watcher_test.ts index b5968dc24..4086c00fd 100644 --- a/packages/0x.js/test/order_state_watcher_test.ts +++ b/packages/0x.js/test/order_state_watcher_test.ts @@ -1,3 +1,4 @@ +import {BlockchainLifecycle} from '@0xproject/dev-utils'; import {Web3Wrapper} from '@0xproject/web3-wrapper'; import BigNumber from 'bignumber.js'; import * as chai from 'chai'; @@ -21,7 +22,6 @@ import { import {OrderStateWatcher} from '../src/order_watcher/order_state_watcher'; import {DoneCallback} from '../src/types'; -import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import {chaiSetup} from './utils/chai_setup'; import {constants} from './utils/constants'; import {FillScenarios} from './utils/fill_scenarios'; @@ -33,7 +33,7 @@ const TIMEOUT_MS = 150; chaiSetup.configure(); const expect = chai.expect; -const blockchainLifecycle = new BlockchainLifecycle(); +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); describe('OrderStateWatcher', () => { let web3: Web3; diff --git a/packages/0x.js/test/order_validation_test.ts b/packages/0x.js/test/order_validation_test.ts index d585c1f3c..9c36ce0d6 100644 --- a/packages/0x.js/test/order_validation_test.ts +++ b/packages/0x.js/test/order_validation_test.ts @@ -1,3 +1,4 @@ +import {BlockchainLifecycle} from '@0xproject/dev-utils'; import BigNumber from 'bignumber.js'; import * as chai from 'chai'; import * as Sinon from 'sinon'; @@ -8,7 +9,6 @@ import {BlockParamLiteral, TradeSide, TransferType} from '../src/types'; import {ExchangeTransferSimulator} from '../src/utils/exchange_transfer_simulator'; import {OrderValidationUtils} from '../src/utils/order_validation_utils'; -import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import {chaiSetup} from './utils/chai_setup'; import {constants} from './utils/constants'; import {FillScenarios} from './utils/fill_scenarios'; @@ -17,7 +17,7 @@ import {web3Factory} from './utils/web3_factory'; chaiSetup.configure(); const expect = chai.expect; -const blockchainLifecycle = new BlockchainLifecycle(); +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); describe('OrderValidation', () => { let web3: Web3; diff --git a/packages/0x.js/test/subscription_test.ts b/packages/0x.js/test/subscription_test.ts index 3aeeaa109..d280d720a 100644 --- a/packages/0x.js/test/subscription_test.ts +++ b/packages/0x.js/test/subscription_test.ts @@ -1,3 +1,4 @@ +import {BlockchainLifecycle} from '@0xproject/dev-utils'; import BigNumber from 'bignumber.js'; import * as chai from 'chai'; import * as _ from 'lodash'; @@ -15,7 +16,6 @@ import { } from '../src'; import {BlockParamLiteral, DoneCallback} from '../src/types'; -import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import {chaiSetup} from './utils/chai_setup'; import {constants} from './utils/constants'; import {reportCallbackErrors} from './utils/report_callback_errors'; @@ -24,7 +24,7 @@ import {web3Factory} from './utils/web3_factory'; chaiSetup.configure(); const expect = chai.expect; -const blockchainLifecycle = new BlockchainLifecycle(); +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); describe('SubscriptionTest', () => { let web3: Web3; diff --git a/packages/0x.js/test/token_registry_wrapper_test.ts b/packages/0x.js/test/token_registry_wrapper_test.ts index f1f307f3a..aaf4fa448 100644 --- a/packages/0x.js/test/token_registry_wrapper_test.ts +++ b/packages/0x.js/test/token_registry_wrapper_test.ts @@ -1,18 +1,18 @@ import {schemas, SchemaValidator} from '@0xproject/json-schemas'; +import {BlockchainLifecycle} from '@0xproject/dev-utils'; import * as chai from 'chai'; import * as _ from 'lodash'; import 'mocha'; import {Token, ZeroEx} from '../src'; -import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import {chaiSetup} from './utils/chai_setup'; import {constants} from './utils/constants'; import {web3Factory} from './utils/web3_factory'; chaiSetup.configure(); const expect = chai.expect; -const blockchainLifecycle = new BlockchainLifecycle(); +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); const TOKEN_REGISTRY_SIZE_AFTER_MIGRATION = 7; diff --git a/packages/0x.js/test/token_wrapper_test.ts b/packages/0x.js/test/token_wrapper_test.ts index ae6016869..2500168f9 100644 --- a/packages/0x.js/test/token_wrapper_test.ts +++ b/packages/0x.js/test/token_wrapper_test.ts @@ -1,4 +1,5 @@ import {promisify} from '@0xproject/utils'; +import {BlockchainLifecycle} from '@0xproject/dev-utils'; import {Web3Wrapper} from '@0xproject/web3-wrapper'; import BigNumber from 'bignumber.js'; import * as chai from 'chai'; @@ -21,7 +22,6 @@ import { } from '../src'; import {BlockParamLiteral, DoneCallback} from '../src/types'; -import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import {chaiSetup} from './utils/chai_setup'; import {constants} from './utils/constants'; import {TokenUtils} from './utils/token_utils'; @@ -29,7 +29,7 @@ import {web3Factory} from './utils/web3_factory'; chaiSetup.configure(); const expect = chai.expect; -const blockchainLifecycle = new BlockchainLifecycle(); +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); describe('TokenWrapper', () => { let web3: Web3; diff --git a/packages/0x.js/test/utils/blockchain_lifecycle.ts b/packages/0x.js/test/utils/blockchain_lifecycle.ts deleted file mode 100644 index 9a44ccd6f..000000000 --- a/packages/0x.js/test/utils/blockchain_lifecycle.ts +++ /dev/null @@ -1,26 +0,0 @@ -import {RPC} from './rpc'; - -export class BlockchainLifecycle { - private rpc: RPC; - private snapshotIdsStack: number[]; - constructor() { - this.rpc = new RPC(); - 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 { - const snapshotId = await this.rpc.takeSnapshotAsync(); - this.snapshotIdsStack.push(snapshotId); - } - public async revertAsync(): Promise { - const snapshotId = this.snapshotIdsStack.pop() as number; - const didRevert = await this.rpc.revertSnapshotAsync(snapshotId); - if (!didRevert) { - throw new Error(`Snapshot with id #${snapshotId} failed to revert`); - } - } - public async mineABlock(): Promise { - await this.rpc.mineBlockAsync(); - } -} diff --git a/packages/0x.js/test/utils/constants.ts b/packages/0x.js/test/utils/constants.ts index 75fdf49c9..d486da581 100644 --- a/packages/0x.js/test/utils/constants.ts +++ b/packages/0x.js/test/utils/constants.ts @@ -1,7 +1,6 @@ export const constants = { NULL_ADDRESS: '0x0000000000000000000000000000000000000000', - RPC_HOST: 'localhost', - RPC_PORT: 8545, + RPC_URL: 'http://localhost:8545', ROPSTEN_NETWORK_ID: 3, KOVAN_NETWORK_ID: 42, TESTRPC_NETWORK_ID: 50, diff --git a/packages/0x.js/test/utils/rpc.ts b/packages/0x.js/test/utils/rpc.ts deleted file mode 100644 index 309a96d5e..000000000 --- a/packages/0x.js/test/utils/rpc.ts +++ /dev/null @@ -1,58 +0,0 @@ -import * as ethUtil from 'ethereumjs-util'; -import * as request from 'request-promise-native'; - -import {constants} from './constants'; - -export class RPC { - private host: string; - private port: number; - private id: number; - constructor() { - this.host = constants.RPC_HOST; - this.port = constants.RPC_PORT; - this.id = 0; - } - public async takeSnapshotAsync(): Promise { - const method = 'evm_snapshot'; - const params: any[] = []; - const payload = this.toPayload(method, params); - const snapshotIdHex = await this.sendAsync(payload); - const snapshotId = ethUtil.bufferToInt(ethUtil.toBuffer(snapshotIdHex)); - return snapshotId; - } - public async revertSnapshotAsync(snapshotId: number): Promise { - const method = 'evm_revert'; - const params = [snapshotId]; - const payload = this.toPayload(method, params); - const didRevert = await this.sendAsync(payload); - return didRevert; - } - public async mineBlockAsync(): Promise { - const method = 'evm_mine'; - const params: any[] = []; - const payload = this.toPayload(method, params); - await this.sendAsync(payload); - } - private toPayload(method: string, params: any[] = []): string { - const payload = JSON.stringify({ - id: this.id, - method, - params, - }); - this.id += 1; - return payload; - } - private async sendAsync(payload: string): Promise { - const opts = { - method: 'POST', - uri: `http://${this.host}:${this.port}`, - body: payload, - headers: { - 'content-type': 'application/json', - }, - }; - const bodyString = await request(opts); - const body = JSON.parse(bodyString); - return body.result; - } -} diff --git a/packages/0x.js/test/utils/web3_factory.ts b/packages/0x.js/test/utils/web3_factory.ts index da4828943..6f72cec58 100644 --- a/packages/0x.js/test/utils/web3_factory.ts +++ b/packages/0x.js/test/utils/web3_factory.ts @@ -21,13 +21,12 @@ export const web3Factory = { }, getRpcProvider(hasAddresses: boolean = true): Web3.Provider { const provider = new ProviderEngine(); - const rpcUrl = `http://${constants.RPC_HOST}:${constants.RPC_PORT}`; if (!hasAddresses) { provider.addProvider(new EmptyWalletSubprovider()); } provider.addProvider(new FakeGasEstimateSubprovider(constants.GAS_ESTIMATE)); provider.addProvider(new RpcSubprovider({ - rpcUrl, + rpcUrl: constants.RPC_URL, })); provider.start(); return provider; -- cgit v1.2.3