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

[order-utils] Add rate and sorting utilities #953

Merged
merged 10 commits into from
Aug 14, 2018

Conversation

BMillman19
Copy link
Contributor

@BMillman19 BMillman19 commented Aug 9, 2018

Description

  • Add utilities for calculating fee-adjusted rates
  • Add utilities for sorting order by best fee-adjusted rate

When calculating rates for orders we need to take into account how much fees the taker needs to pay to fill the order. In the case of fee orders (ZRX/WETH) the fee is taken out of the makerAmount. In the case of non ZRX orders (ex. REP/WETH) a feeRate can be provided to estimate how much takerAsset is actually required to fill that order when performing fee abstraction

Testing instructions

yarn install && PKG=@0xproject/order-utils yarn build
cd packages/order-utils && yarn test

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist:

  • Prefixed the title of this PR with [WIP] if it is a work in progress.
  • Prefixed the title of this PR with bracketed package name(s) corresponding to the changed package(s). For example: [sol-cov] Fixed bug.
  • Added tests to cover my changes, or decided that tests would be too impractical.
  • Updated documentation, or decided that no doc change is needed.
  • Added new entries to the relevant CHANGELOG.jsons.

@coveralls
Copy link

coveralls commented Aug 9, 2018

Coverage Status

Coverage increased (+0.2%) to 84.276% when pulling 99b744b on feature/order-utils/order-sorting-utils into f97ec00 on development.

* Defaults to 0
* @return The rate (takerAsset/makerAsset) of the order adjusted for fees
*/
getFeeAdjustedRateOfOrder(signedOrder: SignedOrder, feeRate: BigNumber = constants.ZERO_AMOUNT): BigNumber {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it have to be SignedOrder, or can it just be Order? Is the convention to always useSignedOrder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm yea good call, I had some issues with some other functions that took in orders and had to return orders, in those cases you actually have to use generics so that the type system knows a SignedOrder should be spit out if a SignedOrder is passed in, let me see if I can finagle it

* @param signedFeeOrder An object that conforms to the signedOrder interface
* @return The rate (WETH/ZRX) of the fee order adjusted for fees
*/
getFeeAdjustedRateOfFeeOrder(signedFeeOrder: SignedOrder): BigNumber {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we assert that the pair is indeed WETH/ZRX? It could be another schema ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit hard to assert w/o having to pass in the network context, i think its fine, the calculations don't necessarily rely on the order being WETH/ZRX, the results just don't make sense if a different type of order is passed in

Copy link
Member

@abandeali1 abandeali1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple really minor changes, then looks good to me!

const sortOrders = (signedOrders: SignedOrder[], rateCalculator: RateCalculator): SignedOrder[] => {
const copiedOrders = _.cloneDeep(signedOrders);
const feeOrderComparator = getOrderComparator(rateCalculator);
copiedOrders.sort(feeOrderComparator);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Javascript sort isn't stable (it can sort in a different order depending on browser/environment, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort). We should use _.sortBy here just in case.

Copy link
Contributor Author

@BMillman19 BMillman19 Aug 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You actually can't sort via a comparator using _.sortBy

) => {
const firstOrderRate = rateCalculator(firstSignedOrder);
const secondOrderRate = rateCalculator(secondSignedOrder);
if (firstOrderRate.lt(secondOrderRate)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: .comparedTo does this for you (http://mikemcl.github.io/bignumber.js/#cmp)

@BMillman19 BMillman19 merged commit 3afe405 into development Aug 14, 2018
@BMillman19 BMillman19 deleted the feature/order-utils/order-sorting-utils branch August 14, 2018 20:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants