aboutsummaryrefslogtreecommitdiffstats
path: root/packages
diff options
context:
space:
mode:
authorfragosti <francesco.agosti93@gmail.com>2018-09-24 19:17:38 +0800
committerfragosti <francesco.agosti93@gmail.com>2018-09-24 19:17:38 +0800
commite4b664bafa24c2ee788a343edecd0124f784780d (patch)
tree54cb57eeba44a7140c326cb17624b3839c6e73d3 /packages
parent7a5376621f2c2503c6415802984b4d8ae05db723 (diff)
downloaddexon-sol-tools-e4b664bafa24c2ee788a343edecd0124f784780d.tar
dexon-sol-tools-e4b664bafa24c2ee788a343edecd0124f784780d.tar.gz
dexon-sol-tools-e4b664bafa24c2ee788a343edecd0124f784780d.tar.bz2
dexon-sol-tools-e4b664bafa24c2ee788a343edecd0124f784780d.tar.lz
dexon-sol-tools-e4b664bafa24c2ee788a343edecd0124f784780d.tar.xz
dexon-sol-tools-e4b664bafa24c2ee788a343edecd0124f784780d.tar.zst
dexon-sol-tools-e4b664bafa24c2ee788a343edecd0124f784780d.zip
Add expiry buffer
Diffstat (limited to 'packages')
-rw-r--r--packages/asset-buyer/src/asset_buyer.ts45
-rw-r--r--packages/asset-buyer/src/constants.ts1
-rw-r--r--packages/asset-buyer/src/utils/order_provider_response_processor.ts14
-rw-r--r--packages/asset-buyer/src/utils/order_utils.ts5
4 files changed, 51 insertions, 14 deletions
diff --git a/packages/asset-buyer/src/asset_buyer.ts b/packages/asset-buyer/src/asset_buyer.ts
index 03f9b5a2b..409e34e74 100644
--- a/packages/asset-buyer/src/asset_buyer.ts
+++ b/packages/asset-buyer/src/asset_buyer.ts
@@ -29,6 +29,7 @@ export class AssetBuyer {
public readonly orderProvider: OrderProvider;
public readonly networkId: number;
public readonly orderRefreshIntervalMs: number;
+ public readonly expiryBufferSeconds: number;
private readonly _contractWrappers: ContractWrappers;
private _lastRefreshTimeIfExists?: number;
private _currentOrdersAndFillableAmountsIfExists?: AssetBuyerOrdersAndFillableAmounts;
@@ -38,8 +39,9 @@ export class AssetBuyer {
* @param orders A non-empty array of objects that conform to SignedOrder. All orders must have the same makerAssetData and takerAssetData (WETH).
* @param feeOrders A array of objects that conform to SignedOrder. All orders must have the same makerAssetData (ZRX) and takerAssetData (WETH). Defaults to an empty array.
* @param networkId The ethereum network id. Defaults to 1 (mainnet).
- * @param orderRefreshIntervalMs The interval in ms that getBuyQuoteAsync should trigger an refresh of orders and order states.
- * Defaults to 10000ms (10s).
+ * @param orderRefreshIntervalMs The interval in ms that getBuyQuoteAsync should trigger an refresh of orders and order states. Defaults to 10000ms (10s).
+ * @param expiryBufferSeconds The number of seconds to add when calculating whether an order is expired or not. Defaults to 15s.
+ *
* @return An instance of AssetBuyer
*/
public static getAssetBuyerForProvidedOrders(
@@ -48,6 +50,7 @@ export class AssetBuyer {
feeOrders: SignedOrder[] = [],
networkId: number = constants.MAINNET_NETWORK_ID,
orderRefreshIntervalMs: number = constants.DEFAULT_ORDER_REFRESH_INTERVAL_MS,
+ expiryBufferSeconds: number = constants.DEFAULT_EXPIRY_BUFFER_SECONDS,
): AssetBuyer {
assert.isWeb3Provider('provider', provider);
assert.doesConformToSchema('orders', orders, schemas.signedOrdersSchema);
@@ -59,7 +62,14 @@ export class AssetBuyer {
assert.assert(orders.length !== 0, `Expected orders to contain at least one order`);
const assetData = orders[0].makerAssetData;
const orderProvider = new BasicOrderProvider(_.concat(orders, feeOrders));
- const assetBuyer = new AssetBuyer(provider, assetData, orderProvider, networkId, orderRefreshIntervalMs);
+ const assetBuyer = new AssetBuyer(
+ provider,
+ assetData,
+ orderProvider,
+ networkId,
+ orderRefreshIntervalMs,
+ expiryBufferSeconds,
+ );
return assetBuyer;
}
/**
@@ -68,8 +78,9 @@ export class AssetBuyer {
* @param assetData The assetData that identifies the desired asset to buy.
* @param sraApiUrl The standard relayer API base HTTP url you would like to source orders from.
* @param networkId The ethereum network id. Defaults to 1 (mainnet).
- * @param orderRefreshIntervalMs The interval in ms that getBuyQuoteAsync should trigger an refresh of orders and order states.
- * Defaults to 10000ms (10s).
+ * @param orderRefreshIntervalMs The interval in ms that getBuyQuoteAsync should trigger an refresh of orders and order states. Defaults to 10000ms (10s).
+ * @param expiryBufferSeconds The number of seconds to add when calculating whether an order is expired or not. Defaults to 15s.
+ *
* @return An instance of AssetBuyer
*/
public static getAssetBuyerForAssetData(
@@ -78,6 +89,7 @@ export class AssetBuyer {
sraApiUrl: string,
networkId: number = constants.MAINNET_NETWORK_ID,
orderRefreshIntervalMs: number = constants.DEFAULT_ORDER_REFRESH_INTERVAL_MS,
+ expiryBufferSeconds: number = constants.DEFAULT_EXPIRY_BUFFER_SECONDS,
): AssetBuyer {
assert.isWeb3Provider('provider', provider);
assert.isHexString('assetData', assetData);
@@ -85,7 +97,14 @@ export class AssetBuyer {
assert.isNumber('networkId', networkId);
assert.isNumber('orderRefreshIntervalMs', orderRefreshIntervalMs);
const orderProvider = new StandardRelayerAPIOrderProvider(sraApiUrl);
- const assetBuyer = new AssetBuyer(provider, assetData, orderProvider, networkId, orderRefreshIntervalMs);
+ const assetBuyer = new AssetBuyer(
+ provider,
+ assetData,
+ orderProvider,
+ networkId,
+ orderRefreshIntervalMs,
+ expiryBufferSeconds,
+ );
return assetBuyer;
}
/**
@@ -94,8 +113,8 @@ export class AssetBuyer {
* @param tokenAddress The ERC20 token address that identifies the desired asset to buy.
* @param sraApiUrl The standard relayer API base HTTP url you would like to source orders from.
* @param networkId The ethereum network id. Defaults to 1 (mainnet).
- * @param orderRefreshIntervalMs The interval in ms that getBuyQuoteAsync should trigger an refresh of orders and order states.
- * Defaults to 10000ms (10s).
+ * @param orderRefreshIntervalMs The interval in ms that getBuyQuoteAsync should trigger an refresh of orders and order states. Defaults to 10000ms (10s).
+ * @param expiryBufferSeconds The number of seconds to add when calculating whether an order is expired or not. Defaults to 15s.
* @return An instance of AssetBuyer
*/
public static getAssetBuyerForERC20TokenAddress(
@@ -104,6 +123,7 @@ export class AssetBuyer {
sraApiUrl: string,
networkId: number = constants.MAINNET_NETWORK_ID,
orderRefreshIntervalMs: number = constants.DEFAULT_ORDER_REFRESH_INTERVAL_MS,
+ expiryBufferSeconds: number = constants.DEFAULT_EXPIRY_BUFFER_SECONDS,
): AssetBuyer {
assert.isWeb3Provider('provider', provider);
assert.isETHAddressHex('tokenAddress', tokenAddress);
@@ -117,6 +137,7 @@ export class AssetBuyer {
sraApiUrl,
networkId,
orderRefreshIntervalMs,
+ expiryBufferSeconds,
);
return assetBuyer;
}
@@ -126,8 +147,9 @@ export class AssetBuyer {
* @param assetData The assetData of the desired asset to buy (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md).
* @param orderProvider An object that conforms to OrderProvider, see type for definition.
* @param networkId The ethereum network id. Defaults to 1 (mainnet).
- * @param orderRefreshIntervalMs The interval in ms that getBuyQuoteAsync should trigger an refresh of orders and order states.
- * Defaults to 10000ms (10s).
+ * @param orderRefreshIntervalMs The interval in ms that getBuyQuoteAsync should trigger an refresh of orders and order states. Defaults to 10000ms (10s).
+ * @param expiryBufferSeconds The number of seconds to add when calculating whether an order is expired or not. Defaults to 15s.
+ *
* @return An instance of AssetBuyer
*/
constructor(
@@ -136,6 +158,7 @@ export class AssetBuyer {
orderProvider: OrderProvider,
networkId: number = constants.MAINNET_NETWORK_ID,
orderRefreshIntervalMs: number = constants.DEFAULT_ORDER_REFRESH_INTERVAL_MS,
+ expiryBufferSeconds: number = constants.DEFAULT_EXPIRY_BUFFER_SECONDS,
) {
assert.isWeb3Provider('provider', provider);
assert.isString('assetData', assetData);
@@ -146,6 +169,7 @@ export class AssetBuyer {
this.assetData = assetData;
this.orderProvider = orderProvider;
this.networkId = networkId;
+ this.expiryBufferSeconds = expiryBufferSeconds;
this.orderRefreshIntervalMs = orderRefreshIntervalMs;
this._contractWrappers = new ContractWrappers(this.provider, {
networkId,
@@ -278,6 +302,7 @@ export class AssetBuyer {
targetOrderProviderResponse,
feeOrderProviderResponse,
zrxTokenAssetData,
+ this.expiryBufferSeconds,
this._contractWrappers.orderValidator,
);
return ordersAndFillableAmounts;
diff --git a/packages/asset-buyer/src/constants.ts b/packages/asset-buyer/src/constants.ts
index 0ebe0f8e2..79b5d9052 100644
--- a/packages/asset-buyer/src/constants.ts
+++ b/packages/asset-buyer/src/constants.ts
@@ -16,4 +16,5 @@ export const constants = {
ETHER_TOKEN_DECIMALS: 18,
DEFAULT_BUY_QUOTE_REQUEST_OPTS,
MAX_PER_PAGE: 10000,
+ DEFAULT_EXPIRY_BUFFER_SECONDS: 15,
};
diff --git a/packages/asset-buyer/src/utils/order_provider_response_processor.ts b/packages/asset-buyer/src/utils/order_provider_response_processor.ts
index 9e54300b3..31fdcc182 100644
--- a/packages/asset-buyer/src/utils/order_provider_response_processor.ts
+++ b/packages/asset-buyer/src/utils/order_provider_response_processor.ts
@@ -41,11 +41,18 @@ export const orderProviderResponseProcessor = {
targetOrderProviderResponse: OrderProviderResponse,
feeOrderProviderResponse: OrderProviderResponse,
zrxTokenAssetData: string,
+ expiryBufferSeconds: number,
orderValidator?: OrderValidatorWrapper,
): Promise<AssetBuyerOrdersAndFillableAmounts> {
// drop orders that are expired or not open
- const filteredTargetOrders = filterOutExpiredAndNonOpenOrders(targetOrderProviderResponse.orders);
- const filteredFeeOrders = filterOutExpiredAndNonOpenOrders(feeOrderProviderResponse.orders);
+ const filteredTargetOrders = filterOutExpiredAndNonOpenOrders(
+ targetOrderProviderResponse.orders,
+ expiryBufferSeconds,
+ );
+ const filteredFeeOrders = filterOutExpiredAndNonOpenOrders(
+ feeOrderProviderResponse.orders,
+ expiryBufferSeconds,
+ );
// set the orders to be sorted equal to the filtered orders
let unsortedTargetOrders = filteredTargetOrders;
let unsortedFeeOrders = filteredFeeOrders;
@@ -98,9 +105,10 @@ export const orderProviderResponseProcessor = {
*/
function filterOutExpiredAndNonOpenOrders(
orders: SignedOrderWithRemainingFillableMakerAssetAmount[],
+ expiryBufferSeconds: number,
): SignedOrderWithRemainingFillableMakerAssetAmount[] {
const result = _.filter(orders, order => {
- return orderUtils.isOpenOrder(order) && !orderUtils.isOrderExpired(order);
+ return orderUtils.isOpenOrder(order) && !orderUtils.willOrderExpire(order, expiryBufferSeconds);
});
return result;
}
diff --git a/packages/asset-buyer/src/utils/order_utils.ts b/packages/asset-buyer/src/utils/order_utils.ts
index 27db73257..62166eb76 100644
--- a/packages/asset-buyer/src/utils/order_utils.ts
+++ b/packages/asset-buyer/src/utils/order_utils.ts
@@ -5,9 +5,12 @@ import { constants } from '../constants';
export const orderUtils = {
isOrderExpired(order: SignedOrder): boolean {
+ return orderUtils.willOrderExpire(order, 0);
+ },
+ willOrderExpire(order: SignedOrder, secondsFromNow: number): boolean {
const millisecondsInSecond = 1000;
const currentUnixTimestampSec = new BigNumber(Date.now() / millisecondsInSecond).round();
- return order.expirationTimeSeconds.lessThan(currentUnixTimestampSec);
+ return order.expirationTimeSeconds.lessThan(currentUnixTimestampSec.minus(secondsFromNow));
},
calculateRemainingMakerAssetAmount(order: SignedOrder, remainingTakerAssetAmount: BigNumber): BigNumber {
if (remainingTakerAssetAmount.eq(0)) {