aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer/src/asset_buyer.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/asset-buyer/src/asset_buyer.ts')
-rw-r--r--packages/asset-buyer/src/asset_buyer.ts67
1 files changed, 64 insertions, 3 deletions
diff --git a/packages/asset-buyer/src/asset_buyer.ts b/packages/asset-buyer/src/asset_buyer.ts
index 934410c55..511c99fb1 100644
--- a/packages/asset-buyer/src/asset_buyer.ts
+++ b/packages/asset-buyer/src/asset_buyer.ts
@@ -16,6 +16,7 @@ import {
BuyQuote,
BuyQuoteExecutionOpts,
BuyQuoteRequestOpts,
+ LiquidityForAssetData,
OrderProvider,
OrderProviderResponse,
OrdersAndFillableAmounts,
@@ -25,12 +26,42 @@ import { assert } from './utils/assert';
import { assetDataUtils } from './utils/asset_data_utils';
import { buyQuoteCalculator } from './utils/buy_quote_calculator';
import { orderProviderResponseProcessor } from './utils/order_provider_response_processor';
+import { orderUtils } from './utils/order_utils';
interface OrdersEntry {
ordersAndFillableAmounts: OrdersAndFillableAmounts;
lastRefreshTime: number;
}
+const calculateLiquidity = (ordersAndFillableAmounts: OrdersAndFillableAmounts): LiquidityForAssetData => {
+ const { orders, remainingFillableMakerAssetAmounts } = ordersAndFillableAmounts;
+ const liquidityInBigNumbers = orders.reduce(
+ (acc, order, curIndex) => {
+ const availableMakerAssetAmount = remainingFillableMakerAssetAmounts[curIndex];
+ if (availableMakerAssetAmount === undefined) {
+ throw new Error(`No corresponding fillableMakerAssetAmounts at index ${curIndex}`);
+ }
+
+ const tokensAvailableForCurrentOrder = availableMakerAssetAmount;
+ const ethValueAvailableForCurrentOrder = orderUtils.getTakerFillAmount(order, availableMakerAssetAmount);
+ return {
+ tokensAvailableInUnitAmount: acc.tokensAvailableInUnitAmount.plus(tokensAvailableForCurrentOrder),
+ ethValueAvailableInWei: acc.ethValueAvailableInWei.plus(ethValueAvailableForCurrentOrder),
+ };
+ },
+ {
+ tokensAvailableInUnitAmount: new BigNumber(0),
+ ethValueAvailableInWei: new BigNumber(0),
+ },
+ );
+
+ // Turn into regular numbers
+ return {
+ tokensAvailableInUnitAmount: liquidityInBigNumbers.tokensAvailableInUnitAmount.toNumber(),
+ ethValueAvailableInWei: liquidityInBigNumbers.ethValueAvailableInWei.toNumber(),
+ };
+};
+
export class AssetBuyer {
public readonly provider: Provider;
public readonly orderProvider: OrderProvider;
@@ -138,10 +169,10 @@ export class AssetBuyer {
// get the relevant orders for the makerAsset and fees
// if the requested assetData is ZRX, don't get the fee info
const [ordersAndFillableAmounts, feeOrdersAndFillableAmounts] = await Promise.all([
- this._getOrdersAndFillableAmountsAsync(assetData, shouldForceOrderRefresh),
+ this.getOrdersAndFillableAmountsAsync(assetData, shouldForceOrderRefresh),
isMakerAssetZrxToken
? Promise.resolve(constants.EMPTY_ORDERS_AND_FILLABLE_AMOUNTS)
- : this._getOrdersAndFillableAmountsAsync(zrxTokenAssetData, shouldForceOrderRefresh),
+ : this.getOrdersAndFillableAmountsAsync(zrxTokenAssetData, shouldForceOrderRefresh),
shouldForceOrderRefresh,
]);
if (ordersAndFillableAmounts.orders.length === 0) {
@@ -177,6 +208,36 @@ export class AssetBuyer {
const buyQuote = this.getBuyQuoteAsync(assetData, assetBuyAmount, options);
return buyQuote;
}
+ public async getLiquidityForAssetDataAsync(
+ assetData: string,
+ options: Partial<BuyQuoteRequestOpts> = {},
+ ): Promise<LiquidityForAssetData> {
+ const { feePercentage, shouldForceOrderRefresh, slippagePercentage } = _.merge(
+ {},
+ constants.DEFAULT_BUY_QUOTE_REQUEST_OPTS,
+ options,
+ );
+ assert.isString('assetData', assetData);
+ assert.isValidPercentage('feePercentage', feePercentage);
+ assert.isBoolean('shouldForceOrderRefresh', shouldForceOrderRefresh);
+ assert.isNumber('slippagePercentage', slippagePercentage);
+
+ const assetPairs = await this.orderProvider.getAvailableMakerAssetDatasAsync(assetData);
+ if (!assetPairs.includes(assetData)) {
+ return {
+ tokensAvailableInUnitAmount: 0,
+ ethValueAvailableInWei: 0,
+ };
+ }
+
+ const ordersAndFillableAmounts = await this.getOrdersAndFillableAmountsAsync(
+ assetData,
+ shouldForceOrderRefresh,
+ );
+
+ return calculateLiquidity(ordersAndFillableAmounts);
+ }
+
/**
* Given a BuyQuote and desired rate, attempt to execute the buy.
* @param buyQuote An object that conforms to BuyQuote. See type definition for more information.
@@ -261,7 +322,7 @@ export class AssetBuyer {
/**
* Grab orders from the map, if there is a miss or it is time to refresh, fetch and process the orders
*/
- private async _getOrdersAndFillableAmountsAsync(
+ public async getOrdersAndFillableAmountsAsync(
assetData: string,
shouldForceOrderRefresh: boolean,
): Promise<OrdersAndFillableAmounts> {
span>2-3/+3 * . Sort.glewis2005-06-071-693/+693 * [1] Add missing dependency on zip.anholt2005-06-042-4/+24 * + update to 1.1.3;thierry2005-06-015-15/+883 * UnBROKEN by fixing pkg-plist.nork2005-05-292-2/+2 * o Update to 3.1M7. [1]nork2005-05-2920-261/+181 * Update to 1.1.5, which includes a port to amd64. (Ports to alpha, powerpc,anholt2005-05-295-99/+44 * . Update to 0.47.glewis2005-05-253-18/+3 * Add 'devel' categoryhq2005-05-232-2/+2 * - Install commons-logging-api.jarhq2005-05-231-5/+6 * Update to 4.0.2hq2005-05-196-30/+30 * . This port is working well enough for me under 5.4 that I can runglewis2005-05-182-4/+8 * Unbreak: plist fix, do not remove common Java class dir.anders2005-05-184-6/+0 * Update to version 1.4.5.archie2005-05-172-3/+3 * - Use find and installlawrance2005-05-171-6/+24 * Fix DOWNLOAD_URL.olgeni2005-05-175-5/+5 * - Allow installation on amd64hq2005-05-172-5/+8 * Upgrade to version 4.1 (plus JDK home fix from ports/76670)olgeni2005-05-1720-4790/+5570 * Upgrade to 0.15.archie2005-05-162-3/+3 * Castor is a data binding framework for Java. It's the shortest pathhq2005-05-154-0/+45 * . Ensure that when files are extracted that their fully resolved path liesglewis2005-05-126-2/+144 * . Fix various problems with time zone handling including:glewis2005-05-122-0/+200 * . Using dladdr(3) to determine the path to the current executable, andglewis2005-05-122-0/+124 * . Ensure that when files are extracted that their fully resolved path liesglewis2005-05-123-0/+72 * Update to version 1.4.4.archie2005-05-112-5/+4 * Fix size mismatch. According to the submitter:thierry2005-05-093-6/+3 * BROKEN: Incomplete pkg-plistkris2005-05-082-0/+4 * Add eclipse-devel 3.1M6, an open extensible IDE for anything and nothingnork2005-05-0731-0/+1104 * . Update to 1.3.2.glewis2005-05-042-6/+5 * . Ensure that when files are extracted that their fully resolved path liesglewis2005-05-033-1/+72 * . Update to 1.5.0_03.glewis2005-04-306-254/+1194 * . Add $FreeBSD$.glewis2005-04-292-0/+4 * . Bump PORTREVISION for the previous fix for jar(1).glewis2005-04-281-8/+8 * . Ensure that when files are extracted that their fully resolved path liesglewis2005-04-282-0/+67 * - Update to 3.2 [1]hq2005-04-285-55/+111 * - Upgrade to version 1.4.3.archie2005-04-263-7/+19 * . Update description.glewis2005-04-251-2/+4 * - Update to version 2005.04.15hq2005-04-202-10/+10 * . Fix various problems with time zone handling including:glewis2005-04-191-0/+101