Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

[order-watcher] Public function getStats #1118

Merged
merged 10 commits into from
Oct 15, 2018
9 changes: 9 additions & 0 deletions packages/order-watcher/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
[
{
"version": "2.1.2",
"changes": [
{
"note": "Added getStats function and returns a Stats object",
"pr": 1118
}
amaurer marked this conversation as resolved.
Show resolved Hide resolved
]
},
{
"version": "2.1.1",
"changes": [
Expand Down
1 change: 1 addition & 0 deletions packages/order-watcher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
OrderState,
ExchangeContractErrs,
OrderRelevantState,
Stats,
} from '@0xproject/types';

export { OnOrderStateChangeCallback, OrderWatcherConfig } from './types';
Expand Down
10 changes: 9 additions & 1 deletion packages/order-watcher/src/order_watcher/order_watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
orderHashUtils,
OrderStateUtils,
} from '@0xproject/order-utils';
import { AssetProxyId, ExchangeContractErrs, OrderState, SignedOrder } from '@0xproject/types';
import { AssetProxyId, ExchangeContractErrs, OrderState, SignedOrder, Stats } from '@0xproject/types';
import { errorUtils, intervalUtils } from '@0xproject/utils';
import { BlockParamLiteral, LogEntryEvent, LogWithDecodedArgs, Provider } from 'ethereum-types';
import * as _ from 'lodash';
Expand Down Expand Up @@ -213,6 +213,14 @@ export class OrderWatcher {
this._expirationWatcher.unsubscribe();
intervalUtils.clearAsyncExcludingInterval(this._cleanupJobIntervalIdIfExists);
}
/**
* Gets statistics of the OrderWatcher Instance.
*/
public getStats(): Stats {
return {
orderCount: _.size(this._orderByOrderHash),
};
}
private async _cleanupAsync(): Promise<void> {
for (const orderHash of _.keys(this._orderByOrderHash)) {
this._cleanupOrderRelatedState(orderHash);
Expand Down
17 changes: 17 additions & 0 deletions packages/order-watcher/test/order_watcher_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,23 @@ describe('OrderWatcher', () => {
expect(() => orderWatcher.subscribe(_.noop.bind(_))).to.throw(OrderWatcherError.SubscriptionAlreadyPresent);
});
});
describe('#getStats', async () => {
it('orderCount should increment and decrement with order additions and removals', async () => {
signedOrder = await fillScenarios.createFillableSignedOrderAsync(
makerAssetData,
takerAssetData,
makerAddress,
takerAddress,
fillableAmount,
);
const orderHash = orderHashUtils.getOrderHashHex(signedOrder);
expect(orderWatcher.getStats().orderCount).to.be.eq(0);
await orderWatcher.addOrderAsync(signedOrder);
expect(orderWatcher.getStats().orderCount).to.be.eq(1);
orderWatcher.removeOrder(orderHash);
expect(orderWatcher.getStats().orderCount).to.be.eq(0);
});
});
describe('tests with cleanup', async () => {
afterEach(async () => {
orderWatcher.unsubscribe();
Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,7 @@ export interface EIP712TypedData {
message: EIP712Object;
primaryType: string;
}

export interface Stats {
orderCount: number;
}