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/rate_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/rate_utils.ts')
-rw-r--r-- | packages/order-utils/src/rate_utils.ts | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/packages/order-utils/src/rate_utils.ts b/packages/order-utils/src/rate_utils.ts new file mode 100644 index 000000000..c9ca72c59 --- /dev/null +++ b/packages/order-utils/src/rate_utils.ts @@ -0,0 +1,48 @@ +import { schemas } from '@0xproject/json-schemas'; +import { Order } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; + +import { assert } from './assert'; +import { constants } from './constants'; + +export const rateUtils = { + /** + * Takes an order and calculates the fee adjusted rate (takerAsset/makerAsset) by calculating how much takerAsset + * is required to cover the fees (feeRate * takerFee), adding the takerAssetAmount and dividing by makerAssetAmount + * @param order An object that conforms to the order interface + * @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 rate (takerAsset/makerAsset) of the order adjusted for fees + */ + getFeeAdjustedRateOfOrder(order: Order, feeRate: BigNumber = constants.ZERO_AMOUNT): BigNumber { + assert.doesConformToSchema('order', order, schemas.orderSchema); + assert.isBigNumber('feeRate', feeRate); + assert.assert( + feeRate.gte(constants.ZERO_AMOUNT), + `Expected feeRate: ${feeRate} to be greater than or equal to 0`, + ); + const takerAssetAmountNeededToPayForFees = order.takerFee.mul(feeRate); + const totalTakerAssetAmount = takerAssetAmountNeededToPayForFees.plus(order.takerAssetAmount); + const rate = totalTakerAssetAmount.div(order.makerAssetAmount); + return rate; + }, + /** + * Takes a fee order (makerAssetData corresponds to ZRX and takerAssetData corresponds to WETH) and calculates + * the fee adjusted rate (WETH/ZRX) by dividing the takerAssetAmount by the makerAmount minus the takerFee + * @param feeOrder An object that conforms to the order interface + * @return The rate (WETH/ZRX) of the fee order adjusted for fees + */ + getFeeAdjustedRateOfFeeOrder(feeOrder: Order): BigNumber { + assert.doesConformToSchema('feeOrder', feeOrder, schemas.orderSchema); + const zrxAmountAfterFees = feeOrder.makerAssetAmount.sub(feeOrder.takerFee); + assert.assert( + zrxAmountAfterFees.greaterThan(constants.ZERO_AMOUNT), + `Expected takerFee: ${JSON.stringify(feeOrder.takerFee)} to be less than makerAssetAmount: ${JSON.stringify( + feeOrder.makerAssetAmount, + )}`, + ); + const rate = feeOrder.takerAssetAmount.div(zrxAmountAfterFees); + return rate; + }, +}; |