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 currency util #410

Merged
merged 4 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion react/lib/components/PayButton/PayButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion react/lib/components/Widget/Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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' };

Expand Down
5 changes: 2 additions & 3 deletions react/lib/components/Widget/WidgetContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<WidgetProps, 'success' | 'setNewTxs' | 'setCurrencyObject'> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -77,5 +75,4 @@ describe('API Client Util Tests', () => {
expect(isFiat(currency)).toBe(false);
});
});
});
});
});
20 changes: 1 addition & 19 deletions react/lib/util/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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') {
Expand Down
18 changes: 18 additions & 0 deletions react/lib/util/currency.ts
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion react/lib/util/satoshis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Loading