aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer/src/utils
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2019-01-18 19:53:04 +0800
committerGitHub <noreply@github.com>2019-01-18 19:53:04 +0800
commitc8212f1d4d255120e5e683664b86f1830fb45ae6 (patch)
tree73f1528a36b8fc8a0778e164b1579eb5865d944a /packages/asset-buyer/src/utils
parent7d166dc7da23c30540fb554727a955015073286f (diff)
parentbef30e4209b26c8d4f443f8672e9acb9471c6a37 (diff)
downloaddexon-0x-contracts-c8212f1d4d255120e5e683664b86f1830fb45ae6.tar.gz
dexon-0x-contracts-c8212f1d4d255120e5e683664b86f1830fb45ae6.tar.zst
dexon-0x-contracts-c8212f1d4d255120e5e683664b86f1830fb45ae6.zip
Merge branch 'development' into feature/bignumber-8.0
Diffstat (limited to 'packages/asset-buyer/src/utils')
-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,
+ };
+};