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

refactor: add number util #408

Merged
merged 7 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 4 additions & 5 deletions react/lib/components/Widget/WidgetContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 { isLessThanZero, resolveNumber } from '../../util/number';

export interface WidgetContainerProps
extends Omit<WidgetProps, 'success' | 'setNewTxs' | 'setCurrencyObject'> {
Expand Down Expand Up @@ -59,7 +59,6 @@ export interface Output {
disassembledScript: string;
}

const zero = new BigNumber(0);
const withSnackbar =
<T extends object>(
Component: React.ComponentType<T>,
Expand Down Expand Up @@ -135,7 +134,7 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =
const {
amount: transactionAmount,
paymentId: transactionPaymentId } = transaction;
const receivedAmount = new BigNumber(transactionAmount);
const receivedAmount = resolveNumber(transactionAmount);

const currencyTicker = getCurrencyTypeFromAddress(to);
if (!hideToasts)
Expand All @@ -146,7 +145,7 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =
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)
Expand Down Expand Up @@ -200,7 +199,7 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =
(tx: Transaction) => {
if (
tx.confirmed === false &&
zero.isLessThan(new BigNumber(tx.amount))
isLessThanZero(resolveNumber(tx.amount))
) {
handlePayment(tx);
}
Expand Down
35 changes: 35 additions & 0 deletions react/lib/tests/number.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import BigNumber from 'bignumber.js';
import { zero, resolveNumber, isLessThanZero } from '../util/number'; // Adjust the import path

describe('Number util functions', () => {
it('zero should be an instance of BigNumber and equal to 0', () => {
expect(zero).toBeInstanceOf(BigNumber);
expect(zero.isEqualTo(0)).toBe(true);
});

it('resolveNumber should convert various types to BigNumber', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's tests floats too (42.0, 42.5) and string floats ('42.0', '42.5'). Also, they shouldn't be in the same tests

expect(resolveNumber(42)).toBeInstanceOf(BigNumber);
expect(resolveNumber(42).isEqualTo(42)).toBe(true);

expect(resolveNumber('42')).toBeInstanceOf(BigNumber);
expect(resolveNumber('42').isEqualTo(42)).toBe(true);

const bigNumberInstance = new BigNumber(42);
expect(resolveNumber(bigNumberInstance)).toBeInstanceOf(BigNumber);
expect(resolveNumber(bigNumberInstance).isEqualTo(42)).toBe(true);
});

it('isLessThanZero should correctly identify values less than zero', () => {
expect(isLessThanZero(-1)).toBe(false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isLessThanZero(-1) returns false? why?

expect(isLessThanZero('-1')).toBe(false);
expect(isLessThanZero(new BigNumber(-1))).toBe(false);

expect(isLessThanZero(0)).toBe(false);
expect(isLessThanZero('0')).toBe(false);
expect(isLessThanZero(zero)).toBe(false);

expect(isLessThanZero(1)).toBe(true);
expect(isLessThanZero('1')).toBe(true);
expect(isLessThanZero(new BigNumber(1))).toBe(true);
});
});
12 changes: 12 additions & 0 deletions react/lib/util/number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import BigNumber from 'bignumber.js';

export const zero = new BigNumber(0);

export const resolveNumber = (value: BigNumber.Value) => {
return new BigNumber(value);
};

export const isLessThanZero = (value: BigNumber.Value) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is implemented backwards. It's named isLessThanZero but it checks if zeroIsLessThan

const formmatedValue = new BigNumber(value)
return zero.isLessThan(formmatedValue)
}
25 changes: 4 additions & 21 deletions react/lib/util/satoshis.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import BigNumber from 'bignumber.js';
import { formatPrice, formatBCH, formatXEC } from './format';
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 = (
Expand All @@ -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);
Expand All @@ -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,
Expand Down
Loading