diff --git a/app/components/Views/SendFlow/Amount/index.js b/app/components/Views/SendFlow/Amount/index.js index 368a175da23..e3ef7640cdc 100644 --- a/app/components/Views/SendFlow/Amount/index.js +++ b/app/components/Views/SendFlow/Amount/index.js @@ -35,7 +35,8 @@ import { fiatNumberToTokenMinimalUnit, weiToFiatNumber, balanceToFiatNumber, - getCurrencySymbol + getCurrencySymbol, + handleWeiNumber } from '../../../../util/number'; import { getTicker, generateTransferData, getEther } from '../../../../util/transactions'; import { hexToBN, BNToHex } from 'gaba/dist/util'; @@ -523,16 +524,23 @@ class Amount extends PureComponent { onInputChange = (inputValue, selectedAsset) => { const { contractExchangeRates, conversionRate, currentCurrency, ticker } = this.props; const { internalPrimaryCurrencyIsCrypto } = this.state; - let inputValueConversion, renderableInputValueConversion, hasExchangeRate; + let inputValueConversion, renderableInputValueConversion, hasExchangeRate, comma; + // Remove spaces from input + inputValue = inputValue && inputValue.replace(/\s+/g, ''); + // Handle semicolon for other languages + if (inputValue && inputValue.includes(',')) { + comma = true; + inputValue = inputValue.replace(',', '.'); + } const processedTicker = getTicker(ticker); - const processedInputValue = isDecimal(inputValue) ? inputValue : '0'; + const processedInputValue = isDecimal(inputValue) ? handleWeiNumber(inputValue) : '0'; selectedAsset = selectedAsset || this.props.selectedAsset; if (selectedAsset.isETH) { hasExchangeRate = true; if (internalPrimaryCurrencyIsCrypto) { - inputValueConversion = `${weiToFiatNumber(toWei(processedInputValue.toString(16)), conversionRate)}`; + inputValueConversion = `${weiToFiatNumber(toWei(processedInputValue), conversionRate)}`; renderableInputValueConversion = `${weiToFiat( - toWei(processedInputValue.toString(16)), + toWei(processedInputValue), conversionRate, currentCurrency )}`; @@ -565,7 +573,7 @@ class Amount extends PureComponent { renderableInputValueConversion = `${inputValueConversion} ${selectedAsset.symbol}`; } } - + if (comma) inputValue = inputValue && inputValue.replace('.', ','); inputValueConversion = inputValueConversion === '0' ? undefined : inputValueConversion; this.setState({ inputValue, diff --git a/app/util/number.js b/app/util/number.js index f2d312f6b98..f48a0341c94 100644 --- a/app/util/number.js +++ b/app/util/number.js @@ -315,6 +315,20 @@ export function weiToFiatNumber(wei, conversionRate, decimalsToShow = 5) { return value; } +/** + * Handles wie input to have less or equal to 18 decimals + * + * @param {string} wei - Amount in decimal notation + * @returns {string} - Number string with less or equal 18 decimals + */ +export function handleWeiNumber(wei) { + const comps = wei.split('.'); + let fraction = comps[1]; + if (fraction && fraction.length > 18) fraction = fraction.substring(0, 18); + const finalWei = fraction ? [comps[0], fraction].join('.') : comps[0]; + return finalWei; +} + /** * Converts fiat number as human-readable fiat string to wei expressed as a BN * @@ -325,8 +339,10 @@ export function weiToFiatNumber(wei, conversionRate, decimalsToShow = 5) { export function fiatNumberToWei(fiat, conversionRate) { const floatFiatConverted = parseFloat(fiat) / conversionRate; const base = Math.pow(10, 18); - const weiNumber = Math.trunc(base * floatFiatConverted); - const weiBN = numberToBN(weiNumber); + let weiNumber = Math.trunc(base * floatFiatConverted); + // avoid decimals + weiNumber = weiNumber.toLocaleString('fullwide', { useGrouping: false }).split('.'); + const weiBN = numberToBN(weiNumber[0]); return weiBN; } diff --git a/app/util/number.test.js b/app/util/number.test.js index 5a2018c746c..f5b253dc95e 100644 --- a/app/util/number.test.js +++ b/app/util/number.test.js @@ -17,7 +17,8 @@ import { fiatNumberToTokenMinimalUnit, balanceToFiat, balanceToFiatNumber, - renderFiat + renderFiat, + handleWeiNumber } from './number'; import numberToBN from 'number-to-bn'; @@ -189,6 +190,16 @@ describe('Number utils :: weiToFiatNumber', () => { }); }); +describe('Number utils :: handleWeiNumber', () => { + it('weiToFiatNumber', () => { + expect(handleWeiNumber('1.123')).toEqual('1.123'); + expect(handleWeiNumber('1')).toEqual('1'); + expect(handleWeiNumber('1.01')).toEqual('1.01'); + expect(handleWeiNumber('1.111111111111111111')).toEqual('1.111111111111111111'); + expect(handleWeiNumber('1.1111111111111111112222')).toEqual('1.111111111111111111'); + }); +}); + describe('Number utils :: fiatNumberToWei', () => { it('fiatNumberToWei', () => { const one = numberToBN(Math.pow(10, 18));