diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2019-01-18 19:46:16 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2019-01-18 19:46:16 +0800 |
commit | e6fe728bcbbab0c500fc7adee1439505282e8eca (patch) | |
tree | d7c4d43725b460776857ff421397d6c4b8c30603 /packages/asset-buyer/src/utils/calculate_liquidity.ts | |
parent | 6c7f4c13fa3de20c554e9c031f272d0f66842580 (diff) | |
parent | bef30e4209b26c8d4f443f8672e9acb9471c6a37 (diff) | |
download | dexon-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.ts | 34 |
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, + }; +}; |