aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer/src/utils/buy_quote_calculator.ts
diff options
context:
space:
mode:
authorBrandon Millman <brandon.millman@gmail.com>2018-09-15 18:59:23 +0800
committerBrandon Millman <brandon.millman@gmail.com>2018-09-15 20:14:48 +0800
commitf1a22e9bd7943bc9cb8d8308daca0c60af6e0039 (patch)
tree91f3e8f54103156500518ad521816e6df409d1d1 /packages/asset-buyer/src/utils/buy_quote_calculator.ts
parent7b46cef83dca0a743bd598a70076004983cbf294 (diff)
downloaddexon-sol-tools-f1a22e9bd7943bc9cb8d8308daca0c60af6e0039.tar
dexon-sol-tools-f1a22e9bd7943bc9cb8d8308daca0c60af6e0039.tar.gz
dexon-sol-tools-f1a22e9bd7943bc9cb8d8308daca0c60af6e0039.tar.bz2
dexon-sol-tools-f1a22e9bd7943bc9cb8d8308daca0c60af6e0039.tar.lz
dexon-sol-tools-f1a22e9bd7943bc9cb8d8308daca0c60af6e0039.tar.xz
dexon-sol-tools-f1a22e9bd7943bc9cb8d8308daca0c60af6e0039.tar.zst
dexon-sol-tools-f1a22e9bd7943bc9cb8d8308daca0c60af6e0039.zip
Flesh out the AssetBuyer class
Diffstat (limited to 'packages/asset-buyer/src/utils/buy_quote_calculator.ts')
-rw-r--r--packages/asset-buyer/src/utils/buy_quote_calculator.ts60
1 files changed, 60 insertions, 0 deletions
diff --git a/packages/asset-buyer/src/utils/buy_quote_calculator.ts b/packages/asset-buyer/src/utils/buy_quote_calculator.ts
new file mode 100644
index 000000000..e05ab1e55
--- /dev/null
+++ b/packages/asset-buyer/src/utils/buy_quote_calculator.ts
@@ -0,0 +1,60 @@
+import { marketUtils } from '@0xproject/order-utils';
+import { BigNumber } from '@0xproject/utils';
+
+import { constants } from '../constants';
+import { AssetBuyerError, AssetBuyerOrdersAndFillableAmounts, BuyQuote } from '../types';
+
+export const buyQuoteCalculator = {
+ calculate(
+ ordersAndFillableAmounts: AssetBuyerOrdersAndFillableAmounts,
+ assetBuyAmount: BigNumber,
+ feePercentage: number,
+ slippagePercentage: number,
+ ): BuyQuote {
+ const {
+ orders,
+ feeOrders,
+ remainingFillableMakerAssetAmounts,
+ remainingFillableFeeAmounts,
+ } = ordersAndFillableAmounts;
+ const slippageBufferAmount = assetBuyAmount.mul(slippagePercentage).round();
+ const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount(
+ orders,
+ assetBuyAmount,
+ {
+ remainingFillableMakerAssetAmounts,
+ slippageBufferAmount,
+ },
+ );
+ if (remainingFillAmount.gt(constants.ZERO_AMOUNT)) {
+ throw new Error(AssetBuyerError.InsufficientAssetLiquidity);
+ }
+ // TODO: optimization
+ // update this logic to find the minimum amount of feeOrders to cover the worst case as opposed to
+ // finding order that cover all fees, this will help with estimating ETH and minimizing gas usage
+ const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
+ resultOrders,
+ feeOrders,
+ {
+ remainingFillableMakerAssetAmounts,
+ remainingFillableFeeAmounts,
+ },
+ );
+ if (remainingFeeAmount.gt(constants.ZERO_AMOUNT)) {
+ throw new Error(AssetBuyerError.InsufficientZrxLiquidity);
+ }
+ const assetData = orders[0].makerAssetData;
+ // TODO: critical
+ // calculate minRate and maxRate by calculating min and max eth usage and then dividing into
+ // assetBuyAmount to get assetData / WETH, needs to take into account feePercentage as well
+ return {
+ assetData,
+ orders: resultOrders,
+ feeOrders: resultFeeOrders,
+ minRate: constants.ZERO_AMOUNT,
+ maxRate: constants.ZERO_AMOUNT,
+ assetBuyAmount,
+ feePercentage,
+ };
+ },
+};