diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-07-18 21:27:38 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-07-18 21:27:38 +0800 |
commit | dad557164ea4ccc012243d43df013078a7c37eb6 (patch) | |
tree | 88e8477941dc0c98273fcb054086b2e89d034e19 /packages/order-watcher | |
parent | acff177c547dee049b97e4b051fe22e1efaf992c (diff) | |
parent | f3241ff86a0d99f4291c5a5f4eaaa5ebe1736da0 (diff) | |
download | dexon-sol-tools-dad557164ea4ccc012243d43df013078a7c37eb6.tar dexon-sol-tools-dad557164ea4ccc012243d43df013078a7c37eb6.tar.gz dexon-sol-tools-dad557164ea4ccc012243d43df013078a7c37eb6.tar.bz2 dexon-sol-tools-dad557164ea4ccc012243d43df013078a7c37eb6.tar.lz dexon-sol-tools-dad557164ea4ccc012243d43df013078a7c37eb6.tar.xz dexon-sol-tools-dad557164ea4ccc012243d43df013078a7c37eb6.tar.zst dexon-sol-tools-dad557164ea4ccc012243d43df013078a7c37eb6.zip |
Merge branch 'v2-prototype' into feature/order-watcher-v2
Diffstat (limited to 'packages/order-watcher')
-rw-r--r-- | packages/order-watcher/CHANGELOG.json | 8 | ||||
-rw-r--r-- | packages/order-watcher/package.json | 2 | ||||
-rw-r--r-- | packages/order-watcher/src/order_watcher/event_watcher.ts | 14 | ||||
-rw-r--r-- | packages/order-watcher/src/order_watcher/expiration_watcher.ts | 2 | ||||
-rw-r--r-- | packages/order-watcher/src/order_watcher/order_watcher.ts | 16 | ||||
-rw-r--r-- | packages/order-watcher/src/types.ts | 4 | ||||
-rw-r--r-- | packages/order-watcher/src/utils/assert.ts | 2 | ||||
-rw-r--r-- | packages/order-watcher/test/global_hooks.ts | 2 | ||||
-rw-r--r-- | packages/order-watcher/test/order_watcher_test.ts | 4 | ||||
-rw-r--r-- | packages/order-watcher/test/utils/token_utils.ts | 34 |
10 files changed, 58 insertions, 30 deletions
diff --git a/packages/order-watcher/CHANGELOG.json b/packages/order-watcher/CHANGELOG.json index a66db6eec..e747a2129 100644 --- a/packages/order-watcher/CHANGELOG.json +++ b/packages/order-watcher/CHANGELOG.json @@ -9,6 +9,14 @@ { "note": "Do not stop subscription if error is encountered", "pr": 825 + }, + { + "note": "Fixed a bug that caused the incorrect block to be fetched via JSON-RPC within Blockstream", + "pr": 875 + }, + { + "note": "Remove stateLayer config from OrderWatcher. It now always operates on the latest block", + "pr": 875 } ], "timestamp": 1531149657 diff --git a/packages/order-watcher/package.json b/packages/order-watcher/package.json index cda99f6b9..cafa38946 100644 --- a/packages/order-watcher/package.json +++ b/packages/order-watcher/package.json @@ -73,7 +73,7 @@ "shx": "^0.2.2", "sinon": "^4.0.0", "source-map-support": "^0.5.0", - "tslint": "5.10.0", + "tslint": "5.11.0", "typescript": "2.7.1" }, "dependencies": { diff --git a/packages/order-watcher/src/order_watcher/event_watcher.ts b/packages/order-watcher/src/order_watcher/event_watcher.ts index 68c043dfe..9509c75de 100644 --- a/packages/order-watcher/src/order_watcher/event_watcher.ts +++ b/packages/order-watcher/src/order_watcher/event_watcher.ts @@ -20,17 +20,17 @@ enum LogEventState { */ export class EventWatcher { private readonly _web3Wrapper: Web3Wrapper; - private readonly _pollingIntervalMs: number; private readonly _stateLayer: BlockParamLiteral; private readonly _isVerbose: boolean; private _blockAndLogStreamerIfExists: BlockAndLogStreamer<Block, Log> | undefined; private _blockAndLogStreamIntervalIfExists?: NodeJS.Timer; private _onLogAddedSubscriptionToken: string | undefined; private _onLogRemovedSubscriptionToken: string | undefined; + private readonly _pollingIntervalMs: number; constructor( provider: Provider, pollingIntervalIfExistsMs: undefined | number, - stateLayer: BlockParamLiteral = BlockParamLiteral.Latest, + stateLayer: BlockParamLiteral, isVerbose: boolean, ) { this._isVerbose = isVerbose; @@ -61,13 +61,9 @@ export class EventWatcher { if (!_.isUndefined(this._blockAndLogStreamerIfExists)) { throw new Error(OrderWatcherError.SubscriptionAlreadyPresent); } - const eventFilter = { - fromBlock: this._stateLayer, - toBlock: this._stateLayer, - }; this._blockAndLogStreamerIfExists = new BlockAndLogStreamer( - this._web3Wrapper.getBlockAsync.bind(this._web3Wrapper, this._stateLayer), - this._web3Wrapper.getLogsAsync.bind(this._web3Wrapper, eventFilter), + this._web3Wrapper.getBlockAsync.bind(this._web3Wrapper), + this._web3Wrapper.getLogsAsync.bind(this._web3Wrapper), this._onBlockAndLogStreamerError.bind(this), ); const catchAllLogFilter = {}; @@ -104,7 +100,7 @@ export class EventWatcher { await this._emitDifferencesAsync(log, isRemoved ? LogEventState.Removed : LogEventState.Added, callback); } private async _reconcileBlockAsync(): Promise<void> { - const latestBlock = await this._web3Wrapper.getBlockAsync(BlockParamLiteral.Latest); + const latestBlock = await this._web3Wrapper.getBlockAsync(this._stateLayer); // We need to coerce to Block type cause Web3.Block includes types for mempool blocks if (!_.isUndefined(this._blockAndLogStreamerIfExists)) { // If we clear the interval while fetching the block - this._blockAndLogStreamer will be undefined diff --git a/packages/order-watcher/src/order_watcher/expiration_watcher.ts b/packages/order-watcher/src/order_watcher/expiration_watcher.ts index c4c94a015..6eadf14c7 100644 --- a/packages/order-watcher/src/order_watcher/expiration_watcher.ts +++ b/packages/order-watcher/src/order_watcher/expiration_watcher.ts @@ -44,7 +44,7 @@ export class ExpirationWatcher { this._orderExpirationCheckingIntervalIdIfExists = intervalUtils.setInterval( this._pruneExpiredOrders.bind(this, callback), this._orderExpirationCheckingIntervalMs, - _.noop, // _pruneExpiredOrders never throws + _.noop.bind(_), // _pruneExpiredOrders never throws ); } public unsubscribe(): void { diff --git a/packages/order-watcher/src/order_watcher/order_watcher.ts b/packages/order-watcher/src/order_watcher/order_watcher.ts index af479f32d..0ad3267d8 100644 --- a/packages/order-watcher/src/order_watcher/order_watcher.ts +++ b/packages/order-watcher/src/order_watcher/order_watcher.ts @@ -58,7 +58,6 @@ interface OrderStateByOrderHash { } const DEFAULT_ORDER_WATCHER_CONFIG: OrderWatcherConfig = { - stateLayer: BlockParamLiteral.Latest, orderExpirationCheckingIntervalMs: 50, eventPollingIntervalMs: 200, expirationMarginMs: 0, @@ -66,6 +65,7 @@ const DEFAULT_ORDER_WATCHER_CONFIG: OrderWatcherConfig = { cleanupJobIntervalMs: 1000 * 60 * 60, // 1h isVerbose: true, }; +const STATE_LAYER = BlockParamLiteral.Latest; /** * This class includes all the functionality related to watching a set of orders @@ -107,24 +107,16 @@ export class OrderWatcher { [artifacts.EtherToken.abi, artifacts.Exchange.abi], ); const contractWrappers = new ContractWrappers(provider, { networkId }); - this._eventWatcher = new EventWatcher( - provider, - config.eventPollingIntervalMs, - config.stateLayer, - config.isVerbose, - ); + this._eventWatcher = new EventWatcher(provider, config.eventPollingIntervalMs, STATE_LAYER, config.isVerbose); const balanceAndProxyAllowanceFetcher = new AssetBalanceAndProxyAllowanceFetcher( contractWrappers.erc20Token, contractWrappers.erc721Token, - config.stateLayer, + STATE_LAYER, ); this._balanceAndProxyAllowanceLazyStore = new BalanceAndProxyAllowanceLazyStore( balanceAndProxyAllowanceFetcher, ); - const orderFilledCancelledFetcher = new OrderFilledCancelledFetcher( - contractWrappers.exchange, - config.stateLayer, - ); + const orderFilledCancelledFetcher = new OrderFilledCancelledFetcher(contractWrappers.exchange, STATE_LAYER); this._orderFilledCancelledLazyStore = new OrderFilledCancelledLazyStore(orderFilledCancelledFetcher); this._orderStateUtils = new OrderStateUtils(balanceAndProxyAllowanceFetcher, orderFilledCancelledFetcher); const expirationMarginIfExistsMs = _.isUndefined(config) ? undefined : config.expirationMarginMs; diff --git a/packages/order-watcher/src/types.ts b/packages/order-watcher/src/types.ts index 7991df58c..27d892985 100644 --- a/packages/order-watcher/src/types.ts +++ b/packages/order-watcher/src/types.ts @@ -1,5 +1,5 @@ import { OrderState } from '@0xproject/types'; -import { BlockParamLiteral, LogEntryEvent } from 'ethereum-types'; +import { LogEntryEvent } from 'ethereum-types'; export enum OrderWatcherError { SubscriptionAlreadyPresent = 'SUBSCRIPTION_ALREADY_PRESENT', @@ -9,7 +9,6 @@ export enum OrderWatcherError { export type EventWatcherCallback = (err: null | Error, log?: LogEntryEvent) => void; /** - * stateLayer: Optional blockchain state layer OrderWatcher will monitor for new events. Default=latest. * orderExpirationCheckingIntervalMs: How often to check for expired orders. Default=50. * eventPollingIntervalMs: How often to poll the Ethereum node for new events. Default=200. * expirationMarginMs: Amount of time before order expiry that you'd like to be notified @@ -18,7 +17,6 @@ export type EventWatcherCallback = (err: null | Error, log?: LogEntryEvent) => v * isVerbose: Weather the order watcher should be verbose. Default=true. */ export interface OrderWatcherConfig { - stateLayer: BlockParamLiteral; orderExpirationCheckingIntervalMs: number; eventPollingIntervalMs: number; expirationMarginMs: number; diff --git a/packages/order-watcher/src/utils/assert.ts b/packages/order-watcher/src/utils/assert.ts index fa22617c7..a891a60d2 100644 --- a/packages/order-watcher/src/utils/assert.ts +++ b/packages/order-watcher/src/utils/assert.ts @@ -18,6 +18,6 @@ export const assert = { signerAddress: string, ): Promise<void> { const isValid = await isValidSignatureAsync(provider, orderHash, signature, signerAddress); - this.assert(isValid, `Expected order with hash '${orderHash}' to have a valid signature`); + assert.assert(isValid, `Expected order with hash '${orderHash}' to have a valid signature`); }, }; diff --git a/packages/order-watcher/test/global_hooks.ts b/packages/order-watcher/test/global_hooks.ts index 4552e01b0..f64f1df78 100644 --- a/packages/order-watcher/test/global_hooks.ts +++ b/packages/order-watcher/test/global_hooks.ts @@ -7,7 +7,7 @@ before('migrate contracts', async function(): Promise<void> { // HACK: Since the migrations take longer then our global mocha timeout limit // we manually increase it for this before hook. const mochaTestTimeoutMs = 25000; - this.timeout(mochaTestTimeoutMs); + this.timeout(mochaTestTimeoutMs); // tslint:disable-line:no-invalid-this const txDefaults = { gas: devConstants.GAS_LIMIT, from: devConstants.TESTRPC_FIRST_ADDRESS, diff --git a/packages/order-watcher/test/order_watcher_test.ts b/packages/order-watcher/test/order_watcher_test.ts index 6339505ce..1281d11c2 100644 --- a/packages/order-watcher/test/order_watcher_test.ts +++ b/packages/order-watcher/test/order_watcher_test.ts @@ -136,8 +136,8 @@ describe('OrderWatcher', () => { orderWatcher.unsubscribe(); }); it('should fail when trying to subscribe twice', async () => { - orderWatcher.subscribe(_.noop); - expect(() => orderWatcher.subscribe(_.noop)).to.throw(OrderWatcherError.SubscriptionAlreadyPresent); + orderWatcher.subscribe(_.noop.bind(_)); + expect(() => orderWatcher.subscribe(_.noop.bind(_))).to.throw(OrderWatcherError.SubscriptionAlreadyPresent); }); }); describe('tests with cleanup', async () => { diff --git a/packages/order-watcher/test/utils/token_utils.ts b/packages/order-watcher/test/utils/token_utils.ts new file mode 100644 index 000000000..f91b3797f --- /dev/null +++ b/packages/order-watcher/test/utils/token_utils.ts @@ -0,0 +1,34 @@ +import { Token } from '@0xproject/types'; +import * as _ from 'lodash'; + +import { InternalOrderWatcherError } from '../../src/types'; + +const PROTOCOL_TOKEN_SYMBOL = 'ZRX'; +const WETH_TOKEN_SYMBOL = 'WETH'; + +export class TokenUtils { + private readonly _tokens: Token[]; + constructor(tokens: Token[]) { + this._tokens = tokens; + } + public getProtocolTokenOrThrow(): Token { + const zrxToken = _.find(this._tokens, { symbol: PROTOCOL_TOKEN_SYMBOL }); + if (_.isUndefined(zrxToken)) { + throw new Error(InternalOrderWatcherError.ZrxNotInTokenRegistry); + } + return zrxToken; + } + public getWethTokenOrThrow(): Token { + const wethToken = _.find(this._tokens, { symbol: WETH_TOKEN_SYMBOL }); + if (_.isUndefined(wethToken)) { + throw new Error(InternalOrderWatcherError.WethNotInTokenRegistry); + } + return wethToken; + } + public getDummyTokens(): Token[] { + const dummyTokens = _.filter(this._tokens, token => { + return !_.includes([PROTOCOL_TOKEN_SYMBOL, WETH_TOKEN_SYMBOL], token.symbol); + }); + return dummyTokens; + } +} |