aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/event_utils.ts
blob: 5829ffe994586433426f822560018453c56ebc8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import * as _ from 'lodash';
import * as Web3 from 'web3';
import {EventCallback, ContractEventArg, ContractEvent, ContractEventObj, ContractEventEmitter} from '../types';
import * as BigNumber from 'bignumber.js';
import promisify = require('es6-promisify');

export const eventUtils = {
    /**
     * Wraps eventCallback function so that all the BigNumber arguments are wrapped in a newer version of BigNumber.
     * @param eventCallback     Event callback function to be wrapped
     * @return Wrapped event callback function
     */
    _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;
    },
    wrapEventEmitter(event: ContractEventObj): ContractEventEmitter {
        const watch = (eventCallback: EventCallback) => {
            const bignumberWrappingEventCallback = eventUtils._getBigNumberWrappingEventCallback(eventCallback);
            event.watch(bignumberWrappingEventCallback);
        };
        const zeroExEvent = {
            watch,
            stopWatchingAsync: async () => {
                await promisify(event.stopWatching, event)();
            },
        };
        return zeroExEvent;
    },
};