From 5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 3 Jul 2017 11:39:26 -0700 Subject: Add initial implementation and tests for zeroEx.token.subscribeAsync --- src/contract_wrappers/token_wrapper.ts | 57 ++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) (limited to 'src/contract_wrappers/token_wrapper.ts') diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts index e34c624ab..d60843cdb 100644 --- a/src/contract_wrappers/token_wrapper.ts +++ b/src/contract_wrappers/token_wrapper.ts @@ -2,11 +2,22 @@ import * as _ from 'lodash'; import * as BigNumber from 'bignumber.js'; import {Web3Wrapper} from '../web3_wrapper'; import {assert} from '../utils/assert'; +import {utils} from '../utils/utils'; +import {eventUtils} from '../utils/event_utils'; import {constants} from '../utils/constants'; import {ContractWrapper} from './contract_wrapper'; import * as TokenArtifacts from '../artifacts/Token.json'; import * as ProxyArtifacts from '../artifacts/Proxy.json'; -import {TokenContract, ZeroExError} from '../types'; +import { + TokenContract, + ZeroExError, + TokenEvents, + IndexedFilterValues, + SubscriptionOpts, + CreateContractEvent, + ContractEventEmitter, + ContractEventObj, +} from '../types'; const ALLOWANCE_TO_ZERO_GAS_AMOUNT = 45730; @@ -17,11 +28,14 @@ const ALLOWANCE_TO_ZERO_GAS_AMOUNT = 45730; */ export class TokenWrapper extends ContractWrapper { private _tokenContractsByAddress: {[address: string]: TokenContract}; + private _tokenLogEventEmitters: ContractEventEmitter[]; constructor(web3Wrapper: Web3Wrapper) { super(web3Wrapper); this._tokenContractsByAddress = {}; + this._tokenLogEventEmitters = []; } - public invalidateContractInstances() { + public async invalidateContractInstancesAsync(): Promise { + await this.stopWatchingAllEventsAsync(); this._tokenContractsByAddress = {}; } /** @@ -178,6 +192,45 @@ export class TokenWrapper extends ContractWrapper { from: senderAddress, }); } + /** + * Subscribe to an event type emitted by the Token smart contract + * @param tokenAddress The hex encoded contract Ethereum address where the ERC20 token is deployed. + * @param eventName The token contract event you would like to subscribe to. + * @param subscriptionOpts Subscriptions options that let you configure the subscription. + * @param indexFilterValues An object where the keys are indexed args returned by the event and + * the value is the value you are interested in. E.g `{maker: aUserAddressHex}` + * @return ContractEventEmitter object + */ + public async subscribeAsync(tokenAddress: string, eventName: TokenEvents, subscriptionOpts: SubscriptionOpts, + indexFilterValues: IndexedFilterValues): + Promise { + const tokenContract = await this._getTokenContractAsync(tokenAddress); + let createLogEvent: CreateContractEvent; + switch (eventName) { + case TokenEvents.Approval: + createLogEvent = tokenContract.Approval; + break; + case TokenEvents.Transfer: + createLogEvent = tokenContract.Transfer; + break; + default: + throw utils.spawnSwitchErr('TokenEvents', eventName); + } + + const logEventObj: ContractEventObj = createLogEvent(indexFilterValues, subscriptionOpts); + const eventEmitter = eventUtils.wrapEventEmitter(logEventObj); + this._tokenLogEventEmitters.push(eventEmitter); + return eventEmitter; + } + /** + * Stops watching for all token events + */ + public async stopWatchingAllEventsAsync(): Promise { + const stopWatchingPromises = _.map(this._tokenLogEventEmitters, + logEventObj => logEventObj.stopWatchingAsync()); + await Promise.all(stopWatchingPromises); + this._tokenLogEventEmitters = []; + } private async _getTokenContractAsync(tokenAddress: string): Promise { let tokenContract = this._tokenContractsByAddress[tokenAddress]; if (!_.isUndefined(tokenContract)) { -- cgit v1.2.3