diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-07-02 01:20:06 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-07-02 10:44:17 +0800 |
commit | 0e54418dbbccab84fad30e7d8b38f189652f76e7 (patch) | |
tree | a47ef80cc2b3f38c097f55aa7c34b7dab9ecf8ab | |
parent | 6e0edb8d8e8ced3b9e602a5722df859dd34ce563 (diff) | |
download | dexon-sol-tools-0e54418dbbccab84fad30e7d8b38f189652f76e7.tar dexon-sol-tools-0e54418dbbccab84fad30e7d8b38f189652f76e7.tar.gz dexon-sol-tools-0e54418dbbccab84fad30e7d8b38f189652f76e7.tar.bz2 dexon-sol-tools-0e54418dbbccab84fad30e7d8b38f189652f76e7.tar.lz dexon-sol-tools-0e54418dbbccab84fad30e7d8b38f189652f76e7.tar.xz dexon-sol-tools-0e54418dbbccab84fad30e7d8b38f189652f76e7.tar.zst dexon-sol-tools-0e54418dbbccab84fad30e7d8b38f189652f76e7.zip |
Wrap all event args in a newer version of BigNumber and test it
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 20 | ||||
-rw-r--r-- | src/types.ts | 1 | ||||
-rw-r--r-- | test/exchange_wrapper_test.ts | 45 |
3 files changed, 44 insertions, 22 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 5d514e5ec..5d4c21154 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -25,6 +25,8 @@ import { LogErrorContractEventArgs, LogFillContractEventArgs, LogCancelContractEventArgs, + EventCallback, + ContractEventArg, } from '../types'; import {assert} from '../utils/assert'; import {utils} from '../utils/utils'; @@ -583,14 +585,30 @@ export class ExchangeWrapper extends ContractWrapper { this._exchangeLogEventEmitters = []; } 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 => { + return value instanceof String ? value : new BigNumber(value); + }; + event.args = _.mapValues(event.args, wrapIfBigNumber); + } + eventCallback(err, event); + }; + return bignumberWrappingEventCallback; + } private async _isValidSignatureUsingContractCallAsync(dataHex: string, ecSignature: ECSignature, signerAddressHex: string): Promise<boolean> { assert.isHexString('dataHex', dataHex); diff --git a/src/types.ts b/src/types.ts index 200e65d56..708695890 100644 --- a/src/types.ts +++ b/src/types.ts @@ -229,6 +229,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 211c2819c..7835de993 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -17,6 +17,7 @@ import { ExchangeContractErrs, OrderCancellationRequest, OrderFillRequest, + LogFillContractEventArgs, } from '../src'; import {DoneCallback} from '../src/types'; import {FillScenarios} from './utils/fill_scenarios'; @@ -643,6 +644,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; @@ -665,10 +672,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); zeroExEvent.watch((err: Error, event: ContractEvent) => { @@ -676,7 +679,6 @@ describe('ExchangeWrapper', () => { expect(event).to.not.be.undefined(); done(); }); - const fillTakerAmountInBaseUnits = new BigNumber(1); await zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, takerAddress, ); @@ -684,10 +686,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); zeroExEvent.watch((err: Error, event: ContractEvent) => { @@ -695,16 +693,11 @@ describe('ExchangeWrapper', () => { expect(event).to.not.be.undefined(); 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, ); @@ -723,8 +716,6 @@ describe('ExchangeWrapper', () => { expect(event).to.not.be.undefined(); done(); }); - - const fillTakerAmountInBaseUnits = new BigNumber(1); await zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, takerAddress, ); @@ -732,10 +723,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, ); @@ -743,13 +730,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; |