aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util
diff options
context:
space:
mode:
authorSteve Klebanoff <steve.klebanoff@gmail.com>2018-12-22 06:57:16 +0800
committerSteve Klebanoff <steve.klebanoff@gmail.com>2018-12-22 06:57:16 +0800
commite144ebbb936c74b4af0e1c1776578208ea148de4 (patch)
treec5ab2823603b25d11d46493e9c8f79801bef0dcd /packages/instant/src/util
parentaa9aa1f58a4e63b5e5c7863f3b7afb020d7712c5 (diff)
parent9b540fd8e52e7578d3749e6d9ef9cd97d602ffb3 (diff)
downloaddexon-sol-tools-e144ebbb936c74b4af0e1c1776578208ea148de4.tar
dexon-sol-tools-e144ebbb936c74b4af0e1c1776578208ea148de4.tar.gz
dexon-sol-tools-e144ebbb936c74b4af0e1c1776578208ea148de4.tar.bz2
dexon-sol-tools-e144ebbb936c74b4af0e1c1776578208ea148de4.tar.lz
dexon-sol-tools-e144ebbb936c74b4af0e1c1776578208ea148de4.tar.xz
dexon-sol-tools-e144ebbb936c74b4af0e1c1776578208ea148de4.tar.zst
dexon-sol-tools-e144ebbb936c74b4af0e1c1776578208ea148de4.zip
Merge branch 'development' into feature/instant/tell-amount-available
Diffstat (limited to 'packages/instant/src/util')
-rw-r--r--packages/instant/src/util/analytics.ts15
-rw-r--r--packages/instant/src/util/asset.ts4
-rw-r--r--packages/instant/src/util/env.ts2
-rw-r--r--packages/instant/src/util/format.ts30
4 files changed, 44 insertions, 7 deletions
diff --git a/packages/instant/src/util/analytics.ts b/packages/instant/src/util/analytics.ts
index e6128f857..4faeaaf5a 100644
--- a/packages/instant/src/util/analytics.ts
+++ b/packages/instant/src/util/analytics.ts
@@ -6,6 +6,7 @@ import { GIT_SHA, HEAP_ENABLED, INSTANT_DISCHARGE_TARGET, NODE_ENV, NPM_PACKAGE_
import {
AffiliateInfo,
Asset,
+ BaseCurrency,
Network,
OrderProcessState,
OrderSource,
@@ -37,6 +38,7 @@ enum EventNames {
ACCOUNT_UNLOCK_REQUESTED = 'Account - Unlock Requested',
ACCOUNT_UNLOCK_DENIED = 'Account - Unlock Denied',
ACCOUNT_ADDRESS_CHANGED = 'Account - Address Changed',
+ BASE_CURRENCY_CHANGED = 'Base Currency - Changed',
PAYMENT_METHOD_DROPDOWN_OPENED = 'Payment Method - Dropdown Opened',
PAYMENT_METHOD_OPENED_ETHERSCAN = 'Payment Method - Opened Etherscan',
PAYMENT_METHOD_COPIED_ADDRESS = 'Payment Method - Copied Address',
@@ -44,9 +46,11 @@ enum EventNames {
BUY_STARTED = 'Buy - Started',
BUY_SIGNATURE_DENIED = 'Buy - Signature Denied',
BUY_SIMULATION_FAILED = 'Buy - Simulation Failed',
+ BUY_UNKNOWN_ERROR = 'Buy - Unknown Error',
BUY_TX_SUBMITTED = 'Buy - Tx Submitted',
BUY_TX_SUCCEEDED = 'Buy - Tx Succeeded',
BUY_TX_FAILED = 'Buy - Tx Failed',
+ USD_PRICE_FETCH_FAILED = 'USD Price - Fetch Failed',
INSTALL_WALLET_CLICKED = 'Install Wallet - Clicked',
INSTALL_WALLET_MODAL_OPENED = 'Install Wallet - Modal - Opened',
INSTALL_WALLET_MODAL_CLICKED_EXPLANATION = 'Install Wallet - Modal - Clicked Explanation',
@@ -118,6 +122,7 @@ export interface AnalyticsEventOptions {
selectedAssetSymbol?: string;
selectedAssetData?: string;
selectedAssetDecimals?: number;
+ baseCurrency?: string;
}
export enum TokenSelectorClosedVia {
ClickedX = 'Clicked X',
@@ -141,6 +146,7 @@ export const analytics = {
window: Window,
selectedAsset?: Asset,
affiliateInfo?: AffiliateInfo,
+ baseCurrency?: BaseCurrency,
): AnalyticsEventOptions => {
const affiliateAddress = affiliateInfo ? affiliateInfo.feeRecipient : 'none';
const affiliateFeePercent = affiliateInfo ? parseFloat(affiliateInfo.feePercentage.toFixed(4)) : 0;
@@ -159,6 +165,7 @@ export const analytics = {
selectedAssetName: selectedAsset ? selectedAsset.metaData.name : 'none',
selectedAssetData: selectedAsset ? selectedAsset.assetData : 'none',
instantEnvironment: INSTANT_DISCHARGE_TARGET || `Local ${NODE_ENV}`,
+ baseCurrency,
};
return eventOptions;
},
@@ -170,6 +177,8 @@ export const analytics = {
trackAccountUnlockDenied: trackingEventFnWithoutPayload(EventNames.ACCOUNT_UNLOCK_DENIED),
trackAccountAddressChanged: (address: string) =>
trackingEventFnWithPayload(EventNames.ACCOUNT_ADDRESS_CHANGED)({ address }),
+ trackBaseCurrencyChanged: (currencyChangedTo: BaseCurrency) =>
+ trackingEventFnWithPayload(EventNames.BASE_CURRENCY_CHANGED)({ currencyChangedTo }),
trackPaymentMethodDropdownOpened: trackingEventFnWithoutPayload(EventNames.PAYMENT_METHOD_DROPDOWN_OPENED),
trackPaymentMethodOpenedEtherscan: trackingEventFnWithoutPayload(EventNames.PAYMENT_METHOD_OPENED_ETHERSCAN),
trackPaymentMethodCopiedAddress: trackingEventFnWithoutPayload(EventNames.PAYMENT_METHOD_COPIED_ADDRESS),
@@ -181,6 +190,11 @@ export const analytics = {
trackingEventFnWithPayload(EventNames.BUY_SIGNATURE_DENIED)(buyQuoteEventProperties(buyQuote)),
trackBuySimulationFailed: (buyQuote: BuyQuote) =>
trackingEventFnWithPayload(EventNames.BUY_SIMULATION_FAILED)(buyQuoteEventProperties(buyQuote)),
+ trackBuyUnknownError: (buyQuote: BuyQuote, errorMessage: string) =>
+ trackingEventFnWithPayload(EventNames.BUY_UNKNOWN_ERROR)({
+ ...buyQuoteEventProperties(buyQuote),
+ errorMessage,
+ }),
trackBuyTxSubmitted: (buyQuote: BuyQuote, txHash: string, startTimeUnix: number, expectedEndTimeUnix: number) =>
trackingEventFnWithPayload(EventNames.BUY_TX_SUBMITTED)({
...buyQuoteEventProperties(buyQuote),
@@ -230,4 +244,5 @@ export const analytics = {
fetchOrigin,
});
},
+ trackUsdPriceFailed: trackingEventFnWithoutPayload(EventNames.USD_PRICE_FETCH_FAILED),
};
diff --git a/packages/instant/src/util/asset.ts b/packages/instant/src/util/asset.ts
index 756194f1f..e7aa55c88 100644
--- a/packages/instant/src/util/asset.ts
+++ b/packages/instant/src/util/asset.ts
@@ -4,7 +4,7 @@ import { BigNumber } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';
import * as _ from 'lodash';
-import { BIG_NUMBER_ZERO } from '../constants';
+import { BIG_NUMBER_ZERO, DEFAULT_UNKOWN_ASSET_NAME } from '../constants';
import { assetDataNetworkMapping } from '../data/asset_data_network_mapping';
import { Asset, AssetMetaData, ERC20Asset, Network, ZeroExInstantError } from '../types';
@@ -74,7 +74,7 @@ export const assetUtils = {
}
return metaData;
},
- bestNameForAsset: (asset?: Asset, defaultName: string = '???'): string => {
+ bestNameForAsset: (asset?: Asset, defaultName: string = DEFAULT_UNKOWN_ASSET_NAME): string => {
if (_.isUndefined(asset)) {
return defaultName;
}
diff --git a/packages/instant/src/util/env.ts b/packages/instant/src/util/env.ts
index 0fda0cc0e..aedf4f5d6 100644
--- a/packages/instant/src/util/env.ts
+++ b/packages/instant/src/util/env.ts
@@ -44,6 +44,8 @@ export const envUtil = {
getProviderType(provider: Provider): ProviderType | undefined {
if (provider.constructor.name === 'EthereumProvider') {
return ProviderType.Mist;
+ } else if ((provider as any).isTrust) {
+ return ProviderType.TrustWallet;
} else if ((provider as any).isParity) {
return ProviderType.Parity;
} else if ((provider as any).isMetaMask) {
diff --git a/packages/instant/src/util/format.ts b/packages/instant/src/util/format.ts
index e9c432b2f..4adb63e21 100644
--- a/packages/instant/src/util/format.ts
+++ b/packages/instant/src/util/format.ts
@@ -2,7 +2,7 @@ import { BigNumber } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';
import * as _ from 'lodash';
-import { ETH_DECIMALS } from '../constants';
+import { BIG_NUMBER_ZERO, ETH_DECIMALS } from '../constants';
export const format = {
ethBaseUnitAmount: (
@@ -20,24 +20,38 @@ export const format = {
ethUnitAmount?: BigNumber,
decimalPlaces: number = 4,
defaultText: React.ReactNode = '0 ETH',
+ minUnitAmountToDisplay: BigNumber = new BigNumber('0.00001'),
): React.ReactNode => {
if (_.isUndefined(ethUnitAmount)) {
return defaultText;
}
- const roundedAmount = ethUnitAmount.round(decimalPlaces).toDigits(decimalPlaces);
- return `${roundedAmount} ETH`;
+ let roundedAmount = ethUnitAmount.round(decimalPlaces).toDigits(decimalPlaces);
+
+ if (roundedAmount.eq(BIG_NUMBER_ZERO) && ethUnitAmount.greaterThan(BIG_NUMBER_ZERO)) {
+ // Sometimes for small ETH amounts (i.e. 0.000045) the amount rounded to 4 decimalPlaces is 0
+ // If that is the case, show to 1 significant digit
+ roundedAmount = new BigNumber(ethUnitAmount.toPrecision(1));
+ }
+
+ const displayAmount =
+ roundedAmount.greaterThan(BIG_NUMBER_ZERO) && roundedAmount.lessThan(minUnitAmountToDisplay)
+ ? `< ${minUnitAmountToDisplay.toString()}`
+ : roundedAmount.toString();
+
+ return `${displayAmount} ETH`;
},
ethBaseUnitAmountInUsd: (
ethBaseUnitAmount?: BigNumber,
ethUsdPrice?: BigNumber,
decimalPlaces: number = 2,
defaultText: React.ReactNode = '$0.00',
+ minUnitAmountToDisplay: BigNumber = new BigNumber('0.00001'),
): React.ReactNode => {
if (_.isUndefined(ethBaseUnitAmount) || _.isUndefined(ethUsdPrice)) {
return defaultText;
}
const ethUnitAmount = Web3Wrapper.toUnitAmount(ethBaseUnitAmount, ETH_DECIMALS);
- return format.ethUnitAmountInUsd(ethUnitAmount, ethUsdPrice, decimalPlaces);
+ return format.ethUnitAmountInUsd(ethUnitAmount, ethUsdPrice, decimalPlaces, minUnitAmountToDisplay);
},
ethUnitAmountInUsd: (
ethUnitAmount?: BigNumber,
@@ -48,7 +62,13 @@ export const format = {
if (_.isUndefined(ethUnitAmount) || _.isUndefined(ethUsdPrice)) {
return defaultText;
}
- return `$${ethUnitAmount.mul(ethUsdPrice).toFixed(decimalPlaces)}`;
+ const rawUsdPrice = ethUnitAmount.mul(ethUsdPrice);
+ const roundedUsdPrice = rawUsdPrice.toFixed(decimalPlaces);
+ if (roundedUsdPrice === '0.00' && rawUsdPrice.gt(BIG_NUMBER_ZERO)) {
+ return '<$0.01';
+ } else {
+ return `$${roundedUsdPrice}`;
+ }
},
ethAddress: (address: string): string => {
return `0x${address.slice(2, 7)}…${address.slice(-5)}`;