diff options
Diffstat (limited to 'src/contract_wrappers/exchange_wrapper.ts')
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 55 |
1 files changed, 16 insertions, 39 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 6726f3eac..5a2da4a98 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -34,14 +34,18 @@ import { } from '../types'; import {assert} from '../utils/assert'; import {utils} from '../utils/utils'; +import {eventUtils} from '../utils/event_utils'; import {ContractWrapper} from './contract_wrapper'; import {ProxyWrapper} from './proxy_wrapper'; import {ExchangeArtifactsByName} from '../exchange_artifacts_by_name'; import {ecSignatureSchema} from '../schemas/ec_signature_schema'; import {signedOrdersSchema} from '../schemas/signed_orders_schema'; +import {subscriptionOptsSchema} from '../schemas/subscription_opts_schema'; +import {indexFilterValuesSchema} from '../schemas/index_filter_values_schema'; import {orderFillRequestsSchema} from '../schemas/order_fill_requests_schema'; import {orderCancellationRequestsSchema} from '../schemas/order_cancel_schema'; import {orderFillOrKillRequestsSchema} from '../schemas/order_fill_or_kill_requests_schema'; +import {orderHashSchema} from '../schemas/order_hash_schema'; import {signedOrderSchema, orderSchema} from '../schemas/order_schemas'; import {constants} from '../utils/constants'; import {TokenWrapper} from './token_wrapper'; @@ -89,10 +93,6 @@ export class ExchangeWrapper extends ContractWrapper { this._exchangeLogEventEmitters = []; this._exchangeContractByAddress = {}; } - public async invalidateContractInstancesAsync(): Promise<void> { - await this.stopWatchingAllEventsAsync(); - this._exchangeContractByAddress = {}; - } /** * Returns the unavailable takerAmount of an order. Unavailable amount is defined as the total * amount that has been filled or cancelled. The remaining takerAmount can be calculated by @@ -104,7 +104,7 @@ export class ExchangeWrapper extends ContractWrapper { */ public async getUnavailableTakerAmountAsync(orderHash: string, exchangeContractAddress: string): Promise<BigNumber.BigNumber> { - assert.isValidOrderHash('orderHash', orderHash); + assert.doesConformToSchema('orderHash', orderHash, orderHashSchema); const exchangeContract = await this._getExchangeContractAsync(exchangeContractAddress); let unavailableAmountInBaseUnits = await exchangeContract.getUnavailableValueT.call(orderHash); @@ -120,7 +120,7 @@ export class ExchangeWrapper extends ContractWrapper { */ public async getFilledTakerAmountAsync(orderHash: string, exchangeContractAddress: string): Promise<BigNumber.BigNumber> { - assert.isValidOrderHash('orderHash', orderHash); + assert.doesConformToSchema('orderHash', orderHash, orderHashSchema); const exchangeContract = await this._getExchangeContractAsync(exchangeContractAddress); let fillAmountInBaseUnits = await exchangeContract.filled.call(orderHash); @@ -137,7 +137,7 @@ export class ExchangeWrapper extends ContractWrapper { */ public async getCanceledTakerAmountAsync(orderHash: string, exchangeContractAddress: string): Promise<BigNumber.BigNumber> { - assert.isValidOrderHash('orderHash', orderHash); + assert.doesConformToSchema('orderHash', orderHash, orderHashSchema); const exchangeContract = await this._getExchangeContractAsync(exchangeContractAddress); let cancelledAmountInBaseUnits = await exchangeContract.cancelled.call(orderHash); @@ -584,6 +584,10 @@ export class ExchangeWrapper extends ContractWrapper { public async subscribeAsync(eventName: ExchangeEvents, subscriptionOpts: SubscriptionOpts, indexFilterValues: IndexedFilterValues, exchangeContractAddress: string): Promise<ContractEventEmitter> { + assert.isETHAddressHex('exchangeContractAddress', exchangeContractAddress); + assert.doesBelongToStringEnum('eventName', eventName, ExchangeEvents); + assert.doesConformToSchema('subscriptionOpts', subscriptionOpts, subscriptionOptsSchema); + assert.doesConformToSchema('indexFilterValues', indexFilterValues, indexFilterValuesSchema); const exchangeContract = await this._getExchangeContractAsync(exchangeContractAddress); let createLogEvent: CreateContractEvent; switch (eventName) { @@ -601,7 +605,7 @@ export class ExchangeWrapper extends ContractWrapper { } const logEventObj: ContractEventObj = createLogEvent(indexFilterValues, subscriptionOpts); - const eventEmitter = this._wrapEventEmitter(logEventObj); + const eventEmitter = eventUtils.wrapEventEmitter(logEventObj); this._exchangeLogEventEmitters.push(eventEmitter); return eventEmitter; } @@ -651,41 +655,14 @@ export class ExchangeWrapper extends ContractWrapper { await Promise.all(stopWatchingPromises); this._exchangeLogEventEmitters = []; } + private async _invalidateContractInstancesAsync(): Promise<void> { + await this.stopWatchingAllEventsAsync(); + this._exchangeContractByAddress = {}; + } private async _isExchangeContractAddressProxyAuthorizedAsync(exchangeContractAddress: string): Promise<boolean> { const isAuthorized = await this._proxyWrapper.isAuthorizedAsync(exchangeContractAddress); return isAuthorized; } - private _wrapEventEmitter(event: ContractEventObj): ContractEventEmitter { - const watch = (eventCallback: EventCallback) => { - const bignumberWrappingEventCallback = this._getBigNumberWrappingEventCallback(eventCallback); - event.watch(bignumberWrappingEventCallback); - }; - const zeroExEvent = { - watch, - stopWatchingAsync: async () => { - await promisify(event.stopWatching, event)(); - }, - }; - return zeroExEvent; - } - private _getBigNumberWrappingEventCallback(eventCallback: EventCallback): EventCallback { - const bignumberWrappingEventCallback = (err: Error, event: ContractEvent) => { - if (_.isNull(err)) { - const wrapIfBigNumber = (value: ContractEventArg): ContractEventArg => { - // HACK: The old version of BigNumber used by Web3@0.19.0 does not support the `isBigNumber` - // and checking for a BigNumber instance using `instanceof` does not work either. We therefore - // compare the constructor functions of the possible BigNumber instance and the BigNumber used by - // Web3. - const web3BigNumber = (Web3.prototype as any).BigNumber; - const isWeb3BigNumber = web3BigNumber.toString() === value.constructor.toString(); - return isWeb3BigNumber ? new BigNumber(value) : value; - }; - event.args = _.mapValues(event.args, wrapIfBigNumber); - } - eventCallback(err, event); - }; - return bignumberWrappingEventCallback; - } private async _isValidSignatureUsingContractCallAsync(dataHex: string, ecSignature: ECSignature, signerAddressHex: string, exchangeContractAddress: string): Promise<boolean> { |