diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-08-17 07:08:02 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-08-17 07:08:02 +0800 |
commit | 89174cd63fb8c178c33d456d3b9d1d645ee07e91 (patch) | |
tree | 1d3b1932bdb4ff435d7a4d44ca443605d78b9bd0 /packages/order-utils/src/sorting_utils.ts | |
parent | 8cb1d2a0af2df12e0b628a7b016a2b96a4a66860 (diff) | |
parent | d0d6a6a500cf8bbaf5da144490056d0275675eb3 (diff) | |
download | dexon-sol-tools-89174cd63fb8c178c33d456d3b9d1d645ee07e91.tar dexon-sol-tools-89174cd63fb8c178c33d456d3b9d1d645ee07e91.tar.gz dexon-sol-tools-89174cd63fb8c178c33d456d3b9d1d645ee07e91.tar.bz2 dexon-sol-tools-89174cd63fb8c178c33d456d3b9d1d645ee07e91.tar.lz dexon-sol-tools-89174cd63fb8c178c33d456d3b9d1d645ee07e91.tar.xz dexon-sol-tools-89174cd63fb8c178c33d456d3b9d1d645ee07e91.tar.zst dexon-sol-tools-89174cd63fb8c178c33d456d3b9d1d645ee07e91.zip |
Merge branch 'development' of https://github.com/0xProject/0x-monorepo into feature/connect/sra-api-v2
Diffstat (limited to 'packages/order-utils/src/sorting_utils.ts')
-rw-r--r-- | packages/order-utils/src/sorting_utils.ts | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/packages/order-utils/src/sorting_utils.ts b/packages/order-utils/src/sorting_utils.ts new file mode 100644 index 000000000..8811bcaf8 --- /dev/null +++ b/packages/order-utils/src/sorting_utils.ts @@ -0,0 +1,54 @@ +import { schemas } from '@0xproject/json-schemas'; +import { Order } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { assert } from './assert'; +import { constants } from './constants'; +import { rateUtils } from './rate_utils'; + +export const sortingUtils = { + /** + * 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 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<T extends Order>(orders: T[], feeRate: BigNumber = constants.ZERO_AMOUNT): T[] { + assert.doesConformToSchema('orders', orders, schemas.ordersSchema); + assert.isBigNumber('feeRate', feeRate); + const rateCalculator = (order: Order) => rateUtils.getFeeAdjustedRateOfOrder(order, feeRate); + const sortedOrders = sortOrders(orders, rateCalculator); + return sortedOrders; + }, + /** + * 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 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(feeOrders: Order[]): Order[] { + assert.doesConformToSchema('feeOrders', feeOrders, schemas.ordersSchema); + const rateCalculator = rateUtils.getFeeAdjustedRateOfFeeOrder.bind(rateUtils); + const sortedOrders = sortOrders(feeOrders, rateCalculator); + return sortedOrders; + }, +}; + +type RateCalculator = (order: Order) => BigNumber; + +// takes an array of orders, copies them, and sorts the copy based on the rate definition provided by rateCalculator +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; +} |