From 4d43a4acc4d40f46630a6556766ba853e832bdbc Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Mon, 28 May 2018 20:06:01 +0200 Subject: [PATCH] fix: display very small numbers as .toString(10) --- src/js/actions/SendFormActions.js | 23 ++++++++----------- src/js/actions/Web3Actions.js | 4 ++-- .../wallet/account/summary/Summary.js | 2 +- .../wallet/aside/AccountSelection.js | 2 +- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/js/actions/SendFormActions.js b/src/js/actions/SendFormActions.js index 03cb0ee0..c71a235c 100644 --- a/src/js/actions/SendFormActions.js +++ b/src/js/actions/SendFormActions.js @@ -130,22 +130,19 @@ export const calculateFee = (gasPrice: string, gasLimit: string): string => { export const calculateTotal = (amount: string, gasPrice: string, gasLimit: string): string => { try { - // return new BigNumber(amount).plus( calculateFee(gasPrice, gasLimit) ).toString(); - const fee = calculateFee(gasPrice, gasLimit); - return new BigNumber(amount).plus( calculateFee(gasPrice, gasLimit) ).toFixed(16); + return new BigNumber(amount).plus( calculateFee(gasPrice, gasLimit) ).toString(10); } catch (error) { return '0'; } } -export const calculateMaxAmount = (balance: string, gasPrice: string, gasLimit: string): string => { +export const calculateMaxAmount = (balance: BigNumber, gasPrice: string, gasLimit: string): string => { try { // TODO - minus pendings const fee = calculateFee(gasPrice, gasLimit); - const b = new BigNumber(balance); - const max = b.minus(fee); + const max = balance.minus(fee); if (max.lessThan(0)) return '0'; - return max.toString(); + return max.toString(10); } catch (error) { return '0'; } @@ -181,10 +178,10 @@ export const calculate = (prevProps: Props, props: Props) => { if (isToken) { const token: ?Token = findToken(tokens, account.address, state.currency, account.deviceState); if (token) { - state.amount = new BigNumber(token.balance).minus(pendingAmount).toString(); + state.amount = new BigNumber(token.balance).minus(pendingAmount).toString(10); } } else { - const b = new BigNumber(account.balance).minus(pendingAmount).toString(); + const b = new BigNumber(account.balance).minus(pendingAmount); state.amount = calculateMaxAmount(b, state.gasPrice, state.gasLimit); } } @@ -203,8 +200,8 @@ export const calculate = (prevProps: Props, props: Props) => { export const getFeeLevels = (symbol: string, gasPrice: BigNumber | string, gasLimit: string, selected?: FeeLevel): Array => { const price: BigNumber = typeof gasPrice === 'string' ? new BigNumber(gasPrice) : gasPrice const quarter: BigNumber = price.dividedBy(4); - const high: string = price.plus(quarter.times(2)).toString(); - const low: string = price.minus(quarter.times(2)).toString(); + const high: string = price.plus(quarter.times(2)).toString(10); + const low: string = price.minus(quarter.times(2)).toString(10); const customLevel: FeeLevel = selected && selected.value === 'Custom' ? { value: 'Custom', @@ -226,7 +223,7 @@ export const getFeeLevels = (symbol: string, gasPrice: BigNumber | string, gasLi { value: 'Normal', gasPrice: gasPrice.toString(), - label: `${ calculateFee(price.toString(), gasLimit) } ${ symbol }` + label: `${ calculateFee(price.toString(10), gasLimit) } ${ symbol }` }, { value: 'Low', @@ -843,7 +840,7 @@ export const onSend = (): AsyncAction => { if (!token) return; const contract = web3.erc20.at(token.address); - const amountValue: string = new BigNumber(currentState.amount).times( Math.pow(10, token.decimals) ).toString(); + const amountValue: string = new BigNumber(currentState.amount).times( Math.pow(10, token.decimals) ).toString(10); data = contract.transfer.getData(currentState.address, amountValue, { from: account.address, diff --git a/src/js/actions/Web3Actions.js b/src/js/actions/Web3Actions.js index e319a720..a65ff05b 100644 --- a/src/js/actions/Web3Actions.js +++ b/src/js/actions/Web3Actions.js @@ -275,7 +275,7 @@ export function getTokenBalance(token: Token): AsyncAction { contract.balanceOf(token.ethAddress, (error: Error, balance: BigNumber) => { if (balance) { - const newBalance: string = balance.dividedBy( Math.pow(10, token.decimals) ).toString(); + const newBalance: string = balance.dividedBy( Math.pow(10, token.decimals) ).toString(10); if (newBalance !== token.balance) { dispatch(TokenActions.setBalance( token.address, @@ -371,7 +371,7 @@ export const getTokenBalanceAsync = (erc20: ContractFactory, token: Token): Prom if (error) { reject(error); } else { - const newBalance: string = balance.dividedBy( Math.pow(10, token.decimals) ).toString(); + const newBalance: string = balance.dividedBy( Math.pow(10, token.decimals) ).toString(10); resolve(newBalance); } }); diff --git a/src/js/components/wallet/account/summary/Summary.js b/src/js/components/wallet/account/summary/Summary.js index 5c240b33..9d0623cb 100644 --- a/src/js/components/wallet/account/summary/Summary.js +++ b/src/js/components/wallet/account/summary/Summary.js @@ -36,7 +36,7 @@ const Summary = (props: Props) => { const explorerLink: string = `${network.explorer.address}${account.address}`; const pendingAmount: BigNumber = stateUtils.getPendingAmount(pending, network.symbol); - const balance: string = new BigNumber(account.balance).minus(pendingAmount).toString(); + const balance: string = new BigNumber(account.balance).minus(pendingAmount).toString(10); return (
diff --git a/src/js/components/wallet/aside/AccountSelection.js b/src/js/components/wallet/aside/AccountSelection.js index 8856139b..07106444 100644 --- a/src/js/components/wallet/aside/AccountSelection.js +++ b/src/js/components/wallet/aside/AccountSelection.js @@ -39,7 +39,7 @@ const AccountSelection = (props: Props): ?React$Element => { if (account.balance !== '') { const pending = stateUtils.getAccountPendingTx(props.pending, account); const pendingAmount: BigNumber = stateUtils.getPendingAmount(pending, selectedCoin.symbol); - const availableBalance: string = new BigNumber(account.balance).minus(pendingAmount).toString(); + const availableBalance: string = new BigNumber(account.balance).minus(pendingAmount).toString(10); if (fiatRate) { const accountBalance = new BigNumber(availableBalance);