diff options
Diffstat (limited to 'src/contract_wrappers')
-rw-r--r-- | src/contract_wrappers/ether_token_wrapper.ts | 3 | ||||
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 55 | ||||
-rw-r--r-- | src/contract_wrappers/proxy_wrapper.ts | 6 | ||||
-rw-r--r-- | src/contract_wrappers/token_registry_wrapper.ts | 6 | ||||
-rw-r--r-- | src/contract_wrappers/token_wrapper.ts | 66 |
5 files changed, 87 insertions, 49 deletions
diff --git a/src/contract_wrappers/ether_token_wrapper.ts b/src/contract_wrappers/ether_token_wrapper.ts index 76e7289b7..03d714bd7 100644 --- a/src/contract_wrappers/ether_token_wrapper.ts +++ b/src/contract_wrappers/ether_token_wrapper.ts @@ -64,6 +64,9 @@ export class EtherTokenWrapper extends ContractWrapper { const wethContract = await this._getEtherTokenContractAsync(); return wethContract.address; } + private _invalidateContractInstance(): void { + delete this._etherTokenContractIfExists; + } private async _getEtherTokenContractAsync(): Promise<EtherTokenContract> { if (!_.isUndefined(this._etherTokenContractIfExists)) { return this._etherTokenContractIfExists; diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 6726f3eac..5a2da4a98 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -34,14 +34,18 @@ 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'; import {ecSignatureSchema} from '../schemas/ec_signature_schema'; import {signedOrdersSchema} from '../schemas/signed_orders_schema'; +import {subscriptionOptsSchema} from '../schemas/subscription_opts_schema'; +import {indexFilterValuesSchema} from '../schemas/index_filter_values_schema'; import {orderFillRequestsSchema} from '../schemas/order_fill_requests_schema'; import {orderCancellationRequestsSchema} from '../schemas/order_cancel_schema'; import {orderFillOrKillRequestsSchema} from '../schemas/order_fill_or_kill_requests_schema'; +import {orderHashSchema} from '../schemas/order_hash_schema'; import {signedOrderSchema, orderSchema} from '../schemas/order_schemas'; import {constants} from '../utils/constants'; import {TokenWrapper} from './token_wrapper'; @@ -89,10 +93,6 @@ export class ExchangeWrapper extends ContractWrapper { this._exchangeLogEventEmitters = []; this._exchangeContractByAddress = {}; } - public async invalidateContractInstancesAsync(): Promise<void> { - await this.stopWatchingAllEventsAsync(); - this._exchangeContractByAddress = {}; - } /** * Returns the unavailable takerAmount of an order. Unavailable amount is defined as the total * amount that has been filled or cancelled. The remaining takerAmount can be calculated by @@ -104,7 +104,7 @@ export class ExchangeWrapper extends ContractWrapper { */ public async getUnavailableTakerAmountAsync(orderHash: string, exchangeContractAddress: string): Promise<BigNumber.BigNumber> { - assert.isValidOrderHash('orderHash', orderHash); + assert.doesConformToSchema('orderHash', orderHash, orderHashSchema); const exchangeContract = await this._getExchangeContractAsync(exchangeContractAddress); let unavailableAmountInBaseUnits = await exchangeContract.getUnavailableValueT.call(orderHash); @@ -120,7 +120,7 @@ export class ExchangeWrapper extends ContractWrapper { */ public async getFilledTakerAmountAsync(orderHash: string, exchangeContractAddress: string): Promise<BigNumber.BigNumber> { - assert.isValidOrderHash('orderHash', orderHash); + assert.doesConformToSchema('orderHash', orderHash, orderHashSchema); const exchangeContract = await this._getExchangeContractAsync(exchangeContractAddress); let fillAmountInBaseUnits = await exchangeContract.filled.call(orderHash); @@ -137,7 +137,7 @@ export class ExchangeWrapper extends ContractWrapper { */ public async getCanceledTakerAmountAsync(orderHash: string, exchangeContractAddress: string): Promise<BigNumber.BigNumber> { - assert.isValidOrderHash('orderHash', orderHash); + assert.doesConformToSchema('orderHash', orderHash, orderHashSchema); const exchangeContract = await this._getExchangeContractAsync(exchangeContractAddress); let cancelledAmountInBaseUnits = await exchangeContract.cancelled.call(orderHash); @@ -584,6 +584,10 @@ export class ExchangeWrapper extends ContractWrapper { public async subscribeAsync(eventName: ExchangeEvents, subscriptionOpts: SubscriptionOpts, indexFilterValues: IndexedFilterValues, exchangeContractAddress: string): Promise<ContractEventEmitter> { + assert.isETHAddressHex('exchangeContractAddress', exchangeContractAddress); + assert.doesBelongToStringEnum('eventName', eventName, ExchangeEvents); + assert.doesConformToSchema('subscriptionOpts', subscriptionOpts, subscriptionOptsSchema); + assert.doesConformToSchema('indexFilterValues', indexFilterValues, indexFilterValuesSchema); const exchangeContract = await this._getExchangeContractAsync(exchangeContractAddress); let createLogEvent: CreateContractEvent; switch (eventName) { @@ -601,7 +605,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; } @@ -651,41 +655,14 @@ export class ExchangeWrapper extends ContractWrapper { await Promise.all(stopWatchingPromises); this._exchangeLogEventEmitters = []; } + private async _invalidateContractInstancesAsync(): Promise<void> { + await this.stopWatchingAllEventsAsync(); + this._exchangeContractByAddress = {}; + } private async _isExchangeContractAddressProxyAuthorizedAsync(exchangeContractAddress: string): Promise<boolean> { 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/proxy_wrapper.ts b/src/contract_wrappers/proxy_wrapper.ts index bdf163f35..05d4e142c 100644 --- a/src/contract_wrappers/proxy_wrapper.ts +++ b/src/contract_wrappers/proxy_wrapper.ts @@ -9,9 +9,6 @@ import {ProxyContract} from '../types'; */ export class ProxyWrapper extends ContractWrapper { private _proxyContractIfExists?: ProxyContract; - public invalidateContractInstance(): void { - delete this._proxyContractIfExists; - } /** * Check if the Exchange contract address is authorized by the Proxy contract. * @param exchangeContractAddress The hex encoded address of the Exchange contract to call. @@ -32,6 +29,9 @@ export class ProxyWrapper extends ContractWrapper { const authorizedAddresses = await proxyContractInstance.getAuthorizedAddresses.call(); return authorizedAddresses; } + private _invalidateContractInstance(): void { + delete this._proxyContractIfExists; + } private async _getProxyContractAsync(): Promise<ProxyContract> { if (!_.isUndefined(this._proxyContractIfExists)) { return this._proxyContractIfExists; diff --git a/src/contract_wrappers/token_registry_wrapper.ts b/src/contract_wrappers/token_registry_wrapper.ts index 3e87e4852..c9f21e46f 100644 --- a/src/contract_wrappers/token_registry_wrapper.ts +++ b/src/contract_wrappers/token_registry_wrapper.ts @@ -13,9 +13,6 @@ export class TokenRegistryWrapper extends ContractWrapper { constructor(web3Wrapper: Web3Wrapper) { super(web3Wrapper); } - public invalidateContractInstance(): void { - delete this._tokenRegistryContractIfExists; - } /** * Retrieves all the tokens currently listed in the Token Registry smart contract * @return An array of objects that conform to the Token interface. @@ -40,6 +37,9 @@ export class TokenRegistryWrapper extends ContractWrapper { }); return tokens; } + private _invalidateContractInstance(): void { + delete this._tokenRegistryContractIfExists; + } private async _getTokenRegistryContractAsync(): Promise<TokenRegistryContract> { if (!_.isUndefined(this._tokenRegistryContractIfExists)) { return this._tokenRegistryContractIfExists; diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts index e34c624ab..fdf711823 100644 --- a/src/contract_wrappers/token_wrapper.ts +++ b/src/contract_wrappers/token_wrapper.ts @@ -2,11 +2,24 @@ 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 {subscriptionOptsSchema} from '../schemas/subscription_opts_schema'; +import {indexFilterValuesSchema} from '../schemas/index_filter_values_schema'; +import { + TokenContract, + ZeroExError, + TokenEvents, + IndexedFilterValues, + SubscriptionOpts, + CreateContractEvent, + ContractEventEmitter, + ContractEventObj, +} from '../types'; const ALLOWANCE_TO_ZERO_GAS_AMOUNT = 45730; @@ -17,12 +30,11 @@ 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 = {}; - } - public invalidateContractInstances() { - this._tokenContractsByAddress = {}; + this._tokenLogEventEmitters = []; } /** * Retrieves an owner's ERC20 token balance. @@ -178,6 +190,52 @@ export class TokenWrapper extends ContractWrapper { from: senderAddress, }); } + /** + * Subscribe to an event type emitted by the Token contract. + * @param tokenAddress The hex encoded 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> { + assert.isETHAddressHex('tokenAddress', tokenAddress); + assert.doesBelongToStringEnum('eventName', eventName, TokenEvents); + assert.doesConformToSchema('subscriptionOpts', subscriptionOpts, subscriptionOptsSchema); + assert.doesConformToSchema('indexFilterValues', indexFilterValues, indexFilterValuesSchema); + 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 _invalidateContractInstancesAsync(): Promise<void> { + await this.stopWatchingAllEventsAsync(); + this._tokenContractsByAddress = {}; + } private async _getTokenContractAsync(tokenAddress: string): Promise<TokenContract> { let tokenContract = this._tokenContractsByAddress[tokenAddress]; if (!_.isUndefined(tokenContract)) { |