diff options
Diffstat (limited to 'src/stores/block_store.ts')
-rw-r--r-- | src/stores/block_store.ts | 66 |
1 files changed, 0 insertions, 66 deletions
diff --git a/src/stores/block_store.ts b/src/stores/block_store.ts deleted file mode 100644 index ae9c232bb..000000000 --- a/src/stores/block_store.ts +++ /dev/null @@ -1,66 +0,0 @@ -import * as _ from 'lodash'; -import * as Web3 from 'web3'; -import {BigNumber} from 'bignumber.js'; -import {BlockParamLiteral, InternalZeroExError, ZeroExError} from '../types'; -import {Web3Wrapper} from '../web3_wrapper'; -import {intervalUtils} from '../utils/interval_utils'; - -const DEFAULT_BLOCK_POLLING_INTERVAL_MS = 500; - -/** - * Store for a current latest block number - */ -export class BlockStore { - private web3Wrapper?: Web3Wrapper; - private latestBlockNumber?: number; - private intervalId?: NodeJS.Timer; - private 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 getBlockNumber(): Web3.BlockParam { - let blockNumber; - if (this.numConfirmations === 0) { - blockNumber = BlockParamLiteral.Pending; - } 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` - blockNumber = BlockParamLiteral.Latest; - } else { - if (_.isUndefined(this.latestBlockNumber)) { - throw new Error(InternalZeroExError.LatestBlockNumberNotSet); - } - // Latest block already has 1 confirmation - blockNumber = this.latestBlockNumber - this.numConfirmations + 1; - } - return blockNumber; - } - public async startAsync(): Promise<void> { - if (this.numConfirmations === 0 || this.numConfirmations === 1) { - return; // no-op - } - await this.updateLatestBlockAsync(); - this.intervalId = intervalUtils.setAsyncExcludingInterval( - this.updateLatestBlockAsync.bind(this), this.blockPollingIntervalMs, - ); - } - public stop(): void { - if (!_.isUndefined(this.intervalId)) { - intervalUtils.clearAsyncExcludingInterval(this.intervalId); - } - } - private async updateLatestBlockAsync(): Promise<void> { - if (_.isUndefined(this.web3Wrapper)) { - throw new Error(InternalZeroExError.Web3WrapperRequiredToStartBlockStore); - } - const block = await this.web3Wrapper.getBlockAsync(BlockParamLiteral.Latest); - if (_.isNull(block.number)) { - throw new Error(ZeroExError.FailedToFetchLatestBlock); - } - this.latestBlockNumber = block.number; - } -} |