aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer/src/utils/calculate_liquidity.ts
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2019-01-18 19:46:16 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2019-01-18 19:46:16 +0800
commite6fe728bcbbab0c500fc7adee1439505282e8eca (patch)
treed7c4d43725b460776857ff421397d6c4b8c30603 /packages/asset-buyer/src/utils/calculate_liquidity.ts
parent6c7f4c13fa3de20c554e9c031f272d0f66842580 (diff)
parentbef30e4209b26c8d4f443f8672e9acb9471c6a37 (diff)
downloaddexon-0x-contracts-e6fe728bcbbab0c500fc7adee1439505282e8eca.tar.gz
dexon-0x-contracts-e6fe728bcbbab0c500fc7adee1439505282e8eca.tar.zst
dexon-0x-contracts-e6fe728bcbbab0c500fc7adee1439505282e8eca.zip
Merge development
Diffstat (limited to 'packages/asset-buyer/src/utils/calculate_liquidity.ts')
-rw-r--r--packages/asset-buyer/src/utils/calculate_liquidity.ts34
1 files changed, 34 insertions, 0 deletions
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..a8d165b4b
--- /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,
+ ethValueAvailableInWei: liquidityInBigNumbers.ethValueAvailableInWei,
+ };
+};