diff --git a/react/lib/components/Widget/WidgetContainer.tsx b/react/lib/components/Widget/WidgetContainer.tsx index 06de6bd3..a5801328 100644 --- a/react/lib/components/Widget/WidgetContainer.tsx +++ b/react/lib/components/Widget/WidgetContainer.tsx @@ -12,9 +12,9 @@ import { getFiatPrice, } from '../../util/api-client'; import Widget, { WidgetProps } from './Widget'; -import BigNumber from 'bignumber.js'; import { generatePaymentId } from '../../util/opReturn'; import { Currency, CurrencyObject, Transaction } from '../../util/types'; +import { isGreaterThanZero, resolveNumber } from '../../util/number'; export interface WidgetContainerProps extends Omit { @@ -59,7 +59,6 @@ export interface Output { disassembledScript: string; } -const zero = new BigNumber(0); const withSnackbar = ( Component: React.ComponentType, @@ -135,7 +134,7 @@ export const WidgetContainer: React.FunctionComponent = const { amount: transactionAmount, paymentId: transactionPaymentId } = transaction; - const receivedAmount = new BigNumber(transactionAmount); + const receivedAmount = resolveNumber(transactionAmount); const currencyTicker = getCurrencyTypeFromAddress(to); if (!hideToasts) @@ -146,7 +145,7 @@ export const WidgetContainer: React.FunctionComponent = snackbarOptions, ); const txPaymentId = transactionPaymentId - const isCryptoAmountValid = (cryptoAmount && receivedAmount.isEqualTo(new BigNumber(cryptoAmount))) || !cryptoAmount; + const isCryptoAmountValid = (cryptoAmount && receivedAmount.isEqualTo(resolveNumber(cryptoAmount))) || !cryptoAmount; const isPaymentIdValid = thisPaymentId ? txPaymentId === thisPaymentId : true; if (isCryptoAmountValid && isPaymentIdValid) @@ -200,7 +199,7 @@ export const WidgetContainer: React.FunctionComponent = (tx: Transaction) => { if ( tx.confirmed === false && - zero.isLessThan(new BigNumber(tx.amount)) + isGreaterThanZero(resolveNumber(tx.amount)) ) { handlePayment(tx); } diff --git a/react/lib/tests/number.test.ts b/react/lib/tests/number.test.ts new file mode 100644 index 00000000..0bfb9a00 --- /dev/null +++ b/react/lib/tests/number.test.ts @@ -0,0 +1,91 @@ +import BigNumber from 'bignumber.js'; +import { zero, resolveNumber, isGreaterThanZero } from '../util/number'; // Adjust the import path + +describe('zero', () => { + it('zero should be an instance of BigNumber and equal to 0', () => { + expect(zero).toBeInstanceOf(BigNumber); + expect(zero.isEqualTo(0)).toBe(true); + }); +}) + +describe('resolveNumber', () => { + it('should convert int type to BigNumber', () => { + expect(resolveNumber(42)).toBeInstanceOf(BigNumber); + expect(resolveNumber(42).isEqualTo(42)).toBe(true); + }); + + it('should convert string representation of int type to BigNumber', () => { + expect(resolveNumber('42')).toBeInstanceOf(BigNumber); + expect(resolveNumber('42').isEqualTo(42)).toBe(true); + }); + + it('should convert float type to BigNumber', () => { + const result = resolveNumber(42.8) + expect(result).toBeInstanceOf(BigNumber); + expect(result.isEqualTo(42.8)).toBe(true); + }); + + it('should convert float type with only zero after the decimal point to BigNumber int', () => { + const result = resolveNumber(42.0) + expect(result).toBeInstanceOf(BigNumber); + expect(result.isEqualTo(42)).toBe(true); + }); + + it('should convert string float type with only zero after the decimal point to BigNumber int', () => { + const result = resolveNumber('42.0') + expect(result).toBeInstanceOf(BigNumber); + expect(result.isEqualTo(42)).toBe(true); + }); + + it('should convert string representation of float type to BigNumber', () => { + const result = resolveNumber('42.8') + expect(result).toBeInstanceOf(BigNumber); + expect(result.isEqualTo(42.8)).toBe(true); + }); + + it('should handle BigNumber instance correctly', () => { + const bigNumberInstance = new BigNumber(42); + expect(resolveNumber(bigNumberInstance)).toBeInstanceOf(BigNumber); + expect(resolveNumber(bigNumberInstance).isEqualTo(42)).toBe(true); + }); +}) + +describe('isGreaterThanZero', () => { + it('should correctly identify values less than zero', () => { + expect(isGreaterThanZero(-1)).toBe(false); + }); + + it('should correctly identify string representation of values less than zero', () => { + expect(isGreaterThanZero('-1')).toBe(false); + }); + + it('should correctly identify BigNumber instance of values less than zero', () => { + expect(isGreaterThanZero(new BigNumber(-1))).toBe(false); + }); + + it('should correctly identify zero as not greater than zero', () => { + expect(isGreaterThanZero(0)).toBe(false); + }); + + it('should correctly identify string representation of zero as not greater than zero', () => { + expect(isGreaterThanZero('0')).toBe(false); + }); + + it('should correctly identify zero BigNumber instance as not greater than zero', () => { + expect(isGreaterThanZero(zero)).toBe(false); + }); + + it('should correctly identify values greater than zero', () => { + expect(isGreaterThanZero(1)).toBe(true); + }); + + it('should correctly identify string representation of values greater than zero', () => { + expect(isGreaterThanZero('1')).toBe(true); + }); + + it('should correctly identify BigNumber instance of values greater than zero', () => { + expect(isGreaterThanZero(new BigNumber(1))).toBe(true); + }); +}) + + diff --git a/react/lib/util/number.ts b/react/lib/util/number.ts new file mode 100644 index 00000000..d938dba8 --- /dev/null +++ b/react/lib/util/number.ts @@ -0,0 +1,11 @@ +import BigNumber from 'bignumber.js'; + +export const zero = new BigNumber(0); + +export const resolveNumber = (value: BigNumber.Value) => { + return new BigNumber(value); +}; + +export const isGreaterThanZero = (value: BigNumber.Value) => { + return zero.isLessThan(value) +} \ No newline at end of file diff --git a/react/lib/util/satoshis.ts b/react/lib/util/satoshis.ts index 157fee8a..204226f4 100644 --- a/react/lib/util/satoshis.ts +++ b/react/lib/util/satoshis.ts @@ -1,4 +1,3 @@ -import BigNumber from 'bignumber.js'; import { formatPrice, formatBCH, formatXEC } from './format'; import { DECIMALS } from './constants' @@ -6,6 +5,7 @@ import { DECIMALS } from './constants' import { isCrypto } from './api-client'; import { randomizeSatoshis } from './randomizeSats'; import { Currency, CurrencyObject } from './types'; +import { resolveNumber } from './number'; export const getCurrencyObject = ( @@ -19,11 +19,11 @@ export const getCurrencyObject = ( if (isCrypto(currencyType)) { let newAmount = randomSatoshis ? randomizeSatoshis(amount, currencyType, randomSatoshis) : amount; const decimals = DECIMALS[currencyType] - const primaryUnit = new BigNumber(`${newAmount}`); + const primaryUnit = resolveNumber(`${newAmount}`); if (primaryUnit?.c !== null) { - float = parseFloat(new BigNumber(primaryUnit).toFixed(decimals)); - string = new BigNumber(`${primaryUnit}`).toFixed(decimals); + float = parseFloat(resolveNumber(primaryUnit).toFixed(decimals)); + string = resolveNumber(`${primaryUnit}`).toFixed(decimals); if (currencyType === 'BCH') { string = formatBCH(string); @@ -43,23 +43,6 @@ export const getCurrencyObject = ( }; }; -// future token functions - -// const toSLPfloat = (amount: number, decimalCount: number) => { -// let val; -// if (isDecimal(amount)) { -// val = new BigNumber(`${amount}e+${decimalCount}`); -// } else { -// val = new BigNumber(amount); -// } - -// return val; -// }; - -// const isDecimal = (n: number) => { -// const result = n - Math.floor(n) !== 0; -// return result; -// }; export default { getCurrencyObject,