Skip to content

Commit

Permalink
Bugfix: send flow amount (#1322)
Browse files Browse the repository at this point in the history
* fix

* test

* rm logs

* fix space

* handle comma

* handle decimals

* log and test

* third
  • Loading branch information
estebanmino authored Feb 13, 2020
1 parent bd5eeaa commit c4231ff
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
20 changes: 14 additions & 6 deletions app/components/Views/SendFlow/Amount/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
)}`;
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 18 additions & 2 deletions app/util/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
}

Expand Down
13 changes: 12 additions & 1 deletion app/util/number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
fiatNumberToTokenMinimalUnit,
balanceToFiat,
balanceToFiatNumber,
renderFiat
renderFiat,
handleWeiNumber
} from './number';
import numberToBN from 'number-to-bn';

Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit c4231ff

Please sign in to comment.