diff options
author | Leonid <logvinov.leon@gmail.com> | 2017-07-05 09:17:57 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-05 09:17:57 +0800 |
commit | 74b2308488832290340f3a6c6473ab7340510dfc (patch) | |
tree | be598e0355a72486125cacfad837a2d06342f170 /src/utils/event_utils.ts | |
parent | 3302d18f6e0a4b7e51b318959c6b2d040ae3c5ed (diff) | |
parent | 371acc0ba12197de735dea20e09d50bbfd524118 (diff) | |
download | dexon-sol-tools-74b2308488832290340f3a6c6473ab7340510dfc.tar dexon-sol-tools-74b2308488832290340f3a6c6473ab7340510dfc.tar.gz dexon-sol-tools-74b2308488832290340f3a6c6473ab7340510dfc.tar.bz2 dexon-sol-tools-74b2308488832290340f3a6c6473ab7340510dfc.tar.lz dexon-sol-tools-74b2308488832290340f3a6c6473ab7340510dfc.tar.xz dexon-sol-tools-74b2308488832290340f3a6c6473ab7340510dfc.tar.zst dexon-sol-tools-74b2308488832290340f3a6c6473ab7340510dfc.zip |
Merge pull request #90 from 0xProject/subscribe-token
Add implementation and tests for zeroEx.token.subscribeAsync
Diffstat (limited to 'src/utils/event_utils.ts')
-rw-r--r-- | src/utils/event_utils.ts | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/utils/event_utils.ts b/src/utils/event_utils.ts new file mode 100644 index 000000000..07418cbc4 --- /dev/null +++ b/src/utils/event_utils.ts @@ -0,0 +1,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 = { + 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; + }, + /** + * 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; + }, +}; |