aboutsummaryrefslogtreecommitdiffstats
path: root/packages
diff options
context:
space:
mode:
authorBrandon Millman <brandon.millman@gmail.com>2018-08-14 09:31:41 +0800
committerBrandon Millman <brandon.millman@gmail.com>2018-08-14 09:32:15 +0800
commit99b744ba52f538752bb0966e6d8b50d9f5a2f032 (patch)
tree149c3bdd1f5960103641fddf3a76d52cb2f3eb66 /packages
parentd8593998414aa3234533b8fa868b05495ac54457 (diff)
downloaddexon-sol-tools-99b744ba52f538752bb0966e6d8b50d9f5a2f032.tar
dexon-sol-tools-99b744ba52f538752bb0966e6d8b50d9f5a2f032.tar.gz
dexon-sol-tools-99b744ba52f538752bb0966e6d8b50d9f5a2f032.tar.bz2
dexon-sol-tools-99b744ba52f538752bb0966e6d8b50d9f5a2f032.tar.lz
dexon-sol-tools-99b744ba52f538752bb0966e6d8b50d9f5a2f032.tar.xz
dexon-sol-tools-99b744ba52f538752bb0966e6d8b50d9f5a2f032.tar.zst
dexon-sol-tools-99b744ba52f538752bb0966e6d8b50d9f5a2f032.zip
Update sortingUtils to support Order and SignedOrder
Diffstat (limited to 'packages')
-rw-r--r--packages/order-utils/src/sorting_utils.ts70
1 files changed, 26 insertions, 44 deletions
diff --git a/packages/order-utils/src/sorting_utils.ts b/packages/order-utils/src/sorting_utils.ts
index f019aa4a8..8811bcaf8 100644
--- a/packages/order-utils/src/sorting_utils.ts
+++ b/packages/order-utils/src/sorting_utils.ts
@@ -1,5 +1,5 @@
import { schemas } from '@0xproject/json-schemas';
-import { SignedOrder } from '@0xproject/types';
+import { Order } from '@0xproject/types';
import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';
@@ -9,64 +9,46 @@ import { rateUtils } from './rate_utils';
export const sortingUtils = {
/**
- * Takes an array of signed orders and sorts them by takerAsset/makerAsset rate in ascending order (best rate first).
+ * Takes an array of orders and sorts them by takerAsset/makerAsset rate in ascending order (best rate first).
* Adjusts the rate of each order according to the feeRate and takerFee for that order.
- * @param signedFeeOrders An array of objects that conform to the SignedOrder interface. All orders should specify ZRX as
- * the makerAsset and WETH as the takerAsset.
- * @param feeRate The market rate of ZRX denominated in takerAssetAmount
- * (ex. feeRate is 0.1 takerAsset/ZRX if it takes 1 unit of takerAsset to buy 10 ZRX)
- * Defaults to 0
+ * @param orders An array of objects that extend the Order interface. All orders should specify ZRX as
+ * the makerAsset and WETH as the takerAsset.
+ * @param feeRate The market rate of ZRX denominated in takerAssetAmount
+ * (ex. feeRate is 0.1 takerAsset/ZRX if it takes 1 unit of takerAsset to buy 10 ZRX)
+ * Defaults to 0
* @return The input orders sorted by rate in ascending order
*/
- sortOrdersByFeeAdjustedRate(
- signedOrders: SignedOrder[],
- feeRate: BigNumber = constants.ZERO_AMOUNT,
- ): SignedOrder[] {
- assert.doesConformToSchema('signedOrders', signedOrders, schemas.signedOrdersSchema);
+ sortOrdersByFeeAdjustedRate<T extends Order>(orders: T[], feeRate: BigNumber = constants.ZERO_AMOUNT): T[] {
+ assert.doesConformToSchema('orders', orders, schemas.ordersSchema);
assert.isBigNumber('feeRate', feeRate);
- const rateCalculator = (signedOrder: SignedOrder) => rateUtils.getFeeAdjustedRateOfOrder(signedOrder, feeRate);
- const sortedOrders = sortOrders(signedOrders, rateCalculator);
+ const rateCalculator = (order: Order) => rateUtils.getFeeAdjustedRateOfOrder(order, feeRate);
+ const sortedOrders = sortOrders(orders, rateCalculator);
return sortedOrders;
},
/**
- * Takes an array of signed fee orders (makerAssetData corresponds to ZRX and takerAssetData corresponds to WETH)
+ * Takes an array of fee orders (makerAssetData corresponds to ZRX and takerAssetData corresponds to WETH)
* and sorts them by rate in ascending order (best rate first). Adjusts the rate according to the takerFee.
- * @param signedFeeOrders An array of objects that conform to the SignedOrder interface. All orders should specify ZRX as
- * the makerAsset and WETH as the takerAsset.
+ * @param feeOrders An array of objects that extend the Order interface. All orders should specify ZRX as
+ * the makerAsset and WETH as the takerAsset.
* @return The input orders sorted by rate in ascending order
*/
- sortFeeOrdersByFeeAdjustedRate(signedFeeOrders: SignedOrder[]): SignedOrder[] {
- assert.doesConformToSchema('signedFeeOrders', signedFeeOrders, schemas.signedOrdersSchema);
+ sortFeeOrdersByFeeAdjustedRate(feeOrders: Order[]): Order[] {
+ assert.doesConformToSchema('feeOrders', feeOrders, schemas.ordersSchema);
const rateCalculator = rateUtils.getFeeAdjustedRateOfFeeOrder.bind(rateUtils);
- const sortedOrders = sortOrders(signedFeeOrders, rateCalculator);
+ const sortedOrders = sortOrders(feeOrders, rateCalculator);
return sortedOrders;
},
};
-type RateCalculator = (signedOrder: SignedOrder) => BigNumber;
+type RateCalculator = (order: Order) => BigNumber;
// takes an array of orders, copies them, and sorts the copy based on the rate definition provided by rateCalculator
-const sortOrders = (signedOrders: SignedOrder[], rateCalculator: RateCalculator): SignedOrder[] => {
- const copiedOrders = _.cloneDeep(signedOrders);
- const feeOrderComparator = getOrderComparator(rateCalculator);
- copiedOrders.sort(feeOrderComparator);
+function sortOrders<T extends Order>(orders: T[], rateCalculator: RateCalculator): T[] {
+ const copiedOrders = _.cloneDeep(orders);
+ copiedOrders.sort((firstOrder, secondOrder) => {
+ const firstOrderRate = rateCalculator(firstOrder);
+ const secondOrderRate = rateCalculator(secondOrder);
+ return firstOrderRate.comparedTo(secondOrderRate);
+ });
return copiedOrders;
-};
-
-type Comparator<T> = (first: T, second: T) => number;
-
-// takes a function that calculates rate for a signed order and returns a comparator that sorts based on rate
-const getOrderComparator = (rateCalculator: RateCalculator): Comparator<SignedOrder> => (
- firstSignedOrder,
- secondSignedOrder,
-) => {
- const firstOrderRate = rateCalculator(firstSignedOrder);
- const secondOrderRate = rateCalculator(secondSignedOrder);
- if (firstOrderRate.lt(secondOrderRate)) {
- return -1;
- } else if (firstOrderRate.gt(secondOrderRate)) {
- return 1;
- } else {
- return 0;
- }
-};
+}