aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util
diff options
context:
space:
mode:
authorSteve Klebanoff <steve.klebanoff@gmail.com>2018-10-27 03:47:24 +0800
committerSteve Klebanoff <steve.klebanoff@gmail.com>2018-10-27 03:47:24 +0800
commit9be4c4749935a765cbd55783735d1d1f719322c0 (patch)
tree49c3f3f42e58b49547799e6dfadc1b10c82381db /packages/instant/src/util
parent9512978de9aee1953930d4f16a105ec5ef7f048e (diff)
parentaf91a56a5594d07d7da6caaeff79f5a7fb31ff98 (diff)
downloaddexon-sol-tools-9be4c4749935a765cbd55783735d1d1f719322c0.tar
dexon-sol-tools-9be4c4749935a765cbd55783735d1d1f719322c0.tar.gz
dexon-sol-tools-9be4c4749935a765cbd55783735d1d1f719322c0.tar.bz2
dexon-sol-tools-9be4c4749935a765cbd55783735d1d1f719322c0.tar.lz
dexon-sol-tools-9be4c4749935a765cbd55783735d1d1f719322c0.tar.xz
dexon-sol-tools-9be4c4749935a765cbd55783735d1d1f719322c0.tar.zst
dexon-sol-tools-9be4c4749935a765cbd55783735d1d1f719322c0.zip
Merge branch 'development' into feature/not-enough-eth
Diffstat (limited to 'packages/instant/src/util')
-rw-r--r--packages/instant/src/util/asset.ts12
-rw-r--r--packages/instant/src/util/big_number_input.ts29
-rw-r--r--packages/instant/src/util/format.ts2
3 files changed, 41 insertions, 2 deletions
diff --git a/packages/instant/src/util/asset.ts b/packages/instant/src/util/asset.ts
index 4e3b2b946..2c5b6325d 100644
--- a/packages/instant/src/util/asset.ts
+++ b/packages/instant/src/util/asset.ts
@@ -2,7 +2,7 @@ import { AssetProxyId, ObjectMap } from '@0x/types';
import * as _ from 'lodash';
import { assetDataNetworkMapping } from '../data/asset_data_network_mapping';
-import { Asset, AssetMetaData, Network, ZeroExInstantError } from '../types';
+import { Asset, AssetMetaData, ERC20Asset, Network, ZeroExInstantError } from '../types';
export const assetUtils = {
createAssetFromAssetData: (
@@ -43,6 +43,16 @@ export const assetUtils = {
return defaultName;
}
},
+ formattedSymbolForAsset: (asset?: ERC20Asset, defaultName: string = '???'): string => {
+ if (_.isUndefined(asset)) {
+ return defaultName;
+ }
+ const symbol = asset.metaData.symbol;
+ if (symbol.length <= 5) {
+ return symbol;
+ }
+ return `${symbol.slice(0, 3)}…`;
+ },
getAssociatedAssetDataIfExists: (assetData: string, network: Network): string | undefined => {
const assetDataGroupIfExists = _.find(assetDataNetworkMapping, value => value[network] === assetData);
if (_.isUndefined(assetDataGroupIfExists)) {
diff --git a/packages/instant/src/util/big_number_input.ts b/packages/instant/src/util/big_number_input.ts
new file mode 100644
index 000000000..d2a9a8dc5
--- /dev/null
+++ b/packages/instant/src/util/big_number_input.ts
@@ -0,0 +1,29 @@
+import { BigNumber } from '@0x/utils';
+import * as _ from 'lodash';
+
+/**
+ * A BigNumber extension that is more flexible about decimal strings.
+ * Such as allowing:
+ * new BigNumberInput('0.') => 0
+ * new BigNumberInput('1.') => 1
+ * new BigNumberInput('1..') => still throws
+ */
+export class BigNumberInput extends BigNumber {
+ private readonly _isEndingWithDecimal: boolean;
+ constructor(bigNumberString: string) {
+ const hasDecimalPeriod = _.endsWith(bigNumberString, '.');
+ let internalString = bigNumberString;
+ if (hasDecimalPeriod) {
+ internalString = bigNumberString.slice(0, -1);
+ }
+ super(internalString);
+ this._isEndingWithDecimal = hasDecimalPeriod;
+ }
+ public toDisplayString(): string {
+ const internalString = super.toString();
+ if (this._isEndingWithDecimal) {
+ return `${internalString}.`;
+ }
+ return internalString;
+ }
+}
diff --git a/packages/instant/src/util/format.ts b/packages/instant/src/util/format.ts
index 2e0ccc03b..4a48dec9d 100644
--- a/packages/instant/src/util/format.ts
+++ b/packages/instant/src/util/format.ts
@@ -24,7 +24,7 @@ export const format = {
if (_.isUndefined(ethUnitAmount)) {
return defaultText;
}
- const roundedAmount = ethUnitAmount.round(decimalPlaces);
+ const roundedAmount = ethUnitAmount.round(decimalPlaces).toDigits(decimalPlaces);
return `${roundedAmount} ETH`;
},
ethBaseAmountInUsd: (