aboutsummaryrefslogtreecommitdiffstats
path: root/src/contract_wrappers
diff options
context:
space:
mode:
Diffstat (limited to 'src/contract_wrappers')
-rw-r--r--src/contract_wrappers/exchange_wrapper.ts34
-rw-r--r--src/contract_wrappers/token_wrapper.ts57
2 files changed, 57 insertions, 34 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts
index 57a116aea..722910a61 100644
--- a/src/contract_wrappers/exchange_wrapper.ts
+++ b/src/contract_wrappers/exchange_wrapper.ts
@@ -34,6 +34,7 @@ import {
} from '../types';
import {assert} from '../utils/assert';
import {utils} from '../utils/utils';
+import {eventUtils} from '../utils/event_utils';
import {ContractWrapper} from './contract_wrapper';
import {ProxyWrapper} from './proxy_wrapper';
import {ExchangeArtifactsByName} from '../exchange_artifacts_by_name';
@@ -601,7 +602,7 @@ export class ExchangeWrapper extends ContractWrapper {
}
const logEventObj: ContractEventObj = createLogEvent(indexFilterValues, subscriptionOpts);
- const eventEmitter = this._wrapEventEmitter(logEventObj);
+ const eventEmitter = eventUtils.wrapEventEmitter(logEventObj);
this._exchangeLogEventEmitters.push(eventEmitter);
return eventEmitter;
}
@@ -655,37 +656,6 @@ export class ExchangeWrapper extends ContractWrapper {
const isAuthorized = await this._proxyWrapper.isAuthorizedAsync(exchangeContractAddress);
return isAuthorized;
}
- private _wrapEventEmitter(event: ContractEventObj): ContractEventEmitter {
- const watch = (eventCallback: EventCallback) => {
- const bignumberWrappingEventCallback = this._getBigNumberWrappingEventCallback(eventCallback);
- event.watch(bignumberWrappingEventCallback);
- };
- const zeroExEvent = {
- watch,
- stopWatchingAsync: async () => {
- await promisify(event.stopWatching, event)();
- },
- };
- return zeroExEvent;
- }
- private _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;
- }
private async _isValidSignatureUsingContractCallAsync(dataHex: string, ecSignature: ECSignature,
signerAddressHex: string,
exchangeContractAddress: string): Promise<boolean> {
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)) {