aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2017-11-21 03:47:09 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2017-11-21 03:47:09 +0800
commit71475d3ceafd8f1a4a76d1e5b49ff0d186bacd9b (patch)
treeaf69b9b067a83d580e6e6d73f9ff42aed37f3354
parenta613c3b7e7654728cb56cceb67566ab75313e16f (diff)
downloaddexon-0x-contracts-71475d3ceafd8f1a4a76d1e5b49ff0d186bacd9b.tar.gz
dexon-0x-contracts-71475d3ceafd8f1a4a76d1e5b49ff0d186bacd9b.tar.zst
dexon-0x-contracts-71475d3ceafd8f1a4a76d1e5b49ff0d186bacd9b.zip
Add expirationMarginMs
-rw-r--r--packages/0x.js/src/order_watcher/expiration_watcher.ts19
-rw-r--r--packages/0x.js/src/order_watcher/order_state_watcher.ts10
-rw-r--r--packages/0x.js/src/types.ts4
-rw-r--r--packages/0x.js/src/utils/utils.ts7
4 files changed, 30 insertions, 10 deletions
diff --git a/packages/0x.js/src/order_watcher/expiration_watcher.ts b/packages/0x.js/src/order_watcher/expiration_watcher.ts
index cf0222e3c..71199e75f 100644
--- a/packages/0x.js/src/order_watcher/expiration_watcher.ts
+++ b/packages/0x.js/src/order_watcher/expiration_watcher.ts
@@ -6,6 +6,7 @@ import {SignedOrder, ZeroExError} from '../types';
import {Heap} from '../utils/heap';
import {ZeroEx} from '../0x';
+const DEFAULT_EXPIRATION_MARGIN_MS = 0;
const DEFAULT_ORDER_EXPIRATION_CHECKING_INTERVAL_MS = 50;
/**
@@ -17,9 +18,13 @@ export class ExpirationWatcher {
private expiration: {[orderHash: string]: BigNumber} = {};
private callbackIfExists?: (orderHash: string) => void;
private orderExpirationCheckingIntervalMs: number;
+ private expirationMarginMs: number;
private orderExpirationCheckingIntervalIdIfExists?: NodeJS.Timer;
- constructor(orderExpirationCheckingIntervalMsIfExists?: number) {
- this.orderExpirationCheckingIntervalMs = orderExpirationCheckingIntervalMsIfExists ||
+ constructor(expirationMarginIfExistsMs?: number,
+ orderExpirationCheckingIntervalIfExistsMs?: number) {
+ this.expirationMarginMs = expirationMarginIfExistsMs ||
+ DEFAULT_ORDER_EXPIRATION_CHECKING_INTERVAL_MS;
+ this.orderExpirationCheckingIntervalMs = expirationMarginIfExistsMs ||
DEFAULT_ORDER_EXPIRATION_CHECKING_INTERVAL_MS;
const scoreFunction = (orderHash: string) => this.expiration[orderHash].toNumber();
this.orderHashHeapByExpiration = new Heap(scoreFunction);
@@ -41,17 +46,19 @@ export class ExpirationWatcher {
delete this.callbackIfExists;
delete this.orderExpirationCheckingIntervalIdIfExists;
}
- public addOrder(orderHash: string, expirationUnixTimestampSec: BigNumber): void {
- this.expiration[orderHash] = expirationUnixTimestampSec;
+ public addOrder(orderHash: string, expirationUnixTimestampMs: BigNumber): void {
+ this.expiration[orderHash] = expirationUnixTimestampMs;
// We don't remove hashes from the heap on order remove because it's slow (linear).
// We just skip them later if the order was already removed from the order watcher.
this.orderHashHeapByExpiration.push(orderHash);
}
private pruneExpiredOrders(): void {
- const currentUnixTimestampSec = utils.getCurrentUnixTimestamp();
+ const currentUnixTimestampMs = utils.getCurrentUnixTimestampMs();
while (
this.orderHashHeapByExpiration.size() !== 0 &&
- this.expiration[this.orderHashHeapByExpiration.head()].lessThan(currentUnixTimestampSec) &&
+ this.expiration[this.orderHashHeapByExpiration.head()].lessThan(
+ currentUnixTimestampMs.plus(this.expirationMarginMs),
+ ) &&
!_.isUndefined(this.callbackIfExists)
) {
const orderHash = this.orderHashHeapByExpiration.pop();
diff --git a/packages/0x.js/src/order_watcher/order_state_watcher.ts b/packages/0x.js/src/order_watcher/order_state_watcher.ts
index 27f4c7d29..3659fc6e2 100644
--- a/packages/0x.js/src/order_watcher/order_state_watcher.ts
+++ b/packages/0x.js/src/order_watcher/order_state_watcher.ts
@@ -77,7 +77,12 @@ export class OrderStateWatcher {
const orderExpirationCheckingIntervalMsIfExists = _.isUndefined(config) ?
undefined :
config.orderExpirationCheckingIntervalMs;
- this._expirationWatcher = new ExpirationWatcher(orderExpirationCheckingIntervalMsIfExists);
+ const expirationMarginIfExistsMs = _.isUndefined(config) ?
+ undefined :
+ config.expirationMarginMs;
+ this._expirationWatcher = new ExpirationWatcher(
+ expirationMarginIfExistsMs, orderExpirationCheckingIntervalMsIfExists,
+ );
}
/**
* Add an order to the orderStateWatcher. Before the order is added, it's
@@ -91,7 +96,8 @@ export class OrderStateWatcher {
this._orderByOrderHash[orderHash] = signedOrder;
this.addToDependentOrderHashes(signedOrder, orderHash);
// We don't remove orders from expirationWatcher because heap removal is linear. We just skip it later
- this._expirationWatcher.addOrder(orderHash, signedOrder.expirationUnixTimestampSec);
+ const expirationUnixTimestampMs = signedOrder.expirationUnixTimestampSec.times(1000);
+ this._expirationWatcher.addOrder(orderHash, expirationUnixTimestampMs);
}
/**
* Removes an order from the orderStateWatcher
diff --git a/packages/0x.js/src/types.ts b/packages/0x.js/src/types.ts
index 37e80e6bc..13a46659d 100644
--- a/packages/0x.js/src/types.ts
+++ b/packages/0x.js/src/types.ts
@@ -396,14 +396,18 @@ export interface JSONRPCPayload {
method: string;
}
+// tslint:disable:max-line-length
/*
* orderExpirationCheckingIntervalMs: How often to check for expired orders. Default: 50
* eventPollingIntervalMs: How often to poll the Ethereum node for new events. Defaults: 200
+ * expirationMarginMs: Amount of time before order expiry that you'd like to be notified of an orders expiration. Defaults: 0
*/
export interface OrderStateWatcherConfig {
orderExpirationCheckingIntervalMs?: number;
eventPollingIntervalMs?: number;
+ expirationMarginMs?: number;
}
+// tslint:enable:max-line-length
/*
* gasPrice: Gas price to use with every transaction
diff --git a/packages/0x.js/src/utils/utils.ts b/packages/0x.js/src/utils/utils.ts
index 280f3e979..5370c3b4b 100644
--- a/packages/0x.js/src/utils/utils.ts
+++ b/packages/0x.js/src/utils/utils.ts
@@ -49,7 +49,10 @@ export const utils = {
const hashHex = ethUtil.bufferToHex(hashBuff);
return hashHex;
},
- getCurrentUnixTimestamp(): BigNumber {
- return new BigNumber(Date.now() / 1000);
+ getCurrentUnixTimestampSec(): BigNumber {
+ return new BigNumber(Date.now() / 1000).round();
+ },
+ getCurrentUnixTimestampMs(): BigNumber {
+ return new BigNumber(Date.now());
},
};