Remove blockStore and default to numConfirmations === 0
This commit is contained in:
@@ -11,7 +11,6 @@ import {AbiDecoder} from '../utils/abi_decoder';
|
||||
import {intervalUtils} from '../utils/interval_utils';
|
||||
import {assert} from '../utils/assert';
|
||||
import {utils} from '../utils/utils';
|
||||
import {BlockStore} from '../stores/block_store';
|
||||
|
||||
const DEFAULT_EVENT_POLLING_INTERVAL = 200;
|
||||
|
||||
@@ -29,10 +28,8 @@ export class EventWatcher {
|
||||
private _pollingIntervalMs: number;
|
||||
private _intervalIdIfExists?: NodeJS.Timer;
|
||||
private _lastEvents: Web3.LogEntry[] = [];
|
||||
private _blockStore: BlockStore;
|
||||
constructor(web3Wrapper: Web3Wrapper, blockStore: BlockStore, pollingIntervalMs: undefined|number) {
|
||||
constructor(web3Wrapper: Web3Wrapper, pollingIntervalMs: undefined|number) {
|
||||
this._web3Wrapper = web3Wrapper;
|
||||
this._blockStore = blockStore;
|
||||
this._pollingIntervalMs = _.isUndefined(pollingIntervalMs) ?
|
||||
DEFAULT_EVENT_POLLING_INTERVAL :
|
||||
pollingIntervalMs;
|
||||
@@ -68,10 +65,9 @@ export class EventWatcher {
|
||||
this._lastEvents = pendingEvents;
|
||||
}
|
||||
private async _getEventsAsync(): Promise<Web3.LogEntry[]> {
|
||||
const blockNumber = this._blockStore.getBlockNumber();
|
||||
const eventFilter = {
|
||||
fromBlock: blockNumber,
|
||||
toBlock: blockNumber,
|
||||
fromBlock: BlockParamLiteral.Pending,
|
||||
toBlock: BlockParamLiteral.Pending,
|
||||
};
|
||||
const events = await this._web3Wrapper.getLogsAsync(eventFilter);
|
||||
return events;
|
||||
|
||||
@@ -26,7 +26,6 @@ import {
|
||||
ZeroExError,
|
||||
} from '../types';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
import {BlockStore} from '../stores/block_store';
|
||||
import {TokenWrapper} from '../contract_wrappers/token_wrapper';
|
||||
import {ExchangeWrapper} from '../contract_wrappers/exchange_wrapper';
|
||||
import {OrderFilledCancelledLazyStore} from '../stores/order_filled_cancelled_lazy_store';
|
||||
@@ -60,7 +59,6 @@ export class OrderStateWatcher {
|
||||
private _exchange: ExchangeWrapper;
|
||||
private _abiDecoder: AbiDecoder;
|
||||
private _orderStateUtils: OrderStateUtils;
|
||||
private _blockStore: BlockStore;
|
||||
private _orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore;
|
||||
private _balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore;
|
||||
constructor(
|
||||
@@ -75,19 +73,11 @@ export class OrderStateWatcher {
|
||||
this._dependentOrderHashes = {};
|
||||
const eventPollingIntervalMs = _.isUndefined(config) ? undefined : config.eventPollingIntervalMs;
|
||||
const blockPollingIntervalMs = _.isUndefined(config) ? undefined : config.blockPollingIntervalMs;
|
||||
const numConfirmations = (_.isUndefined(config) || _.isUndefined(config.numConfirmations)) ?
|
||||
DEFAULT_NUM_CONFIRMATIONS :
|
||||
config.numConfirmations;
|
||||
this._blockStore = new BlockStore(numConfirmations, this._web3Wrapper, blockPollingIntervalMs);
|
||||
this._eventWatcher = new EventWatcher(
|
||||
web3Wrapper, this._blockStore, eventPollingIntervalMs,
|
||||
);
|
||||
this._eventWatcher = new EventWatcher(web3Wrapper, eventPollingIntervalMs);
|
||||
this._balanceAndProxyAllowanceLazyStore = new BalanceAndProxyAllowanceLazyStore(
|
||||
this._token, this._blockStore,
|
||||
);
|
||||
this._orderFilledCancelledLazyStore = new OrderFilledCancelledLazyStore(
|
||||
this._exchange, this._blockStore,
|
||||
this._token,
|
||||
);
|
||||
this._orderFilledCancelledLazyStore = new OrderFilledCancelledLazyStore(this._exchange);
|
||||
this._orderStateUtils = new OrderStateUtils(
|
||||
this._balanceAndProxyAllowanceLazyStore, this._orderFilledCancelledLazyStore,
|
||||
);
|
||||
@@ -123,12 +113,11 @@ export class OrderStateWatcher {
|
||||
* @param callback Receives the orderHash of the order that should be re-validated, together
|
||||
* with all the order-relevant blockchain state needed to re-validate the order.
|
||||
*/
|
||||
public async subscribeAsync(callback: OnOrderStateChangeCallback): Promise<void> {
|
||||
public subscribe(callback: OnOrderStateChangeCallback): void {
|
||||
assert.isFunction('callback', callback);
|
||||
if (!_.isUndefined(this._callbackIfExistsAsync)) {
|
||||
throw new Error(ZeroExError.SubscriptionAlreadyPresent);
|
||||
}
|
||||
await this._blockStore.startAsync();
|
||||
this._callbackIfExistsAsync = callback;
|
||||
this._eventWatcher.subscribe(this._onEventWatcherCallbackAsync.bind(this));
|
||||
}
|
||||
@@ -139,7 +128,6 @@ export class OrderStateWatcher {
|
||||
if (_.isUndefined(this._callbackIfExistsAsync)) {
|
||||
throw new Error(ZeroExError.SubscriptionNotFound);
|
||||
}
|
||||
this._blockStore.stop();
|
||||
this._balanceAndProxyAllowanceLazyStore.deleteAll();
|
||||
this._orderFilledCancelledLazyStore.deleteAll();
|
||||
delete this._callbackIfExistsAsync;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -402,12 +402,10 @@ export interface JSONRPCPayload {
|
||||
/*
|
||||
* eventPollingIntervalMs: How often to poll the Ethereum node for new events
|
||||
* blockPollingIntervalMs: How often to poll the Ethereum node for new blocks
|
||||
* numConfirmations: How many confirmed blocks deep you wish to listen for events at.
|
||||
*/
|
||||
export interface OrderStateWatcherConfig {
|
||||
eventPollingIntervalMs?: number;
|
||||
blockPollingIntervalMs?: number;
|
||||
numConfirmations?: number;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -3,7 +3,6 @@ import BigNumber from 'bignumber.js';
|
||||
import {ExchangeContractErrs, TradeSide, TransferType, BlockParamLiteral} from '../types';
|
||||
import {TokenWrapper} from '../contract_wrappers/token_wrapper';
|
||||
import {BalanceAndProxyAllowanceLazyStore} from '../stores/balance_proxy_allowance_lazy_store';
|
||||
import {BlockStore} from '../stores/block_store';
|
||||
|
||||
enum FailureReason {
|
||||
Balance = 'balance',
|
||||
@@ -37,9 +36,7 @@ export class ExchangeTransferSimulator {
|
||||
private store: BalanceAndProxyAllowanceLazyStore;
|
||||
private UNLIMITED_ALLOWANCE_IN_BASE_UNITS: BigNumber;
|
||||
constructor(token: TokenWrapper) {
|
||||
const latestBlockConfirmationNumber = 1;
|
||||
const blockStore = new BlockStore(latestBlockConfirmationNumber);
|
||||
this.store = new BalanceAndProxyAllowanceLazyStore(token, blockStore);
|
||||
this.store = new BalanceAndProxyAllowanceLazyStore(token);
|
||||
this.UNLIMITED_ALLOWANCE_IN_BASE_UNITS = token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS;
|
||||
}
|
||||
/**
|
||||
|
||||
@@ -8,7 +8,6 @@ import {chaiSetup} from './utils/chai_setup';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
import {Web3Wrapper} from '../src/web3_wrapper';
|
||||
import {EventWatcher} from '../src/order_watcher/event_watcher';
|
||||
import {BlockStore} from '../src/stores/block_store';
|
||||
import {
|
||||
ZeroEx,
|
||||
LogEvent,
|
||||
@@ -24,7 +23,6 @@ describe('EventWatcher', () => {
|
||||
let stubs: Sinon.SinonStub[] = [];
|
||||
let eventWatcher: EventWatcher;
|
||||
let web3Wrapper: Web3Wrapper;
|
||||
let blockStore: BlockStore;
|
||||
const numConfirmations = 0;
|
||||
const logA: Web3.LogEntry = {
|
||||
address: '0x71d271f8b14adef568f8f28f1587ce7271ac4ca5',
|
||||
@@ -60,8 +58,7 @@ describe('EventWatcher', () => {
|
||||
web3 = web3Factory.create();
|
||||
const pollingIntervalMs = 10;
|
||||
web3Wrapper = new Web3Wrapper(web3.currentProvider);
|
||||
blockStore = new BlockStore(numConfirmations);
|
||||
eventWatcher = new EventWatcher(web3Wrapper, blockStore, pollingIntervalMs);
|
||||
eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalMs);
|
||||
});
|
||||
afterEach(() => {
|
||||
// clean up any stubs after the test has completed
|
||||
|
||||
@@ -94,8 +94,8 @@ describe('OrderStateWatcher', () => {
|
||||
zeroEx.orderStateWatcher.unsubscribe();
|
||||
});
|
||||
it('should fail when trying to subscribe twice', async () => {
|
||||
await zeroEx.orderStateWatcher.subscribeAsync(_.noop);
|
||||
return expect(zeroEx.orderStateWatcher.subscribeAsync(_.noop))
|
||||
await zeroEx.orderStateWatcher.subscribe(_.noop);
|
||||
return expect(zeroEx.orderStateWatcher.subscribe(_.noop))
|
||||
.to.be.rejectedWith(ZeroExError.SubscriptionAlreadyPresent);
|
||||
});
|
||||
});
|
||||
@@ -119,7 +119,7 @@ describe('OrderStateWatcher', () => {
|
||||
expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.InsufficientMakerAllowance);
|
||||
done();
|
||||
});
|
||||
await zeroEx.orderStateWatcher.subscribeAsync(callback);
|
||||
await zeroEx.orderStateWatcher.subscribe(callback);
|
||||
await zeroEx.token.setProxyAllowanceAsync(makerToken.address, maker, new BigNumber(0));
|
||||
})().catch(done);
|
||||
});
|
||||
@@ -158,7 +158,7 @@ describe('OrderStateWatcher', () => {
|
||||
expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.InsufficientMakerBalance);
|
||||
done();
|
||||
});
|
||||
await zeroEx.orderStateWatcher.subscribeAsync(callback);
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
const anyRecipient = taker;
|
||||
const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker);
|
||||
await zeroEx.token.transferAsync(makerToken.address, maker, anyRecipient, makerBalance);
|
||||
@@ -183,7 +183,7 @@ describe('OrderStateWatcher', () => {
|
||||
done();
|
||||
}
|
||||
});
|
||||
await zeroEx.orderStateWatcher.subscribeAsync(callback);
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
|
||||
const shouldThrowOnInsufficientBalanceOrAllowance = true;
|
||||
await zeroEx.exchange.fillOrderAsync(
|
||||
@@ -220,7 +220,7 @@ describe('OrderStateWatcher', () => {
|
||||
done();
|
||||
}
|
||||
});
|
||||
await zeroEx.orderStateWatcher.subscribeAsync(callback);
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
const shouldThrowOnInsufficientBalanceOrAllowance = true;
|
||||
await zeroEx.exchange.fillOrderAsync(
|
||||
signedOrder, fillAmountInBaseUnits, shouldThrowOnInsufficientBalanceOrAllowance, taker,
|
||||
@@ -321,7 +321,7 @@ describe('OrderStateWatcher', () => {
|
||||
expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.OrderRemainingFillAmountZero);
|
||||
done();
|
||||
});
|
||||
await zeroEx.orderStateWatcher.subscribeAsync(callback);
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
|
||||
const shouldThrowOnInsufficientBalanceOrAllowance = true;
|
||||
await zeroEx.exchange.cancelOrderAsync(signedOrder, fillableAmount);
|
||||
@@ -348,7 +348,7 @@ describe('OrderStateWatcher', () => {
|
||||
expect(orderRelevantState.canceledTakerTokenAmount).to.be.bignumber.equal(cancelAmountInBaseUnits);
|
||||
done();
|
||||
});
|
||||
await zeroEx.orderStateWatcher.subscribeAsync(callback);
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
await zeroEx.exchange.cancelOrderAsync(signedOrder, cancelAmountInBaseUnits);
|
||||
})().catch(done);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user