Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: send flow amount #1322

Merged
merged 10 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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