Add isVerbose flag to contract-wrappers subscribe methods

This commit is contained in:
Fabio Berger
2018-07-17 16:55:55 +02:00
parent e678fed55b
commit 66d63e8f4f
4 changed files with 19 additions and 6 deletions

View File

@@ -7,7 +7,7 @@ import {
LogWithDecodedArgs, LogWithDecodedArgs,
RawLog, RawLog,
} from '@0xproject/types'; } from '@0xproject/types';
import { AbiDecoder, intervalUtils } from '@0xproject/utils'; import { intervalUtils, logUtils } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { Web3Wrapper } from '@0xproject/web3-wrapper';
import { Block, BlockAndLogStreamer, Log } from 'ethereumjs-blockstream'; import { Block, BlockAndLogStreamer, Log } from 'ethereumjs-blockstream';
import * as _ from 'lodash'; import * as _ from 'lodash';
@@ -46,9 +46,12 @@ export class ContractWrapper {
}; };
private _onLogAddedSubscriptionToken: string | undefined; private _onLogAddedSubscriptionToken: string | undefined;
private _onLogRemovedSubscriptionToken: string | undefined; private _onLogRemovedSubscriptionToken: string | undefined;
private static _onBlockstreamError(err: Error): void { private static _onBlockstreamError(isVerbose: boolean, err: Error): void {
// Noop on blockstream errors since they are automatically // Noop on blockstream errors since they are automatically
// recovered from and don't cause Blockstream to exit. // recovered from and don't cause Blockstream to exit.
if (isVerbose) {
logUtils.warn(err.message);
}
} }
constructor(web3Wrapper: Web3Wrapper, networkId: number) { constructor(web3Wrapper: Web3Wrapper, networkId: number) {
this._web3Wrapper = web3Wrapper; this._web3Wrapper = web3Wrapper;
@@ -85,10 +88,11 @@ export class ContractWrapper {
indexFilterValues: IndexedFilterValues, indexFilterValues: IndexedFilterValues,
abi: ContractAbi, abi: ContractAbi,
callback: EventCallback<ArgsType>, callback: EventCallback<ArgsType>,
isVerbose: boolean = false,
): string { ): string {
const filter = filterUtils.getFilter(address, eventName, indexFilterValues, abi); const filter = filterUtils.getFilter(address, eventName, indexFilterValues, abi);
if (_.isUndefined(this._blockAndLogStreamerIfExists)) { if (_.isUndefined(this._blockAndLogStreamerIfExists)) {
this._startBlockAndLogStream(); this._startBlockAndLogStream(isVerbose);
} }
const filterToken = filterUtils.generateUUID(); const filterToken = filterUtils.generateUUID();
this._filters[filterToken] = filter; this._filters[filterToken] = filter;
@@ -159,21 +163,21 @@ export class ContractWrapper {
} }
}); });
} }
private _startBlockAndLogStream(): void { private _startBlockAndLogStream(isVerbose: boolean): void {
if (!_.isUndefined(this._blockAndLogStreamerIfExists)) { if (!_.isUndefined(this._blockAndLogStreamerIfExists)) {
throw new Error(ContractWrappersError.SubscriptionAlreadyPresent); throw new Error(ContractWrappersError.SubscriptionAlreadyPresent);
} }
this._blockAndLogStreamerIfExists = new BlockAndLogStreamer( this._blockAndLogStreamerIfExists = new BlockAndLogStreamer(
this._web3Wrapper.getBlockAsync.bind(this._web3Wrapper), this._web3Wrapper.getBlockAsync.bind(this._web3Wrapper),
this._web3Wrapper.getLogsAsync.bind(this._web3Wrapper), this._web3Wrapper.getLogsAsync.bind(this._web3Wrapper),
ContractWrapper._onBlockstreamError.bind(this), ContractWrapper._onBlockstreamError.bind(this, isVerbose),
); );
const catchAllLogFilter = {}; const catchAllLogFilter = {};
this._blockAndLogStreamerIfExists.addLogFilter(catchAllLogFilter); this._blockAndLogStreamerIfExists.addLogFilter(catchAllLogFilter);
this._blockAndLogStreamIntervalIfExists = intervalUtils.setAsyncExcludingInterval( this._blockAndLogStreamIntervalIfExists = intervalUtils.setAsyncExcludingInterval(
this._reconcileBlockAsync.bind(this), this._reconcileBlockAsync.bind(this),
constants.DEFAULT_BLOCK_POLLING_INTERVAL, constants.DEFAULT_BLOCK_POLLING_INTERVAL,
ContractWrapper._onBlockstreamError.bind(this), ContractWrapper._onBlockstreamError.bind(this, isVerbose),
); );
let isRemoved = false; let isRemoved = false;
this._onLogAddedSubscriptionToken = this._blockAndLogStreamerIfExists.subscribeToOnLogAdded( this._onLogAddedSubscriptionToken = this._blockAndLogStreamerIfExists.subscribeToOnLogAdded(

View File

@@ -133,6 +133,7 @@ export class EtherTokenWrapper extends ContractWrapper {
* @param indexFilterValues An object where the keys are indexed args returned by the event and * @param indexFilterValues An object where the keys are indexed args returned by the event and
* the value is the value you are interested in. E.g `{_owner: aUserAddressHex}` * the value is the value you are interested in. E.g `{_owner: aUserAddressHex}`
* @param callback Callback that gets called when a log is added/removed * @param callback Callback that gets called when a log is added/removed
* @param isVerbose Enable verbose subscription warnings (e.g recoverable network issues encountered)
* @return Subscription token used later to unsubscribe * @return Subscription token used later to unsubscribe
*/ */
public subscribe<ArgsType extends EtherTokenContractEventArgs>( public subscribe<ArgsType extends EtherTokenContractEventArgs>(
@@ -140,6 +141,7 @@ export class EtherTokenWrapper extends ContractWrapper {
eventName: EtherTokenEvents, eventName: EtherTokenEvents,
indexFilterValues: IndexedFilterValues, indexFilterValues: IndexedFilterValues,
callback: EventCallback<ArgsType>, callback: EventCallback<ArgsType>,
isVerbose: boolean = false,
): string { ): string {
assert.isETHAddressHex('etherTokenAddress', etherTokenAddress); assert.isETHAddressHex('etherTokenAddress', etherTokenAddress);
const normalizedEtherTokenAddress = etherTokenAddress.toLowerCase(); const normalizedEtherTokenAddress = etherTokenAddress.toLowerCase();
@@ -152,6 +154,7 @@ export class EtherTokenWrapper extends ContractWrapper {
indexFilterValues, indexFilterValues,
artifacts.EtherToken.abi, artifacts.EtherToken.abi,
callback, callback,
isVerbose,
); );
return subscriptionToken; return subscriptionToken;
} }

View File

@@ -652,12 +652,14 @@ export class ExchangeWrapper extends ContractWrapper {
* @param indexFilterValues An object where the keys are indexed args returned by the event and * @param indexFilterValues An object where the keys are indexed args returned by the event and
* the value is the value you are interested in. E.g `{maker: aUserAddressHex}` * the value is the value you are interested in. E.g `{maker: aUserAddressHex}`
* @param callback Callback that gets called when a log is added/removed * @param callback Callback that gets called when a log is added/removed
* @param isVerbose Enable verbose subscription warnings (e.g recoverable network issues encountered)
* @return Subscription token used later to unsubscribe * @return Subscription token used later to unsubscribe
*/ */
public subscribe<ArgsType extends ExchangeContractEventArgs>( public subscribe<ArgsType extends ExchangeContractEventArgs>(
eventName: ExchangeEvents, eventName: ExchangeEvents,
indexFilterValues: IndexedFilterValues, indexFilterValues: IndexedFilterValues,
callback: EventCallback<ArgsType>, callback: EventCallback<ArgsType>,
isVerbose: boolean = false,
): string { ): string {
assert.doesBelongToStringEnum('eventName', eventName, ExchangeEvents); assert.doesBelongToStringEnum('eventName', eventName, ExchangeEvents);
assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema);
@@ -669,6 +671,7 @@ export class ExchangeWrapper extends ContractWrapper {
indexFilterValues, indexFilterValues,
artifacts.Exchange.abi, artifacts.Exchange.abi,
callback, callback,
isVerbose,
); );
return subscriptionToken; return subscriptionToken;
} }

View File

@@ -350,6 +350,7 @@ export class TokenWrapper extends ContractWrapper {
* @param indexFilterValues An object where the keys are indexed args returned by the event and * @param indexFilterValues An object where the keys are indexed args returned by the event and
* the value is the value you are interested in. E.g `{maker: aUserAddressHex}` * the value is the value you are interested in. E.g `{maker: aUserAddressHex}`
* @param callback Callback that gets called when a log is added/removed * @param callback Callback that gets called when a log is added/removed
* @param isVerbose Enable verbose subscription warnings (e.g recoverable network issues encountered)
* @return Subscription token used later to unsubscribe * @return Subscription token used later to unsubscribe
*/ */
public subscribe<ArgsType extends TokenContractEventArgs>( public subscribe<ArgsType extends TokenContractEventArgs>(
@@ -357,6 +358,7 @@ export class TokenWrapper extends ContractWrapper {
eventName: TokenEvents, eventName: TokenEvents,
indexFilterValues: IndexedFilterValues, indexFilterValues: IndexedFilterValues,
callback: EventCallback<ArgsType>, callback: EventCallback<ArgsType>,
isVerbose: boolean = false,
): string { ): string {
assert.isETHAddressHex('tokenAddress', tokenAddress); assert.isETHAddressHex('tokenAddress', tokenAddress);
const normalizedTokenAddress = tokenAddress.toLowerCase(); const normalizedTokenAddress = tokenAddress.toLowerCase();
@@ -369,6 +371,7 @@ export class TokenWrapper extends ContractWrapper {
indexFilterValues, indexFilterValues,
artifacts.Token.abi, artifacts.Token.abi,
callback, callback,
isVerbose,
); );
return subscriptionToken; return subscriptionToken;
} }