From 6ad1900ada799eec18e870f48de785af14780a60 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 13 Jun 2017 15:52:50 +0200 Subject: Refactor subscription to return zeroExSignature object --- src/contract_wrappers/exchange_wrapper.ts | 43 +++++++++++++++++++------------ src/index.ts | 1 + src/types.ts | 5 ++++ src/utils/utils.ts | 4 +-- 4 files changed, 35 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 74b8d25c1..659eff81c 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -14,6 +14,7 @@ import { SignedOrder, ContractEvent, ExchangeEvents, + ZeroExEvent, SubscriptionOpts, IndexFilterValues, CreateContractEvent, @@ -47,7 +48,7 @@ export class ExchangeWrapper extends ContractWrapper { [ExchangeContractErrCodes.ERROR_FILL_BALANCE_ALLOWANCE]: ExchangeContractErrs.FILL_BALANCE_ALLOWANCE_ERROR, }; private _exchangeContractIfExists?: ExchangeContract; - private _exchangeLogEventObjs: ContractEventObj[]; + private _exchangeLogEventObjs: ZeroExEvent[]; private _tokenWrapper: TokenWrapper; private static _getOrderAddressesAndValues(order: Order): [OrderAddresses, OrderValues] { const orderAddresses: OrderAddresses = [ @@ -73,7 +74,7 @@ export class ExchangeWrapper extends ContractWrapper { this._exchangeLogEventObjs = []; } public async invalidateContractInstanceAsync(): Promise { - await this._stopWatchingExchangeLogEventsAsync(); + await this.stopWatchingAllEventsAsync(); delete this._exchangeContractIfExists; } /** @@ -517,11 +518,11 @@ export class ExchangeWrapper extends ContractWrapper { * @param subscriptionOpts Subscriptions options that let you configure the subscription. * @param indexFilterValues A JS 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}` - * @param callback The callback that will be called everytime a matching event is found. + * @return ZeroExEvent object */ public async subscribeAsync(eventName: ExchangeEvents, subscriptionOpts: SubscriptionOpts, - indexFilterValues: IndexFilterValues, callback: EventCallback): - Promise { + indexFilterValues: IndexFilterValues): + Promise { const exchangeContract = await this._getExchangeContractAsync(); let createLogEvent: CreateContractEvent; switch (eventName) { @@ -535,13 +536,13 @@ export class ExchangeWrapper extends ContractWrapper { createLogEvent = exchangeContract.LogCancel; break; default: - utils.spawnSwitchErr('ExchangeEvents', eventName); - return; + throw utils.spawnSwitchErr('ExchangeEvents', eventName); } const logEventObj: ContractEventObj = createLogEvent(indexFilterValues, subscriptionOpts); - logEventObj.watch(callback); - this._exchangeLogEventObjs.push(logEventObj); + const zeroExEvent = this._wrapEventAsZeroExEvent(logEventObj); + this._exchangeLogEventObjs.push(zeroExEvent); + return zeroExEvent; } /** * Returns the ethereum address of the current exchange contract @@ -552,6 +553,23 @@ export class ExchangeWrapper extends ContractWrapper { const exchangeContract = await this._getExchangeContractAsync(); return exchangeContract.address; } + /** + * Stops watching for all exchange events + */ + public async stopWatchingAllEventsAsync(): Promise { + const stopWatchingPromises = _.map(this._exchangeLogEventObjs, logEventObj => logEventObj.stopWatchingAsync()); + await Promise.all(stopWatchingPromises); + this._exchangeLogEventObjs = []; + } + private _wrapEventAsZeroExEvent(event: ContractEventObj): ZeroExEvent { + const zeroExEvent = { + watch: event.watch, + stopWatchingAsync: async () => { + await promisify(event.stopWatching, event)(); + }, + }; + return zeroExEvent; + } private async _isValidSignatureUsingContractCallAsync(dataHex: string, ecSignature: ECSignature, signerAddressHex: string): Promise { assert.isHexString('dataHex', dataHex); @@ -580,13 +598,6 @@ export class ExchangeWrapper extends ContractWrapper { const orderHashHex = await exchangeInstance.getOrderHash.call(orderAddresses, orderValues); return orderHashHex; } - private async _stopWatchingExchangeLogEventsAsync() { - const stopWatchingPromises = _.map(this._exchangeLogEventObjs, logEventObj => { - return promisify(logEventObj.stopWatching, logEventObj)(); - }); - await Promise.all(stopWatchingPromises); - this._exchangeLogEventObjs = []; - } private async _validateFillOrderAndThrowIfInvalidAsync(signedOrder: SignedOrder, fillTakerAmount: BigNumber.BigNumber, senderAddress: string): Promise { diff --git a/src/index.ts b/src/index.ts index 16e48862b..2a2411456 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,4 +22,5 @@ export { OrderCancellationRequest, OrderFillRequest, DoneCallback, + ZeroExEvent, } from './types'; diff --git a/src/types.ts b/src/types.ts index 1b70d950b..8edb9ce5e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -275,3 +275,8 @@ export interface ContractInstance { export interface Artifact { networks: {[networkId: number]: any}; } + +export interface ZeroExEvent { + watch: (eventCallback: EventCallback) => void; + stopWatchingAsync: () => Promise; +} diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 5786bab07..bad5b6498 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -22,11 +22,11 @@ export const utils = { isParityNode(nodeVersion: string): boolean { return _.includes(nodeVersion, 'Parity'); }, - isValidOrderHash(orderHashHex: string) { + isValidOrderHash(orderHashHex: string): boolean { const isValid = /^0x[0-9A-F]{64}$/i.test(orderHashHex); return isValid; }, - spawnSwitchErr(name: string, value: any) { + spawnSwitchErr(name: string, value: any): Error { return new Error(`Unexpected switch value: ${value} encountered for ${name}`); }, getOrderHashHex(order: Order|SignedOrder, exchangeContractAddr: string): string { -- cgit v1.2.3 From 048302625f61ee614bc15c8e84568a49315da63c Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 13 Jun 2017 17:26:08 +0200 Subject: Bind watch function on an event --- src/contract_wrappers/exchange_wrapper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 659eff81c..a617d8003 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -563,7 +563,7 @@ export class ExchangeWrapper extends ContractWrapper { } private _wrapEventAsZeroExEvent(event: ContractEventObj): ZeroExEvent { const zeroExEvent = { - watch: event.watch, + watch: event.watch.bind(event), stopWatchingAsync: async () => { await promisify(event.stopWatching, event)(); }, -- cgit v1.2.3 From 5ebc3a4660ba347a591862288b9c908717f2e835 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 13 Jun 2017 17:52:06 +0200 Subject: Rename ZeroExEvent to EventEmitter --- src/contract_wrappers/exchange_wrapper.ts | 24 ++++++++++++------------ src/index.ts | 2 +- src/types.ts | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index a617d8003..14d633d66 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -14,12 +14,11 @@ import { SignedOrder, ContractEvent, ExchangeEvents, - ZeroExEvent, + EventEmitter, SubscriptionOpts, IndexFilterValues, CreateContractEvent, ContractEventObj, - EventCallback, ContractResponse, OrderCancellationRequest, OrderFillRequest, @@ -48,7 +47,7 @@ export class ExchangeWrapper extends ContractWrapper { [ExchangeContractErrCodes.ERROR_FILL_BALANCE_ALLOWANCE]: ExchangeContractErrs.FILL_BALANCE_ALLOWANCE_ERROR, }; private _exchangeContractIfExists?: ExchangeContract; - private _exchangeLogEventObjs: ZeroExEvent[]; + private _exchangeLogEventEmitters: EventEmitter[]; private _tokenWrapper: TokenWrapper; private static _getOrderAddressesAndValues(order: Order): [OrderAddresses, OrderValues] { const orderAddresses: OrderAddresses = [ @@ -71,7 +70,7 @@ export class ExchangeWrapper extends ContractWrapper { constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper) { super(web3Wrapper); this._tokenWrapper = tokenWrapper; - this._exchangeLogEventObjs = []; + this._exchangeLogEventEmitters = []; } public async invalidateContractInstanceAsync(): Promise { await this.stopWatchingAllEventsAsync(); @@ -518,11 +517,11 @@ export class ExchangeWrapper extends ContractWrapper { * @param subscriptionOpts Subscriptions options that let you configure the subscription. * @param indexFilterValues A JS 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}` - * @return ZeroExEvent object + * @return EventEmitter object */ public async subscribeAsync(eventName: ExchangeEvents, subscriptionOpts: SubscriptionOpts, indexFilterValues: IndexFilterValues): - Promise { + Promise { const exchangeContract = await this._getExchangeContractAsync(); let createLogEvent: CreateContractEvent; switch (eventName) { @@ -540,9 +539,9 @@ export class ExchangeWrapper extends ContractWrapper { } const logEventObj: ContractEventObj = createLogEvent(indexFilterValues, subscriptionOpts); - const zeroExEvent = this._wrapEventAsZeroExEvent(logEventObj); - this._exchangeLogEventObjs.push(zeroExEvent); - return zeroExEvent; + const eventEmitter = this._wrapEventEmitter(logEventObj); + this._exchangeLogEventEmitters.push(eventEmitter); + return eventEmitter; } /** * Returns the ethereum address of the current exchange contract @@ -557,11 +556,12 @@ export class ExchangeWrapper extends ContractWrapper { * Stops watching for all exchange events */ public async stopWatchingAllEventsAsync(): Promise { - const stopWatchingPromises = _.map(this._exchangeLogEventObjs, logEventObj => logEventObj.stopWatchingAsync()); + const stopWatchingPromises = _.map(this._exchangeLogEventEmitters, + logEventObj => logEventObj.stopWatchingAsync()); await Promise.all(stopWatchingPromises); - this._exchangeLogEventObjs = []; + this._exchangeLogEventEmitters = []; } - private _wrapEventAsZeroExEvent(event: ContractEventObj): ZeroExEvent { + private _wrapEventEmitter(event: ContractEventObj): EventEmitter { const zeroExEvent = { watch: event.watch.bind(event), stopWatchingAsync: async () => { diff --git a/src/index.ts b/src/index.ts index 2a2411456..b2963aca7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,5 +22,5 @@ export { OrderCancellationRequest, OrderFillRequest, DoneCallback, - ZeroExEvent, + EventEmitter, } from './types'; diff --git a/src/types.ts b/src/types.ts index 8edb9ce5e..2197fbfc3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -276,7 +276,7 @@ export interface Artifact { networks: {[networkId: number]: any}; } -export interface ZeroExEvent { +export interface EventEmitter { watch: (eventCallback: EventCallback) => void; stopWatchingAsync: () => Promise; } -- cgit v1.2.3