aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer
diff options
context:
space:
mode:
authorSteve Klebanoff <steve.klebanoff@gmail.com>2019-01-12 01:28:51 +0800
committerSteve Klebanoff <steve.klebanoff@gmail.com>2019-01-12 01:28:51 +0800
commita5e7ce9e1acb2f8fdfcef629789dd315e3adcbdc (patch)
tree72446a83bc7c25dbd3f4a90fa8f1c013f0653ea1 /packages/asset-buyer
parentdbaed69d778e610c74930748816e3ba63752334c (diff)
downloaddexon-sol-tools-a5e7ce9e1acb2f8fdfcef629789dd315e3adcbdc.tar.gz
dexon-sol-tools-a5e7ce9e1acb2f8fdfcef629789dd315e3adcbdc.tar.zst
dexon-sol-tools-a5e7ce9e1acb2f8fdfcef629789dd315e3adcbdc.zip
Move calculateLiquidity to helper function
Diffstat (limited to 'packages/asset-buyer')
-rw-r--r--packages/asset-buyer/src/asset_buyer.ts32
-rw-r--r--packages/asset-buyer/src/utils/calculate_liquidity.ts34
2 files changed, 35 insertions, 31 deletions
diff --git a/packages/asset-buyer/src/asset_buyer.ts b/packages/asset-buyer/src/asset_buyer.ts
index 43b60f928..cd60f0eff 100644
--- a/packages/asset-buyer/src/asset_buyer.ts
+++ b/packages/asset-buyer/src/asset_buyer.ts
@@ -19,50 +19,20 @@ import {
LiquidityForAssetData,
LiquidityRequestOpts,
OrderProvider,
- OrderProviderResponse,
OrdersAndFillableAmounts,
} from './types';
import { assert } from './utils/assert';
import { assetDataUtils } from './utils/asset_data_utils';
import { buyQuoteCalculator } from './utils/buy_quote_calculator';
+import { calculateLiquidity } from './utils/calculate_liquidity';
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 {
- tokensAvailableInBaseUnits: acc.tokensAvailableInBaseUnits.plus(tokensAvailableForCurrentOrder),
- ethValueAvailableInWei: acc.ethValueAvailableInWei.plus(ethValueAvailableForCurrentOrder),
- };
- },
- {
- tokensAvailableInBaseUnits: new BigNumber(0),
- ethValueAvailableInWei: new BigNumber(0),
- },
- );
-
- // Turn into regular numbers
- return {
- tokensAvailableInBaseUnits: liquidityInBigNumbers.tokensAvailableInBaseUnits.toNumber(),
- ethValueAvailableInWei: liquidityInBigNumbers.ethValueAvailableInWei.toNumber(),
- };
-};
-
export class AssetBuyer {
public readonly provider: Provider;
public readonly orderProvider: OrderProvider;
diff --git a/packages/asset-buyer/src/utils/calculate_liquidity.ts b/packages/asset-buyer/src/utils/calculate_liquidity.ts
new file mode 100644
index 000000000..910c756ac
--- /dev/null
+++ b/packages/asset-buyer/src/utils/calculate_liquidity.ts
@@ -0,0 +1,34 @@
+import { BigNumber } from '@0x/utils';
+
+import { LiquidityForAssetData, OrdersAndFillableAmounts } from '../types';
+
+import { orderUtils } from './order_utils';
+
+export 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 {
+ tokensAvailableInBaseUnits: acc.tokensAvailableInBaseUnits.plus(tokensAvailableForCurrentOrder),
+ ethValueAvailableInWei: acc.ethValueAvailableInWei.plus(ethValueAvailableForCurrentOrder),
+ };
+ },
+ {
+ tokensAvailableInBaseUnits: new BigNumber(0),
+ ethValueAvailableInWei: new BigNumber(0),
+ },
+ );
+
+ // Turn into regular numbers
+ return {
+ tokensAvailableInBaseUnits: liquidityInBigNumbers.tokensAvailableInBaseUnits.toNumber(),
+ ethValueAvailableInWei: liquidityInBigNumbers.ethValueAvailableInWei.toNumber(),
+ };
+};