aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util/format.ts
diff options
context:
space:
mode:
authorFrancesco Agosti <francesco.agosti93@gmail.com>2018-10-17 09:18:41 +0800
committerGitHub <noreply@github.com>2018-10-17 09:18:41 +0800
commit336e456984e24807e3d0d5017de4ea8d1e9beb19 (patch)
treeb81fcdfb7f1026b3007a9c5a81f39974d74ae483 /packages/instant/src/util/format.ts
parent35b001b0818eda5a55e0c5f9fad9ec7122c28745 (diff)
parent32beeae2f0fa4538f12036aca8dd8d30b1de8bc9 (diff)
downloaddexon-sol-tools-336e456984e24807e3d0d5017de4ea8d1e9beb19.tar
dexon-sol-tools-336e456984e24807e3d0d5017de4ea8d1e9beb19.tar.gz
dexon-sol-tools-336e456984e24807e3d0d5017de4ea8d1e9beb19.tar.bz2
dexon-sol-tools-336e456984e24807e3d0d5017de4ea8d1e9beb19.tar.lz
dexon-sol-tools-336e456984e24807e3d0d5017de4ea8d1e9beb19.tar.xz
dexon-sol-tools-336e456984e24807e3d0d5017de4ea8d1e9beb19.tar.zst
dexon-sol-tools-336e456984e24807e3d0d5017de4ea8d1e9beb19.zip
Merge pull request #1131 from 0xProject/feature/instant/move-features-over-from-zrx-buyer
[instant][types][order-utils][asset-buyer] Move over and clean up features from zrx-buyer
Diffstat (limited to 'packages/instant/src/util/format.ts')
-rw-r--r--packages/instant/src/util/format.ts45
1 files changed, 45 insertions, 0 deletions
diff --git a/packages/instant/src/util/format.ts b/packages/instant/src/util/format.ts
new file mode 100644
index 000000000..b62c968fb
--- /dev/null
+++ b/packages/instant/src/util/format.ts
@@ -0,0 +1,45 @@
+import { BigNumber } from '@0xproject/utils';
+import { Web3Wrapper } from '@0xproject/web3-wrapper';
+import * as _ from 'lodash';
+
+import { ethDecimals } from '../constants';
+
+export const format = {
+ ethBaseAmount: (ethBaseAmount?: BigNumber, decimalPlaces: number = 4, defaultText: string = '0 ETH'): string => {
+ if (_.isUndefined(ethBaseAmount)) {
+ return defaultText;
+ }
+ const ethUnitAmount = Web3Wrapper.toUnitAmount(ethBaseAmount, ethDecimals);
+ return format.ethUnitAmount(ethUnitAmount, decimalPlaces);
+ },
+ ethUnitAmount: (ethUnitAmount?: BigNumber, decimalPlaces: number = 4, defaultText: string = '0 ETH'): string => {
+ if (_.isUndefined(ethUnitAmount)) {
+ return defaultText;
+ }
+ const roundedAmount = ethUnitAmount.round(decimalPlaces);
+ return `${roundedAmount} ETH`;
+ },
+ ethBaseAmountInUsd: (
+ ethBaseAmount?: BigNumber,
+ ethUsdPrice?: BigNumber,
+ decimalPlaces: number = 2,
+ defaultText: string = '$0.00',
+ ): string => {
+ if (_.isUndefined(ethBaseAmount) || _.isUndefined(ethUsdPrice)) {
+ return defaultText;
+ }
+ const ethUnitAmount = Web3Wrapper.toUnitAmount(ethBaseAmount, ethDecimals);
+ return format.ethUnitAmountInUsd(ethUnitAmount, ethUsdPrice, decimalPlaces);
+ },
+ ethUnitAmountInUsd: (
+ ethUnitAmount?: BigNumber,
+ ethUsdPrice?: BigNumber,
+ decimalPlaces: number = 2,
+ defaultText: string = '$0.00',
+ ): string => {
+ if (_.isUndefined(ethUnitAmount) || _.isUndefined(ethUsdPrice)) {
+ return defaultText;
+ }
+ return `$${ethUnitAmount.mul(ethUsdPrice).toFixed(decimalPlaces)}`;
+ },
+};