diff options
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 27 | ||||
-rw-r--r-- | src/types.ts | 1 | ||||
-rw-r--r-- | test/exchange_wrapper_test.ts | 45 |
3 files changed, 51 insertions, 22 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index f596cb429..2d46b99de 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -1,6 +1,7 @@ import * as _ from 'lodash'; import * as BigNumber from 'bignumber.js'; import promisify = require('es6-promisify'); +import * as Web3 from 'web3'; import {Web3Wrapper} from '../web3_wrapper'; import { ECSignature, @@ -26,6 +27,8 @@ import { LogErrorContractEventArgs, LogFillContractEventArgs, LogCancelContractEventArgs, + EventCallback, + ContractEventArg, ExchangeContractByAddress, ContractArtifact, } from '../types'; @@ -653,14 +656,36 @@ export class ExchangeWrapper extends ContractWrapper { return isAuthorized; } private _wrapEventEmitter(event: ContractEventObj): ContractEventEmitter { + const watch = (eventCallback: EventCallback) => { + const bignumberWrappingEventCallback = this._wrapEventCallback(eventCallback); + event.watch(bignumberWrappingEventCallback); + }; const zeroExEvent = { - watch: event.watch.bind(event), + watch, stopWatchingAsync: async () => { await promisify(event.stopWatching, event)(); }, }; return zeroExEvent; } + private _wrapEventCallback(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> { diff --git a/src/types.ts b/src/types.ts index 2af778677..b7ee9c946 100644 --- a/src/types.ts +++ b/src/types.ts @@ -237,6 +237,7 @@ export interface LogErrorContractEventArgs { orderHash: string; } export type ContractEventArgs = LogFillContractEventArgs|LogCancelContractEventArgs|LogErrorContractEventArgs; +export type ContractEventArg = string|BigNumber.BigNumber; export interface Order { maker: string; diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 5833a8c23..873ff48db 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -18,6 +18,7 @@ import { ExchangeContractErrs, OrderCancellationRequest, OrderFillRequest, + LogFillContractEventArgs, } from '../src'; import {DoneCallback} from '../src/types'; import {FillScenarios} from './utils/fill_scenarios'; @@ -680,6 +681,12 @@ describe('ExchangeWrapper', () => { let makerAddress: string; let fillableAmount: BigNumber.BigNumber; let signedOrder: SignedOrder; + const subscriptionOpts: SubscriptionOpts = { + fromBlock: 0, + toBlock: 'latest', + }; + const fillTakerAmountInBaseUnits = new BigNumber(1); + const cancelTakerAmountInBaseUnits = new BigNumber(1); before(() => { [coinbase, makerAddress, takerAddress] = userAddresses; const [makerToken, takerToken] = tokens; @@ -702,10 +709,6 @@ describe('ExchangeWrapper', () => { // Source: https://github.com/mochajs/mocha/issues/2407 it('Should receive the LogFill event when an order is filled', (done: DoneCallback) => { (async () => { - const subscriptionOpts: SubscriptionOpts = { - fromBlock: 0, - toBlock: 'latest', - }; const zeroExEvent = await zeroEx.exchange.subscribeAsync( ExchangeEvents.LogFill, subscriptionOpts, indexFilterValues, exchangeContractAddress, ); @@ -715,7 +718,6 @@ describe('ExchangeWrapper', () => { expect(event.event).to.be.equal('LogFill'); done(); }); - const fillTakerAmountInBaseUnits = new BigNumber(1); await zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, takerAddress, ); @@ -723,10 +725,6 @@ describe('ExchangeWrapper', () => { }); it('Should receive the LogCancel event when an order is cancelled', (done: DoneCallback) => { (async () => { - const subscriptionOpts: SubscriptionOpts = { - fromBlock: 0, - toBlock: 'latest', - }; const zeroExEvent = await zeroEx.exchange.subscribeAsync( ExchangeEvents.LogCancel, subscriptionOpts, indexFilterValues, exchangeContractAddress, ); @@ -736,16 +734,11 @@ describe('ExchangeWrapper', () => { expect(event.event).to.be.equal('LogCancel'); done(); }); - const cancelTakerAmountInBaseUnits = new BigNumber(1); await zeroEx.exchange.cancelOrderAsync(signedOrder, cancelTakerAmountInBaseUnits); })(); }); it('Outstanding subscriptions are cancelled when zeroEx.setProviderAsync called', (done: DoneCallback) => { (async () => { - const subscriptionOpts: SubscriptionOpts = { - fromBlock: 0, - toBlock: 'latest', - }; const eventSubscriptionToBeCancelled = await zeroEx.exchange.subscribeAsync( ExchangeEvents.LogFill, subscriptionOpts, indexFilterValues, exchangeContractAddress, ); @@ -765,8 +758,6 @@ describe('ExchangeWrapper', () => { expect(event.event).to.be.equal('LogFill'); done(); }); - - const fillTakerAmountInBaseUnits = new BigNumber(1); await zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, takerAddress, ); @@ -774,10 +765,6 @@ describe('ExchangeWrapper', () => { }); it('Should stop watch for events when stopWatchingAsync called on the eventEmitter', (done: DoneCallback) => { (async () => { - const subscriptionOpts: SubscriptionOpts = { - fromBlock: 0, - toBlock: 'latest', - }; const eventSubscriptionToBeStopped = await zeroEx.exchange.subscribeAsync( ExchangeEvents.LogFill, subscriptionOpts, indexFilterValues, exchangeContractAddress, ); @@ -785,13 +772,29 @@ describe('ExchangeWrapper', () => { done(new Error('Expected this subscription to have been stopped')); }); await eventSubscriptionToBeStopped.stopWatchingAsync(); - const fillTakerAmountInBaseUnits = new BigNumber(1); await zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, takerAddress, ); done(); })(); }); + it('Should wrap all event args BigNumber instances in a newer version of BigNumber', (done: DoneCallback) => { + (async () => { + const zeroExEvent = await zeroEx.exchange.subscribeAsync(ExchangeEvents.LogFill, subscriptionOpts, + indexFilterValues); + zeroExEvent.watch((err: Error, event: ContractEvent) => { + const args = event.args as LogFillContractEventArgs; + expect(args.filledValueM.isBigNumber).to.be.true(); + expect(args.filledValueT.isBigNumber).to.be.true(); + expect(args.feeMPaid.isBigNumber).to.be.true(); + expect(args.feeTPaid.isBigNumber).to.be.true(); + done(); + }); + await zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, takerAddress, + ); + })(); + }); }); describe('#getOrderHashHexUsingContractCallAsync', () => { let makerTokenAddress: string; |