Add oracle rates
This commit is contained in:
@@ -23,3 +23,5 @@ export {
|
||||
useBlockableInput,
|
||||
useIsInputCaptured,
|
||||
} from "./useInputLayer.js";
|
||||
export { useRate, useBchToFiatRate } from "./useRates.js";
|
||||
export { useSatoshisConversion } from "./useSatoshisConversion.js";
|
||||
|
||||
68
src/tui/hooks/useRates.tsx
Normal file
68
src/tui/hooks/useRates.tsx
Normal file
@@ -0,0 +1,68 @@
|
||||
import { useCallback, useMemo, useSyncExternalStore } from 'react';
|
||||
import type { RatesServiceEventMap } from '../../services/rates.js';
|
||||
import { useAppContext } from './useAppContext.js';
|
||||
|
||||
/**
|
||||
* Reactive hook for a single market pair.
|
||||
*
|
||||
* Pair format is NUMERATOR / DENOMINATOR, e.g. USD / BCH.
|
||||
*/
|
||||
export function useRate(
|
||||
numeratorUnitCode: string,
|
||||
denominatorUnitCode: string,
|
||||
): number | null {
|
||||
const { appService } = useAppContext();
|
||||
|
||||
const normalizedNumerator = useMemo(
|
||||
() => numeratorUnitCode.toUpperCase(),
|
||||
[numeratorUnitCode],
|
||||
);
|
||||
|
||||
const normalizedDenominator = useMemo(
|
||||
() => denominatorUnitCode.toUpperCase(),
|
||||
[denominatorUnitCode],
|
||||
);
|
||||
|
||||
const subscribe = useCallback(
|
||||
(callback: () => void) => {
|
||||
if (!appService) {
|
||||
return () => {};
|
||||
}
|
||||
|
||||
const onRateUpdated = (event: RatesServiceEventMap['rate-updated']) => {
|
||||
if (
|
||||
event.numeratorUnitCode === normalizedNumerator &&
|
||||
event.denominatorUnitCode === normalizedDenominator
|
||||
) {
|
||||
callback();
|
||||
}
|
||||
};
|
||||
|
||||
const unsubscribe = appService.rates.on('rate-updated', onRateUpdated);
|
||||
|
||||
return () => {
|
||||
unsubscribe();
|
||||
};
|
||||
},
|
||||
[appService, normalizedNumerator, normalizedDenominator],
|
||||
);
|
||||
|
||||
const getSnapshot = useCallback(() => {
|
||||
if (!appService) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return appService.rates.getRate(normalizedNumerator, normalizedDenominator);
|
||||
}, [appService, normalizedNumerator, normalizedDenominator]);
|
||||
|
||||
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience hook for BCH -> fiat market rates.
|
||||
*/
|
||||
export function useBchToFiatRate(
|
||||
targetCurrency: string = 'USD',
|
||||
): number | null {
|
||||
return useRate(targetCurrency, 'BCH');
|
||||
}
|
||||
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