diff --git a/web/cashtab/src/components/Send/Send.js b/web/cashtab/src/components/Send/Send.js --- a/web/cashtab/src/components/Send/Send.js +++ b/web/cashtab/src/components/Send/Send.js @@ -22,6 +22,8 @@ toLegacy, } from '@components/Common/Ticker.js'; import { Event } from '@utils/GoogleAnalytics'; +import BigNumber from 'bignumber.js'; + export const BalanceHeader = styled.div` p { color: #777; @@ -208,7 +210,7 @@ let bchValue = value; if (selectedCurrency === 'USD') { - bchValue = (value / fiatPrice).toFixed(8); + bchValue = new BigNumber((value / fiatPrice).toFixed(8)).toString(); } try { @@ -333,23 +335,27 @@ const handleBchAmountChange = e => { const { value, name } = e.target; let error = false; - let bchValue = value; + + let BigNumCashAmount = new BigNumber(value); if (selectedCurrency === 'USD') { - bchValue = (value / fiatPrice).toFixed(8); + BigNumCashAmount = new BigNumber((value / fiatPrice).toFixed(8)); } // Validate value for > 0 - if (isNaN(bchValue)) { + if (isNaN(BigNumCashAmount)) { error = 'Amount must be a number'; - } else if (bchValue <= 0) { + } else if (BigNumCashAmount.lte(0)) { error = 'Amount must be greater than 0'; - } else if (bchValue < currency.dust) { + } else if (BigNumCashAmount.lt(currency.dust)) { error = `Send amount must be at least ${currency.dust} ${currency.ticker}`; - } else if (bchValue > balances.totalBalance) { + } else if (BigNumCashAmount.gt(new BigNumber(balances.totalBalance))) { error = `Amount cannot exceed your ${currency.ticker} balance`; - } else if (!isNaN(bchValue) && bchValue.toString().includes('.')) { - if (bchValue.toString().split('.')[1].length > 8) { + } else if ( + !isNaN(BigNumCashAmount) && + BigNumCashAmount.toString().includes('.') + ) { + if (BigNumCashAmount.toString().split('.')[1].length > 8) { error = `${currency.ticker} transactions do not support more than 8 decimal places`; } } diff --git a/web/cashtab/src/hooks/__tests__/useBCH.test.js b/web/cashtab/src/hooks/__tests__/useBCH.test.js --- a/web/cashtab/src/hooks/__tests__/useBCH.test.js +++ b/web/cashtab/src/hooks/__tests__/useBCH.test.js @@ -7,6 +7,7 @@ import BCHJS from '@psf/bch-js'; // TODO: should be removed when external lib not needed anymore import { currency } from '../../components/Common/Ticker'; import sendBCH from '../__mocks__/sendBCH'; +import BigNumber from 'bignumber.js'; describe('useBCH hook', () => { it('gets Rest Api Url on testnet', () => { @@ -159,7 +160,11 @@ .mockResolvedValue(expectedTxId); const failedSendBch = sendBch(BCH, wallet, utxos, { addresses, - values: [0.00000546 - 0.00000001], + values: [ + new BigNumber('0.00000546') + .minus(new BigNumber('0.00000001')) + .toString(), + ], }); expect(failedSendBch).rejects.toThrow(new Error('dust')); const nullValuesSendBch = await sendBch(BCH, wallet, utxos, {