diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-07-04 02:39:26 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-07-04 06:54:05 +0800 |
commit | 5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b (patch) | |
tree | 6980d7de11f4ff45fc6d7d33c9087e7193dc102b /src/contract_wrappers/token_wrapper.ts | |
parent | c9edeae6d8dad1fba6c4f5eca63ff5db3e6555e2 (diff) | |
download | dexon-sol-tools-5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b.tar dexon-sol-tools-5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b.tar.gz dexon-sol-tools-5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b.tar.bz2 dexon-sol-tools-5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b.tar.lz dexon-sol-tools-5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b.tar.xz dexon-sol-tools-5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b.tar.zst dexon-sol-tools-5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b.zip |
Add initial implementation and tests for zeroEx.token.subscribeAsync
Diffstat (limited to 'src/contract_wrappers/token_wrapper.ts')
-rw-r--r-- | src/contract_wrappers/token_wrapper.ts | 57 |
1 files changed, 55 insertions, 2 deletions
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<void> { + 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<ContractEventEmitter> { + 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<void> { + const stopWatchingPromises = _.map(this._tokenLogEventEmitters, + logEventObj => logEventObj.stopWatchingAsync()); + await Promise.all(stopWatchingPromises); + this._tokenLogEventEmitters = []; + } private async _getTokenContractAsync(tokenAddress: string): Promise<TokenContract> { let tokenContract = this._tokenContractsByAddress[tokenAddress]; if (!_.isUndefined(tokenContract)) { |