aboutsummaryrefslogtreecommitdiffstats
path: root/src/contract_wrappers
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-06-03 03:18:02 +0800
committerGitHub <noreply@github.com>2017-06-03 03:18:02 +0800
commit3d75a502359b54226311215182d388615dbd8498 (patch)
treeeba084371931a9bd7534fd9c2dfadee8e18e64fc /src/contract_wrappers
parent88815e5b4514e4953cc150241340e0b53dadc198 (diff)
parent2e5e8c8987c681ffd0dd1535cb26874a39b8abec (diff)
downloaddexon-0x-contracts-3d75a502359b54226311215182d388615dbd8498.tar.gz
dexon-0x-contracts-3d75a502359b54226311215182d388615dbd8498.tar.zst
dexon-0x-contracts-3d75a502359b54226311215182d388615dbd8498.zip
Merge pull request #32 from 0xProject/addEventSubscriptions
Implement zeroEx.exchange.subscribeAsync
Diffstat (limited to 'src/contract_wrappers')
-rw-r--r--src/contract_wrappers/exchange_wrapper.ts47
1 files changed, 46 insertions, 1 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts
index cb869b498..ee0b2696f 100644
--- a/src/contract_wrappers/exchange_wrapper.ts
+++ b/src/contract_wrappers/exchange_wrapper.ts
@@ -1,5 +1,6 @@
import * as _ from 'lodash';
import * as BigNumber from 'bignumber.js';
+import promisify = require('es6-promisify');
import {Web3Wrapper} from '../web3_wrapper';
import {
ECSignature,
@@ -10,9 +11,17 @@ import {
OrderAddresses,
SignedOrder,
ContractEvent,
+ ZeroExError,
+ ExchangeEvents,
+ SubscriptionOpts,
+ IndexFilterValues,
+ CreateContractEvent,
+ ContractEventObj,
+ EventCallback,
ContractResponse,
} from '../types';
import {assert} from '../utils/assert';
+import {utils} from '../utils/utils';
import {ContractWrapper} from './contract_wrapper';
import * as ExchangeArtifacts from '../artifacts/Exchange.json';
import {ecSignatureSchema} from '../schemas/ec_signature_schema';
@@ -31,12 +40,15 @@ export class ExchangeWrapper extends ContractWrapper {
[ExchangeContractErrCodes.ERROR_FILL_BALANCE_ALLOWANCE]: ExchangeContractErrs.FILL_BALANCE_ALLOWANCE_ERROR,
};
private exchangeContractIfExists?: ExchangeContract;
+ private exchangeLogEventObjs: ContractEventObj[];
private tokenWrapper: TokenWrapper;
constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper) {
super(web3Wrapper);
this.tokenWrapper = tokenWrapper;
+ this.exchangeLogEventObjs = [];
}
- public invalidateContractInstance(): void {
+ public async invalidateContractInstanceAsync(): Promise<void> {
+ await this.stopWatchingExchangeLogEventsAsync();
delete this.exchangeContractIfExists;
}
public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature,
@@ -160,6 +172,39 @@ export class ExchangeWrapper extends ContractWrapper {
);
this.throwErrorLogsAsErrors(response.logs);
}
+ /**
+ * Subscribe to an event type emitted by the Exchange smart contract
+ */
+ public async subscribeAsync(eventName: ExchangeEvents, subscriptionOpts: SubscriptionOpts,
+ indexFilterValues: IndexFilterValues, callback: EventCallback) {
+ const exchangeContract = await this.getExchangeContractAsync();
+ let createLogEvent: CreateContractEvent;
+ switch (eventName) {
+ case ExchangeEvents.LogFill:
+ createLogEvent = exchangeContract.LogFill;
+ break;
+ case ExchangeEvents.LogError:
+ createLogEvent = exchangeContract.LogError;
+ break;
+ case ExchangeEvents.LogCancel:
+ createLogEvent = exchangeContract.LogCancel;
+ break;
+ default:
+ utils.spawnSwitchErr('ExchangeEvents', eventName);
+ return;
+ }
+
+ const logEventObj: ContractEventObj = createLogEvent(indexFilterValues, subscriptionOpts);
+ logEventObj.watch(callback);
+ this.exchangeLogEventObjs.push(logEventObj);
+ }
+ private async stopWatchingExchangeLogEventsAsync() {
+ const stopWatchingPromises = _.map(this.exchangeLogEventObjs, logEventObj => {
+ return promisify(logEventObj.stopWatching, logEventObj)();
+ });
+ await Promise.all(stopWatchingPromises);
+ this.exchangeLogEventObjs = [];
+ }
private async validateFillOrderAndThrowIfInvalidAsync(signedOrder: SignedOrder,
fillTakerAmount: BigNumber.BigNumber,
senderAddress: string): Promise<void> {