From a9ae555b88cc36ff2cbd92fdd37a339702860c01 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Sun, 12 Nov 2017 13:06:25 -0500 Subject: Store number of confirmations in a blockStore --- src/order_watcher/event_watcher.ts | 7 ++----- src/order_watcher/order_state_watcher.ts | 8 ++++---- src/stores/balance_proxy_allowance_lazy_store.ts | 8 +++----- src/stores/block_store.ts | 15 ++++++++++----- src/stores/order_filled_cancelled_lazy_store.ts | 8 +++----- src/utils/exchange_transfer_simulator.ts | 4 ++-- 6 files changed, 24 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/order_watcher/event_watcher.ts b/src/order_watcher/event_watcher.ts index 5303bb651..7e27d5e79 100644 --- a/src/order_watcher/event_watcher.ts +++ b/src/order_watcher/event_watcher.ts @@ -29,13 +29,10 @@ export class EventWatcher { private _pollingIntervalMs: number; private _intervalIdIfExists?: NodeJS.Timer; private _lastEvents: Web3.LogEntry[] = []; - private _numConfirmations: number; private _blockStore: BlockStore; - constructor(web3Wrapper: Web3Wrapper, blockStore: BlockStore, pollingIntervalMs: undefined|number, - numConfirmations: number) { + constructor(web3Wrapper: Web3Wrapper, blockStore: BlockStore, pollingIntervalMs: undefined|number) { this._web3Wrapper = web3Wrapper; this._blockStore = blockStore; - this._numConfirmations = numConfirmations; this._pollingIntervalMs = _.isUndefined(pollingIntervalMs) ? DEFAULT_EVENT_POLLING_INTERVAL : pollingIntervalMs; @@ -71,7 +68,7 @@ export class EventWatcher { this._lastEvents = pendingEvents; } private async _getEventsAsync(): Promise { - const blockNumber = this._blockStore.getBlockNumberWithNConfirmations(this._numConfirmations); + const blockNumber = this._blockStore.getBlockNumber(); const eventFilter = { fromBlock: blockNumber, toBlock: blockNumber, diff --git a/src/order_watcher/order_state_watcher.ts b/src/order_watcher/order_state_watcher.ts index e6250cef5..0f5b5c30c 100644 --- a/src/order_watcher/order_state_watcher.ts +++ b/src/order_watcher/order_state_watcher.ts @@ -78,15 +78,15 @@ export class OrderStateWatcher { const numConfirmations = (_.isUndefined(config) || _.isUndefined(config.numConfirmations)) ? DEFAULT_NUM_CONFIRMATIONS : config.numConfirmations; - this._blockStore = new BlockStore(this._web3Wrapper, blockPollingIntervalMs); + this._blockStore = new BlockStore(numConfirmations, this._web3Wrapper, blockPollingIntervalMs); this._eventWatcher = new EventWatcher( - web3Wrapper, this._blockStore, eventPollingIntervalMs, numConfirmations, + web3Wrapper, this._blockStore, eventPollingIntervalMs, ); this._balanceAndProxyAllowanceLazyStore = new BalanceAndProxyAllowanceLazyStore( - this._token, this._blockStore, numConfirmations, + this._token, this._blockStore, ); this._orderFilledCancelledLazyStore = new OrderFilledCancelledLazyStore( - this._exchange, this._blockStore, numConfirmations, + this._exchange, this._blockStore, ); this._orderStateUtils = new OrderStateUtils( this._balanceAndProxyAllowanceLazyStore, this._orderFilledCancelledLazyStore, diff --git a/src/stores/balance_proxy_allowance_lazy_store.ts b/src/stores/balance_proxy_allowance_lazy_store.ts index ac21c0818..88152e145 100644 --- a/src/stores/balance_proxy_allowance_lazy_store.ts +++ b/src/stores/balance_proxy_allowance_lazy_store.ts @@ -9,7 +9,6 @@ import {BlockStore} from './block_store'; */ export class BalanceAndProxyAllowanceLazyStore { private token: TokenWrapper; - private numConfirmations: number; private blockStore: BlockStore; private balance: { [tokenAddress: string]: { @@ -21,16 +20,15 @@ export class BalanceAndProxyAllowanceLazyStore { [userAddress: string]: BigNumber, }, }; - constructor(token: TokenWrapper, blockStore: BlockStore, numConfirmations: number) { + constructor(token: TokenWrapper, blockStore: BlockStore) { this.token = token; - this.numConfirmations = numConfirmations; this.blockStore = blockStore; this.balance = {}; this.proxyAllowance = {}; } public async getBalanceAsync(tokenAddress: string, userAddress: string): Promise { if (_.isUndefined(this.balance[tokenAddress]) || _.isUndefined(this.balance[tokenAddress][userAddress])) { - const defaultBlock = this.blockStore.getBlockNumberWithNConfirmations(this.numConfirmations); + const defaultBlock = this.blockStore.getBlockNumber(); const methodOpts = { defaultBlock, }; @@ -54,7 +52,7 @@ export class BalanceAndProxyAllowanceLazyStore { public async getProxyAllowanceAsync(tokenAddress: string, userAddress: string): Promise { if (_.isUndefined(this.proxyAllowance[tokenAddress]) || _.isUndefined(this.proxyAllowance[tokenAddress][userAddress])) { - const defaultBlock = this.blockStore.getBlockNumberWithNConfirmations(this.numConfirmations); + const defaultBlock = this.blockStore.getBlockNumber(); const methodOpts = { defaultBlock, }; diff --git a/src/stores/block_store.ts b/src/stores/block_store.ts index d1f6c329a..ae9c232bb 100644 --- a/src/stores/block_store.ts +++ b/src/stores/block_store.ts @@ -15,15 +15,17 @@ export class BlockStore { private latestBlockNumber?: number; private intervalId?: NodeJS.Timer; private blockPollingIntervalMs: number; - constructor(web3Wrapper?: Web3Wrapper, blockPollingIntervalMs?: number) { + private numConfirmations: number; + constructor(numConfirmations: number, web3Wrapper?: Web3Wrapper, blockPollingIntervalMs?: number) { + this.numConfirmations = numConfirmations; this.web3Wrapper = web3Wrapper; this.blockPollingIntervalMs = blockPollingIntervalMs || DEFAULT_BLOCK_POLLING_INTERVAL_MS; } - public getBlockNumberWithNConfirmations(numConfirmations: number): Web3.BlockParam { + public getBlockNumber(): Web3.BlockParam { let blockNumber; - if (numConfirmations === 0) { + if (this.numConfirmations === 0) { blockNumber = BlockParamLiteral.Pending; - } else if (numConfirmations === 1) { + } else if (this.numConfirmations === 1) { // HACK: We special-case `numConfirmations === 1` so that we can use this block store without actually // setting `latestBlockNumber` when block number is latest (in order validation) whhich allows us to omit // an async call in a constructor of `ExchangeTransferSimulator` @@ -33,11 +35,14 @@ export class BlockStore { throw new Error(InternalZeroExError.LatestBlockNumberNotSet); } // Latest block already has 1 confirmation - blockNumber = this.latestBlockNumber - numConfirmations + 1; + blockNumber = this.latestBlockNumber - this.numConfirmations + 1; } return blockNumber; } public async startAsync(): Promise { + if (this.numConfirmations === 0 || this.numConfirmations === 1) { + return; // no-op + } await this.updateLatestBlockAsync(); this.intervalId = intervalUtils.setAsyncExcludingInterval( this.updateLatestBlockAsync.bind(this), this.blockPollingIntervalMs, diff --git a/src/stores/order_filled_cancelled_lazy_store.ts b/src/stores/order_filled_cancelled_lazy_store.ts index 5b0dab35c..104a8240e 100644 --- a/src/stores/order_filled_cancelled_lazy_store.ts +++ b/src/stores/order_filled_cancelled_lazy_store.ts @@ -9,7 +9,6 @@ import {BlockStore} from './block_store'; */ export class OrderFilledCancelledLazyStore { private exchange: ExchangeWrapper; - private numConfirmations: number; private blockStore: BlockStore; private filledTakerAmount: { [orderHash: string]: BigNumber, @@ -17,16 +16,15 @@ export class OrderFilledCancelledLazyStore { private cancelledTakerAmount: { [orderHash: string]: BigNumber, }; - constructor(exchange: ExchangeWrapper, blockStore: BlockStore, numConfirmations: number) { + constructor(exchange: ExchangeWrapper, blockStore: BlockStore) { this.exchange = exchange; - this.numConfirmations = numConfirmations; this.blockStore = blockStore; this.filledTakerAmount = {}; this.cancelledTakerAmount = {}; } public async getFilledTakerAmountAsync(orderHash: string): Promise { if (_.isUndefined(this.filledTakerAmount[orderHash])) { - const defaultBlock = this.blockStore.getBlockNumberWithNConfirmations(this.numConfirmations); + const defaultBlock = this.blockStore.getBlockNumber(); const methodOpts = { defaultBlock, }; @@ -44,7 +42,7 @@ export class OrderFilledCancelledLazyStore { } public async getCancelledTakerAmountAsync(orderHash: string): Promise { if (_.isUndefined(this.cancelledTakerAmount[orderHash])) { - const defaultBlock = this.blockStore.getBlockNumberWithNConfirmations(this.numConfirmations); + const defaultBlock = this.blockStore.getBlockNumber(); const methodOpts = { defaultBlock, }; diff --git a/src/utils/exchange_transfer_simulator.ts b/src/utils/exchange_transfer_simulator.ts index cd46397ed..475dcf953 100644 --- a/src/utils/exchange_transfer_simulator.ts +++ b/src/utils/exchange_transfer_simulator.ts @@ -37,9 +37,9 @@ export class ExchangeTransferSimulator { private store: BalanceAndProxyAllowanceLazyStore; private UNLIMITED_ALLOWANCE_IN_BASE_UNITS: BigNumber; constructor(token: TokenWrapper) { - const blockStore = new BlockStore(); const latestBlockConfirmationNumber = 1; - this.store = new BalanceAndProxyAllowanceLazyStore(token, blockStore, latestBlockConfirmationNumber); + const blockStore = new BlockStore(latestBlockConfirmationNumber); + this.store = new BalanceAndProxyAllowanceLazyStore(token, blockStore); this.UNLIMITED_ALLOWANCE_IN_BASE_UNITS = token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; } /** -- cgit v1.2.3