aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2017-11-13 07:10:47 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2017-11-13 09:06:13 +0800
commit84c965d459b948eb03cb8a70a66663bd7b35f463 (patch)
treefbcb47a43d4b029c2f1a46644eb64ca1d75f5921 /src/stores
parent22cd6989a0217f2a49e59ce64bcc69b2c238aba4 (diff)
downloaddexon-sol-tools-84c965d459b948eb03cb8a70a66663bd7b35f463.tar
dexon-sol-tools-84c965d459b948eb03cb8a70a66663bd7b35f463.tar.gz
dexon-sol-tools-84c965d459b948eb03cb8a70a66663bd7b35f463.tar.bz2
dexon-sol-tools-84c965d459b948eb03cb8a70a66663bd7b35f463.tar.lz
dexon-sol-tools-84c965d459b948eb03cb8a70a66663bd7b35f463.tar.xz
dexon-sol-tools-84c965d459b948eb03cb8a70a66663bd7b35f463.tar.zst
dexon-sol-tools-84c965d459b948eb03cb8a70a66663bd7b35f463.zip
Remove blockStore and default to numConfirmations === 0
Diffstat (limited to 'src/stores')
-rw-r--r--src/stores/balance_proxy_allowance_lazy_store.ts12
-rw-r--r--src/stores/block_store.ts66
-rw-r--r--src/stores/order_filled_cancelled_lazy_store.ts12
3 files changed, 8 insertions, 82 deletions
diff --git a/src/stores/balance_proxy_allowance_lazy_store.ts b/src/stores/balance_proxy_allowance_lazy_store.ts
index 88152e145..5c54fbb3b 100644
--- a/src/stores/balance_proxy_allowance_lazy_store.ts
+++ b/src/stores/balance_proxy_allowance_lazy_store.ts
@@ -2,14 +2,13 @@ import * as _ from 'lodash';
import * as Web3 from 'web3';
import {BigNumber} from 'bignumber.js';
import {TokenWrapper} from '../contract_wrappers/token_wrapper';
-import {BlockStore} from './block_store';
+import {BlockParamLiteral} from '../types';
/**
* Copy on read store for balances/proxyAllowances of tokens/accounts
*/
export class BalanceAndProxyAllowanceLazyStore {
private token: TokenWrapper;
- private blockStore: BlockStore;
private balance: {
[tokenAddress: string]: {
[userAddress: string]: BigNumber,
@@ -20,17 +19,15 @@ export class BalanceAndProxyAllowanceLazyStore {
[userAddress: string]: BigNumber,
},
};
- constructor(token: TokenWrapper, blockStore: BlockStore) {
+ constructor(token: TokenWrapper) {
this.token = token;
- this.blockStore = blockStore;
this.balance = {};
this.proxyAllowance = {};
}
public async getBalanceAsync(tokenAddress: string, userAddress: string): Promise<BigNumber> {
if (_.isUndefined(this.balance[tokenAddress]) || _.isUndefined(this.balance[tokenAddress][userAddress])) {
- const defaultBlock = this.blockStore.getBlockNumber();
const methodOpts = {
- defaultBlock,
+ defaultBlock: BlockParamLiteral.Pending,
};
const balance = await this.token.getBalanceAsync(tokenAddress, userAddress, methodOpts);
this.setBalance(tokenAddress, userAddress, balance);
@@ -52,9 +49,8 @@ export class BalanceAndProxyAllowanceLazyStore {
public async getProxyAllowanceAsync(tokenAddress: string, userAddress: string): Promise<BigNumber> {
if (_.isUndefined(this.proxyAllowance[tokenAddress]) ||
_.isUndefined(this.proxyAllowance[tokenAddress][userAddress])) {
- const defaultBlock = this.blockStore.getBlockNumber();
const methodOpts = {
- defaultBlock,
+ defaultBlock: BlockParamLiteral.Pending,
};
const proxyAllowance = await this.token.getProxyAllowanceAsync(tokenAddress, userAddress, methodOpts);
this.setProxyAllowance(tokenAddress, userAddress, proxyAllowance);
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;
- }
-}
diff --git a/src/stores/order_filled_cancelled_lazy_store.ts b/src/stores/order_filled_cancelled_lazy_store.ts
index 104a8240e..39adff4d1 100644
--- a/src/stores/order_filled_cancelled_lazy_store.ts
+++ b/src/stores/order_filled_cancelled_lazy_store.ts
@@ -2,31 +2,28 @@ import * as _ from 'lodash';
import * as Web3 from 'web3';
import {BigNumber} from 'bignumber.js';
import {ExchangeWrapper} from '../contract_wrappers/exchange_wrapper';
-import {BlockStore} from './block_store';
+import {BlockParamLiteral} from '../types';
/**
* Copy on read store for filled/cancelled taker amounts
*/
export class OrderFilledCancelledLazyStore {
private exchange: ExchangeWrapper;
- private blockStore: BlockStore;
private filledTakerAmount: {
[orderHash: string]: BigNumber,
};
private cancelledTakerAmount: {
[orderHash: string]: BigNumber,
};
- constructor(exchange: ExchangeWrapper, blockStore: BlockStore) {
+ constructor(exchange: ExchangeWrapper) {
this.exchange = exchange;
- this.blockStore = blockStore;
this.filledTakerAmount = {};
this.cancelledTakerAmount = {};
}
public async getFilledTakerAmountAsync(orderHash: string): Promise<BigNumber> {
if (_.isUndefined(this.filledTakerAmount[orderHash])) {
- const defaultBlock = this.blockStore.getBlockNumber();
const methodOpts = {
- defaultBlock,
+ defaultBlock: BlockParamLiteral.Pending,
};
const filledTakerAmount = await this.exchange.getFilledTakerAmountAsync(orderHash, methodOpts);
this.setFilledTakerAmount(orderHash, filledTakerAmount);
@@ -42,9 +39,8 @@ export class OrderFilledCancelledLazyStore {
}
public async getCancelledTakerAmountAsync(orderHash: string): Promise<BigNumber> {
if (_.isUndefined(this.cancelledTakerAmount[orderHash])) {
- const defaultBlock = this.blockStore.getBlockNumber();
const methodOpts = {
- defaultBlock,
+ defaultBlock: BlockParamLiteral.Pending,
};
const cancelledTakerAmount = await this.exchange.getCanceledTakerAmountAsync(orderHash, methodOpts);
this.setCancelledTakerAmount(orderHash, cancelledTakerAmount);