Add oracle rates
This commit is contained in:
42
src/tui/hooks/useSatoshisConversion.tsx
Normal file
42
src/tui/hooks/useSatoshisConversion.tsx
Normal file
@@ -0,0 +1,42 @@
|
||||
import { useCallback, useMemo } from 'react';
|
||||
import { useAppContext } from './useAppContext.js';
|
||||
import { useBchToFiatRate } from './useRates.js';
|
||||
|
||||
/**
|
||||
* Reactive BCH satoshis -> fiat conversion helpers for TUI screens.
|
||||
*
|
||||
* This hook subscribes to rate updates through `useBchToFiatRate`, so any
|
||||
* component using it will re-render automatically when the selected pair
|
||||
* receives a new quote.
|
||||
*/
|
||||
export function useSatoshisConversion(targetCurrency: string = 'USD') {
|
||||
const { appService } = useAppContext();
|
||||
const currencyCode = useMemo(() => targetCurrency.toUpperCase(), [targetCurrency]);
|
||||
const fiatPerBchRate = useBchToFiatRate(currencyCode);
|
||||
|
||||
const formattedFiatPerBchRate = useMemo(() => {
|
||||
if (!appService || fiatPerBchRate === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return appService.rates.formatCurrency(fiatPerBchRate, currencyCode);
|
||||
}, [appService, fiatPerBchRate, currencyCode]);
|
||||
|
||||
const formatSatoshisToFiat = useCallback(
|
||||
(satoshis: bigint): string | null => {
|
||||
if (!appService || fiatPerBchRate === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return appService.rates.formatBchToFiat(satoshis, currencyCode);
|
||||
},
|
||||
[appService, fiatPerBchRate, currencyCode],
|
||||
);
|
||||
|
||||
return {
|
||||
currencyCode,
|
||||
fiatPerBchRate,
|
||||
formattedFiatPerBchRate,
|
||||
formatSatoshisToFiat,
|
||||
} as const;
|
||||
}
|
||||
Reference in New Issue
Block a user