From e72ba39c41a86cd17a3115426ad579efbfe11ddc Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 10 Nov 2017 15:07:05 -0500 Subject: Make orderStateUtils operate on stores --- src/order_watcher/order_state_watcher.ts | 9 +----- src/utils/order_state_utils.ts | 53 +++++++++++++++++++------------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/order_watcher/order_state_watcher.ts b/src/order_watcher/order_state_watcher.ts index 4866f8409..ec7e69b72 100644 --- a/src/order_watcher/order_state_watcher.ts +++ b/src/order_watcher/order_state_watcher.ts @@ -164,16 +164,9 @@ export class OrderStateWatcher { } } private async _emitRevalidateOrdersAsync(orderHashes: string[], blockNumber: number): Promise { - const defaultBlock = this._numConfirmations === 0 ? - BlockParamLiteral.Pending : - blockNumber; - const methodOpts = { - defaultBlock, - }; - for (const orderHash of orderHashes) { const signedOrder = this._orderByOrderHash[orderHash] as SignedOrder; - const orderState = await this._orderStateUtils.getOrderStateAsync(signedOrder, methodOpts); + const orderState = await this._orderStateUtils.getOrderStateAsync(signedOrder); if (!_.isUndefined(this._callbackIfExistsAsync)) { await this._callbackIfExistsAsync(orderState); } else { diff --git a/src/utils/order_state_utils.ts b/src/utils/order_state_utils.ts index 36a4b68d6..f82601cae 100644 --- a/src/utils/order_state_utils.ts +++ b/src/utils/order_state_utils.ts @@ -1,4 +1,5 @@ import * as _ from 'lodash'; +import * as Web3 from 'web3'; import BigNumber from 'bignumber.js'; import { ExchangeContractErrs, @@ -14,16 +15,19 @@ import {TokenWrapper} from '../contract_wrappers/token_wrapper'; import {ExchangeWrapper} from '../contract_wrappers/exchange_wrapper'; import {utils} from '../utils/utils'; import {constants} from '../utils/constants'; +import {OrderFilledCancelledLazyStore} from '../stores/order_filled_cancelled_lazy_store'; +import {BalanceAndProxyAllowanceLazyStore} from '../stores/balance_proxy_allowance_lazy_store'; export class OrderStateUtils { - private tokenWrapper: TokenWrapper; - private exchangeWrapper: ExchangeWrapper; - constructor(tokenWrapper: TokenWrapper, exchangeWrapper: ExchangeWrapper) { - this.tokenWrapper = tokenWrapper; - this.exchangeWrapper = exchangeWrapper; + private balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore; + private orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore; + constructor(balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore, + orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore) { + this.balanceAndProxyAllowanceLazyStore = balanceAndProxyAllowanceLazyStore; + this.orderFilledCancelledLazyStore = orderFilledCancelledLazyStore; } - public async getOrderStateAsync(signedOrder: SignedOrder, methodOpts?: MethodOpts): Promise { - const orderRelevantState = await this.getOrderRelevantStateAsync(signedOrder, methodOpts); + public async getOrderStateAsync(signedOrder: SignedOrder): Promise { + const orderRelevantState = await this.getOrderRelevantStateAsync(signedOrder); const orderHash = ZeroEx.getOrderHashHex(signedOrder); try { this.validateIfOrderIsValid(signedOrder, orderRelevantState); @@ -42,26 +46,31 @@ export class OrderStateUtils { return orderState; } } - public async getOrderRelevantStateAsync( - signedOrder: SignedOrder, methodOpts?: MethodOpts): Promise { - const zrxTokenAddress = await this.exchangeWrapper.getZRXTokenAddressAsync(); + public async getOrderRelevantStateAsync(signedOrder: SignedOrder): Promise { + // HACK: We access the private property here but otherwise the interface will be less nice. + // If we pass it from the instantiator - there is no opportunity to get it there + // because JS doesn't support async constructors. + // Moreover - it's cached under the hood so it's equivalent to an async constructor. + const exchange = (this.orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper; + const zrxTokenAddress = await exchange.getZRXTokenAddressAsync(); const orderHash = ZeroEx.getOrderHashHex(signedOrder); - const makerBalance = await this.tokenWrapper.getBalanceAsync( - signedOrder.makerTokenAddress, signedOrder.maker, methodOpts, + const makerBalance = await this.balanceAndProxyAllowanceLazyStore.getBalanceAsync( + signedOrder.makerTokenAddress, signedOrder.maker, ); - const makerProxyAllowance = await this.tokenWrapper.getProxyAllowanceAsync( - signedOrder.makerTokenAddress, signedOrder.maker, methodOpts, + const makerProxyAllowance = await this.balanceAndProxyAllowanceLazyStore.getProxyAllowanceAsync( + signedOrder.makerTokenAddress, signedOrder.maker, ); - const makerFeeBalance = await this.tokenWrapper.getBalanceAsync( - zrxTokenAddress, signedOrder.maker, methodOpts, + const makerFeeBalance = await this.balanceAndProxyAllowanceLazyStore.getBalanceAsync( + zrxTokenAddress, signedOrder.maker, ); - const makerFeeProxyAllowance = await this.tokenWrapper.getProxyAllowanceAsync( - zrxTokenAddress, signedOrder.maker, methodOpts, + const makerFeeProxyAllowance = await this.balanceAndProxyAllowanceLazyStore.getProxyAllowanceAsync( + zrxTokenAddress, signedOrder.maker, ); - const filledTakerTokenAmount = await this.exchangeWrapper.getFilledTakerAmountAsync(orderHash, methodOpts); - const canceledTakerTokenAmount = await this.exchangeWrapper.getCanceledTakerAmountAsync(orderHash, methodOpts); - const unavailableTakerTokenAmount = - await this.exchangeWrapper.getUnavailableTakerAmountAsync(orderHash, methodOpts); + const filledTakerTokenAmount = await this.orderFilledCancelledLazyStore.getFilledTakerAmountAsync(orderHash); + const canceledTakerTokenAmount = await this.orderFilledCancelledLazyStore.getCancelledTakerAmountAsync( + orderHash, + ); + const unavailableTakerTokenAmount = await exchange.getUnavailableTakerAmountAsync(orderHash); const totalMakerTokenAmount = signedOrder.makerTokenAmount; const totalTakerTokenAmount = signedOrder.takerTokenAmount; const remainingTakerTokenAmount = totalTakerTokenAmount.minus(unavailableTakerTokenAmount); -- cgit v1.2.3