diff options
author | Fabio Berger <me@fabioberger.com> | 2017-11-10 04:02:28 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-11-10 04:02:28 +0800 |
commit | 9ff42053c3f145ab6d5486d62325ed222363a8c5 (patch) | |
tree | f76517056ccbb94317389ee042662eb64bc229b8 /src/mempool/event_watcher.ts | |
parent | 5623400557232b974e11e786cf6bfa49128a04e2 (diff) | |
download | dexon-0x-contracts-9ff42053c3f145ab6d5486d62325ed222363a8c5.tar.gz dexon-0x-contracts-9ff42053c3f145ab6d5486d62325ed222363a8c5.tar.zst dexon-0x-contracts-9ff42053c3f145ab6d5486d62325ed222363a8c5.zip |
Add numConfirmations arg so that caller can decide on numConfirmations at which they want to watch orders
Diffstat (limited to 'src/mempool/event_watcher.ts')
-rw-r--r-- | src/mempool/event_watcher.ts | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/mempool/event_watcher.ts b/src/mempool/event_watcher.ts index cb8921cfd..3f40606e7 100644 --- a/src/mempool/event_watcher.ts +++ b/src/mempool/event_watcher.ts @@ -19,10 +19,10 @@ export class EventWatcher { DEFAULT_MEMPOOL_POLLING_INTERVAL : pollingIntervalMs; } - public subscribe(callback: MempoolEventCallback): void { + public subscribe(callback: MempoolEventCallback, numConfirmations: number): void { this._callbackAsync = callback; this._intervalId = intervalUtils.setAsyncExcludingInterval( - this._pollForMempoolEventsAsync.bind(this), this._pollingIntervalMs, + this._pollForMempoolEventsAsync.bind(this, numConfirmations), this._pollingIntervalMs, ); } public unsubscribe(): void { @@ -30,8 +30,8 @@ export class EventWatcher { this._lastMempoolEvents = []; intervalUtils.clearAsyncExcludingInterval(this._intervalId); } - private async _pollForMempoolEventsAsync(): Promise<void> { - const pendingEvents = await this._getMempoolEventsAsync(); + private async _pollForMempoolEventsAsync(numConfirmations: number): Promise<void> { + const pendingEvents = await this._getMempoolEventsAsync(numConfirmations); if (pendingEvents.length === 0) { // HACK: Sometimes when node rebuilds the pending block we get back the empty result. // We don't want to emit a lot of removal events and bring them back after a couple of miliseconds, @@ -46,11 +46,19 @@ export class EventWatcher { await this._emitDifferencesAsync(newEvents, isRemoved); this._lastMempoolEvents = pendingEvents; } - private async _getMempoolEventsAsync(): Promise<Web3.LogEntry[]> { - // TODO: Allow users to listen to any number of confirmations deep, not just mempool + private async _getMempoolEventsAsync(numConfirmations: number): Promise<Web3.LogEntry[]> { + let fromBlock: BlockParamLiteral|number; + let toBlock: BlockParamLiteral|number; + if (numConfirmations === 0) { + fromBlock = BlockParamLiteral.Pending; + toBlock = BlockParamLiteral.Pending; + } else { + toBlock = await this._web3Wrapper.getBlockNumberAsync(); + fromBlock = toBlock - numConfirmations; + } const mempoolFilter = { - fromBlock: BlockParamLiteral.Pending, - toBlock: BlockParamLiteral.Pending, + fromBlock, + toBlock, }; const pendingEvents = await this._web3Wrapper.getLogsAsync(mempoolFilter); return pendingEvents; |