diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-10-13 17:52:59 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-10-13 17:52:59 +0800 |
commit | 0eaca6c691d92a10b08c0e69306291aa8de06bfb (patch) | |
tree | ffa00109b4115025b8be35822abbbbd4ddb05155 /src/contract_wrappers/exchange_wrapper.ts | |
parent | ba654c04a086b8c4ce4330b3d6064716a4090599 (diff) | |
download | dexon-0x-contracts-0eaca6c691d92a10b08c0e69306291aa8de06bfb.tar dexon-0x-contracts-0eaca6c691d92a10b08c0e69306291aa8de06bfb.tar.gz dexon-0x-contracts-0eaca6c691d92a10b08c0e69306291aa8de06bfb.tar.bz2 dexon-0x-contracts-0eaca6c691d92a10b08c0e69306291aa8de06bfb.tar.lz dexon-0x-contracts-0eaca6c691d92a10b08c0e69306291aa8de06bfb.tar.xz dexon-0x-contracts-0eaca6c691d92a10b08c0e69306291aa8de06bfb.tar.zst dexon-0x-contracts-0eaca6c691d92a10b08c0e69306291aa8de06bfb.zip |
Make logs fetching and sunscriptions more type-safe
Diffstat (limited to 'src/contract_wrappers/exchange_wrapper.ts')
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 9e4936de6..739fe74c6 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -1,4 +1,5 @@ import * as _ from 'lodash'; +import * as Web3 from 'web3'; import * as BigNumber from 'bignumber.js'; import {schemas} from '0x-json-schemas'; import {Web3Wrapper} from '../web3_wrapper'; @@ -28,6 +29,8 @@ import { OrderTransactionOpts, RawLog, EventCallback, + ExchangeContractEventArgs, + DecodedLogArgs, } from '../types'; import {assert} from '../utils/assert'; import {utils} from '../utils/utils'; @@ -654,13 +657,14 @@ export class ExchangeWrapper extends ContractWrapper { * @param callback Callback that gets called when a log is added/removed * @return Subscription token used later to unsubscribe */ - public async subscribeAsync(eventName: ExchangeEvents, indexFilterValues: IndexedFilterValues, - callback: EventCallback): Promise<string> { + public async subscribeAsync<ArgsType extends ExchangeContractEventArgs>( + eventName: ExchangeEvents, indexFilterValues: IndexedFilterValues, + callback: EventCallback<ArgsType>): Promise<string> { assert.doesBelongToStringEnum('eventName', eventName, ExchangeEvents); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); assert.isFunction('callback', callback); const exchangeContractAddress = await this.getContractAddressAsync(); - const subscriptionToken = this._subscribe( + const subscriptionToken = this._subscribe<ArgsType>( exchangeContractAddress, eventName, indexFilterValues, artifacts.ExchangeArtifact.abi, callback, ); this._activeSubscriptions.push(subscriptionToken); @@ -682,13 +686,14 @@ export class ExchangeWrapper extends ContractWrapper { * the value is the value you are interested in. E.g `{_from: aUserAddressHex}` * @return Array of logs that match the parameters */ - public async getLogsAsync(eventName: ExchangeEvents, subscriptionOpts: SubscriptionOpts, - indexFilterValues: IndexedFilterValues): Promise<LogWithDecodedArgs[]> { + public async getLogsAsync<ArgsType extends ExchangeContractEventArgs>( + eventName: ExchangeEvents, subscriptionOpts: SubscriptionOpts, indexFilterValues: IndexedFilterValues, + ): Promise<Array<LogWithDecodedArgs<ArgsType>>> { assert.doesBelongToStringEnum('eventName', eventName, ExchangeEvents); assert.doesConformToSchema('subscriptionOpts', subscriptionOpts, schemas.subscriptionOptsSchema); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); const exchangeContractAddress = await this.getContractAddressAsync(); - const logs = await this._getLogsAsync( + const logs = await this._getLogsAsync<ArgsType>( exchangeContractAddress, eventName, subscriptionOpts, indexFilterValues, artifacts.ExchangeArtifact.abi, ); return logs; @@ -799,12 +804,14 @@ export class ExchangeWrapper extends ContractWrapper { } /** * Checks if logs contain LogError, which is emmited by Exchange contract on transaction failure. - * @param logsWithDecodedArgs Transaction logs as returned by `zeroEx.awaitTransactionMinedAsync` + * @param logs Transaction logs as returned by `zeroEx.awaitTransactionMinedAsync` */ - public throwLogErrorsAsErrors(logsWithDecodedArgs: LogWithDecodedArgs[]): void { - const errLog = _.find(logsWithDecodedArgs, {event: ExchangeEvents.LogError}); + public throwLogErrorsAsErrors(logs: Array<LogWithDecodedArgs<DecodedLogArgs>|Web3.LogEntry>): void { + const errLog = _.find(logs, { + event: ExchangeEvents.LogError, + }) as LogWithDecodedArgs<LogErrorContractEventArgs>|undefined; if (!_.isUndefined(errLog)) { - const logArgs: LogErrorContractEventArgs = errLog.args as any; + const logArgs = errLog.args; const errCode = logArgs.errorId.toNumber(); const errMessage = this._exchangeContractErrCodesToMsg[errCode]; throw new Error(errMessage); |