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/src/0x.ts | 3 +- packages/0x.js/src/bignumber_config.ts | 11 ---- .../src/contract_wrappers/contract_wrapper.ts | 2 +- packages/0x.js/src/order_watcher/event_watcher.ts | 2 +- .../0x.js/src/order_watcher/expiration_watcher.ts | 2 +- .../0x.js/src/order_watcher/order_state_watcher.ts | 2 +- packages/0x.js/src/utils/class_utils.ts | 18 ------- packages/0x.js/src/utils/interval_utils.ts | 20 -------- 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 +- packages/contracts/package.json | 1 + .../contracts/test/ts/multi_sig_with_time_lock.ts | 6 ++- packages/contracts/util/balances.ts | 2 +- packages/contracts/util/bignumber_config.ts | 11 ---- packages/contracts/util/rpc.ts | 43 ---------------- packages/types/src/index.ts | 4 ++ packages/utils/src/bignumber_config.ts | 11 ++++ packages/utils/src/class_utils.ts | 18 +++++++ packages/utils/src/index.ts | 3 ++ packages/utils/src/interval_utils.ts | 20 ++++++++ 32 files changed, 90 insertions(+), 220 deletions(-) delete mode 100644 packages/0x.js/src/bignumber_config.ts delete mode 100644 packages/0x.js/src/utils/class_utils.ts delete mode 100644 packages/0x.js/src/utils/interval_utils.ts delete mode 100644 packages/0x.js/test/utils/blockchain_lifecycle.ts delete mode 100644 packages/0x.js/test/utils/rpc.ts delete mode 100644 packages/contracts/util/bignumber_config.ts delete mode 100644 packages/contracts/util/rpc.ts create mode 100644 packages/utils/src/bignumber_config.ts create mode 100644 packages/utils/src/class_utils.ts create mode 100644 packages/utils/src/interval_utils.ts diff --git a/packages/0x.js/src/0x.ts b/packages/0x.js/src/0x.ts index 935e4ac1a..e93b5e615 100644 --- a/packages/0x.js/src/0x.ts +++ b/packages/0x.js/src/0x.ts @@ -1,11 +1,11 @@ import {schemas, SchemaValidator} from '@0xproject/json-schemas'; +import {intervalUtils, bigNumberConfigs} from '@0xproject/utils'; import {Web3Wrapper} from '@0xproject/web3-wrapper'; import BigNumber from 'bignumber.js'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; import {artifacts} from './artifacts'; -import {bigNumberConfigs} from './bignumber_config'; import {EtherTokenWrapper} from './contract_wrappers/ether_token_wrapper'; import {ExchangeWrapper} from './contract_wrappers/exchange_wrapper'; import {TokenRegistryWrapper} from './contract_wrappers/token_registry_wrapper'; @@ -26,7 +26,6 @@ import { import {AbiDecoder} from './utils/abi_decoder'; import {assert} from './utils/assert'; import {constants} from './utils/constants'; -import {intervalUtils} from './utils/interval_utils'; import {OrderStateUtils} from './utils/order_state_utils'; import {signatureUtils} from './utils/signature_utils'; import {utils} from './utils/utils'; diff --git a/packages/0x.js/src/bignumber_config.ts b/packages/0x.js/src/bignumber_config.ts deleted file mode 100644 index 2d5214e6f..000000000 --- a/packages/0x.js/src/bignumber_config.ts +++ /dev/null @@ -1,11 +0,0 @@ -import BigNumber from 'bignumber.js'; - -export const bigNumberConfigs = { - configure() { - // By default BigNumber's `toString` method converts to exponential notation if the value has - // more then 20 digits. We want to avoid this behavior, so we set EXPONENTIAL_AT to a high number - BigNumber.config({ - EXPONENTIAL_AT: 1000, - }); - }, -}; diff --git a/packages/0x.js/src/contract_wrappers/contract_wrapper.ts b/packages/0x.js/src/contract_wrappers/contract_wrapper.ts index 5caf06db2..a796dc1ec 100644 --- a/packages/0x.js/src/contract_wrappers/contract_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/contract_wrapper.ts @@ -1,3 +1,4 @@ +import {intervalUtils} from '@0xproject/utils'; import {Web3Wrapper} from '@0xproject/web3-wrapper'; import {Block, BlockAndLogStreamer} from 'ethereumjs-blockstream'; import * as _ from 'lodash'; @@ -19,7 +20,6 @@ import { import {AbiDecoder} from '../utils/abi_decoder'; import {constants} from '../utils/constants'; import {filterUtils} from '../utils/filter_utils'; -import {intervalUtils} from '../utils/interval_utils'; const CONTRACT_NAME_TO_NOT_FOUND_ERROR: {[contractName: string]: ZeroExError} = { ZRX: ZeroExError.ZRXContractDoesNotExist, diff --git a/packages/0x.js/src/order_watcher/event_watcher.ts b/packages/0x.js/src/order_watcher/event_watcher.ts index d5b30d567..c11079208 100644 --- a/packages/0x.js/src/order_watcher/event_watcher.ts +++ b/packages/0x.js/src/order_watcher/event_watcher.ts @@ -1,3 +1,4 @@ +import {intervalUtils} from '@0xproject/utils'; import {Web3Wrapper} from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; import * as Web3 from 'web3'; @@ -10,7 +11,6 @@ import { } from '../types'; import {AbiDecoder} from '../utils/abi_decoder'; import {assert} from '../utils/assert'; -import {intervalUtils} from '../utils/interval_utils'; import {utils} from '../utils/utils'; const DEFAULT_EVENT_POLLING_INTERVAL_MS = 200; diff --git a/packages/0x.js/src/order_watcher/expiration_watcher.ts b/packages/0x.js/src/order_watcher/expiration_watcher.ts index 9a1bb1ca4..5faac43cf 100644 --- a/packages/0x.js/src/order_watcher/expiration_watcher.ts +++ b/packages/0x.js/src/order_watcher/expiration_watcher.ts @@ -1,10 +1,10 @@ +import {intervalUtils} from '@0xproject/utils'; import {BigNumber} from 'bignumber.js'; import {RBTree} from 'bintrees'; import * as _ from 'lodash'; import {ZeroEx} from '../0x'; import {SignedOrder, ZeroExError} from '../types'; -import {intervalUtils} from '../utils/interval_utils'; import {utils} from '../utils/utils'; const DEFAULT_EXPIRATION_MARGIN_MS = 0; diff --git a/packages/0x.js/src/order_watcher/order_state_watcher.ts b/packages/0x.js/src/order_watcher/order_state_watcher.ts index 08f52d6e1..9ae1579c6 100644 --- a/packages/0x.js/src/order_watcher/order_state_watcher.ts +++ b/packages/0x.js/src/order_watcher/order_state_watcher.ts @@ -1,4 +1,5 @@ import {schemas} from '@0xproject/json-schemas'; +import {intervalUtils} from '@0xproject/utils'; import {Web3Wrapper} from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; @@ -29,7 +30,6 @@ import { } from '../types'; import {AbiDecoder} from '../utils/abi_decoder'; import {assert} from '../utils/assert'; -import {intervalUtils} from '../utils/interval_utils'; import {OrderStateUtils} from '../utils/order_state_utils'; import {utils} from '../utils/utils'; diff --git a/packages/0x.js/src/utils/class_utils.ts b/packages/0x.js/src/utils/class_utils.ts deleted file mode 100644 index 04e60ee57..000000000 --- a/packages/0x.js/src/utils/class_utils.ts +++ /dev/null @@ -1,18 +0,0 @@ -import * as _ from 'lodash'; - -export const classUtils = { - // This is useful for classes that have nested methods. Nested methods don't get bound out of the box. - bindAll(self: any, exclude: string[] = ['contructor'], thisArg?: any): void { - for (const key of Object.getOwnPropertyNames(self)) { - const val = self[key]; - if (!_.includes(exclude, key)) { - if (_.isFunction(val)) { - self[key] = val.bind(thisArg || self); - } else if (_.isObject(val)) { - classUtils.bindAll(val, exclude, self); - } - } - } - return self; - }, -}; diff --git a/packages/0x.js/src/utils/interval_utils.ts b/packages/0x.js/src/utils/interval_utils.ts deleted file mode 100644 index 62b79f2f5..000000000 --- a/packages/0x.js/src/utils/interval_utils.ts +++ /dev/null @@ -1,20 +0,0 @@ -import * as _ from 'lodash'; - -export const intervalUtils = { - setAsyncExcludingInterval(fn: () => Promise, intervalMs: number) { - let locked = false; - const intervalId = setInterval(async () => { - if (locked) { - return; - } else { - locked = true; - await fn(); - locked = false; - } - }, intervalMs); - return intervalId; - }, - clearAsyncExcludingInterval(intervalId: NodeJS.Timer): void { - clearInterval(intervalId); - }, -}; 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; diff --git a/packages/contracts/package.json b/packages/contracts/package.json index 93f28f990..c8ccd0a3a 100644 --- a/packages/contracts/package.json +++ b/packages/contracts/package.json @@ -29,6 +29,7 @@ }, "homepage": "https://github.com/0xProject/0x.js/packages/contracts/README.md", "devDependencies": { + "@0xproject/dev-utils": "^0.0.1", "@0xproject/tslint-config": "^0.2.1", "@0xproject/types": "^0.1.0", "@types/bluebird": "^3.5.3", diff --git a/packages/contracts/test/ts/multi_sig_with_time_lock.ts b/packages/contracts/test/ts/multi_sig_with_time_lock.ts index 84eb21b4a..d92c5967f 100644 --- a/packages/contracts/test/ts/multi_sig_with_time_lock.ts +++ b/packages/contracts/test/ts/multi_sig_with_time_lock.ts @@ -1,13 +1,14 @@ +import {RPC} from '@0xproject/dev-utils'; import {promisify} from '@0xproject/utils'; import {BigNumber} from 'bignumber.js'; import * as chai from 'chai'; import Web3 = require('web3'); import * as multiSigWalletJSON from '../../build/contracts/MultiSigWalletWithTimeLock.json'; +import * as truffleConf from '../../truffle.js'; import {Artifacts} from '../../util/artifacts'; import {constants} from '../../util/constants'; import {MultiSigWrapper} from '../../util/multi_sig_wrapper'; -import {RPC} from '../../util/rpc'; import {ContractInstance} from '../../util/types'; import {chaiSetup} from './utils/chai_setup'; @@ -38,7 +39,8 @@ contract('MultiSigWalletWithTimeLock', (accounts: string[]) => { const secondsTimeLocked = await multiSig.secondsTimeLocked.call(); initialSecondsTimeLocked = secondsTimeLocked.toNumber(); - rpc = new RPC(); + const rpcUrl = `http://${truffleConf.networks.development.host}:${truffleConf.networks.development.port}`; + rpc = new RPC(rpcUrl); }); describe('changeTimeLock', () => { diff --git a/packages/contracts/util/balances.ts b/packages/contracts/util/balances.ts index fce15db6d..7f5e843a5 100644 --- a/packages/contracts/util/balances.ts +++ b/packages/contracts/util/balances.ts @@ -1,7 +1,7 @@ +import {bigNumberConfigs} from '@0xproject/utils'; import {BigNumber} from 'bignumber.js'; import * as _ from 'lodash'; -import {bigNumberConfigs} from './bignumber_config'; import {BalancesByOwner, ContractInstance} from './types'; bigNumberConfigs.configure(); diff --git a/packages/contracts/util/bignumber_config.ts b/packages/contracts/util/bignumber_config.ts deleted file mode 100644 index 38f59d341..000000000 --- a/packages/contracts/util/bignumber_config.ts +++ /dev/null @@ -1,11 +0,0 @@ -import {BigNumber} from 'bignumber.js'; - -export const bigNumberConfigs = { - configure() { - // By default BigNumber's `toString` method converts to exponential notation if the value has - // more then 20 digits. We want to avoid this behavior, so we set EXPONENTIAL_AT to a high number - BigNumber.config({ - EXPONENTIAL_AT: 1000, - }); - }, -}; diff --git a/packages/contracts/util/rpc.ts b/packages/contracts/util/rpc.ts deleted file mode 100644 index 023602bd6..000000000 --- a/packages/contracts/util/rpc.ts +++ /dev/null @@ -1,43 +0,0 @@ -import 'isomorphic-fetch'; - -import * as truffleConf from '../truffle.js'; - -export class RPC { - private host: string; - private port: number; - private id: number; - constructor() { - this.host = truffleConf.networks.development.host; - this.port = truffleConf.networks.development.port; - this.id = 0; - } - public async increaseTimeAsync(time: number) { - const method = 'evm_increaseTime'; - const params = [time]; - const payload = this.toPayload(method, params); - return this.sendAsync(payload); - } - public async mineBlockAsync() { - const method = 'evm_mine'; - const payload = this.toPayload(method); - return this.sendAsync(payload); - } - private toPayload(method: string, params: any[] = []) { - const payload = JSON.stringify({ - id: this.id, - method, - params, - }); - this.id++; - return payload; - } - private async sendAsync(payload: string): Promise { - const opts = { - method: 'POST', - body: payload, - }; - const response = await fetch(`http://${this.host}:${this.port}`, opts); - const responsePayload = await response.json(); - return responsePayload; - } -} diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 8d69af63d..3db1aebe1 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -8,6 +8,10 @@ export interface TxData { nonce?: number; } +export interface TxDataPayable extends TxData { + value?: BigNumber; +} + export interface TransactionReceipt { blockHash: string; blockNumber: number; diff --git a/packages/utils/src/bignumber_config.ts b/packages/utils/src/bignumber_config.ts new file mode 100644 index 000000000..2d5214e6f --- /dev/null +++ b/packages/utils/src/bignumber_config.ts @@ -0,0 +1,11 @@ +import BigNumber from 'bignumber.js'; + +export const bigNumberConfigs = { + configure() { + // By default BigNumber's `toString` method converts to exponential notation if the value has + // more then 20 digits. We want to avoid this behavior, so we set EXPONENTIAL_AT to a high number + BigNumber.config({ + EXPONENTIAL_AT: 1000, + }); + }, +}; diff --git a/packages/utils/src/class_utils.ts b/packages/utils/src/class_utils.ts new file mode 100644 index 000000000..04e60ee57 --- /dev/null +++ b/packages/utils/src/class_utils.ts @@ -0,0 +1,18 @@ +import * as _ from 'lodash'; + +export const classUtils = { + // This is useful for classes that have nested methods. Nested methods don't get bound out of the box. + bindAll(self: any, exclude: string[] = ['contructor'], thisArg?: any): void { + for (const key of Object.getOwnPropertyNames(self)) { + const val = self[key]; + if (!_.includes(exclude, key)) { + if (_.isFunction(val)) { + self[key] = val.bind(thisArg || self); + } else if (_.isObject(val)) { + classUtils.bindAll(val, exclude, self); + } + } + } + return self; + }, +}; diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index edfd192b2..403bc7236 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1,2 +1,5 @@ export {promisify} from './promisify'; export {addressUtils} from './address_utils'; +export {classUtils} from './class_utils'; +export {intervalUtils} from './interval_utils'; +export {bigNumberConfigs} from './bignumber_config'; diff --git a/packages/utils/src/interval_utils.ts b/packages/utils/src/interval_utils.ts new file mode 100644 index 000000000..62b79f2f5 --- /dev/null +++ b/packages/utils/src/interval_utils.ts @@ -0,0 +1,20 @@ +import * as _ from 'lodash'; + +export const intervalUtils = { + setAsyncExcludingInterval(fn: () => Promise, intervalMs: number) { + let locked = false; + const intervalId = setInterval(async () => { + if (locked) { + return; + } else { + locked = true; + await fn(); + locked = false; + } + }, intervalMs); + return intervalId; + }, + clearAsyncExcludingInterval(intervalId: NodeJS.Timer): void { + clearInterval(intervalId); + }, +}; -- cgit v1.2.3