aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2017-06-13 21:52:50 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2017-06-13 21:52:50 +0800
commit6ad1900ada799eec18e870f48de785af14780a60 (patch)
tree25c064c33816a7da818079b08c19a523061d70ee /src
parenta045eb7bbb76cee83d26b1ca18074d3b1961e084 (diff)
downloaddexon-sol-tools-6ad1900ada799eec18e870f48de785af14780a60.tar.gz
dexon-sol-tools-6ad1900ada799eec18e870f48de785af14780a60.tar.zst
dexon-sol-tools-6ad1900ada799eec18e870f48de785af14780a60.zip
Refactor subscription to return zeroExSignature object
Diffstat (limited to 'src')
-rw-r--r--src/contract_wrappers/exchange_wrapper.ts43
-rw-r--r--src/index.ts1
-rw-r--r--src/types.ts5
-rw-r--r--src/utils/utils.ts4
4 files changed, 35 insertions, 18 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts
index 74b8d25c1..659eff81c 100644
--- a/src/contract_wrappers/exchange_wrapper.ts
+++ b/src/contract_wrappers/exchange_wrapper.ts
@@ -14,6 +14,7 @@ import {
SignedOrder,
ContractEvent,
ExchangeEvents,
+ ZeroExEvent,
SubscriptionOpts,
IndexFilterValues,
CreateContractEvent,
@@ -47,7 +48,7 @@ export class ExchangeWrapper extends ContractWrapper {
[ExchangeContractErrCodes.ERROR_FILL_BALANCE_ALLOWANCE]: ExchangeContractErrs.FILL_BALANCE_ALLOWANCE_ERROR,
};
private _exchangeContractIfExists?: ExchangeContract;
- private _exchangeLogEventObjs: ContractEventObj[];
+ private _exchangeLogEventObjs: ZeroExEvent[];
private _tokenWrapper: TokenWrapper;
private static _getOrderAddressesAndValues(order: Order): [OrderAddresses, OrderValues] {
const orderAddresses: OrderAddresses = [
@@ -73,7 +74,7 @@ export class ExchangeWrapper extends ContractWrapper {
this._exchangeLogEventObjs = [];
}
public async invalidateContractInstanceAsync(): Promise<void> {
- await this._stopWatchingExchangeLogEventsAsync();
+ await this.stopWatchingAllEventsAsync();
delete this._exchangeContractIfExists;
}
/**
@@ -517,11 +518,11 @@ export class ExchangeWrapper extends ContractWrapper {
* @param subscriptionOpts Subscriptions options that let you configure the subscription.
* @param indexFilterValues A JS 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}`
- * @param callback The callback that will be called everytime a matching event is found.
+ * @return ZeroExEvent object
*/
public async subscribeAsync(eventName: ExchangeEvents, subscriptionOpts: SubscriptionOpts,
- indexFilterValues: IndexFilterValues, callback: EventCallback):
- Promise<void> {
+ indexFilterValues: IndexFilterValues):
+ Promise<ZeroExEvent> {
const exchangeContract = await this._getExchangeContractAsync();
let createLogEvent: CreateContractEvent;
switch (eventName) {
@@ -535,13 +536,13 @@ export class ExchangeWrapper extends ContractWrapper {
createLogEvent = exchangeContract.LogCancel;
break;
default:
- utils.spawnSwitchErr('ExchangeEvents', eventName);
- return;
+ throw utils.spawnSwitchErr('ExchangeEvents', eventName);
}
const logEventObj: ContractEventObj = createLogEvent(indexFilterValues, subscriptionOpts);
- logEventObj.watch(callback);
- this._exchangeLogEventObjs.push(logEventObj);
+ const zeroExEvent = this._wrapEventAsZeroExEvent(logEventObj);
+ this._exchangeLogEventObjs.push(zeroExEvent);
+ return zeroExEvent;
}
/**
* Returns the ethereum address of the current exchange contract
@@ -552,6 +553,23 @@ export class ExchangeWrapper extends ContractWrapper {
const exchangeContract = await this._getExchangeContractAsync();
return exchangeContract.address;
}
+ /**
+ * Stops watching for all exchange events
+ */
+ public async stopWatchingAllEventsAsync(): Promise<void> {
+ const stopWatchingPromises = _.map(this._exchangeLogEventObjs, logEventObj => logEventObj.stopWatchingAsync());
+ await Promise.all(stopWatchingPromises);
+ this._exchangeLogEventObjs = [];
+ }
+ private _wrapEventAsZeroExEvent(event: ContractEventObj): ZeroExEvent {
+ const zeroExEvent = {
+ watch: event.watch,
+ stopWatchingAsync: async () => {
+ await promisify(event.stopWatching, event)();
+ },
+ };
+ return zeroExEvent;
+ }
private async _isValidSignatureUsingContractCallAsync(dataHex: string, ecSignature: ECSignature,
signerAddressHex: string): Promise<boolean> {
assert.isHexString('dataHex', dataHex);
@@ -580,13 +598,6 @@ export class ExchangeWrapper extends ContractWrapper {
const orderHashHex = await exchangeInstance.getOrderHash.call(orderAddresses, orderValues);
return orderHashHex;
}
- 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> {
diff --git a/src/index.ts b/src/index.ts
index 16e48862b..2a2411456 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -22,4 +22,5 @@ export {
OrderCancellationRequest,
OrderFillRequest,
DoneCallback,
+ ZeroExEvent,
} from './types';
diff --git a/src/types.ts b/src/types.ts
index 1b70d950b..8edb9ce5e 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -275,3 +275,8 @@ export interface ContractInstance {
export interface Artifact {
networks: {[networkId: number]: any};
}
+
+export interface ZeroExEvent {
+ watch: (eventCallback: EventCallback) => void;
+ stopWatchingAsync: () => Promise<void>;
+}
diff --git a/src/utils/utils.ts b/src/utils/utils.ts
index 5786bab07..bad5b6498 100644
--- a/src/utils/utils.ts
+++ b/src/utils/utils.ts
@@ -22,11 +22,11 @@ export const utils = {
isParityNode(nodeVersion: string): boolean {
return _.includes(nodeVersion, 'Parity');
},
- isValidOrderHash(orderHashHex: string) {
+ isValidOrderHash(orderHashHex: string): boolean {
const isValid = /^0x[0-9A-F]{64}$/i.test(orderHashHex);
return isValid;
},
- spawnSwitchErr(name: string, value: any) {
+ spawnSwitchErr(name: string, value: any): Error {
return new Error(`Unexpected switch value: ${value} encountered for ${name}`);
},
getOrderHashHex(order: Order|SignedOrder, exchangeContractAddr: string): string {