diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-11-11 04:04:14 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-11-13 09:03:18 +0800 |
commit | 473ce8b61750103843173518bac599186405f7b3 (patch) | |
tree | 9bfa1efedc90d1ed73eae048619fea4d568d985a | |
parent | 70436fa535f51eca5c1b5951e1218e72f9a767e0 (diff) | |
download | dexon-sol-tools-473ce8b61750103843173518bac599186405f7b3.tar dexon-sol-tools-473ce8b61750103843173518bac599186405f7b3.tar.gz dexon-sol-tools-473ce8b61750103843173518bac599186405f7b3.tar.bz2 dexon-sol-tools-473ce8b61750103843173518bac599186405f7b3.tar.lz dexon-sol-tools-473ce8b61750103843173518bac599186405f7b3.tar.xz dexon-sol-tools-473ce8b61750103843173518bac599186405f7b3.tar.zst dexon-sol-tools-473ce8b61750103843173518bac599186405f7b3.zip |
Add initial incomplete BlockStore implementation
-rw-r--r-- | src/stores/block_store.ts | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/stores/block_store.ts b/src/stores/block_store.ts new file mode 100644 index 000000000..d1b4d3c54 --- /dev/null +++ b/src/stores/block_store.ts @@ -0,0 +1,38 @@ +import * as _ from 'lodash'; +import * as Web3 from 'web3'; +import {BigNumber} from 'bignumber.js'; +import {BlockParamLiteral, InternalZeroExError} from '../types'; +import {Web3Wrapper} from '../web3_wrapper'; + +/** + * Store for a current latest block number + */ +export class BlockStore { + private web3Wrapper?: Web3Wrapper; + private latestBlockNumber?: number; + constructor(web3Wrapper?: Web3Wrapper) { + this.web3Wrapper = web3Wrapper; + // TODO start a subscription + } + public getBlockNumberWithNConfirmations(numConfirmations: number): Web3.BlockParam { + let blockNumber; + if (numConfirmations === 0) { + blockNumber = BlockParamLiteral.Pending; + } else if (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 - numConfirmations + 1; + } + return blockNumber; + } + public setLatestBlock(latestBlockNumber: number): void { + this.latestBlockNumber = latestBlockNumber; + } +} |