26 lines
629 B
TypeScript
26 lines
629 B
TypeScript
import Decimal from 'decimal.js';
|
|
import type { ShopFiatCurrency } from '../../../types/ShopFiatCurrency';
|
|
|
|
export const extractCoinPaprikaTickerPrice = (
|
|
response: { quotes: Record<string, { price: number }> },
|
|
shopFiatCurrency: ShopFiatCurrency
|
|
): number | null => {
|
|
const quote = response.quotes[shopFiatCurrency];
|
|
|
|
if (!quote) {
|
|
return null;
|
|
}
|
|
|
|
const rate = quote.price;
|
|
|
|
if (typeof rate !== 'number' || !Number.isFinite(rate) || rate <= 0) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
return new Decimal(rate).toDecimalPlaces(2).toNumber();
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|