aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2017-11-11 05:32:23 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2017-11-13 09:06:13 +0800
commit6bcd9adb9e41ccc45ef5e117df243526bbd281ec (patch)
tree2dbaad897aa6d8997fed17350d008c2df67cb882 /src/stores
parent61e7b735dcae37195e4b306cab3e4c50cd9c3ba5 (diff)
downloaddexon-sol-tools-6bcd9adb9e41ccc45ef5e117df243526bbd281ec.tar
dexon-sol-tools-6bcd9adb9e41ccc45ef5e117df243526bbd281ec.tar.gz
dexon-sol-tools-6bcd9adb9e41ccc45ef5e117df243526bbd281ec.tar.bz2
dexon-sol-tools-6bcd9adb9e41ccc45ef5e117df243526bbd281ec.tar.lz
dexon-sol-tools-6bcd9adb9e41ccc45ef5e117df243526bbd281ec.tar.xz
dexon-sol-tools-6bcd9adb9e41ccc45ef5e117df243526bbd281ec.tar.zst
dexon-sol-tools-6bcd9adb9e41ccc45ef5e117df243526bbd281ec.zip
Make subscribe function async and make blockStore operational
Diffstat (limited to 'src/stores')
-rw-r--r--src/stores/block_store.ts29
1 files changed, 25 insertions, 4 deletions
diff --git a/src/stores/block_store.ts b/src/stores/block_store.ts
index d1b4d3c54..70798a999 100644
--- a/src/stores/block_store.ts
+++ b/src/stores/block_store.ts
@@ -1,8 +1,11 @@
import * as _ from 'lodash';
import * as Web3 from 'web3';
import {BigNumber} from 'bignumber.js';
-import {BlockParamLiteral, InternalZeroExError} from '../types';
+import {BlockParamLiteral, InternalZeroExError, ZeroExError} from '../types';
import {Web3Wrapper} from '../web3_wrapper';
+import {intervalUtils} from '../utils/interval_utils';
+
+const POLLING_INTERVAL_MS = 500;
/**
* Store for a current latest block number
@@ -10,9 +13,9 @@ import {Web3Wrapper} from '../web3_wrapper';
export class BlockStore {
private web3Wrapper?: Web3Wrapper;
private latestBlockNumber?: number;
+ private intervalId?: NodeJS.Timer;
constructor(web3Wrapper?: Web3Wrapper) {
this.web3Wrapper = web3Wrapper;
- // TODO start a subscription
}
public getBlockNumberWithNConfirmations(numConfirmations: number): Web3.BlockParam {
let blockNumber;
@@ -32,7 +35,25 @@ export class BlockStore {
}
return blockNumber;
}
- public setLatestBlock(latestBlockNumber: number): void {
- this.latestBlockNumber = latestBlockNumber;
+ public async startAsync(): Promise<void> {
+ await this.updateLatestBlockAsync();
+ this.intervalId = intervalUtils.setAsyncExcludingInterval(
+ this.updateLatestBlockAsync.bind(this), POLLING_INTERVAL_MS,
+ );
+ }
+ 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;
}
}