aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util
diff options
context:
space:
mode:
authorfragosti <francesco.agosti93@gmail.com>2018-10-12 08:35:22 +0800
committerfragosti <francesco.agosti93@gmail.com>2018-10-12 08:35:22 +0800
commit03b235bb428e8a61934ff603f22f057d8394b56a (patch)
treeba8671e02273c8d991b35e1920e0a2a29a9d0986 /packages/instant/src/util
parent1c92ae0ed0b90818aca7bf899c05fd150672d75b (diff)
downloaddexon-sol-tools-03b235bb428e8a61934ff603f22f057d8394b56a.tar
dexon-sol-tools-03b235bb428e8a61934ff603f22f057d8394b56a.tar.gz
dexon-sol-tools-03b235bb428e8a61934ff603f22f057d8394b56a.tar.bz2
dexon-sol-tools-03b235bb428e8a61934ff603f22f057d8394b56a.tar.lz
dexon-sol-tools-03b235bb428e8a61934ff603f22f057d8394b56a.tar.xz
dexon-sol-tools-03b235bb428e8a61934ff603f22f057d8394b56a.tar.zst
dexon-sol-tools-03b235bb428e8a61934ff603f22f057d8394b56a.zip
feat: populate order details with information from worst buy quote
Diffstat (limited to 'packages/instant/src/util')
-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..de7eb62e6
--- /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).round(decimalPlaces)}`;
+ },
+};