From a856770a21654aa16e1662d52fda1196aff0d04b Mon Sep 17 00:00:00 2001 From: lissavxo Date: Tue, 21 May 2024 21:25:01 -0300 Subject: [PATCH 1/4] feat: add currency util --- react/lib/util/currency.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 react/lib/util/currency.ts diff --git a/react/lib/util/currency.ts b/react/lib/util/currency.ts new file mode 100644 index 00000000..0629de6f --- /dev/null +++ b/react/lib/util/currency.ts @@ -0,0 +1,18 @@ +import { CRYPTO_CURRENCIES, FIAT_CURRENCIES } from "./constants"; +import { CryptoCurrency, FiatCurrency } from "./types"; + +export function isFiat(unknownString: string): unknownString is FiatCurrency { + return FIAT_CURRENCIES.includes(unknownString); + } + + export function isCrypto( + unknownString: string, + ): unknownString is CryptoCurrency { + return CRYPTO_CURRENCIES.includes(unknownString); + } + + export function isValidCurrency( + unknownString: string, + ): unknownString is CryptoCurrency { + return isFiat(unknownString) || isCrypto(unknownString); + } \ No newline at end of file From 4c7ef71c1cd31b95e7d443fd0cc6e7f5964c4629 Mon Sep 17 00:00:00 2001 From: lissavxo Date: Tue, 21 May 2024 21:26:16 -0300 Subject: [PATCH 2/4] feat: use currency util --- react/lib/components/PayButton/PayButton.tsx | 3 ++- react/lib/components/Widget/WidgetContainer.tsx | 5 ++--- react/lib/util/satoshis.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/react/lib/components/PayButton/PayButton.tsx b/react/lib/components/PayButton/PayButton.tsx index 65348ff3..485ac34e 100644 --- a/react/lib/components/PayButton/PayButton.tsx +++ b/react/lib/components/PayButton/PayButton.tsx @@ -2,12 +2,13 @@ import React, { useState, useEffect, useCallback, useRef } from 'react'; import { Theme, ThemeName, ThemeProvider, useTheme } from '../../themes'; import Button, { ButtonProps } from '../Button/Button'; -import { isFiat, getFiatPrice } from '../../util/api-client'; +import { getFiatPrice } from '../../util/api-client'; import { PaymentDialog } from '../PaymentDialog/PaymentDialog'; import { getCurrencyTypeFromAddress, isValidCashAddress, isValidXecAddress } from '../../util/address'; import { getCurrencyObject } from '../../util/satoshis'; import { generatePaymentId } from '../../util/opReturn'; import { Currency, Transaction, CurrencyObject } from '../../util/types'; +import { isFiat } from '../../util/currency'; export interface PayButtonProps extends ButtonProps { to: string; diff --git a/react/lib/components/Widget/WidgetContainer.tsx b/react/lib/components/Widget/WidgetContainer.tsx index a5801328..104d7e22 100644 --- a/react/lib/components/Widget/WidgetContainer.tsx +++ b/react/lib/components/Widget/WidgetContainer.tsx @@ -6,15 +6,14 @@ import { getCurrencyTypeFromAddress, } from '../../util/address'; import { - isCrypto, - isValidCurrency, - isFiat, getFiatPrice, } from '../../util/api-client'; + import Widget, { WidgetProps } from './Widget'; import { generatePaymentId } from '../../util/opReturn'; import { Currency, CurrencyObject, Transaction } from '../../util/types'; import { isGreaterThanZero, resolveNumber } from '../../util/number'; +import { isCrypto, isFiat, isValidCurrency } from '../../util/currency'; export interface WidgetContainerProps extends Omit { diff --git a/react/lib/util/satoshis.ts b/react/lib/util/satoshis.ts index 204226f4..ca7e537b 100644 --- a/react/lib/util/satoshis.ts +++ b/react/lib/util/satoshis.ts @@ -2,7 +2,7 @@ import { formatPrice, formatBCH, formatXEC } from './format'; import { DECIMALS } from './constants' -import { isCrypto } from './api-client'; +import { isCrypto } from './currency'; import { randomizeSatoshis } from './randomizeSats'; import { Currency, CurrencyObject } from './types'; import { resolveNumber } from './number'; From 4909fe0e15aa56be3737b7234d282ce1e1cbcf86 Mon Sep 17 00:00:00 2001 From: lissavxo Date: Tue, 21 May 2024 21:26:58 -0300 Subject: [PATCH 3/4] refactor: remove duplicated functions --- react/lib/util/api-client.ts | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/react/lib/util/api-client.ts b/react/lib/util/api-client.ts index 2af092bd..80664108 100644 --- a/react/lib/util/api-client.ts +++ b/react/lib/util/api-client.ts @@ -8,10 +8,8 @@ import { PriceData, TransactionDetails, Currency, - CryptoCurrency, - FiatCurrency, } from './types'; -import { FIAT_CURRENCIES, CRYPTO_CURRENCIES } from './constants'; +import { isFiat } from './currency'; export const getAddressDetails = async ( address: string, @@ -99,22 +97,6 @@ export default { getAddressBalance, }; -export function isFiat(unknownString: string): unknownString is FiatCurrency { - return FIAT_CURRENCIES.includes(unknownString as FiatCurrency); -} - -export function isCrypto( - unknownString: string, -): unknownString is CryptoCurrency { - return CRYPTO_CURRENCIES.includes(unknownString as CryptoCurrency) -} - -export function isValidCurrency( - unknownString: string, -): unknownString is CryptoCurrency { - return isFiat(unknownString) || isCrypto(unknownString) -} - export const getCashtabProviderStatus = () => { const windowAny = window as any if (window && windowAny.bitcoinAbc && windowAny.bitcoinAbc === 'cashtab') { From 75b516dbd3a57d93a807099375871fb8e120a38a Mon Sep 17 00:00:00 2001 From: lissavxo Date: Tue, 21 May 2024 21:27:38 -0300 Subject: [PATCH 4/4] test: currency util --- react/lib/components/Widget/Widget.tsx | 3 ++- .../tests/{api-client.test.ts => currency.test.ts} | 11 ++++------- 2 files changed, 6 insertions(+), 8 deletions(-) rename react/lib/tests/{api-client.test.ts => currency.test.ts} (91%) diff --git a/react/lib/components/Widget/Widget.tsx b/react/lib/components/Widget/Widget.tsx index 90b4630c..55ae6db5 100644 --- a/react/lib/components/Widget/Widget.tsx +++ b/react/lib/components/Widget/Widget.tsx @@ -26,10 +26,11 @@ import BarChart from '../BarChart/BarChart'; import PencilIcon from '../../assets/edit-pencil'; import io, { Socket } from 'socket.io-client'; import { encodeOpReturnProps } from '../../util/opReturn'; -import { getAddressDetails, getAddressBalance, isFiat, getCashtabProviderStatus } from '../../util/api-client'; +import { getAddressDetails, getAddressBalance, getCashtabProviderStatus } from '../../util/api-client'; import { getCurrencyObject } from '../../util/satoshis'; import { Currency, CurrencyObject, Transaction, CryptoCurrency } from '../../util/types'; import { setListener } from '../../util/socket'; +import { isFiat } from '../../util/currency'; type QRCodeProps = BaseQRCodeProps & { renderAs: 'svg' }; diff --git a/react/lib/tests/api-client.test.ts b/react/lib/tests/currency.test.ts similarity index 91% rename from react/lib/tests/api-client.test.ts rename to react/lib/tests/currency.test.ts index 670034fc..33eaf5d5 100644 --- a/react/lib/tests/api-client.test.ts +++ b/react/lib/tests/currency.test.ts @@ -1,9 +1,7 @@ -import { isCrypto, isFiat, isValidCurrency } from '../util/api-client'; -import { FIAT_CURRENCIES } from '../util/constants' +import { FIAT_CURRENCIES } from "../util/constants"; +import { isCrypto, isFiat, isValidCurrency } from "../util/currency"; - -describe('API Client Util Tests', () => { - describe('isCrypto', () => { +describe('isCrypto', () => { it('recognizes valid crypto currencies', () => { expect(isCrypto('BCH')).toBeTruthy(); expect(isCrypto('XEC')).toBeTruthy(); @@ -77,5 +75,4 @@ describe('API Client Util Tests', () => { expect(isFiat(currency)).toBe(false); }); }); - }); -}); \ No newline at end of file + }); \ No newline at end of file